link_asrc.py 2.5 KB

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