root/branches/Carsten_PtrWork2/Tools/scons-local/sconsign.py

Revision 568, 14.6 kB (checked in by vossg, 2 years ago)

added : scons local (0.96.95)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 #! /usr/bin/env python
2 #
3 # SCons - a Software Constructor
4 #
5 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 The SCons Foundation
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #
26
27 __revision__ = "/home/scons/scons/branch.0/branch.96/baseline/src/script/sconsign.py 0.96.95.D002 2007/02/14 11:01:59 knight"
28
29 __version__ = "0.96.95"
30
31 __build__ = "D002"
32
33 __buildsys__ = "roxbury"
34
35 __date__ = "2007/02/14 11:01:59"
36
37 __developer__ = "knight"
38
39 import os
40 import os.path
41 import sys
42 import time
43
44 ##############################################################################
45 # BEGIN STANDARD SCons SCRIPT HEADER
46 #
47 # This is the cut-and-paste logic so that a self-contained script can
48 # interoperate correctly with different SCons versions and installation
49 # locations for the engine.  If you modify anything in this section, you
50 # should also change other scripts that use this same header.
51 ##############################################################################
52
53 # Strip the script directory from sys.path() so on case-insensitive
54 # (WIN32) systems Python doesn't think that the "scons" script is the
55 # "SCons" package.  Replace it with our own library directories
56 # (version-specific first, in case they installed by hand there,
57 # followed by generic) so we pick up the right version of the build
58 # engine modules if they're in either directory.
59
60 script_dir = sys.path[0]
61
62 if script_dir in sys.path:
63     sys.path.remove(script_dir)
64
65 libs = []
66
67 if os.environ.has_key("SCONS_LIB_DIR"):
68     libs.append(os.environ["SCONS_LIB_DIR"])
69
70 local = 'scons-local-' + __version__
71 if script_dir:
72     local = os.path.join(script_dir, local)
73 libs.append(os.path.abspath(local))
74
75 scons_version = 'scons-%s' % __version__
76
77 prefs = []
78
79 if sys.platform == 'win32':
80     # sys.prefix is (likely) C:\Python*;
81     # check only C:\Python*.
82     prefs.append(sys.prefix)
83     prefs.append(os.path.join(sys.prefix, 'Lib', 'site-packages'))
84 else:
85     # On other (POSIX) platforms, things are more complicated due to
86     # the variety of path names and library locations.  Try to be smart
87     # about it.
88     if script_dir == 'bin':
89         # script_dir is `pwd`/bin;
90         # check `pwd`/lib/scons*.
91         prefs.append(os.getcwd())
92     else:
93         if script_dir == '.' or script_dir == '':
94             script_dir = os.getcwd()
95         head, tail = os.path.split(script_dir)
96         if tail == "bin":
97             # script_dir is /foo/bin;
98             # check /foo/lib/scons*.
99             prefs.append(head)
100
101     head, tail = os.path.split(sys.prefix)
102     if tail == "usr":
103         # sys.prefix is /foo/usr;
104         # check /foo/usr/lib/scons* first,
105         # then /foo/usr/local/lib/scons*.
106         prefs.append(sys.prefix)
107         prefs.append(os.path.join(sys.prefix, "local"))
108     elif tail == "local":
109         h, t = os.path.split(head)
110         if t == "usr":
111             # sys.prefix is /foo/usr/local;
112             # check /foo/usr/local/lib/scons* first,
113             # then /foo/usr/lib/scons*.
114             prefs.append(sys.prefix)
115             prefs.append(head)
116         else:
117             # sys.prefix is /foo/local;
118             # check only /foo/local/lib/scons*.
119             prefs.append(sys.prefix)
120     else:
121         # sys.prefix is /foo (ends in neither /usr or /local);
122         # check only /foo/lib/scons*.
123         prefs.append(sys.prefix)
124
125     temp = map(lambda x: os.path.join(x, 'lib'), prefs)
126     temp.extend(map(lambda x: os.path.join(x,
127                                            'lib',
128                                            'python' + sys.version[:3],
129                                            'site-packages'),
130                            prefs))
131     prefs = temp
132
133     # Add the parent directory of the current python's library to the
134     # preferences.  On SuSE-91/AMD64, for example, this is /usr/lib64,
135     # not /usr/lib.
136     try:
137         libpath = os.__file__
138     except AttributeError:
139         pass
140     else:
141         while libpath:
142             libpath, tail = os.path.split(libpath)
143             if tail[:6] == "python":
144                 break
145         if libpath:
146             # Python library is in /usr/libfoo/python*;
147             # check /usr/libfoo/scons*.
148             prefs.append(libpath)
149
150 # Look first for 'scons-__version__' in all of our preference libs,
151 # then for 'scons'.
152 libs.extend(map(lambda x: os.path.join(x, scons_version), prefs))
153 libs.extend(map(lambda x: os.path.join(x, 'scons'), prefs))
154
155 sys.path = libs + sys.path
156
157 ##############################################################################
158 # END STANDARD SCons SCRIPT HEADER
159 ##############################################################################
160
161 import cPickle
162 import imp
163 import string
164 import whichdb
165
166 import SCons.SConsign
167
168 def my_whichdb(filename):
169     if filename[-7:] == ".dblite":
170         return "SCons.dblite"
171     try:
172         f = open(filename + ".dblite", "rb")
173         f.close()
174         return "SCons.dblite"
175     except IOError:
176         pass
177     return _orig_whichdb(filename)
178
179 _orig_whichdb = whichdb.whichdb
180 whichdb.whichdb = my_whichdb
181
182 def my_import(mname):
183     if '.' in mname:
184         i = string.rfind(mname, '.')
185         parent = my_import(mname[:i])
186         fp, pathname, description = imp.find_module(mname[i+1:],
187                                                     parent.__path__)
188     else:
189         fp, pathname, description = imp.find_module(mname)
190     return imp.load_module(mname, fp, pathname, description)
191
192 class Flagger:
193     default_value = 1
194     def __setitem__(self, item, value):
195         self.__dict__[item] = value
196         self.default_value = 0
197     def __getitem__(self, item):
198         return self.__dict__.get(item, self.default_value)
199
200 Do_Call = None
201 Print_Directories = []
202 Print_Entries = []
203 Print_Flags = Flagger()
204 Verbose = 0
205 Readable = 0
206 Raw = 0
207
208 def default_mapper(entry, name):
209     try:
210         val = eval("entry."+name)
211     except:
212         val = None
213     return str(val)
214
215 def map_timestamp(entry, name):
216     try:
217         timestamp = entry.timestamp
218     except AttributeError:
219         timestamp = None
220     if Readable and timestamp:
221         return "'" + time.ctime(timestamp) + "'"
222     else:
223         return str(timestamp)
224
225 def map_bkids(entry, name):
226     try:
227         bkids = entry.bsources + entry.bdepends + entry.bimplicit
228         bkidsigs = entry.bsourcesigs + entry.bdependsigs + entry.bimplicitsigs
229     except AttributeError:
230         return None
231     result = []
232     for i in xrange(len(bkids)):
233         result.append("%s: %s" % (bkids[i], bkidsigs[i]))
234     if result == []:
235         return None
236     return string.join(result, "\n        ")
237
238 map_field = {
239     'timestamp' : map_timestamp,
240     'bkids'     : map_bkids,
241 }
242
243 map_name = {
244     'implicit'  : 'bkids',
245 }
246
247 def field(name, entry, verbose=Verbose):
248     if not Print_Flags[name]:
249         return None
250     fieldname = map_name.get(name, name)
251     mapper = map_field.get(fieldname, default_mapper)
252     val = mapper(entry, name)
253     if verbose:
254         val = name + ": " + val
255     return val
256
257 def nodeinfo_raw(name, ninfo, prefix=""):
258     # This does essentially what the pprint module does,
259     # except that it sorts the keys for deterministic output.
260     d = ninfo.__dict__
261     keys = d.keys()
262     keys.sort()
263     l = []
264     for k in keys:
265         l.append('%s: %s' % (repr(k), repr(d[k])))
266     return name + ': {' + string.join(l, ', ') + '}'
267
268 def nodeinfo_string(name, ninfo, prefix=""):
269     fieldlist = ["bsig", "csig", "timestamp", "size"]
270     f = lambda x, ni=ninfo, v=Verbose: field(x, ni, v)
271     outlist = [name+":"] + filter(None, map(f, fieldlist))
272     if Verbose:
273         sep = "\n    " + prefix
274     else:
275         sep = " "
276     return string.join(outlist, sep)
277
278 def printfield(name, entry, prefix=""):
279     if Raw:
280         print nodeinfo_raw(name, entry.ninfo, prefix)
281     else:
282         print nodeinfo_string(name, entry.ninfo, prefix)
283
284     outlist = field("implicit", entry, 0)
285     if outlist:
286         if Verbose:
287             print "    implicit:"
288         print "        " + outlist
289
290 def printentries(entries):
291     if Print_Entries:
292         for name in Print_Entries:
293             try:
294                 entry = entries[name]
295             except KeyError:
296                 sys.stderr.write("sconsign: no entry `%s' in `%s'\n" % (name, args[0]))
297             else:
298                 printfield(name, entry)
299     else:
300         names = entries.keys()
301         names.sort()
302         for name in names:
303             printfield(name, entries[name])
304
305 class Do_SConsignDB:
306     def __init__(self, dbm_name, dbm):
307         self.dbm_name = dbm_name
308         self.dbm = dbm
309
310     def __call__(self, fname):
311         # The *dbm modules stick their own file suffixes on the names
312         # that are passed in.  This is causes us to jump through some
313         # hoops here to be able to allow the user
314         try:
315             # Try opening the specified file name.  Example:
316             #   SPECIFIED                  OPENED BY self.dbm.open()
317             #   ---------                  -------------------------
318             #   .sconsign               => .sconsign.dblite
319             #   .sconsign.dblite        => .sconsign.dblite.dblite
320             db = self.dbm.open(fname, "r")
321         except (IOError, OSError), e:
322             print_e = e
323             try:
324                 # That didn't work, so try opening the base name,
325                 # so that if the actually passed in 'sconsign.dblite'
326                 # (for example), the dbm module will put the suffix back
327                 # on for us and open it anyway.
328                 db = self.dbm.open(os.path.splitext(fname)[0], "r")
329             except (IOError, OSError):
330                 # That didn't work either.  See if the file name
331                 # they specified just exists (independent of the dbm
332                 # suffix-mangling).
333                 try:
334                     open(fname, "r")
335                 except (IOError, OSError), e:
336                     # Nope, that file doesn't even exist, so report that
337                     # fact back.
338                     print_e = e
339                 sys.stderr.write("sconsign: %s\n" % (print_e))
340                 return
341         except:
342             sys.stderr.write("sconsign: ignoring invalid `%s' file `%s'\n" % (self.dbm_name, fname))
343             return
344
345         if Print_Directories:
346             for dir in Print_Directories:
347                 try:
348                     val = db[dir]
349                 except KeyError:
350                     sys.stderr.write("sconsign: no dir `%s' in `%s'\n" % (dir, args[0]))
351                 else:
352                     self.printentries(dir, val)
353         else:
354             keys = db.keys()
355             keys.sort()
356             for dir in keys:
357                 self.printentries(dir, db[dir])
358
359     def printentries(self, dir, val):
360         print '=== ' + dir + ':'
361         printentries(cPickle.loads(val))
362
363 def Do_SConsignDir(name):
364     try:
365         fp = open(name, 'rb')
366     except (IOError, OSError), e:
367         sys.stderr.write("sconsign: %s\n" % (e))
368         return
369     try:
370         sconsign = SCons.SConsign.Dir(fp)
371     except:
372         sys.stderr.write("sconsign: ignoring invalid .sconsign file `%s'\n" % name)
373         return
374     printentries(sconsign.entries)
375
376 ##############################################################################
377
378 import getopt
379
380 helpstr = """\
381 Usage: sconsign [OPTIONS] FILE [...]
382 Options:
383   -b, --bsig                  Print build signature information.
384   -c, --csig                  Print content signature information.
385   -d DIR, --dir=DIR           Print only info about DIR.
386   -e ENTRY, --entry=ENTRY     Print only info about ENTRY.
387   -f FORMAT, --format=FORMAT  FILE is in the specified FORMAT.
388   -h, --help                  Print this message and exit.
389   -i, --implicit              Print implicit dependency information.
390   -r, --readable              Print timestamps in human-readable form.
391   --raw                       Print raw Python object representations.
392   -s, --size                  Print file sizes.
393   -t, --timestamp             Print timestamp information.
394   -v, --verbose               Verbose, describe each field.
395 """
396
397 opts, args = getopt.getopt(sys.argv[1:], "bcd:e:f:hirstv",
398                             ['bsig', 'csig', 'dir=', 'entry=',
399                              'format=', 'help', 'implicit',
400                              'raw', 'readable',
401                              'size', 'timestamp', 'verbose'])
402
403
404 for o, a in opts:
405     if o in ('-b', '--bsig'):
406         Print_Flags['bsig'] = 1
407     elif o in ('-c', '--csig'):
408         Print_Flags['csig'] = 1
409     elif o in ('-d', '--dir'):
410         Print_Directories.append(a)
411     elif o in ('-e', '--entry'):
412         Print_Entries.append(a)
413     elif o in ('-f', '--format'):
414         Module_Map = {'dblite'   : 'SCons.dblite',
415                       'sconsign' : None}
416         dbm_name = Module_Map.get(a, a)
417         if dbm_name:
418             try:
419                 dbm = my_import(dbm_name)
420             except:
421                 sys.stderr.write("sconsign: illegal file format `%s'\n" % a)
422                 print helpstr
423                 sys.exit(2)
424             Do_Call = Do_SConsignDB(a, dbm)
425         else:
426             Do_Call = Do_SConsignDir
427     elif o in ('-h', '--help'):
428         print helpstr
429         sys.exit(0)
430     elif o in ('-i', '--implicit'):
431         Print_Flags['implicit'] = 1
432     elif o in ('--raw',):
433         Raw = 1
434     elif o in ('-r', '--readable'):
435         Readable = 1
436     elif o in ('-s', '--size'):
437         Print_Flags['size'] = 1
438     elif o in ('-t', '--timestamp'):
439         Print_Flags['timestamp'] = 1
440     elif o in ('-v', '--verbose'):
441         Verbose = 1
442
443 if Do_Call:
444     for a in args:
445         Do_Call(a)
446 else:
447     for a in args:
448         dbm_name = whichdb.whichdb(a)
449         if dbm_name:
450             Map_Module = {'SCons.dblite' : 'dblite'}
451             dbm = my_import(dbm_name)
452             Do_SConsignDB(Map_Module.get(dbm_name, dbm_name), dbm)(a)
453         else:
454             Do_SConsignDir(a)
455
456 sys.exit(0)
Note: See TracBrowser for help on using the browser.