diff options
-rw-r--r-- | git_command.py | 46 | ||||
-rwxr-xr-x | main.py | 48 | ||||
-rw-r--r-- | tests/test_git_command.py | 13 |
3 files changed, 62 insertions, 45 deletions
diff --git a/git_command.py b/git_command.py index 67423035..32dcde09 100644 --- a/git_command.py +++ b/git_command.py | |||
@@ -98,6 +98,52 @@ class _GitCall(object): | |||
98 | return fun | 98 | return fun |
99 | git = _GitCall() | 99 | git = _GitCall() |
100 | 100 | ||
101 | |||
102 | _user_agent = None | ||
103 | |||
104 | def RepoUserAgent(): | ||
105 | """Return a User-Agent string suitable for HTTP-like services. | ||
106 | |||
107 | We follow the style as documented here: | ||
108 | https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent | ||
109 | """ | ||
110 | global _user_agent | ||
111 | |||
112 | if _user_agent is None: | ||
113 | py_version = sys.version_info | ||
114 | |||
115 | os_name = sys.platform | ||
116 | if os_name == 'linux2': | ||
117 | os_name = 'Linux' | ||
118 | elif os_name == 'win32': | ||
119 | os_name = 'Win32' | ||
120 | elif os_name == 'cygwin': | ||
121 | os_name = 'Cygwin' | ||
122 | elif os_name == 'darwin': | ||
123 | os_name = 'Darwin' | ||
124 | |||
125 | p = GitCommand( | ||
126 | None, ['describe', 'HEAD'], | ||
127 | cwd=os.path.dirname(__file__), | ||
128 | capture_stdout=True) | ||
129 | if p.Wait() == 0: | ||
130 | repo_version = p.stdout | ||
131 | if repo_version and repo_version[-1] == '\n': | ||
132 | repo_version = repo_version[0:-1] | ||
133 | if repo_version and repo_version[0] == 'v': | ||
134 | repo_version = repo_version[1:] | ||
135 | else: | ||
136 | repo_version = 'unknown' | ||
137 | |||
138 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( | ||
139 | repo_version, | ||
140 | os_name, | ||
141 | git.version_tuple().full, | ||
142 | py_version.major, py_version.minor, py_version.micro) | ||
143 | |||
144 | return _user_agent | ||
145 | |||
146 | |||
101 | def git_require(min_version, fail=False, msg=''): | 147 | def git_require(min_version, fail=False, msg=''): |
102 | git_version = git.version_tuple() | 148 | git_version = git.version_tuple() |
103 | if min_version <= git_version: | 149 | if min_version <= git_version: |
@@ -46,7 +46,7 @@ except ImportError: | |||
46 | from color import SetDefaultColoring | 46 | from color import SetDefaultColoring |
47 | import event_log | 47 | import event_log |
48 | from repo_trace import SetTrace | 48 | from repo_trace import SetTrace |
49 | from git_command import git, GitCommand | 49 | from git_command import git, GitCommand, RepoUserAgent |
50 | from git_config import init_ssh, close_ssh | 50 | from git_config import init_ssh, close_ssh |
51 | from command import InteractiveCommand | 51 | from command import InteractiveCommand |
52 | from command import MirrorSafeCommand | 52 | from command import MirrorSafeCommand |
@@ -244,10 +244,6 @@ class _Repo(object): | |||
244 | return result | 244 | return result |
245 | 245 | ||
246 | 246 | ||
247 | def _MyRepoPath(): | ||
248 | return os.path.dirname(__file__) | ||
249 | |||
250 | |||
251 | def _CheckWrapperVersion(ver, repo_path): | 247 | def _CheckWrapperVersion(ver, repo_path): |
252 | if not repo_path: | 248 | if not repo_path: |
253 | repo_path = '~/bin/repo' | 249 | repo_path = '~/bin/repo' |
@@ -299,51 +295,13 @@ def _PruneOptions(argv, opt): | |||
299 | continue | 295 | continue |
300 | i += 1 | 296 | i += 1 |
301 | 297 | ||
302 | _user_agent = None | ||
303 | |||
304 | def _UserAgent(): | ||
305 | global _user_agent | ||
306 | |||
307 | if _user_agent is None: | ||
308 | py_version = sys.version_info | ||
309 | |||
310 | os_name = sys.platform | ||
311 | if os_name == 'linux2': | ||
312 | os_name = 'Linux' | ||
313 | elif os_name == 'win32': | ||
314 | os_name = 'Win32' | ||
315 | elif os_name == 'cygwin': | ||
316 | os_name = 'Cygwin' | ||
317 | elif os_name == 'darwin': | ||
318 | os_name = 'Darwin' | ||
319 | |||
320 | p = GitCommand( | ||
321 | None, ['describe', 'HEAD'], | ||
322 | cwd = _MyRepoPath(), | ||
323 | capture_stdout = True) | ||
324 | if p.Wait() == 0: | ||
325 | repo_version = p.stdout | ||
326 | if len(repo_version) > 0 and repo_version[-1] == '\n': | ||
327 | repo_version = repo_version[0:-1] | ||
328 | if len(repo_version) > 0 and repo_version[0] == 'v': | ||
329 | repo_version = repo_version[1:] | ||
330 | else: | ||
331 | repo_version = 'unknown' | ||
332 | |||
333 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( | ||
334 | repo_version, | ||
335 | os_name, | ||
336 | git.version_tuple().full, | ||
337 | py_version[0], py_version[1], py_version[2]) | ||
338 | return _user_agent | ||
339 | |||
340 | class _UserAgentHandler(urllib.request.BaseHandler): | 298 | class _UserAgentHandler(urllib.request.BaseHandler): |
341 | def http_request(self, req): | 299 | def http_request(self, req): |
342 | req.add_header('User-Agent', _UserAgent()) | 300 | req.add_header('User-Agent', RepoUserAgent()) |
343 | return req | 301 | return req |
344 | 302 | ||
345 | def https_request(self, req): | 303 | def https_request(self, req): |
346 | req.add_header('User-Agent', _UserAgent()) | 304 | req.add_header('User-Agent', RepoUserAgent()) |
347 | return req | 305 | return req |
348 | 306 | ||
349 | def _AddPasswordFromUserInput(handler, msg, req): | 307 | def _AddPasswordFromUserInput(handler, msg, req): |
diff --git a/tests/test_git_command.py b/tests/test_git_command.py index 928eb402..4d65d3ce 100644 --- a/tests/test_git_command.py +++ b/tests/test_git_command.py | |||
@@ -18,6 +18,7 @@ | |||
18 | 18 | ||
19 | from __future__ import print_function | 19 | from __future__ import print_function |
20 | 20 | ||
21 | import re | ||
21 | import unittest | 22 | import unittest |
22 | 23 | ||
23 | import git_command | 24 | import git_command |
@@ -47,3 +48,15 @@ class GitCallUnitTest(unittest.TestCase): | |||
47 | self.assertLess(ver, (9999, 9999, 9999)) | 48 | self.assertLess(ver, (9999, 9999, 9999)) |
48 | 49 | ||
49 | self.assertNotEqual('', ver.full) | 50 | self.assertNotEqual('', ver.full) |
51 | |||
52 | |||
53 | class RepoUserAgentUnitTest(unittest.TestCase): | ||
54 | """Tests the RepoUserAgent function.""" | ||
55 | |||
56 | def test_smoke(self): | ||
57 | """Make sure it returns something useful.""" | ||
58 | ua = git_command.RepoUserAgent() | ||
59 | # We can't dive too deep because of OS/tool differences, but we can check | ||
60 | # the general form. | ||
61 | m = re.match(r'^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+', ua) | ||
62 | self.assertIsNotNone(m) | ||