import os
import re
import subprocess
import sys
import threading
from builtins import bytes
def git_executable():
envgit = os.environ.get('GIT_EXECUTABLE')
searchlist = ['git', 'git.exe', 'git.bat']
if envgit:
searchlist.insert(0, envgit)
with open(os.devnull, 'w') as devnull:
for git in searchlist:
try:
subprocess.call([git, '--version'], stdout=devnull)
except (OSError,):
continue
return git
return None
DEFAULT_DEPS_PATH = os.path.normpath(
os.path.join(os.path.dirname(__file__), os.pardir, 'DEPS'))
def usage(deps_file_path = None):
sys.stderr.write(
'Usage: run to grab dependencies, with optional platform support:\n')
sys.stderr.write(' %s %s' % (sys.executable, __file__))
if deps_file_path:
parsed_deps = parse_file_to_dict(deps_file_path)
if 'deps_os' in parsed_deps:
for deps_os in parsed_deps['deps_os']:
sys.stderr.write(' [%s]' % deps_os)
sys.stderr.write('\n\n')
sys.stderr.write(__doc__)
def git_repository_sync_is_disabled(git, directory):
try:
disable = subprocess.check_output(
[git, 'config', 'sync-deps.disable'], cwd=directory)
return disable.lower().strip() in ['true', '1', 'yes', 'on']
except subprocess.CalledProcessError:
return False
def is_git_toplevel(git, directory):
try:
toplevel = subprocess.check_output(
[git, 'rev-parse', '--show-toplevel'], cwd=directory).strip()
return os.path.realpath(bytes(directory, 'utf8')) == os.path.realpath(toplevel)
except subprocess.CalledProcessError:
return False
def status(directory, checkoutable):
def truncate(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))
def git_checkout_to_directory(git, repo, checkoutable, directory, verbose):
if not os.path.isdir(directory):
subprocess.check_call(
[git, 'clone', '--quiet', repo, directory])
if not is_git_toplevel(git, directory):
sys.stdout.write('%s\n IS NOT TOP-LEVEL GIT DIRECTORY.\n' % directory)
return
if git_repository_sync_is_disabled(git, directory):
sys.stdout.write('%s\n SYNC IS DISABLED.\n' % directory)
return
with open(os.devnull, 'w') as devnull:
if 0 == subprocess.call([git, 'checkout', '--quiet', checkoutable],
cwd=directory, stderr=devnull):
if verbose:
status(directory, checkoutable) return
subprocess.check_call(
[git, 'remote', 'set-url', 'origin', repo], cwd=directory)
subprocess.check_call([git, 'fetch', '--quiet'], cwd=directory)
subprocess.check_call([git, 'checkout', '--quiet', checkoutable], cwd=directory)
if verbose:
status(directory, checkoutable)
def parse_file_to_dict(path):
dictionary = {}
contents = open(path).read()
contents = re.sub(r"Var\((.*?)\)", r"vars[\1]", contents)
exec(contents, dictionary)
return dictionary
def git_sync_deps(deps_file_path, command_line_os_requests, verbose):
git = git_executable()
assert git
deps_file_directory = os.path.dirname(deps_file_path)
deps_file = parse_file_to_dict(deps_file_path)
dependencies = deps_file['deps'].copy()
os_specific_dependencies = deps_file.get('deps_os', dict())
if 'all' in command_line_os_requests:
for value in list(os_specific_dependencies.values()):
dependencies.update(value)
else:
for os_name in command_line_os_requests:
if os_name in os_specific_dependencies:
dependencies.update(os_specific_dependencies[os_name])
for directory in dependencies:
for other_dir in dependencies:
if directory.startswith(other_dir + '/'):
raise Exception('%r is parent of %r' % (other_dir, directory))
list_of_arg_lists = []
for directory in sorted(dependencies):
if '@' in dependencies[directory]:
repo, checkoutable = dependencies[directory].split('@', 1)
else:
raise Exception("please specify commit or tag")
relative_directory = os.path.join(deps_file_directory, directory)
list_of_arg_lists.append(
(git, repo, checkoutable, relative_directory, verbose))
multithread(git_checkout_to_directory, list_of_arg_lists)
for directory in deps_file.get('recursedeps', []):
recursive_path = os.path.join(deps_file_directory, directory, 'DEPS')
git_sync_deps(recursive_path, command_line_os_requests, verbose)
def multithread(function, list_of_arg_lists):
threads = []
for args in list_of_arg_lists:
thread = threading.Thread(None, function, None, args)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
def main(argv):
deps_file_path = os.environ.get('GIT_SYNC_DEPS_PATH', DEFAULT_DEPS_PATH)
verbose = not bool(os.environ.get('GIT_SYNC_DEPS_QUIET', False))
if '--help' in argv or '-h' in argv:
usage(deps_file_path)
return 1
git_sync_deps(deps_file_path, argv, verbose)
return 0
if __name__ == '__main__':
exit(main(sys.argv[1:]))