generate_mf.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import json
  2. import optparse
  3. import os
  4. import sys
  5. import io
  6. import six
  7. # Explicitly enable local imports
  8. # Don't forget to add imported scripts to inputs of the calling command!
  9. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  10. import process_command_files as pcf
  11. class BadMfError(Exception):
  12. pass
  13. class GplNotAllowed(Exception):
  14. pass
  15. def process_quotes(s):
  16. for quote_char in '\'"':
  17. if s.startswith(quote_char) and s.endswith(quote_char):
  18. return s[1:-1]
  19. return s
  20. def parse_args():
  21. args = pcf.get_args(sys.argv[1:])
  22. lics, peers, free_args, credits = [], [], [], []
  23. current_list = free_args
  24. for a in args:
  25. if a == '-Ya,lics':
  26. current_list = lics
  27. elif a == '-Ya,peers':
  28. current_list = peers
  29. elif a == '-Ya,credits':
  30. current_list = credits
  31. elif a and a.startswith('-'):
  32. current_list = free_args
  33. current_list.append(a)
  34. else:
  35. current_list.append(a)
  36. parser = optparse.OptionParser()
  37. parser.add_option('--build-root')
  38. parser.add_option('--module-name')
  39. parser.add_option('-o', '--output')
  40. parser.add_option('-c', '--credits-output')
  41. parser.add_option('-t', '--type')
  42. opts, _ = parser.parse_args(free_args)
  43. return (
  44. lics,
  45. peers,
  46. credits,
  47. opts,
  48. )
  49. def generate_header(meta):
  50. return '-' * 20 + meta.get('path', 'Unknown module') + '-' * 20
  51. def generate_mf():
  52. lics, peers, credits, options = parse_args()
  53. meta = {
  54. 'module_name': options.module_name,
  55. 'path': os.path.dirname(options.output),
  56. 'licenses': lics,
  57. 'dependencies': [],
  58. 'license_texts': '',
  59. }
  60. build_root = options.build_root
  61. file_name = os.path.join(build_root, options.output)
  62. if options.type != 'LIBRARY':
  63. for rel_filename in peers:
  64. with open(os.path.join(build_root, rel_filename + '.mf')) as peer_file:
  65. peer_meta = json.load(peer_file)
  66. meta['dependencies'].append(peer_meta)
  67. if credits:
  68. union_texts = []
  69. for texts_file in credits:
  70. with open(process_quotes(texts_file)) as f:
  71. union_texts.append(f.read())
  72. meta['license_texts'] = '\n\n'.join(union_texts)
  73. if options.credits_output:
  74. final_credits = []
  75. if meta['license_texts']:
  76. final_credits.append(generate_header(meta) + '\n' + meta['license_texts'])
  77. for peer in peers:
  78. candidate = os.path.join(build_root, peer + '.mf')
  79. with open(candidate) as src:
  80. data = json.loads(src.read())
  81. texts = data.get('license_texts')
  82. if texts:
  83. candidate_text = generate_header(data) + '\n' + texts
  84. if isinstance(candidate_text, six.text_type):
  85. candidate_text = candidate_text.encode('utf-8')
  86. final_credits.append(candidate_text)
  87. with io.open(options.credits_output, 'w', encoding='utf-8') as f:
  88. data = '\n\n'.join(final_credits)
  89. if isinstance(data, str):
  90. data = data.decode('utf-8')
  91. f.write(data)
  92. with open(file_name, 'w') as mf_file:
  93. json.dump(meta, mf_file, indent=4)
  94. if __name__ == '__main__':
  95. try:
  96. generate_mf()
  97. except Exception as e:
  98. sys.stderr.write(str(e) + '\n')
  99. sys.exit(1)