git-sync-deps 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #!/usr/bin/env python3
  2. # Copyright 2014 Google Inc.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """Parse a DEPS file and git checkout all of the dependencies.
  30. """
  31. EXTRA_HELP = """
  32. Environment Variables:
  33. GIT_EXECUTABLE: path to "git" binary; if unset, will look for one of
  34. ['git', 'git.exe', 'git.bat'] in your default path.
  35. GIT_SYNC_DEPS_PATH: file to get the dependency list from; if unset,
  36. will use the file ../DEPS relative to this script's directory.
  37. GIT_SYNC_DEPS_QUIET: if set to non-empty string, suppress messages.
  38. Git Config:
  39. To disable syncing of a single repository:
  40. cd path/to/repository
  41. git config sync-deps.disable true
  42. To re-enable sync:
  43. cd path/to/repository
  44. git config --unset sync-deps.disable
  45. """
  46. import argparse
  47. import os
  48. import re
  49. import subprocess
  50. import sys
  51. import threading
  52. from builtins import bytes
  53. def git_executable():
  54. """Find the git executable.
  55. Returns:
  56. A triple:
  57. A string suitable for passing to subprocess functions, or None.
  58. The major version number
  59. The minor version number
  60. """
  61. envgit = os.environ.get('GIT_EXECUTABLE')
  62. searchlist = ['git', 'git.exe', 'git.bat']
  63. if envgit:
  64. searchlist.insert(0, envgit)
  65. with open(os.devnull, 'w') as devnull:
  66. for git in searchlist:
  67. major=None
  68. minor=None
  69. try:
  70. version_info = subprocess.check_output([git, '--version']).decode('utf-8')
  71. match = re.search(r"^git version (\d+)\.(\d+)",version_info)
  72. print("Using {}".format(version_info))
  73. if match:
  74. major = int(match.group(1))
  75. minor = int(match.group(2))
  76. else:
  77. continue
  78. except (OSError,):
  79. continue
  80. return (git,major,minor)
  81. return (None,0,0)
  82. DEFAULT_DEPS_PATH = os.path.normpath(
  83. os.path.join(os.path.dirname(__file__), os.pardir, 'DEPS'))
  84. def get_deps_os_str(deps_file):
  85. parsed_deps = parse_file_to_dict(deps_file)
  86. parts = []
  87. if 'deps_os' in parsed_deps:
  88. for deps_os in parsed_deps['deps_os']:
  89. parts.append(' [{}]]'.format(deps_os))
  90. return "\n".join(parts)
  91. def looks_like_raw_commit(commit):
  92. return re.match('^[a-f0-9]{40}$', commit) is not None
  93. def git_repository_sync_is_disabled(git, directory):
  94. try:
  95. disable = subprocess.check_output(
  96. [git, 'config', 'sync-deps.disable'], cwd=directory)
  97. return disable.lower().strip() in ['true', '1', 'yes', 'on']
  98. except subprocess.CalledProcessError:
  99. return False
  100. def is_git_toplevel(git, directory):
  101. """Return true iff the directory is the top level of a Git repository.
  102. Args:
  103. git (string) the git executable
  104. directory (string) the path into which the repository
  105. is expected to be checked out.
  106. """
  107. try:
  108. toplevel = subprocess.check_output(
  109. [git, 'rev-parse', '--show-toplevel'], cwd=directory).strip()
  110. return os.path.realpath(bytes(directory, 'utf8')) == os.path.realpath(toplevel)
  111. except subprocess.CalledProcessError:
  112. return False
  113. def status(directory, checkoutable):
  114. def truncate(s, length):
  115. return s if len(s) <= length else '...' + s[-(length - 3):]
  116. dlen = 36
  117. directory = truncate(directory, dlen)
  118. checkoutable = truncate(checkoutable, 40)
  119. sys.stdout.write('%-*s @ %s\n' % (dlen, directory, checkoutable))
  120. def git_checkout_to_directory(git, repo, checkoutable, directory, verbose, treeless):
  121. """Checkout (and clone if needed) a Git repository.
  122. Args:
  123. git (string) the git executable
  124. repo (string) the location of the repository, suitable
  125. for passing to `git clone`.
  126. checkoutable (string) a tag, branch, or commit, suitable for
  127. passing to `git checkout`
  128. directory (string) the path into which the repository
  129. should be checked out.
  130. verbose (boolean): emit status info to stdout
  131. treeless (boolean): when true, clone without any trees.
  132. Raises an exception if any calls to git fail.
  133. """
  134. if not os.path.isdir(directory):
  135. # Use blobless or treeless checkouts for faster downloads.
  136. # This defers some work to checkout time.
  137. # https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/
  138. filter = ['--filter=tree:0'] if treeless else ['--filter=blob:none']
  139. # If the thing to check out looks like a tag (and not like a commit),
  140. # then limit the checkout to that branch.
  141. branch = [] if looks_like_raw_commit(checkoutable) else ['--branch={}'.format(checkoutable)]
  142. subprocess.check_call(
  143. [git, 'clone', '--quiet', '--single-branch'] + filter + branch + [repo, directory])
  144. if not is_git_toplevel(git, directory):
  145. # if the directory exists, but isn't a git repo, you will modify
  146. # the parent repostory, which isn't what you want.
  147. sys.stdout.write('%s\n IS NOT TOP-LEVEL GIT DIRECTORY.\n' % directory)
  148. return
  149. # Check to see if this repo is disabled. Quick return.
  150. if git_repository_sync_is_disabled(git, directory):
  151. sys.stdout.write('%s\n SYNC IS DISABLED.\n' % directory)
  152. return
  153. with open(os.devnull, 'w') as devnull:
  154. # If this fails, we will fetch before trying again. Don't spam user
  155. # with error information.
  156. if 0 == subprocess.call([git, 'checkout', '--quiet', checkoutable],
  157. cwd=directory, stderr=devnull):
  158. # if this succeeds, skip slow `git fetch`.
  159. if verbose:
  160. status(directory, checkoutable) # Success.
  161. return
  162. # If the repo has changed, always force use of the correct repo.
  163. # If origin already points to repo, this is a quick no-op.
  164. subprocess.check_call(
  165. [git, 'remote', 'set-url', 'origin', repo], cwd=directory)
  166. subprocess.check_call([git, 'fetch', '--quiet'], cwd=directory)
  167. subprocess.check_call([git, 'checkout', '--quiet', checkoutable], cwd=directory)
  168. if verbose:
  169. status(directory, checkoutable) # Success.
  170. def parse_file_to_dict(path):
  171. dictionary = {}
  172. contents = open(path).read()
  173. # Need to convert Var() to vars[], so that the DEPS is actually Python. Var()
  174. # comes from Autoroller using gclient which has a slightly different DEPS
  175. # format.
  176. contents = re.sub(r"Var\((.*?)\)", r"vars[\1]", contents)
  177. exec(contents, dictionary)
  178. return dictionary
  179. def git_sync_deps(deps_file_path, command_line_os_requests, verbose, treeless):
  180. """Grab dependencies, with optional platform support.
  181. Args:
  182. deps_file_path (string) Path to the DEPS file.
  183. command_line_os_requests (list of strings) Can be empty list.
  184. List of strings that should each be a key in the deps_os
  185. dictionary in the DEPS file.
  186. verbose (boolean): emit status info to stdout
  187. treeless (boolean): when true, clone as treeless instead of blobless
  188. Raises git Exceptions.
  189. """
  190. (git,git_major,git_minor) = git_executable()
  191. assert git
  192. # --filter=tree:0 is available in git 2.20 and later
  193. if (git_major,git_minor) < (2,20):
  194. print("disabling --treeless: git is older than v2.20")
  195. treeless = False
  196. deps_file_directory = os.path.dirname(deps_file_path)
  197. deps_file = parse_file_to_dict(deps_file_path)
  198. dependencies = deps_file['deps'].copy()
  199. os_specific_dependencies = deps_file.get('deps_os', dict())
  200. if 'all' in command_line_os_requests:
  201. for value in list(os_specific_dependencies.values()):
  202. dependencies.update(value)
  203. else:
  204. for os_name in command_line_os_requests:
  205. # Add OS-specific dependencies
  206. if os_name in os_specific_dependencies:
  207. dependencies.update(os_specific_dependencies[os_name])
  208. for directory in dependencies:
  209. for other_dir in dependencies:
  210. if directory.startswith(other_dir + '/'):
  211. raise Exception('%r is parent of %r' % (other_dir, directory))
  212. list_of_arg_lists = []
  213. for directory in sorted(dependencies):
  214. if '@' in dependencies[directory]:
  215. repo, checkoutable = dependencies[directory].split('@', 1)
  216. else:
  217. raise Exception("please specify commit or tag")
  218. relative_directory = os.path.join(deps_file_directory, directory)
  219. list_of_arg_lists.append(
  220. (git, repo, checkoutable, relative_directory, verbose, treeless))
  221. multithread(git_checkout_to_directory, list_of_arg_lists)
  222. for directory in deps_file.get('recursedeps', []):
  223. recursive_path = os.path.join(deps_file_directory, directory, 'DEPS')
  224. git_sync_deps(recursive_path, command_line_os_requests, verbose)
  225. def multithread(function, list_of_arg_lists):
  226. # for args in list_of_arg_lists:
  227. # function(*args)
  228. # return
  229. threads = []
  230. for args in list_of_arg_lists:
  231. thread = threading.Thread(None, function, None, args)
  232. thread.start()
  233. threads.append(thread)
  234. for thread in threads:
  235. thread.join()
  236. def main(argv):
  237. argparser = argparse.ArgumentParser(
  238. prog = "git-sync-deps",
  239. description = "Checkout git-based dependencies as specified by the DEPS file",
  240. add_help=False # Because we want to print deps_os with -h option
  241. )
  242. argparser.add_argument("--help", "-h",
  243. action='store_true',
  244. help="show this help message and exit")
  245. argparser.add_argument("--deps",
  246. default = os.environ.get('GIT_SYNC_DEPS_PATH', DEFAULT_DEPS_PATH),
  247. help="location of the the DEPS file")
  248. argparser.add_argument("--verbose",
  249. default=not bool(os.environ.get('GIT_SYNC_DEPS_QUIET', False)),
  250. action='store_true',
  251. help="be verbose: print status messages")
  252. argparser.add_argument("--treeless",
  253. default=False,
  254. action='store_true',
  255. help="""
  256. Clone repos without trees (--filter=tree:0).
  257. This is the fastest option for a build machine,
  258. when you only need a single commit.
  259. Defers getting objects until checking out a commit.
  260. The default is to clone with trees but without blobs.
  261. Only takes effect if using git 2.20 or later.
  262. See https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/
  263. """)
  264. argparser.add_argument("os_requests",nargs="*",
  265. help="OS requests, as keys in the deps_os dictionariy in the DEPS file")
  266. args = argparser.parse_args()
  267. if args.help:
  268. print(argparser.format_help())
  269. print(EXTRA_HELP)
  270. print(get_deps_os_str(args.deps))
  271. return 0
  272. git_sync_deps(args.deps, args.os_requests, args.verbose, args.treeless)
  273. return 0
  274. if __name__ == '__main__':
  275. exit(main(sys.argv[1:]))