go_proto_wrapper.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from __future__ import absolute_import, unicode_literals
  2. import argparse
  3. import os
  4. import re
  5. import shutil
  6. import subprocess
  7. import sys
  8. import tempfile
  9. OUT_DIR_FLAG_PATTERN = re.compile(r'^(--go(([-_]\w+))*_out=)')
  10. def parse_args():
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument('--arcadia-prefix', required=True)
  13. parser.add_argument('--check', nargs='?', default=None)
  14. parser.add_argument('--contrib-prefix', required=True)
  15. parser.add_argument('--namespace', required=True)
  16. parser.add_argument('--proto', required=True)
  17. parser.add_argument('args', nargs='+')
  18. return parser.parse_args()
  19. def move_tree(src_root, dst_root):
  20. for root, _, files in os.walk(src_root):
  21. rel_dir = os.path.relpath(root, src_root)
  22. dst_dir = os.path.join(dst_root, rel_dir)
  23. if not os.path.exists(dst_dir):
  24. os.mkdir(dst_dir)
  25. for file in files:
  26. os.rename(os.path.join(root, file), os.path.join(dst_dir, file))
  27. def main(args):
  28. arcadia_prefix = args.arcadia_prefix
  29. contrib_prefix = args.contrib_prefix
  30. proto_namespace = args.namespace
  31. check_output = args.check
  32. proto_file = args.proto
  33. args = list(args.args)
  34. args.append(proto_file)
  35. out_dir_orig = None
  36. out_dir_temp = None
  37. for i in range(len(args)):
  38. m = re.match(OUT_DIR_FLAG_PATTERN, args[i])
  39. if m:
  40. out_dir_flag = m.group(1)
  41. index = max(len(out_dir_flag), args[i].rfind(':') + 1)
  42. out_dir = args[i][index:]
  43. if out_dir_orig:
  44. assert out_dir_orig == out_dir, 'Output directories do not match: [{}] and [{}]'.format(
  45. out_dir_orig, out_dir
  46. )
  47. else:
  48. out_dir_orig = out_dir
  49. out_dir_temp = tempfile.mkdtemp(dir=out_dir_orig)
  50. args[i] = (args[i][:index] + out_dir_temp).replace('|', ',')
  51. assert out_dir_temp is not None, 'Output directory is not specified'
  52. try:
  53. subprocess.check_output(args, stdin=None, stderr=subprocess.STDOUT)
  54. except subprocess.CalledProcessError as e:
  55. sys.stderr.write(
  56. '{} returned non-zero exit code {}.\n{}\n'.format(' '.join(e.cmd), e.returncode, e.output.decode('utf-8'))
  57. )
  58. return e.returncode
  59. # All Arcadia GO projects should have 'a.yandex-team.ru/' namespace prefix.
  60. # If the namespace doesn't start with 'a.yandex-team.ru/' prefix then this
  61. # project is from vendor directory under the root of Arcadia.
  62. out_dir_src = os.path.normpath(os.path.join(out_dir_temp, arcadia_prefix, proto_namespace))
  63. out_dir_dst = out_dir_orig
  64. if not os.path.isdir(out_dir_src):
  65. out_dir_src = out_dir_temp
  66. out_dir_dst = os.path.join(out_dir_orig, contrib_prefix)
  67. move_tree(out_dir_src, out_dir_dst)
  68. if check_output and not os.path.exists(check_output):
  69. package_name = None
  70. option_re = re.compile(r'^\s*option\s+go_package\s*=\s*"([^"]+)"')
  71. with open(proto_file, 'r') as f:
  72. for line in f:
  73. m = re.match(option_re, line)
  74. if m:
  75. package_name = m.group(1).split(';')[-1].split('/')[-1]
  76. break
  77. with open(check_output, 'w') as fout:
  78. fout.write(
  79. '// Code generated by go_proto_wrapper.py script. DO NOT EDIT.\n\npackage {}\n'.format(package_name)
  80. )
  81. shutil.rmtree(out_dir_temp)
  82. return 0
  83. if __name__ == '__main__':
  84. args = parse_args()
  85. sys.exit(main(args))