]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/genheader.py
release: 1.2.8
[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 re
13 import string
14 import getopt
15 import time
16
17 class My_options:
18     def __init__(self):
19         self.commentify = None
20         self.add_hdr_def = 0
21         self.classname = ''
22
23 my_options = My_options()
24
25
26 def name():
27         return os.environ['USERNAME']
28
29 # field 4 of passwd is also used for finger info (phone no.,  office etc)
30 #   return pwd.getpwuid(os.getuid())[4]
31
32 def c_commentify(str):
33     return  '/* ' + re.sub('^','  ', str) + '\n */';
34
35 def sh_commentify(str):
36     return re.sub('^', '# ', str)
37
38 def tex_commentify(str):
39     return re.sub('^', '% ', str)
40
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
50
51 def head_str(filename):
52     if my_options.add_hdr_def:
53         what = "declare " 
54     else:
55         what=" implement "
56
57         
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
67
68
69 def c_include(filename):
70     startdef= filename;
71     trans = string.maketrans( string.lowercase + '-.', string.uppercase + '__')
72     startdef = string.translate(filename, trans)
73
74    
75     headstr = "\n\n#ifndef %s\n#define %s\n" % (startdef, startdef)
76     terminatestr = "#endif /* %s */\n"  % (startdef);
77
78     return headstr+ '\n\n'+ terminatestr;
79
80
81 def help ():
82     sys.stdout.write ("Usage: genheader [options] FILENAME\n"
83                  + "Generate file with header FILENAME\n\n"
84                  + "Options:\n"
85                  + "  -h, --header           generate header\n"
86                  + "  --help                 print this help\n"
87                  + "  -p, --package=DIR      specify package\n"
88                       )
89     
90     sys.exit (0)
91
92
93 (options, files) = getopt.getopt(sys.argv[1:], 'tcshp:', ['class', 'package=', 'help']) 
94
95 for opt in options:
96     o = opt[0]
97     a = opt[1]
98     if o == '-c':
99         my_options.commentify = c_commentify
100     elif o == '-t':
101         my_options.commentify = tex_commentify
102     elif o == '-s':
103         my_options.commentify = sh_commentify
104     elif o == '-h' or o == '--header':
105         my_options.add_hdr_def = 1
106     elif o == '--class':
107         my_options.classname = a
108     elif o == '--help':
109         help ()
110
111 # FIXME:  should create xxx.cc and include/xxx.hh, with implement/declare Xxx
112 # in  one run
113 if my_options.classname:
114     pass
115         
116 def do_file(nm):
117     s = my_options.commentify(head_str(nm)) 
118     if my_options.add_hdr_def:
119         s = s + c_include(nm)
120     return s
121
122
123 def extension(ext,nm):
124     ext = '\\.' + ext
125     return re.search(ext, nm) <> None
126
127 def c_extension(nm):
128     return extension('hh',nm) or extension('cc',nm) \
129            or extension('icc', nm) or extension('tcc',nm)
130
131 def select_commentification(nm):
132     if c_extension (nm):
133         return c_commentify
134     elif extension('py',nm) or extension('pl',nm) or extension('sh',nm):
135         return  sh_commentify
136     elif extension('mf',nm) or extension('tex',nm) or extension('ly',nm):
137         return tex_commentify
138     else:
139         sys.stderr.write ('unknown extension for file %s\n' % nm)
140         raise 'help'
141
142 for nm in files:
143     if extension('hh', nm) or extension('icc', nm) or  extension('tcc', nm): 
144         my_options.add_hdr_def = 1
145     if my_options.commentify == None:
146         my_options.commentify = select_commentification(nm)
147     print do_file(nm)
148