Index: vcs.py |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/vcs.py |
@@ -0,0 +1,103 @@ |
+# coding: utf-8 |
+ |
+# This Source Code Form is subject to the terms of the Mozilla Public |
+# License, v. 2.0. If a copy of the MPL was not distributed with this |
+# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
+ |
+from collections import OrderedDict |
+import os |
+import subprocess |
Wladimir Palant
2015/05/18 15:27:00
This has been moved from ensure_dependencies.py wi
|
+ |
+def _ensure_line_exists(path, pattern): |
+ with open(path, 'a+') as f: |
+ file_content = [l.strip() for l in f.readlines()] |
+ if not pattern in file_content: |
+ file_content.append(pattern) |
+ f.seek(0, os.SEEK_SET) |
+ f.truncate() |
+ for l in file_content: |
+ print >>f, l |
+ |
+class Mercurial(): |
+ def istype(self, repodir): |
+ return os.path.exists(os.path.join(repodir, ".hg")) |
+ |
+ def clone(self, source, target): |
+ if not source.endswith("/"): |
+ source += "/" |
+ subprocess.check_call(["hg", "clone", "--quiet", "--noupdate", source, target]) |
+ |
+ def get_revision_id(self, repo, rev=None): |
+ command = ["hg", "id", "--repository", repo, "--id"] |
+ if rev: |
+ command.extend(["--rev", rev]) |
+ |
+ # Ignore stderr output and return code here: if revision lookup failed we |
+ # should simply return an empty string. |
+ result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] |
+ return result.strip() |
+ |
+ def pull(self, repo): |
+ subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"]) |
+ |
+ def update(self, repo, rev): |
+ subprocess.check_call(["hg", "update", "--repository", repo, "--quiet", "--check", "--rev", rev]) |
+ |
+ def ignore(self, target, repo): |
+ |
+ if not self.istype(target): |
+ |
+ config_path = os.path.join(repo, ".hg", "hgrc") |
+ ignore_path = os.path.abspath(os.path.join(repo, ".hg", "dependencies")) |
+ |
+ config = RawConfigParser() |
+ config.read(config_path) |
+ |
+ if not config.has_section("ui"): |
+ config.add_section("ui") |
+ |
+ config.set("ui", "ignore.dependencies", ignore_path) |
+ with open(config_path, "w") as stream: |
+ config.write(stream) |
+ |
+ module = os.path.relpath(target, repo) |
+ _ensure_line_exists(ignore_path, module) |
+ |
+ def postprocess_url(self, url): |
+ return url |
+ |
+class Git(): |
+ def istype(self, repodir): |
+ return os.path.exists(os.path.join(repodir, ".git")) |
+ |
+ def clone(self, source, target): |
+ source = source.rstrip("/") |
+ if not source.endswith(".git"): |
+ source += ".git" |
+ subprocess.check_call(["git", "clone", "--quiet", source, target]) |
+ |
+ def get_revision_id(self, repo, rev="HEAD"): |
+ command = ["git", "rev-parse", "--revs-only", rev + '^{commit}'] |
+ return subprocess.check_output(command, cwd=repo).strip() |
+ |
+ def pull(self, repo): |
+ subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=repo) |
+ |
+ def update(self, repo, rev): |
+ subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo) |
+ |
+ def ignore(self, target, repo): |
+ module = os.path.relpath(target, repo) |
+ exclude_file = os.path.join(repo, ".git", "info", "exclude") |
+ _ensure_line_exists(exclude_file, module) |
+ |
+ def postprocess_url(self, url): |
+ # Handle alternative syntax of SSH URLS |
+ if "@" in url and ":" in url and not urlparse.urlsplit(url).scheme: |
+ return "ssh://" + url.replace(":", "/", 1) |
+ return url |
+ |
+repo_types = OrderedDict(( |
+ ("hg", Mercurial()), |
+ ("git", Git()), |
+)) |