link_asrc.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import argparse
  2. import itertools
  3. import os
  4. import tarfile
  5. DELIM_JAVA = '__DELIM_JAVA__'
  6. DELIM_RES = '__DELIM_RES__'
  7. DELIM_ASSETS = '__DELIM_ASSETS__'
  8. DELIM_AIDL = '__DELIM_AIDL__'
  9. DELIMS = (
  10. DELIM_JAVA,
  11. DELIM_RES,
  12. DELIM_ASSETS,
  13. DELIM_AIDL,
  14. )
  15. DESTS = {
  16. DELIM_JAVA: 'src',
  17. DELIM_RES: 'res',
  18. DELIM_ASSETS: 'assets',
  19. DELIM_AIDL: 'aidl',
  20. }
  21. def parse_args():
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument('--asrcs', nargs='*')
  24. parser.add_argument('--input', nargs='*')
  25. parser.add_argument('--jsrcs', nargs='*')
  26. parser.add_argument('--output', required=True)
  27. parser.add_argument('--work', required=True)
  28. return parser.parse_args()
  29. def main():
  30. args = parse_args()
  31. files = []
  32. parts = []
  33. if args.input and len(args.input) > 0:
  34. for x in args.input:
  35. if x in DELIMS:
  36. assert(len(parts) == 0 or len(parts[-1]) > 1)
  37. parts.append([x])
  38. else:
  39. assert(len(parts) > 0)
  40. parts[-1].append(x)
  41. assert(len(parts[-1]) > 1)
  42. if args.jsrcs and len(args.jsrcs):
  43. src_dir = os.path.join(args.work, DESTS[DELIM_JAVA])
  44. os.makedirs(src_dir)
  45. for jsrc in filter(lambda x: x.endswith('.jsrc'), args.jsrcs):
  46. with tarfile.open(jsrc, 'r') as tar:
  47. names = tar.getnames()
  48. if names and len(names) > 0:
  49. parts.append([DELIM_JAVA, src_dir])
  50. parts[-1].extend(itertools.imap(lambda x: os.path.join(src_dir, x), names))
  51. tar.extractall(path=src_dir)
  52. if args.asrcs and len(args.asrcs):
  53. for asrc in filter(lambda x: x.endswith('.asrc') and os.path.exists(x), args.asrcs):
  54. with tarfile.open(asrc, 'r') as tar:
  55. files.extend(tar.getnames())
  56. tar.extractall(path=args.work)
  57. with tarfile.open(args.output, 'w') as out:
  58. for part in parts:
  59. dest = DESTS[part[0]]
  60. prefix = part[1]
  61. for f in part[2:]:
  62. out.add(f, arcname=os.path.join(dest, os.path.relpath(f, prefix)))
  63. for f in files:
  64. out.add(os.path.join(args.work, f), arcname=f)
  65. if __name__ == '__main__':
  66. main()