prebuilt_zip.py 2.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#/usr/bin/python

import os
import os.path
import re
import shutil
import sys
import tempfile
import zipfile

def main(argv=None):
  if argv is None:
    argv = sys.argv
  if len(argv) == 4:
    log_filename = argv[1]
    install_prefix = argv[2]
    zip_filename = argv[3]
    tempdir = tempfile.mkdtemp()
    rootdir = os.path.join(tempdir, os.path.basename(zip_filename).replace(".zip", ""))
    if not os.path.exists(rootdir):
      os.makedirs(rootdir)
    fin = open(log_filename, 'r')
23
    regexp1 = re.compile(r'^((.*?)/bin/)?install ((-c|-m [0-9]+) )+(.*)$')
24
    regexp2 = re.compile(r'^cp -p (.*)$')
25
    for line in fin:
26 27
      l = None
      result = regexp1.match(line)
28
      if result is not None:
29 30 31 32 33 34
        l = result.group(5).split()
      else:
        result = regexp2.match(line)
        if result is not None:
          l = result.group(1).split()
      if l is not None:
35 36 37 38 39 40 41 42 43 44 45 46 47
        if len(l) < 2:
          continue
        l = [item.replace('"', '').replace("'", "") for item in l]
        srcs = l[:-1]
        dst = l[-1]
        curpath = dst.replace(install_prefix, '')
        if curpath.startswith('/'):
          curpath = curpath[1:]
        curpath = os.path.join(rootdir, curpath)
        if os.path.isdir(dst):
          if not os.path.exists(curpath):
            os.makedirs(curpath)
          for src in srcs:
48 49 50 51
            if '*' in src:
              # TODO: handle wildcard and multiline with '\'
              continue
            shutil.copy(os.path.join(dst, os.path.basename(src)), curpath)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        else:
          if not os.path.exists(os.path.dirname(curpath)):
            os.makedirs(os.path.dirname(curpath))
          shutil.copyfile(dst, curpath)
    zipf = zipfile.ZipFile(zip_filename, 'w')
    for root, dirs, files in os.walk(tempdir):
      for f in files:
        fullpath = os.path.join(root, f)
        relativepath = fullpath.replace(tempdir, '')
        if relativepath.startswith('/'):
          relativepath = relativepath[1:]
        zipf.write(fullpath, relativepath, zipfile.ZIP_DEFLATED)
    zipf.close()
    shutil.rmtree(tempdir)

if __name__ == "__main__":
  sys.exit(main())