#!/usr/bin/env python import argparse import os import shutil import subprocess from docutils.core import publish_file def build_html(srcdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) for fname in os.listdir(srcdir): (basename,ext) = os.path.splitext(fname) if ext == '.rst': publish_file(source_path=os.path.join(srcdir,fname), destination_path=os.path.join(destdir,basename+'.html'), writer_name="html", settings_overrides={'initial_header_level':'2'}) else: shutil.copy(os.path.join(srcdir,fname),os.path.join(destdir,fname)) def build_latex(srcdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) for fname in os.listdir(srcdir): (basename,ext) = os.path.splitext(fname) if ext == '.rst': publish_file(source_path=os.path.join(srcdir,fname), destination_path=os.path.join(destdir,basename+'.tex'), writer_name="xetex", settings_overrides={'stylesheet':'amssymb'}) #elif ext == '.svg': # subprocess.call(['inkscape','--export-pdf', # os.path.join(destdir,basename+'pdf'), # os.path.join(srcdir,fname)]) else: shutil.copy(os.path.join(srcdir,fname),os.path.join(destdir,fname)) def build_pdf(latexdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) destdir = os.path.abspath(destdir) old_dir = os.path.abspath(os.getcwd()) os.chdir(latexdir) for fname in os.listdir('.'): (basename,ext) = os.path.splitext(fname) if ext == '.tex': if subprocess.call(['xelatex',fname]) == 0: subprocess.call(['xelatex',fname]) subprocess.call(['xelatex',fname]) shutil.copy(basename+'.pdf',destdir) os.chdir(old_dir) def build_odt(srcdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) for fname in os.listdir(srcdir): (basename,ext) = os.path.splitext(fname) if ext == '.rst': try: publish_file(source_path=os.path.join(srcdir,fname), destination_path=os.path.join(destdir,basename+'.odt'), writer_name="odf_odt") except IOError: # allow it to die on PIL issues pass def main(): parser = argparse.ArgumentParser(description='building script') parser.add_argument('directory',nargs='+',choices=['esempi','strumenti']) parser.add_argument('-H','--html',action='store_true') parser.add_argument('-l','--latex',action='store_true') parser.add_argument('-p','--pdf',action='store_true') parser.add_argument('-o','--odt',action='store_true') args = parser.parse_args() for dirname in args.directory: if args.html: build_html(dirname,os.path.join('build','html',dirname)) if args.latex or args.pdf: build_latex(dirname,os.path.join('build','latex',dirname)) if args.pdf: build_pdf(os.path.join('build','latex',dirname), os.path.join('build','pdf',dirname)) if args.odt: build_odt(dirname,os.path.join('build','odt',dirname)) if __name__ == '__main__': main()