1 | ''' |
---|
2 | Date: june/29/2005 |
---|
3 | Description: Class and methods to use CMT with Python sources, based on the christian's python class. |
---|
4 | |
---|
5 | Simple usage: |
---|
6 | import CMT |
---|
7 | |
---|
8 | if __name__ == "__main__": |
---|
9 | |
---|
10 | cmt = CMT() |
---|
11 | |
---|
12 | package = 'CMTpy' |
---|
13 | |
---|
14 | package_root = cmt.macro_value (package + '_root') |
---|
15 | print package_root |
---|
16 | |
---|
17 | if cmt.tag (package + '_with_installarea'): |
---|
18 | print package + ' strategy is with installarea' |
---|
19 | |
---|
20 | print "Macros: ", cmt.macros , '\n' |
---|
21 | print "Tags: ", cmt.tags , '\n' |
---|
22 | print "Sets: ", cmt.sets , '\n' |
---|
23 | |
---|
24 | ''' |
---|
25 | __author__ = "Vincent Garonne" |
---|
26 | __email__ = "garonne at lal dot in2p3 dot fr" |
---|
27 | __version__ = "v1" |
---|
28 | __all__ = ["CMT"] |
---|
29 | |
---|
30 | import os |
---|
31 | import sys |
---|
32 | import string |
---|
33 | import popen2 |
---|
34 | import stat |
---|
35 | import re |
---|
36 | import commands |
---|
37 | from os import path |
---|
38 | |
---|
39 | |
---|
40 | #---------------------------------------------------- |
---|
41 | def execute (cmd): |
---|
42 | """Executing a shell command |
---|
43 | """ |
---|
44 | #print '> ' + cmd |
---|
45 | r, w, e = popen2.popen3(cmd) |
---|
46 | lines = [] |
---|
47 | for line in r.readlines(): |
---|
48 | ##print line |
---|
49 | line = string.rstrip (line) |
---|
50 | lines.append (line) |
---|
51 | #for line in e.readlines(): |
---|
52 | # line = string.rstrip (line) |
---|
53 | # lines.append (line) |
---|
54 | r.close() |
---|
55 | w.close() |
---|
56 | e.close() |
---|
57 | return lines |
---|
58 | |
---|
59 | class CMT: |
---|
60 | |
---|
61 | def __init__ (self): |
---|
62 | """Constructor |
---|
63 | """ |
---|
64 | # __init__ |
---|
65 | # Variables |
---|
66 | self.cmtexe = os.environ ['CMTROOT'] + '/' + os.environ ['CMTBIN'] + '/cmt.exe -tag_add=Doxygen ' |
---|
67 | self.macros = dict () |
---|
68 | self.tags = dict () |
---|
69 | self.sets = dict () |
---|
70 | self.uses = [] |
---|
71 | self.top_uses = [] |
---|
72 | |
---|
73 | # Initialyzation |
---|
74 | self.init_sets () |
---|
75 | self.init_macros () |
---|
76 | self.init_tags () |
---|
77 | self.init_uses () |
---|
78 | |
---|
79 | def expand_pattern (self, text, p1, p2): |
---|
80 | v = text |
---|
81 | rexp = '' |
---|
82 | for c in p1: |
---|
83 | rexp += r'[' + c + r']' |
---|
84 | rexp += '([^' + p2 + r']*)[' + p2 + r']' |
---|
85 | while True: |
---|
86 | ms = re.findall (rexp, v) |
---|
87 | if len(ms) == 0: |
---|
88 | break |
---|
89 | for m in ms: |
---|
90 | if m in self.macros: |
---|
91 | v = string.replace (v, p1 + m + p2, self.macros[m]) |
---|
92 | elif m in os.environ: |
---|
93 | v = string.replace (v, p1 + m + p2, os.environ [m]) |
---|
94 | else: |
---|
95 | v = string.replace (v, p1 + m + p2, '') |
---|
96 | # while True |
---|
97 | return (v) |
---|
98 | |
---|
99 | def expand (self, text): |
---|
100 | v = text |
---|
101 | v = self.expand_pattern (v, '$(', ')') |
---|
102 | v = self.expand_pattern (v, '${', '}') |
---|
103 | v = self.expand_pattern (v, '%', '%') |
---|
104 | return (v) |
---|
105 | # expand |
---|
106 | |
---|
107 | # Pre-fetch the macro values and expand all of them |
---|
108 | def init_macros (self): |
---|
109 | lines = execute (self.cmtexe + ' show macros') |
---|
110 | for line in lines: |
---|
111 | w = string.split (line, '=') |
---|
112 | name = w[0] |
---|
113 | if len(w)>=2: |
---|
114 | value = re.sub ('^[\']', '', w[1]) |
---|
115 | value = re.sub ('[\']$', '', value) |
---|
116 | self.macros [name] = value |
---|
117 | for key in self.macros.keys(): |
---|
118 | self.macros[key] = self.expand (self.macros[key]) |
---|
119 | #print key + '=' + v |
---|
120 | # for key in self.macros.keys(): |
---|
121 | # init_macros |
---|
122 | |
---|
123 | def init_sets (self): |
---|
124 | lines = execute (self.cmtexe + ' show sets') |
---|
125 | for line in lines: |
---|
126 | w = string.split (line, '=') |
---|
127 | name = w[0] |
---|
128 | if len(w)>=2: |
---|
129 | value = re.sub ('^[\']', '', w[1]) |
---|
130 | value = re.sub ('[\']$', '', value) |
---|
131 | self.macros [name] = value |
---|
132 | for key in self.macros.keys(): |
---|
133 | self.macros[key] = self.expand (self.macros[key]) |
---|
134 | #print key + '=' + v |
---|
135 | # for key in self.macros.keys(): |
---|
136 | # init_sets |
---|
137 | |
---|
138 | def init_tags (self): |
---|
139 | lines = execute (self.cmtexe + ' show tags') |
---|
140 | for line in lines: |
---|
141 | w = string.split (line, ' ') |
---|
142 | name = w[0] |
---|
143 | #print 'tag=' + name |
---|
144 | self.tags [name] = True |
---|
145 | # init_tags |
---|
146 | |
---|
147 | def init_uses (self): |
---|
148 | lines = execute (self.cmtexe + ' show uses') |
---|
149 | for line in lines: |
---|
150 | if line[0] == '#': |
---|
151 | if line[0:5] == '# use': |
---|
152 | w = string.split (line, ' ') |
---|
153 | #print 'init_uses> [' + w[2] + ']' |
---|
154 | self.top_uses.append (w[2]) |
---|
155 | continue |
---|
156 | self.uses.append (line) |
---|
157 | # init_uses |
---|
158 | |
---|
159 | def macro_value (self, name): |
---|
160 | if not self.macros.has_key (name): |
---|
161 | return '' |
---|
162 | return self.macros[name] |
---|
163 | # macro_value |
---|
164 | |
---|
165 | def tag (self, name): |
---|
166 | if not self.tags.has_key (name): |
---|
167 | return False |
---|
168 | return True |
---|
169 | # tag |
---|
170 | |
---|
171 | def do (self, cmd): |
---|
172 | execute (self.cmtexe + ' ' + cmd) |
---|
173 | # do |
---|
174 | |
---|
175 | ############################################################################## |
---|
176 | # Executing this module from the command line |
---|
177 | ############################################################################## |
---|
178 | |
---|
179 | if __name__ == "__main__": |
---|
180 | |
---|
181 | cmt = CMT() |
---|
182 | |
---|
183 | package = 'CMTpy' |
---|
184 | |
---|
185 | package_root = cmt.macro_value (package + '_root') |
---|
186 | print package_root |
---|
187 | |
---|
188 | if cmt.tag (package + '_with_installarea'): |
---|
189 | print package + ' strategy is with installarea' |
---|
190 | |
---|
191 | print "Macros: ", cmt.macros , '\n' |
---|
192 | print "Tags: ", cmt.tags , '\n' |
---|
193 | print "Sets: ", cmt.sets , '\n' |
---|
194 | |
---|
195 | #--------------------------------- EoF --------------------------------------# |
---|
196 | |
---|
197 | |
---|