#!/usr/bin/python # -*- coding: utf-8 -*- # import getopt, sys import os.path import re import json import locale import shutil import locale import platform from utils.utils import recursiveFind reTitle = re.compile('(?<=beginsong\\{)(.(? 0: out.write(formatDefinition('songslist', songslist(library, songs, prefixes))) out.write('\\makeatother\n') # output template commentPattern = re.compile(r"^\s*%") f = open("templates/"+template) content = [ line for line in f if not commentPattern.match(line) ] for index, line in enumerate(content): if re.compile("getLibraryImgDirectory").search(line): line = line.replace("\\getLibraryImgDirectory", library + "img/") content[index] = line if re.compile("getLibraryLilypondDirectory").search(line): line = line.replace("\\getLibraryLilypondDirectory", library + "lilypond/") content[index] = line f.close() out.write(''.join(content)) out.close() def makeDepend(sb, library, output): name = output[:-2] indexPattern = re.compile(r"^[^%]*\\(?:newauthor|new)index\{.*\}\{(.*?)\}") lilypondPattern = re.compile(r"^[^%]*\\(?:lilypond)\{(.*?)\}") # check for deps (in sb data) deps = []; if sb["songs"] == "all": deps += recursiveFind(os.path.join(library, 'songs'), '*.sg') else: deps += map(lambda x: library + "songs/" + x, sb["songs"]) # check for lilypond deps (in songs data) if necessary lilypond = [] if "bookoptions" in sb and "lilypond" in sb["bookoptions"]: for filename in deps: tmpl = open(filename) lilypond += matchRegexp(lilypondPattern, tmpl) tmpl.close() # check for index (in template file) if "template" in sb: filename = sb["template"] else: filename = "patacrep.tmpl" tmpl = open("templates/"+filename) idx = map(lambda x: x.replace("\getname", name), matchRegexp(indexPattern, tmpl)) tmpl.close() # write .d file out = open(output, 'w') out.write('{0} {1} : {2}\n'.format(output, name+".tex", ' '.join(deps))) out.write('{0} : {1}\n'.format(name+".pdf", ' '.join(map(lambda x: x+".sbx",idx)+map(lambda x: library+"lilypond/"+x+".pdf", lilypond)))) out.write('\t$(LATEX) {0}\n'.format(name+".tex")) out.write('{0} : {1}\n'.format(' '.join(map(lambda x: x+".sxd",idx)), name+".aux")) out.close() def usage(): print "No usage information yet." def main(): locale.setlocale(locale.LC_ALL, '') # set script locale to match user's try: opts, args = getopt.getopt(sys.argv[1:], "hs:o:l:d", ["help","songbook=","output=","depend","library="]) except getopt.GetoptError, err: # print help and exit print str(err) usage() sys.exit(2) songbook = None depend = False output = None library = './' for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() elif o in ("-s", "--songbook"): songbook = a elif o in ("-d", "--depend"): depend = True elif o in ("-o", "--output"): output = a elif o in ("-l", "--library"): if not a.endswith('/'): a += '/' library = a else: assert False, "unhandled option" if songbook and output: f = open(songbook) sb = json.load(f) f.close() if depend: makeDepend(sb, library, output) else: makeTexFile(sb, library, output) if __name__ == '__main__': main()