home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / freeze / checkextensions.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  2.0 KB  |  91 lines

  1. # Check for a module in a set of extension directories.
  2. # An extension directory should contain a Setup file
  3. # and one or more .o files or a lib.a file.
  4.  
  5. import os
  6. import string
  7. import parsesetup
  8.  
  9. def checkextensions(unknown, extensions):
  10.     files = []
  11.     modules = []
  12.     edict = {}
  13.     for e in extensions:
  14.         setup = os.path.join(e, 'Setup')
  15.         liba = os.path.join(e, 'lib.a')
  16.         if not os.path.isfile(liba):
  17.             liba = None
  18.         edict[e] = parsesetup.getsetupinfo(setup), liba
  19.     for mod in unknown:
  20.         for e in extensions:
  21.             (mods, vars), liba = edict[e]
  22.             if not mods.has_key(mod):
  23.                 continue
  24.             modules.append(mod)
  25.             if liba:
  26.                 # If we find a lib.a, use it, ignore the
  27.                 # .o files, and use *all* libraries for
  28.                 # *all* modules in the Setup file
  29.                 if liba in files:
  30.                     break
  31.                 files.append(liba)
  32.                 for m in mods.keys():
  33.                     files = files + select(e, mods, vars,
  34.                                    m, 1)
  35.                 break
  36.             files = files + select(e, mods, vars, mod, 0)
  37.             break
  38.     return files, modules
  39.  
  40. def select(e, mods, vars, mod, skipofiles):
  41.     files = []
  42.     for w in mods[mod]:
  43.         w = treatword(w)
  44.         if not w:
  45.             continue
  46.         w = expandvars(w, vars)
  47.         for w in string.split(w):
  48.             if skipofiles and w[-2:] == '.o':
  49.                 continue
  50.             if w[0] != '-' and w[-2:] in ('.o', '.a'):
  51.                 w = os.path.join(e, w)
  52.             if w[:2] in ('-L', '-R'):
  53.                 w = w[:2] + os.path.join(e, w[2:])
  54.             files.append(w)
  55.     return files
  56.  
  57. cc_flags = ['-I', '-D', '-U']
  58. cc_exts = ['.c', '.C', '.cc', '.c++']
  59.  
  60. def treatword(w):
  61.     if w[:2] in cc_flags:
  62.         return None
  63.     if w[:1] == '-':
  64.         return w # Assume loader flag
  65.     head, tail = os.path.split(w)
  66.     base, ext = os.path.splitext(tail)
  67.     if ext in cc_exts:
  68.         tail = base + '.o'
  69.         w = os.path.join(head, tail)
  70.     return w
  71.  
  72. def expandvars(str, vars):
  73.     i = 0
  74.     while i < len(str):
  75.         i = k = string.find(str, '$', i)
  76.         if i < 0:
  77.             break
  78.         i = i+1
  79.         var = str[i:i+1]
  80.         i = i+1
  81.         if var == '(':
  82.             j = string.find(str, ')', i)
  83.             if j < 0:
  84.                 break
  85.             var = str[i:j]
  86.             i = j+1
  87.         if vars.has_key(var):
  88.             str = str[:k] + vars[var] + str[i:]
  89.             i = k
  90.     return str
  91.