Patch based from diff with skia repository from commit 013397884c73959dc07cb0a26ee742b1cdfbda8a Adds support for Python3, but removes the constraint of only SHA based refs in DEPS diff --git a/tools/git-sync-deps b/tools/git-sync-deps index c7379c0b5c..f63d4d9ccf 100755 --- a/tools/git-sync-deps +++ b/tools/git-sync-deps @@ -43,7 +43,7 @@ def git_executable(): A string suitable for passing to subprocess functions, or None. """ envgit = os.environ.get('GIT_EXECUTABLE') - searchlist = ['git'] + searchlist = ['git', 'git.bat'] if envgit: searchlist.insert(0, envgit) with open(os.devnull, 'w') as devnull: @@ -94,21 +94,25 @@ def is_git_toplevel(git, directory): try: toplevel = subprocess.check_output( [git, 'rev-parse', '--show-toplevel'], cwd=directory).strip() - return os.path.realpath(directory) == os.path.realpath(toplevel) + return os.path.realpath(directory) == os.path.realpath(toplevel.decode()) except subprocess.CalledProcessError: return False -def status(directory, checkoutable): - def truncate(s, length): +def status(directory, commithash, change): + def truncate_beginning(s, length): + return s if len(s) <= length else '...' + s[-(length-3):] + def truncate_end(s, length): return s if len(s) <= length else s[:(length - 3)] + '...' + dlen = 36 - directory = truncate(directory, dlen) - checkoutable = truncate(checkoutable, 40) - sys.stdout.write('%-*s @ %s\n' % (dlen, directory, checkoutable)) + directory = truncate_beginning(directory, dlen) + commithash = truncate_end(commithash, 40) + symbol = '>' if change else '@' + sys.stdout.write('%-*s %s %s\n' % (dlen, directory, symbol, commithash)) -def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): +def git_checkout_to_directory(git, repo, commithash, directory, verbose): """Checkout (and clone if needed) a Git repository. Args: @@ -117,8 +121,7 @@ def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): repo (string) the location of the repository, suitable for passing to `git clone`. - checkoutable (string) a tag, branch, or commit, suitable for - passing to `git checkout` + commithash (string) a commit, suitable for passing to `git checkout` directory (string) the path into which the repository should be checked out. @@ -129,7 +132,12 @@ def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): """ if not os.path.isdir(directory): subprocess.check_call( - [git, 'clone', '--quiet', repo, directory]) + [git, 'clone', '--quiet', '--no-checkout', repo, directory]) + subprocess.check_call([git, 'checkout', '--quiet', commithash], + cwd=directory) + if verbose: + status(directory, commithash, True) + return if not is_git_toplevel(git, directory): # if the directory exists, but isn't a git repo, you will modify @@ -145,11 +153,11 @@ def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): with open(os.devnull, 'w') as devnull: # If this fails, we will fetch before trying again. Don't spam user # with error infomation. - if 0 == subprocess.call([git, 'checkout', '--quiet', checkoutable], + if 0 == subprocess.call([git, 'checkout', '--quiet', commithash], cwd=directory, stderr=devnull): # if this succeeds, skip slow `git fetch`. if verbose: - status(directory, checkoutable) # Success. + status(directory, commithash, False) # Success. return # If the repo has changed, always force use of the correct repo. @@ -159,18 +167,24 @@ def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): subprocess.check_call([git, 'fetch', '--quiet'], cwd=directory) - subprocess.check_call([git, 'checkout', '--quiet', checkoutable], cwd=directory) + subprocess.check_call([git, 'checkout', '--quiet', commithash], cwd=directory) if verbose: - status(directory, checkoutable) # Success. + status(directory, commithash, True) # Success. def parse_file_to_dict(path): dictionary = {} - execfile(path, dictionary) + with open(path) as f: + exec('def Var(x): return vars[x]\n' + f.read(), dictionary) return dictionary +def is_sha1_sum(s): + """SHA1 sums are 160 bits, encoded as lowercase hexadecimal.""" + return len(s) == 40 and all(c in '0123456789abcdef' for c in s) + + def git_sync_deps(deps_file_path, command_line_os_requests, verbose): """Grab dependencies, with optional platform support. @@ -204,19 +218,19 @@ def git_sync_deps(deps_file_path, command_line_os_requests, verbose): raise Exception('%r is parent of %r' % (other_dir, directory)) list_of_arg_lists = [] for directory in sorted(dependencies): - if not isinstance(dependencies[directory], basestring): + if not isinstance(dependencies[directory], str): if verbose: - print 'Skipping "%s".' % directory + sys.stdout.write( 'Skipping "%s".\n' % directory) continue if '@' in dependencies[directory]: - repo, checkoutable = dependencies[directory].split('@', 1) + repo, commithash = dependencies[directory].split('@', 1) else: - raise Exception("please specify commit or tag") + raise Exception("please specify commit") relative_directory = os.path.join(deps_file_directory, directory) list_of_arg_lists.append( - (git, repo, checkoutable, relative_directory, verbose)) + (git, repo, commithash, relative_directory, verbose)) multithread(git_checkout_to_directory, list_of_arg_lists)