From 29c06f00d98e7050b2e0eacec473bd74e6a7c30f Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Tue, 15 Jul 2014 11:55:27 -0600 Subject: [PATCH] Add and use new fetch() method The old fetch_branch() wasn't behaving properly with the ceph.com git mirror. This method works with github.com and ceph.com. Add a couple unit tests, and leave the old fetch_branch() in place for now, even though nothing uses it. Signed-off-by: Zack Cerza --- teuthology/repo_utils.py | 27 +++++++++++++++++++++++---- teuthology/test/test_repo_utils.py | 12 ++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/teuthology/repo_utils.py b/teuthology/repo_utils.py index 0399aed3802e2..bd0d220cd01b5 100644 --- a/teuthology/repo_utils.py +++ b/teuthology/repo_utils.py @@ -25,7 +25,7 @@ def enforce_repo_state(repo_url, dest_path, branch, remove_on_error=True): clone_repo(repo_url, dest_path, branch) elif time.time() - os.stat('/etc/passwd').st_mtime > 60: # only do this at most once per minute - fetch_branch(dest_path, branch) + fetch(dest_path) out = subprocess.check_output(('touch', dest_path)) if out: log.info(out) @@ -66,11 +66,30 @@ def clone_repo(repo_url, dest_path, branch): raise RuntimeError("git clone failed!") -def fetch_branch(dest_path, branch): +def fetch(repo_path): + """ + Call "git fetch -p origin" + + :param repo_path: The full path to the repository + :raises: RuntimeError if the operation fails + """ + log.debug("Fetching from upstream into %s", repo_path) + proc = subprocess.Popen( + ('git', 'fetch', '-p', 'origin'), + cwd=repo_path, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + if proc.wait() != 0: + out = proc.stdout.read() + log.error(out) + raise RuntimeError("git fetch failed!") + + +def fetch_branch(repo_path, branch): """ Call "git fetch -p origin " - :param dest_path: The full path to the destination directory + :param repo_path: The full path to the repository :param branch: The branch. :raises: BranchNotFoundError if the branch is not found; RuntimeError for other errors @@ -79,7 +98,7 @@ def fetch_branch(dest_path, branch): log.info("Fetching %s from upstream", branch) proc = subprocess.Popen( ('git', 'fetch', '-p', 'origin', branch), - cwd=dest_path, + cwd=repo_path, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if proc.wait() != 0: diff --git a/teuthology/test/test_repo_utils.py b/teuthology/test/test_repo_utils.py index eeda81acb170d..d85fed8dbc1f9 100644 --- a/teuthology/test/test_repo_utils.py +++ b/teuthology/test/test_repo_utils.py @@ -65,6 +65,18 @@ class TestRepoUtils(object): repo_utils.clone_repo(self.repo_url, self.dest_path, 'nobranch') assert not os.path.exists(self.dest_path) + def test_fetch_no_repo(self): + fake_dest_path = '/tmp/not_a_repo' + assert not os.path.exists(fake_dest_path) + with raises(OSError): + repo_utils.fetch(fake_dest_path) + assert not os.path.exists(fake_dest_path) + + def test_fetch_noop(self): + repo_utils.clone_repo(self.repo_url, self.dest_path, 'master') + repo_utils.fetch(self.dest_path) + assert os.path.exists(self.dest_path) + def test_fetch_branch_no_repo(self): fake_dest_path = '/tmp/not_a_repo' assert not os.path.exists(fake_dest_path) -- 2.39.5