summaryrefslogtreecommitdiffstats
path: root/main.py
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2011-09-19 08:05:31 -0700
committerShawn O. Pearce <sop@google.com>2011-09-19 14:51:47 -0700
commit334851e4b6390f4c78e463b977003f1d967c88ed (patch)
tree7c8c5004c7cbb3079b460aee97095955013147ff /main.py
parent014d0609899dba70a1af0b32de377ca08c54aded (diff)
downloadgit-repo-334851e4b6390f4c78e463b977003f1d967c88ed.tar.gz
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 <sop@google.com>
Diffstat (limited to 'main.py')
-rwxr-xr-xmain.py60
1 files changed, 58 insertions, 2 deletions
diff --git a/main.py b/main.py
index 48edc1cc..9c545b31 100755
--- a/main.py
+++ b/main.py
@@ -29,6 +29,7 @@ import sys
29import urllib2 29import urllib2
30 30
31from trace import SetTrace 31from trace import SetTrace
32from git_command import git, GitCommand
32from git_config import init_ssh, close_ssh 33from git_config import init_ssh, close_ssh
33from command import InteractiveCommand 34from command import InteractiveCommand
34from command import MirrorSafeCommand 35from command import MirrorSafeCommand
@@ -134,6 +135,9 @@ class _Repo(object):
134 print >>sys.stderr, 'error: no project in current directory' 135 print >>sys.stderr, 'error: no project in current directory'
135 sys.exit(1) 136 sys.exit(1)
136 137
138def _MyRepoPath():
139 return os.path.dirname(__file__)
140
137def _MyWrapperPath(): 141def _MyWrapperPath():
138 return os.path.join(os.path.dirname(__file__), 'repo') 142 return os.path.join(os.path.dirname(__file__), 'repo')
139 143
@@ -200,11 +204,63 @@ def _PruneOptions(argv, opt):
200 continue 204 continue
201 i += 1 205 i += 1
202 206
207_user_agent = None
208
209def _UserAgent():
210 global _user_agent
211
212 if _user_agent is None:
213 py_version = sys.version_info
214
215 os_name = sys.platform
216 if os_name == 'linux2':
217 os_name = 'Linux'
218 elif os_name == 'win32':
219 os_name = 'Win32'
220 elif os_name == 'cygwin':
221 os_name = 'Cygwin'
222 elif os_name == 'darwin':
223 os_name = 'Darwin'
224
225 p = GitCommand(
226 None, ['describe', 'HEAD'],
227 cwd = _MyRepoPath(),
228 capture_stdout = True)
229 if p.Wait() == 0:
230 repo_version = p.stdout
231 if len(repo_version) > 0 and repo_version[-1] == '\n':
232 repo_version = repo_version[0:-1]
233 if len(repo_version) > 0 and repo_version[0] == 'v':
234 repo_version = repo_version[1:]
235 else:
236 repo_version = 'unknown'
237
238 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
239 repo_version,
240 os_name,
241 '.'.join(map(lambda d: str(d), git.version_tuple())),
242 py_version[0], py_version[1], py_version[2])
243 return _user_agent
244
245class _UserAgentHandler(urllib2.BaseHandler):
246 def http_request(self, req):
247 req.add_header('User-Agent', _UserAgent())
248 return req
249
250 def https_request(self, req):
251 req.add_header('User-Agent', _UserAgent())
252 return req
253
203def init_http(): 254def init_http():
255 handlers = [_UserAgentHandler()]
256
204 if 'http_proxy' in os.environ: 257 if 'http_proxy' in os.environ:
205 url = os.environ['http_proxy'] 258 url = os.environ['http_proxy']
206 proxy_support = urllib2.ProxyHandler({'http': url, 'https': url}) 259 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))
207 urllib2.install_opener(urllib2.build_opener(proxy_support)) 260 if 'REPO_CURL_VERBOSE' in os.environ:
261 handlers.append(urllib2.HTTPHandler(debuglevel=1))
262 handlers.append(urllib2.HTTPSHandler(debuglevel=1))
263 urllib2.install_opener(urllib2.build_opener(*handlers))
208 264
209def _Main(argv): 265def _Main(argv):
210 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") 266 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")