commit-bot@chromium.org | 71e800a | 2014-04-16 19:21:00 +0000 | [diff] [blame] | 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: |
halcanary | 20fb7c6 | 2014-06-25 13:28:29 -0700 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 71e800a | 2014-04-16 19:21:00 +0000 | [diff] [blame] | 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 | |
halcanary | 20fb7c6 | 2014-06-25 13:28:29 -0700 | [diff] [blame] | 38 | |
| 39 | def git_executable(): |
| 40 | """Find the git executable. |
| 41 | |
| 42 | Returns: |
| 43 | A string suitable for passing to subprocess functions, or None. |
| 44 | """ |
| 45 | envgit = os.environ.get('GIT_EXECUTABLE') |
| 46 | searchlist = ['git', 'git.exe', 'git.bat'] |
| 47 | if envgit: |
| 48 | searchlist.insert(0, envgit) |
| 49 | with open(os.devnull, 'w') as devnull: |
| 50 | for git in searchlist: |
| 51 | try: |
| 52 | subprocess.call([git, '--version'], stdout=devnull) |
| 53 | except (OSError,): |
| 54 | continue |
| 55 | return git |
| 56 | return None |
commit-bot@chromium.org | 71e800a | 2014-04-16 19:21:00 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | DEFAULT_DEPS_PATH = os.path.normpath( |
| 60 | os.path.join(os.path.dirname(__file__), os.pardir, 'DEPS')) |
| 61 | |
| 62 | |
| 63 | def usage(deps_file_path = None): |
| 64 | sys.stderr.write( |
| 65 | 'Usage: run to grab dependencies, with optional platform support:\n') |
| 66 | sys.stderr.write(' python %s' % __file__) |
| 67 | if deps_file_path: |
| 68 | for deps_os in parse_file_to_dict(deps_file_path)['deps_os']: |
| 69 | sys.stderr.write(' [%s]' % deps_os) |
| 70 | else: |
| 71 | sys.stderr.write(' [DEPS_OS...]') |
| 72 | sys.stderr.write('\n\n') |
| 73 | sys.stderr.write(__doc__) |
| 74 | |
| 75 | |
| 76 | def git_repository_sync_is_disabled(git, directory): |
| 77 | try: |
| 78 | disable = subprocess.check_output( |
| 79 | [git, 'config', 'sync-deps.disable'], cwd=directory) |
| 80 | return disable.lower().strip() in ['true', '1', 'yes', 'on'] |
| 81 | except subprocess.CalledProcessError: |
| 82 | return False |
| 83 | |
| 84 | |
commit-bot@chromium.org | aae6e6f | 2014-04-21 19:03:05 +0000 | [diff] [blame] | 85 | def is_git_toplevel(git, directory): |
| 86 | """Return true iff the directory is the top level of a Git repository. |
| 87 | |
| 88 | Args: |
| 89 | git (string) the git executable |
| 90 | |
| 91 | directory (string) the path into which the repository |
| 92 | is expected to be checked out. |
| 93 | """ |
| 94 | try: |
| 95 | toplevel = subprocess.check_output( |
| 96 | [git, 'rev-parse', '--show-toplevel'], cwd=directory).strip() |
commit-bot@chromium.org | 2349a1e | 2014-04-28 13:39:47 +0000 | [diff] [blame] | 97 | return os.path.realpath(directory) == os.path.realpath(toplevel) |
commit-bot@chromium.org | aae6e6f | 2014-04-21 19:03:05 +0000 | [diff] [blame] | 98 | except subprocess.CalledProcessError: |
| 99 | return False |
| 100 | |
| 101 | |
commit-bot@chromium.org | 71e800a | 2014-04-16 19:21:00 +0000 | [diff] [blame] | 102 | def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): |
| 103 | """Checkout (and clone if needed) a Git repository. |
| 104 | |
| 105 | Args: |
| 106 | git (string) the git executable |
| 107 | |
| 108 | repo (string) the location of the repository, suitable |
| 109 | for passing to `git clone`. |
| 110 | |
| 111 | checkoutable (string) a tag, branch, or commit, suitable for |
| 112 | passing to `git checkout` |
| 113 | |
| 114 | directory (string) the path into which the repository |
| 115 | should be checked out. |
| 116 | |
| 117 | verbose (boolean) |
| 118 | |
| 119 | Raises an exception if any calls to git fail. |
| 120 | """ |
| 121 | if not os.path.isdir(directory): |
| 122 | subprocess.check_call( |
| 123 | [git, 'clone', '--quiet', repo, directory]) |
| 124 | |
commit-bot@chromium.org | aae6e6f | 2014-04-21 19:03:05 +0000 | [diff] [blame] | 125 | if not is_git_toplevel(git, directory): |
| 126 | # if the directory exists, but isn't a git repo, you will modify |
| 127 | # the parent repostory, which isn't what you want. |
| 128 | sys.stdout.write('%s\n IS NOT TOP-LEVEL GIT DIRECTORY.\n' % directory) |
| 129 | return |
| 130 | |
commit-bot@chromium.org | 71e800a | 2014-04-16 19:21:00 +0000 | [diff] [blame] | 131 | # Check to see if this repo is disabled. Quick return. |
| 132 | if git_repository_sync_is_disabled(git, directory): |
| 133 | sys.stdout.write('%s\n SYNC IS DISABLED.\n' % directory) |
| 134 | return |
| 135 | |
| 136 | subprocess.check_call([git, 'fetch', '--quiet'], cwd=directory) |
| 137 | |
| 138 | subprocess.check_call( |
| 139 | [git, 'checkout', '--quiet', checkoutable], cwd=directory) |
| 140 | |
| 141 | if verbose: |
| 142 | sys.stdout.write('%s\n @ %s\n' % (directory, checkoutable)) # Success. |
| 143 | |
| 144 | |
| 145 | def parse_file_to_dict(path): |
| 146 | dictionary = {} |
| 147 | execfile(path, dictionary) |
| 148 | return dictionary |
| 149 | |
| 150 | |
| 151 | class DepsError(Exception): |
| 152 | """Raised if deps_os is a bad key. |
| 153 | """ |
| 154 | pass |
| 155 | |
| 156 | |
| 157 | def git_sync_deps(deps_file_path, deps_os_list, verbose): |
| 158 | """Grab dependencies, with optional platform support. |
| 159 | |
| 160 | Args: |
| 161 | deps_file_path (string) Path to the DEPS file. |
| 162 | |
| 163 | deps_os_list (list of strings) Can be empty list. List of |
| 164 | strings that should each be a key in the deps_os |
| 165 | dictionary in the DEPS file. |
| 166 | |
| 167 | Raises DepsError exception and git Exceptions. |
| 168 | """ |
halcanary | 20fb7c6 | 2014-06-25 13:28:29 -0700 | [diff] [blame] | 169 | git = git_executable() |
| 170 | assert git |
| 171 | |
commit-bot@chromium.org | 71e800a | 2014-04-16 19:21:00 +0000 | [diff] [blame] | 172 | deps_file_directory = os.path.dirname(deps_file_path) |
| 173 | deps = parse_file_to_dict(deps_file_path) |
| 174 | dependencies = deps['deps'].copy() |
| 175 | for deps_os in deps_os_list: |
| 176 | # Add OS-specific dependencies |
| 177 | if deps_os not in deps['deps_os']: |
| 178 | raise DepsError( |
| 179 | 'Argument "%s" not found within deps_os keys %r' % |
| 180 | (deps_os, deps['deps_os'].keys())) |
| 181 | for dep in deps['deps_os'][deps_os]: |
| 182 | dependencies[dep] = deps['deps_os'][deps_os][dep] |
| 183 | list_of_arg_lists = [] |
| 184 | for directory in dependencies: |
| 185 | if '@' in dependencies[directory]: |
| 186 | repo, checkoutable = dependencies[directory].split('@', 1) |
| 187 | else: |
| 188 | repo, checkoutable = dependencies[directory], 'origin/master' |
| 189 | |
| 190 | relative_directory = os.path.join(deps_file_directory, directory) |
| 191 | |
| 192 | list_of_arg_lists.append( |
halcanary | 20fb7c6 | 2014-06-25 13:28:29 -0700 | [diff] [blame] | 193 | (git, repo, checkoutable, relative_directory, verbose)) |
commit-bot@chromium.org | 71e800a | 2014-04-16 19:21:00 +0000 | [diff] [blame] | 194 | |
| 195 | multithread(git_checkout_to_directory, list_of_arg_lists) |
| 196 | |
| 197 | |
| 198 | def multithread(function, list_of_arg_lists): |
| 199 | # for args in list_of_arg_lists: |
| 200 | # function(*args) |
| 201 | # return |
| 202 | threads = [] |
| 203 | for args in list_of_arg_lists: |
| 204 | thread = threading.Thread(None, function, None, args) |
| 205 | thread.start() |
| 206 | threads.append(thread) |
| 207 | for thread in threads: |
| 208 | thread.join() |
| 209 | |
| 210 | |
| 211 | def main(argv): |
| 212 | deps_file_path = os.environ.get('GIT_SYNC_DEPS_PATH', DEFAULT_DEPS_PATH) |
| 213 | verbose = not bool(os.environ.get('GIT_SYNC_DEPS_QUIET', False)) |
| 214 | try: |
| 215 | git_sync_deps(deps_file_path, argv, verbose) |
| 216 | return 0 |
| 217 | except DepsError: |
| 218 | usage(deps_file_path) |
| 219 | return 1 |
| 220 | |
| 221 | |
| 222 | if __name__ == '__main__': |
| 223 | exit(main(sys.argv[1:])) |