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