]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/genheader.py
release: 1.1.1
[lilypond.git] / buildscripts / genheader.py
1 #!@PYTHON@
2
3 # genheader.py -- do headers (like these) 
4
5 # source file of the GNU LilyPond music typesetter
6
7 # (c) 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9 import os
10 import sys
11 import pwd
12 import regex
13 import regsub
14 import string
15 import getopt
16 import time
17
18 class My_options:
19     def __init__(self):
20         self.commentify = None
21         self.add_hdr_def = 0
22         self.classname = ''
23
24 my_options = My_options()
25
26
27 def name():
28         return os.environ['USERNAME']
29
30 # field 4 of passwd is also used for finger info (phone no.,  office etc)
31 #   return pwd.getpwuid(os.getuid())[4]
32
33 def c_commentify(str):
34     return  '/* ' + regsub.gsub('^','  ', str) + '\n */';
35
36 def sh_commentify(str):
37     return regsub.gsub('^', '# ', str)
38
39 def tex_commentify(str):
40     return regsub.gsub('^', '% ', str)
41
42 def project_str():
43     cwd = os.getcwd()
44     if regex.search('flower', cwd) <> -1:
45         PROJECT = "the Flower Library"
46     elif regex.search('mf$', cwd) <> -1:
47         PROJECT = "the Feta (defintively not an abbreviation for Font-En-Tja) music font"
48     else:
49         PROJECT = "the GNU LilyPond music typesetter"
50     return PROJECT
51
52 def head_str(filename):
53     if my_options.add_hdr_def:
54         what = "declare " 
55     else:
56         what=" implement "
57
58         
59     mailaddres = ''
60     try:
61             mailaddres = '<%s>' % os.environ['MAILADDRESS']
62     except KeyError:
63             pass
64     headstr = '\n%s -- %s\n\nsource file of %s\n\n(c) %d %s %s\n' \
65               %(filename, what, project_str(),
66                 time.localtime (time.time ())[0], name(), mailaddres)
67     return headstr
68
69
70 def c_include(filename):
71     startdef= filename;
72     trans = string.maketrans( string.lowercase + '-.', string.uppercase + '__')
73     startdef = string.translate(filename, trans)
74
75    
76     headstr = "\n\n#ifndef %s\n#define %s\n" % (startdef, startdef)
77     terminatestr = "#endif /* %s */\n"  % (startdef);
78
79     return headstr+ '\n\n'+ terminatestr;
80
81
82 def help ():
83     sys.stdout.write ("Usage: genheader [options] FILENAME\n"
84                  + "Generate file with header FILENAME\n\n"
85                  + "Options:\n"
86                  + "  -h, --header           generate header\n"
87                  + "  --help                 print this help\n"
88                  + "  -p, --package=DIR      specify package\n"
89                       )
90     
91     sys.exit (0)
92
93
94 (options, files) = getopt.getopt(sys.argv[1:], 'tcshp:', ['class', 'package=', 'help']) 
95
96 for opt in options:
97     o = opt[0]
98     a = opt[1]
99     if o == '-c':
100         my_options.commentify = c_commentify
101     elif o == '-t':
102         my_options.commentify = tex_commentify
103     elif o == '-s':
104         my_options.commentify = sh_commentify
105     elif o == '-h' or o == '--header':
106         my_options.add_hdr_def = 1
107     elif o == '--class':
108         my_options.classname = a
109     elif o == '--help':
110         help ()
111
112 # FIXME:  should create xxx.cc and include/xxx.hh, with implement/declare Xxx
113 # in  one run
114 if my_options.classname:
115     pass
116         
117 def do_file(nm):
118     s = my_options.commentify(head_str(nm)) 
119     if my_options.add_hdr_def:
120         s = s + c_include(nm)
121     return s
122
123
124 def extension(ext,nm):
125     ext = '\\.' + ext
126     return regex.search(ext, nm) <> -1
127
128 def c_extension(nm):
129     return extension('hh',nm) or extension('cc',nm) \
130            or extension('icc', nm) or extension('tcc',nm)
131
132 def select_commentification(nm):
133     if c_extension (nm):
134         return c_commentify
135     elif extension('py',nm) or extension('pl',nm) or extension('sh',nm):
136         return  sh_commentify
137     elif extension('mf',nm) or extension('tex',nm) or extension('ly',nm):
138         return tex_commentify
139     else:
140         sys.stderr.write ('unknown extension for file %s\n' % nm)
141         raise 'help'
142
143 for nm in files:
144     if extension('hh', nm) or extension('icc', nm) or  extension('tcc', nm): 
145         my_options.add_hdr_def = 1
146     if my_options.commentify == None:
147         my_options.commentify = select_commentification(nm)
148     print do_file(nm)
149