From 334851e4b6390f4c78e463b977003f1d967c88ed Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 19 Sep 2011 08:05:31 -0700 Subject: [PATCH] Enhance HTTP support Setting REPO_CURL_VERBOSE=1 in the environment will register a debug level HTTPHandler on the urllib2 library, showing HTTP requests and responses on the stderr channel of repo. During any HTTP or HTTPS request created inside of the repo process, a custom User-Agent header is now defined: User-Agent: git-repo/1.7.5 (Linux) git/1.7.7 Python/2.6.5 Change-Id: Ia5026fb1e1500659bd2af27416d85e205048bf26 Signed-off-by: Shawn O. Pearce --- git_command.py | 35 +++++++++++++++-------------- main.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/git_command.py b/git_command.py index 513b9ebf..d1e0c971 100644 --- a/git_command.py +++ b/git_command.py @@ -72,6 +72,8 @@ def terminate_ssh_clients(): pass _ssh_clients = [] +_git_version = None + class _GitCall(object): def version(self): p = GitCommand(None, ['--version'], capture_stdout=True) @@ -79,6 +81,21 @@ class _GitCall(object): return p.stdout return None + def version_tuple(self): + global _git_version + + if _git_version is None: + ver_str = git.version() + if ver_str.startswith('git version '): + _git_version = tuple( + map(lambda x: int(x), + ver_str[len('git version '):].strip().split('.')[0:3] + )) + else: + print >>sys.stderr, 'fatal: "%s" unsupported' % ver_str + sys.exit(1) + return _git_version + def __getattr__(self, name): name = name.replace('_','-') def fun(*cmdv): @@ -88,23 +105,9 @@ class _GitCall(object): return fun git = _GitCall() -_git_version = None - def git_require(min_version, fail=False): - global _git_version - - if _git_version is None: - ver_str = git.version() - if ver_str.startswith('git version '): - _git_version = tuple( - map(lambda x: int(x), - ver_str[len('git version '):].strip().split('.')[0:3] - )) - else: - print >>sys.stderr, 'fatal: "%s" unsupported' % ver_str - sys.exit(1) - - if min_version <= _git_version: + git_version = git.version_tuple() + if min_version <= git_version: return True if fail: need = '.'.join(map(lambda x: str(x), min_version)) diff --git a/main.py b/main.py index 48edc1cc..9c545b31 100755 --- a/main.py +++ b/main.py @@ -29,6 +29,7 @@ import sys import urllib2 from trace import SetTrace +from git_command import git, GitCommand from git_config import init_ssh, close_ssh from command import InteractiveCommand from command import MirrorSafeCommand @@ -134,6 +135,9 @@ class _Repo(object): print >>sys.stderr, 'error: no project in current directory' sys.exit(1) +def _MyRepoPath(): + return os.path.dirname(__file__) + def _MyWrapperPath(): return os.path.join(os.path.dirname(__file__), 'repo') @@ -200,11 +204,63 @@ def _PruneOptions(argv, opt): continue i += 1 +_user_agent = None + +def _UserAgent(): + global _user_agent + + if _user_agent is None: + py_version = sys.version_info + + os_name = sys.platform + if os_name == 'linux2': + os_name = 'Linux' + elif os_name == 'win32': + os_name = 'Win32' + elif os_name == 'cygwin': + os_name = 'Cygwin' + elif os_name == 'darwin': + os_name = 'Darwin' + + p = GitCommand( + None, ['describe', 'HEAD'], + cwd = _MyRepoPath(), + capture_stdout = True) + if p.Wait() == 0: + repo_version = p.stdout + if len(repo_version) > 0 and repo_version[-1] == '\n': + repo_version = repo_version[0:-1] + if len(repo_version) > 0 and repo_version[0] == 'v': + repo_version = repo_version[1:] + else: + repo_version = 'unknown' + + _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( + repo_version, + os_name, + '.'.join(map(lambda d: str(d), git.version_tuple())), + py_version[0], py_version[1], py_version[2]) + return _user_agent + +class _UserAgentHandler(urllib2.BaseHandler): + def http_request(self, req): + req.add_header('User-Agent', _UserAgent()) + return req + + def https_request(self, req): + req.add_header('User-Agent', _UserAgent()) + return req + def init_http(): + handlers = [_UserAgentHandler()] + if 'http_proxy' in os.environ: url = os.environ['http_proxy'] - proxy_support = urllib2.ProxyHandler({'http': url, 'https': url}) - urllib2.install_opener(urllib2.build_opener(proxy_support)) + handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) + if 'REPO_CURL_VERBOSE' in os.environ: + handlers.append(urllib2.HTTPHandler(debuglevel=1)) + handlers.append(urllib2.HTTPSHandler(debuglevel=1)) + urllib2.install_opener(urllib2.build_opener(*handlers)) def _Main(argv): opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")