OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This Source Code Form is subject to the terms of the Mozilla Public |
| 4 # License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
| 7 from collections import OrderedDict |
| 8 import os |
| 9 import subprocess |
| 10 |
| 11 def _ensure_line_exists(path, pattern): |
| 12 with open(path, 'a+') as f: |
| 13 file_content = [l.strip() for l in f.readlines()] |
| 14 if not pattern in file_content: |
| 15 file_content.append(pattern) |
| 16 f.seek(0, os.SEEK_SET) |
| 17 f.truncate() |
| 18 for l in file_content: |
| 19 print >>f, l |
| 20 |
| 21 class Mercurial(): |
| 22 def istype(self, repodir): |
| 23 return os.path.exists(os.path.join(repodir, ".hg")) |
| 24 |
| 25 def clone(self, source, target): |
| 26 if not source.endswith("/"): |
| 27 source += "/" |
| 28 subprocess.check_call(["hg", "clone", "--quiet", "--noupdate", source, targe
t]) |
| 29 |
| 30 def get_revision_id(self, repo, rev=None): |
| 31 command = ["hg", "id", "--repository", repo, "--id"] |
| 32 if rev: |
| 33 command.extend(["--rev", rev]) |
| 34 |
| 35 # Ignore stderr output and return code here: if revision lookup failed we |
| 36 # should simply return an empty string. |
| 37 result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess
.PIPE).communicate()[0] |
| 38 return result.strip() |
| 39 |
| 40 def pull(self, repo): |
| 41 subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"]) |
| 42 |
| 43 def update(self, repo, rev): |
| 44 subprocess.check_call(["hg", "update", "--repository", repo, "--quiet", "--c
heck", "--rev", rev]) |
| 45 |
| 46 def ignore(self, target, repo): |
| 47 |
| 48 if not self.istype(target): |
| 49 |
| 50 config_path = os.path.join(repo, ".hg", "hgrc") |
| 51 ignore_path = os.path.abspath(os.path.join(repo, ".hg", "dependencies")) |
| 52 |
| 53 config = RawConfigParser() |
| 54 config.read(config_path) |
| 55 |
| 56 if not config.has_section("ui"): |
| 57 config.add_section("ui") |
| 58 |
| 59 config.set("ui", "ignore.dependencies", ignore_path) |
| 60 with open(config_path, "w") as stream: |
| 61 config.write(stream) |
| 62 |
| 63 module = os.path.relpath(target, repo) |
| 64 _ensure_line_exists(ignore_path, module) |
| 65 |
| 66 def postprocess_url(self, url): |
| 67 return url |
| 68 |
| 69 class Git(): |
| 70 def istype(self, repodir): |
| 71 return os.path.exists(os.path.join(repodir, ".git")) |
| 72 |
| 73 def clone(self, source, target): |
| 74 source = source.rstrip("/") |
| 75 if not source.endswith(".git"): |
| 76 source += ".git" |
| 77 subprocess.check_call(["git", "clone", "--quiet", source, target]) |
| 78 |
| 79 def get_revision_id(self, repo, rev="HEAD"): |
| 80 command = ["git", "rev-parse", "--revs-only", rev + '^{commit}'] |
| 81 return subprocess.check_output(command, cwd=repo).strip() |
| 82 |
| 83 def pull(self, repo): |
| 84 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re
po) |
| 85 |
| 86 def update(self, repo, rev): |
| 87 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo) |
| 88 |
| 89 def ignore(self, target, repo): |
| 90 module = os.path.relpath(target, repo) |
| 91 exclude_file = os.path.join(repo, ".git", "info", "exclude") |
| 92 _ensure_line_exists(exclude_file, module) |
| 93 |
| 94 def postprocess_url(self, url): |
| 95 # Handle alternative syntax of SSH URLS |
| 96 if "@" in url and ":" in url and not urlparse.urlsplit(url).scheme: |
| 97 return "ssh://" + url.replace(":", "/", 1) |
| 98 return url |
| 99 |
| 100 repo_types = OrderedDict(( |
| 101 ("hg", Mercurial()), |
| 102 ("git", Git()), |
| 103 )) |
OLD | NEW |