generate_mf.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import json
  2. import logging
  3. import optparse
  4. import os
  5. import sys
  6. import io
  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 lics, peers, credits, opts,
  41. def generate_header(meta):
  42. return '-' * 20 + meta.get('path', 'Unknown module') + '-' * 20
  43. def generate_mf():
  44. lics, peers, credits, options = parse_args()
  45. meta = {
  46. 'module_name': options.module_name,
  47. 'path': os.path.dirname(options.output),
  48. 'licenses': lics,
  49. 'dependencies': [],
  50. 'license_texts': ''
  51. }
  52. build_root = options.build_root
  53. file_name = os.path.join(build_root, options.output)
  54. if options.type != 'LIBRARY':
  55. for rel_filename in peers:
  56. with open(os.path.join(build_root, rel_filename + '.mf')) as peer_file:
  57. peer_meta = json.load(peer_file)
  58. meta['dependencies'].append(peer_meta)
  59. if credits:
  60. union_texts = []
  61. for texts_file in credits:
  62. with open(process_quotes(texts_file)) as f:
  63. union_texts.append(f.read())
  64. meta['license_texts'] = '\n\n'.join(union_texts)
  65. if options.credits_output:
  66. final_credits = []
  67. if meta['license_texts']:
  68. final_credits.append(generate_header(meta) + '\n' + meta['license_texts'])
  69. for peer in peers:
  70. candidate = os.path.join(build_root, peer + '.mf')
  71. with open(candidate) as src:
  72. data = json.loads(src.read())
  73. texts = data.get('license_texts')
  74. if texts:
  75. candidate_text = generate_header(data) + '\n' + texts
  76. if isinstance(candidate_text, unicode):
  77. candidate_text = candidate_text.encode('utf-8')
  78. final_credits.append(candidate_text)
  79. with io.open(options.credits_output, 'w', encoding='utf-8') as f:
  80. data = '\n\n'.join(final_credits)
  81. if isinstance(data, str):
  82. data = data.decode('utf-8')
  83. f.write(data)
  84. with open(file_name, 'w') as mf_file:
  85. json.dump(meta, mf_file, indent=4)
  86. if __name__ == '__main__':
  87. try:
  88. generate_mf()
  89. except Exception as e:
  90. sys.stderr.write(str(e) + '\n')
  91. sys.exit(1)