Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Unified Diff: recipe_engine/fetch.py

Issue 1997023002: recipe engine: add remote_run command (Closed) Base URL: https://github.com/luci/recipes-py.git@master
Patch Set: trybots Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | recipe_engine/package.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: recipe_engine/fetch.py
diff --git a/recipe_engine/fetch.py b/recipe_engine/fetch.py
new file mode 100644
index 0000000000000000000000000000000000000000..63e404ef623fe09f4a53f0c7e609c4bc9ea9bd44
--- /dev/null
+++ b/recipe_engine/fetch.py
@@ -0,0 +1,68 @@
+# 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 sys
+
+from .third_party import subprocess42
+
+
+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']
+ else:
+ cmd = ['git']
+
+ if checkout_dir is not None:
+ cmd += ['-C', checkout_dir]
+ cmd += list(args)
+
+ logging.info('Running: %s', cmd)
+ return subprocess42.check_output(cmd)
+
+
+def ensure_git_checkout(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 not allow_fetch:
+ raise FetchNotAllowedError(
+ 'need to clone %s but fetch not allowed' % repo)
+ _run_git(None, 'clone', '-q', repo, checkout_dir)
+ elif not os.path.isdir(os.path.join(checkout_dir, '.git')):
+ raise UncleanFilesystemError(
+ '%s exists but is not a git repo' % checkout_dir)
+
+ actual_origin = _run_git(checkout_dir, 'config', 'remote.origin.url').strip()
+ if actual_origin != repo:
+ raise UncleanFilesystemError(
+ ('workdir %r exists but uses a different origin url %r '
+ 'than requested %r') % (checkout_dir, actual_origin, repo))
+
+ try:
+ _run_git(checkout_dir, 'rev-parse', '-q', '--verify',
+ '%s^{commit}' % revision)
+ except subprocess42.CalledProcessError:
+ if not allow_fetch:
+ raise FetchNotAllowedError(
+ 'need to fetch %s but fetch not allowed' % repo)
+ _run_git(checkout_dir, 'fetch')
+ _run_git(checkout_dir, 'reset', '-q', '--hard', revision)
« no previous file with comments | « no previous file | recipe_engine/package.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698