shython: Initial release.

This commit is contained in:
Ole Tange 2017-06-28 09:10:29 +02:00
parent 944834f76a
commit 329ed04861
3 changed files with 149 additions and 2 deletions

View file

@ -1,9 +1,9 @@
CMD = blink bsearch em encdir field forever G gitnext gitundo \
goodpasswd histogram mtrr neno off pdfman puniq ramusage rand rclean \
rn rrm sound-reload stdout T timestamp tracefile upsidedown \
rn rrm shython sound-reload stdout swapout T timestamp tracefile upsidedown \
w4it-for-port-open wifi-reload wssh ytv yyyymmdd
all: blink/blink.1 bsearch/bsearch.1 encdir/encdir.1 G/G.1 gitnext/gitnext.1 gitundo/gitundo.1 goodpasswd/goodpasswd.1 histogram/histogram.1 neno/neno.1 off/off.1 pdfman/pdfman.1 puniq/puniq.1 rand/rand.1 rn/rn.1 rrm/rrm.1 sound-reload/sound-reload.1 stdout/stdout.1 timestamp/timestamp.1 tracefile/tracefile.1 T/T.1 upsidedown/upsidedown.1 wifi-reload/wifi-reload.1 wssh/wssh.1 ytv/ytv.1 yyyymmdd/yyyymmdd.1
all: blink/blink.1 bsearch/bsearch.1 encdir/encdir.1 G/G.1 gitnext/gitnext.1 gitundo/gitundo.1 goodpasswd/goodpasswd.1 histogram/histogram.1 neno/neno.1 off/off.1 pdfman/pdfman.1 puniq/puniq.1 rand/rand.1 rn/rn.1 rrm/rrm.1 shython/shython.1 sound-reload/sound-reload.1 stdout/stdout.1 timestamp/timestamp.1 tracefile/tracefile.1 T/T.1 upsidedown/upsidedown.1 wifi-reload/wifi-reload.1 wssh/wssh.1 ytv/ytv.1 yyyymmdd/yyyymmdd.1
%.1: %
pod2man $< > $@

4
README
View file

@ -40,10 +40,14 @@ rn - Move file(s)/dir(s) to .rm/ (wastebasket).
rrm - Record file's MD5 sum before removing it.
shython - Shebang wrapper for cython.
sound-reload - Reload sound system.
stdout - Redirect both STDERR and STDOUT to STDOUT.
swapout - Force swapping out.
T - tee stdout to and from temporary files.
timestamp - prepend timestamp to output.

143
shython/shython Executable file
View file

@ -0,0 +1,143 @@
#!/usr/bin/perl
=pod
=head1 NAME
shython - Shebang wrapper for cython
=head1 SYNOPSIS
#!/usr/bin/shython
python program
=head1 DESCRIPTION
B<shython> uses B<cython> to compile a python script into a C-file
which is then compiled and run.
It caches the compiled C-file in $XDG_CACHE_HOME/shython or
$HOME/.cache/shython, and recompiles only if the source script has a
newer timestamp.
=head1 EXAMPLE
#!/usr/bin/shython
import sys
print "Hello world, ",sys.argv
=head1 EXAMPLE: Shython in $PATH
#!/usr/bin/env shython
import sys
print "Hello world, ",sys.argv
=head1 AUTHOR
Copyright (C) 2017 Ole Tange,
http://ole.tange.dk and Free Software Foundation, Inc.
=head1 LICENSE
Copyright (C) 2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 DEPENDENCIES
B<shython> uses B<cython>, B<gcc>, and B<perl>.
=head1 SEE ALSO
B<cython>
=cut
use File::Path;
my $pyfile = shift;
my $cache_home = append_shython('XDG_CACHE_HOME') ||
$ENV{'HOME'}."/.cache/shython";
my $cachefile = $cache_home."/".$pyfile;
-d $cache_home or File::Path::mkpath($cache_home);
if (not -e $cachefile
or
-M $cachefile > -M $pyfile) {
compile($pyfile,$cachefile);
}
# run
system($cachefile,@ARGV);
sub compile {
my $pyfile = shift;
my $cachefile = shift;
my $cython = (which("cython3","cython"))[0];
my $cfile = $cachefile.".tmp";
$cfile =~ s/(.py.?)?$/.c/;
# cython3 --embed -o hello.c hello.py
system($cython,"--embed","-o",$cfile,$pyfile);
my @installed_lib;
my @installed_inc;
for my $inc (</usr/include/python*>,</usr/local/include/python*>) {
my $ver = $inc;
$ver =~ s:.*/::;
for my $lib (qw(/lib64 /lib /lib/x86_64-linux-gnu
/usr/lib/gcc/x86_64-linux-gnu/6 /usr/lib
/usr/lib/x86_64-linux-gnu /usr/local/lib)) {
if(-e "$lib/lib$ver.so" and -e "$lib/lib$ver.a") {
push @installed_lib,$ver;
push @installed_inc,$inc;
}
}
}
my $inc = (reverse sort @installed_inc)[0];
my $lib = (reverse sort @installed_lib)[0];
system("gcc","-Os","-I",$inc,"-o",$cachefile,$cfile,"-l$lib","-lpthread","-lm","-lutil","-ldl");
unlink $cfile;
}
sub which {
# Input:
# @programs = programs to find the path to
# Returns:
# @full_path = full paths to @programs. Nothing if not found
my @which;
for my $prg (@_) {
push(@which, grep { not -d $_ and -x $_ }
map { $_."/".$prg } split(":",$ENV{'PATH'}));
}
return @which;
}
sub append_shython {
my $v = shift;
if($ENV{$v}) {
return(map { "$_/shython" }
grep { -d $_ }
split /:/, $ENV{$v});
} else {
return "";
}
}