Chromium Code Reviews| Index: recipe_engine/fetch.py |
| diff --git a/recipe_engine/fetch.py b/recipe_engine/fetch.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d537e70dc7b1884c190592380e85131dc6711469 |
| --- /dev/null |
| +++ b/recipe_engine/fetch.py |
| @@ -0,0 +1,73 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import logging |
| +import os |
| +import subprocess |
| +import sys |
| + |
| + |
| +class FetchError(Exception): |
| + pass |
| + |
| + |
| +class UncleanFilesystemError(FetchError): |
| + pass |
| + |
| + |
| +class FetchNotAllowedError(FetchError): |
| + pass |
| + |
| + |
| +def _run_git(checkout_dir, *args): |
| + if sys.platform.startswith(('win', 'cygwin')): |
| + cmd = ['git.bat'] |
|
iannucci1
2016/05/20 18:04:22
I think we need a full path here, right? Otherwise
Paweł Hajdan Jr.
2016/05/20 21:59:57
This is existing code as you noticed, and point #2
|
| + else: |
| + cmd = ['git'] |
| + |
| + if checkout_dir is not None: |
| + cmd += ['-C', checkout_dir] |
| + cmd += list(args) |
| + |
| + logging.info('Running: %s', cmd) |
| + return subprocess.check_output(cmd) |
| + |
| + |
| +def _cleanup_pyc(path): |
| + """Removes any .pyc files from |path|'s directory tree. |
| + This ensures we always use the fresh code. |
| + """ |
| + for root, dirs, files in os.walk(path): |
| + for f in files: |
| + if f.endswith('.pyc'): |
| + os.unlink(os.path.join(root, f)) |
| + |
| + |
| +def fetch_from_git(repo, revision, checkout_dir, allow_fetch): |
| + """Fetches given |repo| at |revision| to |checkout_dir| using git. |
| + Network operations are performed only if |allow_fetch| is True. |
| + """ |
| + logging.info('Freshening repository %s in %s', repo, checkout_dir) |
| + |
| + if not os.path.isdir(checkout_dir): |
| + if allow_fetch: |
| + _run_git(None, 'clone', '-q', repo, checkout_dir) |
| + else: |
| + raise FetchNotAllowedError( |
| + 'need to clone %s but fetch not allowed' % repo) |
| + elif not os.path.isdir(os.path.join(checkout_dir, '.git')): |
| + raise UncleanFilesystemError( |
| + '%s exists but is not a git repo' % checkout_dir) |
| + |
| + try: |
| + _run_git(checkout_dir, 'rev-parse', '-q', '--verify', |
| + '%s^{commit}' % revision) |
| + except subprocess.CalledProcessError: |
| + if allow_fetch: |
| + _run_git(checkout_dir, 'fetch') |
| + else: |
| + raise FetchNotAllowedError( |
| + 'need to fetch %s but fetch not allowed' % repo) |
| + _run_git(checkout_dir, 'reset', '-q', '--hard', revision) |
| + _cleanup_pyc(checkout_dir) |