build_environment.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/python
  2. # Copyright Abel Sinkovics (abel@sinkovics.hu) 2016.
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. import os
  7. import subprocess
  8. import json
  9. import argparse
  10. def load_json(filename):
  11. with open(filename, 'r') as f:
  12. return json.load(f)
  13. class ChildProcess:
  14. def __init__(self, cmd, cwd = os.getcwd()):
  15. self.cmd = cmd
  16. self.cwd = cwd
  17. def run(self, cmd):
  18. cmd_string = ' '.join(cmd)
  19. print 'Running {0}'.format(cmd_string)
  20. proc = subprocess.Popen(
  21. self.cmd + cmd,
  22. cwd = self.cwd,
  23. stdout = subprocess.PIPE
  24. )
  25. out = proc.communicate()[0]
  26. if proc.returncode == 0:
  27. return out
  28. else:
  29. raise Exception(
  30. 'Command {0} exited with {1}'.format(
  31. cmd_string,
  32. proc.returncode
  33. )
  34. )
  35. def in_dir(self, cwd):
  36. return ChildProcess(self.cmd, cwd)
  37. def in_subdir(self, subdir):
  38. return self.in_dir(os.path.join(self.cwd, subdir))
  39. def head_of_master(submodule, git, ref):
  40. git.run(['fetch'])
  41. return git.run(['show-ref', ref]).split()[0]
  42. def build_environment(submodules_file, out_dir, git, repo, action, ref):
  43. submodules = load_json(submodules_file)
  44. git.run(['clone', repo, out_dir])
  45. git_in_boost = git.in_dir(out_dir)
  46. git_in_boost.run(
  47. ['submodule', 'init', '--'] + [k for k in submodules.keys() if k != '']
  48. )
  49. git_in_boost.run(['submodule', 'update'])
  50. if action == 'update':
  51. with open(submodules_file, 'w') as f:
  52. f.write(json.dumps(
  53. dict([
  54. (k, head_of_master(k, git_in_boost.in_subdir(k), ref))
  55. for k, v in submodules.iteritems()
  56. ]),
  57. sort_keys=True,
  58. indent=2
  59. ))
  60. elif action == 'checkout':
  61. for name, commit in submodules.iteritems():
  62. git_in_boost.in_subdir(name).run(['checkout', commit])
  63. else:
  64. raise Exception('Invalid action {0}'.format(action))
  65. def main():
  66. """The main function of the utility"""
  67. parser = argparse.ArgumentParser(
  68. description='Manage the build environment of Boost.Metaparse'
  69. )
  70. parser.add_argument(
  71. '--dep_json',
  72. required=True,
  73. help='The json file describing the dependencies'
  74. )
  75. parser.add_argument(
  76. '--git',
  77. required=False,
  78. default='git',
  79. help='The git command to use'
  80. )
  81. parser.add_argument(
  82. '--out',
  83. required=False,
  84. default='boost',
  85. help='The directory to clone into'
  86. )
  87. parser.add_argument(
  88. '--action',
  89. required=True,
  90. choices=['update', 'checkout'],
  91. help='The action to do with the dependencies'
  92. )
  93. parser.add_argument(
  94. '--boost_repository',
  95. required=False,
  96. default='https://github.com/boostorg/boost.git',
  97. help='The Boost repository to clone'
  98. )
  99. parser.add_argument(
  100. '--ref',
  101. required=False,
  102. default='origin/master',
  103. help='The reference to set to in update'
  104. )
  105. args = parser.parse_args()
  106. build_environment(
  107. args.dep_json,
  108. args.out,
  109. ChildProcess([args.git]),
  110. args.boost_repository,
  111. args.action,
  112. args.ref
  113. )
  114. if __name__ == '__main__':
  115. main()