1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 from __future__ import print_function
22 import logging
23 from logging.handlers import SysLogHandler
24 from fnmatch import fnmatch
25 import os, re, sys, subprocess, getent
26
27 colonPairPat = re.compile(r"([^:]+):(.*)")
29 """
30 Configure the logging functions
31 """
32 logger = logging.getLogger()
33 logger.setLevel(logging.INFO)
34 syslog = SysLogHandler(address='/dev/log', facility='local3')
35 formatter = logging.Formatter('XALT: %(name)s: %(levelname)s %(message)r')
36 syslog.setFormatter(formatter)
37 logger.addHandler(syslog)
38 return logger
39
41 """
42 Take the output of pstree and find the compiler
43 @param pstree: the single line of processes back to init.
44 """
45 result = "unknown"
46 ignoreT = {
47 'pstree' : True,
48 'ld' : True,
49 'collect2' : True,
50 }
51
52 if (pstree == "unknown"):
53 return result
54
55 a = pstree.split("---")
56 n = len(a)
57
58 for cmd in reversed(a):
59 if (not (cmd in ignoreT)):
60 result = cmd
61 break
62
63 return cmd
64
66 """
67 Find a list of files in directory [[path]] that match [[pattern]]
68 @param path: directory.
69 @param pattern: file glob pattern.
70 """
71 fileA = []
72 wd = os.getcwd()
73 if (not os.path.isdir(path)):
74 return fileA
75
76 os.chdir(path)
77 path = os.getcwd()
78 os.chdir(wd)
79
80 for root, dirs, files in os.walk(path):
81 for name in files:
82 fn = os.path.join(root, name)
83 if (fnmatch(fn,pattern)):
84 fileA.append(fn)
85 return fileA
86
88 """
89 Find full path to [[program]]
90 @param program: program to find in path.
91 """
92
93 def is_exe(fpath):
94 """
95 Check to see if [[fpath]] exists and is executable
96 @param fpath: full path to executable
97 """
98 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
99
100 if (not program):
101 return None
102
103 fpath, fname = os.path.split(program)
104 if (fpath):
105 if (is_exe(program)):
106 return os.path.realpath(program)
107 else:
108 for path in os.environ.get("PATH","").split(os.pathsep):
109 exe_file = os.path.join(path, program)
110 for candidate in ext_candidates(exe_file):
111 if (is_exe(candidate)):
112 return os.path.realpath(candidate)
113
114 return None
115
117 """
118 Capture standard out for a command.
119 @param cmd: Command string or array.
120 """
121
122 if (type(cmd) == type(' ')):
123 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
124 stderr=subprocess.STDOUT, shell =True)
125 else:
126 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
127 stderr=subprocess.STDOUT)
128
129
130 return p.communicate()[0]
131
132
134 """
135 Check to make sure that the first part of v contains key. Raise exception if not.
136
137 @param key: key
138 @param v: value
139 """
140
141 k = "unknown"
142 m = colonPairPat.match(v)
143 found = False
144 if (m):
145 k = m.group(1)
146 if (k == key):
147 found = True
148 if (not found):
149 print("Wanted: ",key, ", got: ",v)
150 raise Exception
151
153 """
154 Remove this list of files. Do not raise exception if file doesn't exist.
155 @param fileA: array of file
156 """
157
158 for f in fileA:
159 try:
160 os.remove(f)
161 except:
162 pass
163
164 numberPat = re.compile(r'[0-9][0-9]*')
166 """
167 Determine what object type [[object_path]] is (a, o, so)
168 @param object_path: name of object.
169 """
170 result = None
171 a = object_path.split('.')
172 for entry in reversed(a):
173 m = numberPat.search(entry)
174 if (m):
175 continue
176 else:
177 result = entry
178 break
179 return result
180
181 defaultPat = re.compile(r'default:?')
183 """
184 Find module for [[object_path]] in [[reverseMapT]] if it is exists, "NULL" otherwise.
185 @param object_path: full object path
186 @param reverseMapT: reverse map table. Paths to modules
187 """
188
189
190 dirNm, fn = os.path.split(object_path)
191 moduleName = 'NULL'
192 pkg = reverseMapT.get(dirNm)
193 if (pkg):
194 flavor = pkg['flavor'][0]
195 flavor = defaultPat.sub('',flavor)
196 if (flavor):
197 moduleName = "'" + pkg['pkg'] + '(' + flavor + ")'"
198 else:
199 moduleName = "'" + pkg['pkg'] + "'"
200 return moduleName
201
202
204 """
205 Build config file name from dbname.
206 @param dbname: db name
207 """
208 return dbname + "_db.conf"
209