source: CMTpy/v1/python/cmt.py @ 276

Last change on this file since 276 was 276, checked in by garonne, 18 years ago

first import

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