generate_mf.py 3.3 KB

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