ya2 · news · projects · code · about

6d7636168cb0bb2219430e7470db23a962f73c3f
[pmachines.git] / ya2 / tools / pdfsingle.py
1 # python ya2/tools/pdfsingle.py path/to/file.py
2 from os import chdir, getcwd, system
3 from os.path import dirname, basename, exists
4 from sys import argv
5
6
7 class InsideDir:
8
9 def __init__(self, dir_):
10 self.dir = dir_
11 self.old_dir = getcwd()
12
13 def __enter__(self):
14 chdir(self.dir)
15
16 def __exit__(self, exc_type, exc_val, exc_tb):
17 chdir(self.old_dir)
18
19
20 filename = argv[1]
21 name = basename(filename)
22 path = dirname(filename)
23 noext = name.rsplit('.', 1)[0]
24 test_tmpl = "tail -n +1 {found} " + \
25 "| sed 's/==> /# ==> /' > tmp.txt ; enscript --font=Courier10 " + \
26 "--continuous-page-numbers --no-header --pretty-print=python " + \
27 "-o - tmp.txt | psnup -2 -P letter -p a4 -m12 | ps2pdf - {name}.pdf ; rm tmp.txt"
28 #"-o - tmp.txt | ps2pdf - {name}.pdf ; rm tmp.txt"
29 found = filename
30 with InsideDir('tests/' + path):
31 if exists('test_' + name):
32 found += ' ya2/tests/%s/test_%s' % (path, name)
33 test_cmd = test_tmpl.format(name=noext, found=found)
34 system(test_cmd)
35 #system('pdfnup --nup 2x1 -o {noext}.pdf {noext}.pdf'.format(noext=noext))