OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2014 Google Inc. |
| 3 # |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 |
| 8 """Parse a DEPS file and git checkout all of the dependencies. |
| 9 |
| 10 Args: |
| 11 An optional list of deps_os values. |
| 12 |
| 13 Environment Variables: |
| 14 GIT_EXECUTABLE: path to "git" binary; if unset, will look for one of |
| 15 ['git', 'git.exe', 'git.bat'] in your default path. |
| 16 |
| 17 GIT_SYNC_DEPS_PATH: file to get the dependency list from; if unset, |
| 18 will use the file ../DEPS relative to this script's directory. |
| 19 |
| 20 GIT_SYNC_DEPS_QUIET: if set to non-empty string, suppress messages. |
| 21 |
| 22 Git Config: |
| 23 To disable syncing of a single repository: |
| 24 cd path/to/repository |
| 25 git config sync-deps.disable true |
| 26 |
| 27 To re-enable sync: |
| 28 cd path/to/repository |
| 29 git config --unset sync-deps.disable |
| 30 """ |
| 31 |
| 32 |
| 33 import os |
| 34 import subprocess |
| 35 import sys |
| 36 import threading |
| 37 |
| 38 from git_utils import git_executable |
| 39 |
| 40 |
| 41 DEFAULT_DEPS_PATH = os.path.normpath( |
| 42 os.path.join(os.path.dirname(__file__), os.pardir, 'DEPS')) |
| 43 |
| 44 |
| 45 def usage(deps_file_path = None): |
| 46 sys.stderr.write( |
| 47 'Usage: run to grab dependencies, with optional platform support:\n') |
| 48 sys.stderr.write(' python %s' % __file__) |
| 49 if deps_file_path: |
| 50 for deps_os in parse_file_to_dict(deps_file_path)['deps_os']: |
| 51 sys.stderr.write(' [%s]' % deps_os) |
| 52 else: |
| 53 sys.stderr.write(' [DEPS_OS...]') |
| 54 sys.stderr.write('\n\n') |
| 55 sys.stderr.write(__doc__) |
| 56 |
| 57 |
| 58 def git_repository_sync_is_disabled(git, directory): |
| 59 try: |
| 60 disable = subprocess.check_output( |
| 61 [git, 'config', 'sync-deps.disable'], cwd=directory) |
| 62 return disable.lower().strip() in ['true', '1', 'yes', 'on'] |
| 63 except subprocess.CalledProcessError: |
| 64 return False |
| 65 |
| 66 |
| 67 def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): |
| 68 """Checkout (and clone if needed) a Git repository. |
| 69 |
| 70 Args: |
| 71 git (string) the git executable |
| 72 |
| 73 repo (string) the location of the repository, suitable |
| 74 for passing to `git clone`. |
| 75 |
| 76 checkoutable (string) a tag, branch, or commit, suitable for |
| 77 passing to `git checkout` |
| 78 |
| 79 directory (string) the path into which the repository |
| 80 should be checked out. |
| 81 |
| 82 verbose (boolean) |
| 83 |
| 84 Raises an exception if any calls to git fail. |
| 85 """ |
| 86 if not os.path.isdir(directory): |
| 87 subprocess.check_call( |
| 88 [git, 'clone', '--quiet', repo, directory]) |
| 89 |
| 90 # Check to see if this repo is disabled. Quick return. |
| 91 if git_repository_sync_is_disabled(git, directory): |
| 92 sys.stdout.write('%s\n SYNC IS DISABLED.\n' % directory) |
| 93 return |
| 94 |
| 95 subprocess.check_call([git, 'fetch', '--quiet'], cwd=directory) |
| 96 |
| 97 subprocess.check_call( |
| 98 [git, 'checkout', '--quiet', checkoutable], cwd=directory) |
| 99 |
| 100 if verbose: |
| 101 sys.stdout.write('%s\n @ %s\n' % (directory, checkoutable)) # Success. |
| 102 |
| 103 |
| 104 def parse_file_to_dict(path): |
| 105 dictionary = {} |
| 106 execfile(path, dictionary) |
| 107 return dictionary |
| 108 |
| 109 |
| 110 class DepsError(Exception): |
| 111 """Raised if deps_os is a bad key. |
| 112 """ |
| 113 pass |
| 114 |
| 115 |
| 116 def git_sync_deps(deps_file_path, deps_os_list, verbose): |
| 117 """Grab dependencies, with optional platform support. |
| 118 |
| 119 Args: |
| 120 deps_file_path (string) Path to the DEPS file. |
| 121 |
| 122 deps_os_list (list of strings) Can be empty list. List of |
| 123 strings that should each be a key in the deps_os |
| 124 dictionary in the DEPS file. |
| 125 |
| 126 Raises DepsError exception and git Exceptions. |
| 127 """ |
| 128 git = git_executable() |
| 129 assert git |
| 130 |
| 131 deps_file_directory = os.path.dirname(deps_file_path) |
| 132 deps = parse_file_to_dict(deps_file_path) |
| 133 dependencies = deps['deps'].copy() |
| 134 for deps_os in deps_os_list: |
| 135 # Add OS-specific dependencies |
| 136 if deps_os not in deps['deps_os']: |
| 137 raise DepsError( |
| 138 'Argument "%s" not found within deps_os keys %r' % |
| 139 (deps_os, deps['deps_os'].keys())) |
| 140 for dep in deps['deps_os'][deps_os]: |
| 141 dependencies[dep] = deps['deps_os'][deps_os][dep] |
| 142 list_of_arg_lists = [] |
| 143 for directory in dependencies: |
| 144 if '@' in dependencies[directory]: |
| 145 repo, checkoutable = dependencies[directory].split('@', 1) |
| 146 else: |
| 147 repo, checkoutable = dependencies[directory], 'origin/master' |
| 148 |
| 149 relative_directory = os.path.join(deps_file_directory, directory) |
| 150 |
| 151 list_of_arg_lists.append( |
| 152 (git, repo, checkoutable, relative_directory, verbose)) |
| 153 |
| 154 multithread(git_checkout_to_directory, list_of_arg_lists) |
| 155 |
| 156 |
| 157 def multithread(function, list_of_arg_lists): |
| 158 # for args in list_of_arg_lists: |
| 159 # function(*args) |
| 160 # return |
| 161 threads = [] |
| 162 for args in list_of_arg_lists: |
| 163 thread = threading.Thread(None, function, None, args) |
| 164 thread.start() |
| 165 threads.append(thread) |
| 166 for thread in threads: |
| 167 thread.join() |
| 168 |
| 169 |
| 170 def main(argv): |
| 171 deps_file_path = os.environ.get('GIT_SYNC_DEPS_PATH', DEFAULT_DEPS_PATH) |
| 172 verbose = not bool(os.environ.get('GIT_SYNC_DEPS_QUIET', False)) |
| 173 try: |
| 174 git_sync_deps(deps_file_path, argv, verbose) |
| 175 return 0 |
| 176 except DepsError: |
| 177 usage(deps_file_path) |
| 178 return 1 |
| 179 |
| 180 |
| 181 if __name__ == '__main__': |
| 182 exit(main(sys.argv[1:])) |
OLD | NEW |