lilypond-1.3.67
[lilypond.git] / buildscripts / genheader.py
blobbfe64c9be67e191c8599a4b9ac98cfcad7059e27
1 #!@PYTHON@
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>
9 import os
10 import sys
11 import pwd
12 import re
13 import string
14 import getopt
15 import time
17 class My_options:
18 def __init__(self):
19 self.commentify = None
20 self.add_hdr_def = 0
21 self.classname = ''
23 my_options = My_options()
26 def name():
27 return os.environ['USERNAME']
29 # field 4 of passwd is also used for finger info (phone no., office etc)
30 # return pwd.getpwuid(os.getuid())[4]
32 def c_commentify(str):
33 return '/* ' + re.sub('^',' ', str) + '\n */';
35 def sh_commentify(str):
36 return re.sub('^', '# ', str)
38 def tex_commentify(str):
39 return re.sub('^', '% ', str)
41 def project_str():
42 cwd = os.getcwd()
43 if re.search('flower', cwd):
44 PROJECT = "the Flower Library"
45 elif re.search('mf$', cwd):
46 PROJECT = "the Feta (defintively not an abbreviation for Font-En-Tja) music font"
47 else:
48 PROJECT = "the GNU LilyPond music typesetter"
49 return PROJECT
51 def head_str(filename):
52 if my_options.add_hdr_def:
53 what = "declare "
54 else:
55 what=" implement "
58 mailaddres = ''
59 try:
60 mailaddres = '<%s>' % os.environ['MAILADDRESS']
61 except KeyError:
62 pass
63 headstr = '\n%s -- %s\n\nsource file of %s\n\n(c) %d %s %s\n' \
64 %(filename, what, project_str(),
65 time.localtime (time.time ())[0], name(), mailaddres)
66 return headstr
69 def c_include(filename):
70 startdef= filename;
71 trans = string.maketrans( string.lowercase + '-.', string.uppercase + '__')
72 startdef = string.translate(filename, trans)
75 headstr = "\n\n#ifndef %s\n#define %s\n" % (startdef, startdef)
76 terminatestr = "#endif /* %s */\n" % (startdef);
78 return headstr+ '\n\n'+ terminatestr;
81 def icc_include (filename):
82 startdef= filename;
83 trans = string.maketrans( string.lowercase + '-.', string.uppercase + '__')
84 startdef = string.translate(filename, trans)
87 headstr = "\n\n#ifndef %s\n#define %s\n" % (startdef, startdef)
88 headstr = headstr + r"""
89 #ifndef INLINE
90 #define INLINE inline
91 #define LOCAL_INLINE_DEF
92 #endif
93 """
94 terminatestr = "#endif /* %s */\n" % (startdef);
96 terminatestr = r"""
97 #ifdef LOCAL_INLINE_DEF
98 #undef LOCAL_INLINE_DEF
99 #undef INLINE
100 #endif
101 """ + terminatestr
102 return headstr+ '\n\n'+ terminatestr;
106 def help ():
107 sys.stdout.write ("Usage: genheader [options] FILENAME\n"
108 + "Generate file with header FILENAME\n\n"
109 + "Options:\n"
110 + " -h, --header generate header\n"
111 + " --help print this help\n"
112 + " -p, --package=DIR specify package\n"
115 sys.exit (0)
118 (options, files) = getopt.getopt(sys.argv[1:], 'tcshp:', ['class', 'package=', 'help'])
120 for opt in options:
121 o = opt[0]
122 a = opt[1]
123 if o == '-c':
124 my_options.commentify = c_commentify
125 elif o == '-t':
126 my_options.commentify = tex_commentify
127 elif o == '-s':
128 my_options.commentify = sh_commentify
129 elif o == '-h' or o == '--header':
130 my_options.add_hdr_def = 1
131 elif o == '--class':
132 my_options.classname = a
133 elif o == '--help':
134 help ()
136 # FIXME: should create xxx.cc and include/xxx.hh, with implement/declare Xxx
137 # in one run
138 if my_options.classname:
139 pass
141 def do_file(nm):
142 s = my_options.commentify(head_str(nm))
143 if my_options.add_hdr_def:
144 s = s + c_include(nm)
145 return s
148 def extension(ext,nm):
149 ext = '\\.' + ext
150 return re.search(ext, nm) <> None
152 def c_extension(nm):
153 return extension('hh',nm) or extension('cc',nm) \
154 or extension('icc', nm) or extension('tcc',nm)
156 def select_commentification(nm):
157 if c_extension (nm):
158 return c_commentify
159 elif extension('py',nm) or extension('pl',nm) or extension('sh',nm):
160 return sh_commentify
161 elif extension('mf',nm) or extension('tex',nm) or extension('ly',nm):
162 return tex_commentify
163 else:
164 sys.stderr.write ('unknown extension for file %s\n' % nm)
165 raise 'help'
167 for nm in files:
168 if extension('hh', nm) or extension('icc', nm) or extension('tcc', nm):
169 my_options.add_hdr_def = 1
170 if my_options.commentify == None:
171 my_options.commentify = select_commentification(nm)
172 print do_file(nm)