]> git.donarmstrong.com Git - lilypond.git/blob - bin/genheader.py
partial: 1.0.1.jcn
[lilypond.git] / bin / genheader.py
1 #!@PYTHON@
2
3
4 # genheader.py -- do headers (like these) 
5
6 # source file of the GNU LilyPond music typesetter
7
8 # (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9
10
11
12
13 import posix
14 import pwd
15 import regex
16 import regsub
17 import string
18 import sys
19 import os
20 import getopt
21 import time
22 lilypath =''
23 try:
24     lilypath = os.environ['LILYPOND_SOURCEDIR'] + '/'
25 except KeyError:
26     print 'Please set LILYPOND_SOURCEDIR to the toplevel source, eg LILYPOND_SOURCEDIR=/home/foobar/lilypond-1.2.3/'
27     sys.exit(1)
28
29 lilypath = lilypath + '/bin/'
30 sys.path.append(lilypath)
31
32 from lilypython import *
33
34 class My_options:
35     def __init__(self):
36         self.commentify = None
37         self.add_hdr_def = 0
38         self.classname = ''
39
40 my_options = My_options()
41
42
43 def name():
44     return pwd.getpwuid(posix.getuid())[4]
45
46 def c_commentify(str):
47     return  '/* ' + regsub.gsub('^','  ', str) + '\n */';
48
49 def sh_commentify(str):
50     return regsub.gsub('^', '# ', str)
51
52 def tex_commentify(str):
53     return regsub.gsub('^', '% ', str)
54
55 def project_str():
56     cwd = posix.getcwd()
57     if regex.search('flower', cwd) <> -1:
58         PROJECT = "the Flower Library"
59     elif regex.search('mf$', cwd) <> -1:
60         PROJECT = "the Feta (defintively not an abbreviation for Font-En-Tja) music font"
61     else:
62         PROJECT = "the GNU LilyPond music typesetter"
63     return PROJECT
64
65 def head_str(filename):
66     if my_options.add_hdr_def:
67         what = "declare " 
68     else:
69         what=" implement "
70
71
72     headstr = '\n%s -- %s\n\nsource file of %s\n\n(c) %d %s <%s>\n' \
73               %(filename, what, project_str(), time.localtime (time.time ())[0], name(), mailaddress())
74     return headstr
75
76
77 def c_include(filename):
78     startdef= filename;
79     trans = string.maketrans( string.lowercase + '-.', string.uppercase + '__')
80     startdef = string.translate(filename, trans)
81
82    
83     headstr = "\n\n#ifndef %s\n#define %s\n" % (startdef, startdef)
84     terminatestr = "#endif /* %s */\n"  % (startdef);
85
86     return headstr+ '\n\n'+ terminatestr;
87
88
89
90
91
92 (options, files) = getopt.getopt(sys.argv[1:], 'tcsh', ['class']) 
93
94 for opt in options:
95     o = opt[0]
96     a = opt[1]
97     if o == '-c':
98         my_options.commentify = c_commentify
99     elif o == '-t':
100         my_options.commentify = tex_commentify
101     elif o == '-s':
102         my_options.commentify = sh_commentify
103     elif o == '-h':
104         my_options.add_hdr_def = 1
105     elif o == '--class':
106         my_options.classname = a
107
108
109
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 regex.search(ext, nm) <> -1
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 def select_commentification(nm):
131     if c_extension (nm):
132         return c_commentify
133     elif extension('py',nm) or extension('pl',nm) or extension('sh',nm):
134         return  sh_commentify
135     elif extension('mf',nm) or extension('tex',nm) or extension('ly',nm):
136         return tex_commentify
137     else: 
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)