Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: ensure_dependencies.py

Issue 5934936779390976: 1377 - Blacklist dependencies from SCM tracking (Closed)
Patch Set: 1377 - Blacklist dependencies from SCM tracking Created Oct. 8, 2014, 8:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # coding: utf-8 2 # coding: utf-8
3 3
4 # This file is part of the Adblock Plus build tools, 4 # This file is part of the Adblock Plus build tools,
5 # Copyright (C) 2006-2014 Eyeo GmbH 5 # Copyright (C) 2006-2014 Eyeo GmbH
6 # 6 #
7 # Adblock Plus is free software: you can redistribute it and/or modify 7 # Adblock Plus is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 3 as 8 # it under the terms of the GNU General Public License version 3 as
9 # published by the Free Software Foundation. 9 # published by the Free Software Foundation.
10 # 10 #
11 # Adblock Plus is distributed in the hope that it will be useful, 11 # Adblock Plus is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details. 14 # GNU General Public License for more details.
15 # 15 #
16 # You should have received a copy of the GNU General Public License 16 # You should have received a copy of the GNU General Public License
17 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 17 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
18 18
19 import sys 19 import sys
20 import os 20 import os
21 import posixpath 21 import posixpath
22 import re 22 import re
23 import io 23 import io
24 import errno 24 import errno
25 import logging 25 import logging
26 import subprocess 26 import subprocess
27 import urlparse 27 import urlparse
28
28 from collections import OrderedDict 29 from collections import OrderedDict
30 from ConfigParser import RawConfigParser
29 31
30 USAGE = """ 32 USAGE = """
31 A dependencies file should look like this: 33 A dependencies file should look like this:
32 34
33 # VCS-specific root URLs for the repositories 35 # VCS-specific root URLs for the repositories
34 _root = hg:https://hg.adblockplus.org/ git:https://github.com/adblockplus/ 36 _root = hg:https://hg.adblockplus.org/ git:https://github.com/adblockplus/
35 # File to update this script from (optional) 37 # File to update this script from (optional)
36 _self = buildtools/ensure_dependencies.py 38 _self = buildtools/ensure_dependencies.py
37 # Check out elemhidehelper repository into extensions/elemhidehelper directory 39 # Check out elemhidehelper repository into extensions/elemhidehelper directory
38 # at tag "1.2". 40 # at tag "1.2".
39 extensions/elemhidehelper = elemhidehelper 1.2 41 extensions/elemhidehelper = elemhidehelper 1.2
40 # Check out buildtools repository into buildtools directory at VCS-specific 42 # Check out buildtools repository into buildtools directory at VCS-specific
41 # revision IDs. 43 # revision IDs.
42 buildtools = buildtools hg:016d16f7137b git:f3f8692f82e5 44 buildtools = buildtools hg:016d16f7137b git:f3f8692f82e5
43 """ 45 """
44 46
45 class Mercurial(): 47 class SCM(object):
48 def _ensure_line_exists(self, ignore_file, pattern):
Wladimir Palant 2014/10/08 21:40:30 The parameter should be called path, not ignore_fi
49 with open(ignore_file, 'a+') as f:
50 file_content = [l.strip() for l in f.readlines()]
51 if not pattern in file_content:
52 file_content.append(pattern)
53 f.seek(0, os.SEEK_SET)
54 for l in file_content:
55 print >>f, l
56 f.truncate()
57
58 class Mercurial(SCM):
46 def istype(self, repodir): 59 def istype(self, repodir):
47 return os.path.exists(os.path.join(repodir, ".hg")) 60 return os.path.exists(os.path.join(repodir, ".hg"))
48 61
49 def clone(self, source, target): 62 def clone(self, source, target):
50 if not source.endswith("/"): 63 if not source.endswith("/"):
51 source += "/" 64 source += "/"
52 subprocess.check_call(["hg", "clone", "--quiet", "--noupdate", source, targe t]) 65 subprocess.check_call(["hg", "clone", "--quiet", "--noupdate", source, targe t])
53 66
54 def get_revision_id(self, repo, rev=None): 67 def get_revision_id(self, repo, rev=None):
55 command = ["hg", "id", "--repository", repo, "--id"] 68 command = ["hg", "id", "--repository", repo, "--id"]
56 if rev: 69 if rev:
57 command.extend(["--rev", rev]) 70 command.extend(["--rev", rev])
58 71
59 # Ignore stderr output and return code here: if revision lookup failed we 72 # Ignore stderr output and return code here: if revision lookup failed we
60 # should simply return an empty string. 73 # should simply return an empty string.
61 result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess .PIPE).communicate()[0] 74 result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess .PIPE).communicate()[0]
62 return result.strip() 75 return result.strip()
63 76
64 def pull(self, repo): 77 def pull(self, repo):
65 subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"]) 78 subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"])
66 79
67 def update(self, repo, rev): 80 def update(self, repo, rev):
68 subprocess.check_call(["hg", "update", "--repository", repo, "--quiet", "--c heck", "--rev", rev]) 81 subprocess.check_call(["hg", "update", "--repository", repo, "--quiet", "--c heck", "--rev", rev])
69 82
70 class Git(): 83 def ignore(self, target, repo):
84
85 if not self.istype(target):
86
87 config_path = os.path.join(repo, ".hg", "hgrc")
88 ignore_path = os.path.abspath(os.path.join(repo, ".hg", "dependencies"))
89
90 config = RawConfigParser()
91 config.read(config_path)
92
93 if not config.has_section("ui"):
94 config.add_section("ui")
95
96 config.set("ui", "ignore.dependencies", ignore_path)
97 with open(config_path, "w") as stream:
98 config.write(stream)
99
100 module = os.path.relpath(target, repo)
101 self._ensure_line_exists(ignore_path, module)
102
103 class Git(SCM):
71 def istype(self, repodir): 104 def istype(self, repodir):
72 return os.path.exists(os.path.join(repodir, ".git")) 105 return os.path.exists(os.path.join(repodir, ".git"))
73 106
74 def clone(self, source, target): 107 def clone(self, source, target):
75 source = source.rstrip("/") 108 source = source.rstrip("/")
76 if not source.endswith(".git"): 109 if not source.endswith(".git"):
77 source += ".git" 110 source += ".git"
78 subprocess.check_call(["git", "clone", "--quiet", source, target]) 111 subprocess.check_call(["git", "clone", "--quiet", source, target])
79 112
80 def get_revision_id(self, repo, rev="HEAD"): 113 def get_revision_id(self, repo, rev="HEAD"):
81 command = ["git", "rev-parse", "--revs-only", rev] 114 command = ["git", "rev-parse", "--revs-only", rev]
82 return subprocess.check_output(command, cwd=repo).strip() 115 return subprocess.check_output(command, cwd=repo).strip()
83 116
84 def pull(self, repo): 117 def pull(self, repo):
85 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po) 118 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po)
86 119
87 def update(self, repo, rev): 120 def update(self, repo, rev):
88 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo) 121 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo)
89 122
123 def ignore(self, target, repo):
124 module = os.path.relpath(target, repo)
125 exclude_file = os.path.join(repo, ".git", "info", "exclude")
126 self._ensure_line_exists(exclude_file, module)
127
90 repo_types = OrderedDict(( 128 repo_types = OrderedDict((
91 ("hg", Mercurial()), 129 ("hg", Mercurial()),
92 ("git", Git()), 130 ("git", Git()),
93 )) 131 ))
94 132
95 def parse_spec(path, line): 133 def parse_spec(path, line):
96 if "=" not in line: 134 if "=" not in line:
97 logging.warning("Invalid line in file %s: %s" % (path, line)) 135 logging.warning("Invalid line in file %s: %s" % (path, line))
98 return None, None 136 return None, None
99 137
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 logging.warning("Too much subrepository nesting, ignoring %s" % repo) 249 logging.warning("Too much subrepository nesting, ignoring %s" % repo)
212 250
213 if overrideroots is not None: 251 if overrideroots is not None:
214 config["_root"] = overrideroots 252 config["_root"] = overrideroots
215 253
216 for dir, revisions in config.iteritems(): 254 for dir, revisions in config.iteritems():
217 if dir.startswith("_") or revisions["_source"] in skipdependencies: 255 if dir.startswith("_") or revisions["_source"] in skipdependencies:
218 continue 256 continue
219 target = safe_join(repodir, dir) 257 target = safe_join(repodir, dir)
220 ensure_repo(repodir, target, config.get("_root", {}), revisions["_source"]) 258 ensure_repo(repodir, target, config.get("_root", {}), revisions["_source"])
259 for repo in repo_types.itervalues():
260 if repo.istype(repodir):
261 repo.ignore(target, repodir)
Wladimir Palant 2014/10/08 21:40:30 Why was this code moved again? As noted before, it
221 update_repo(target, revisions) 262 update_repo(target, revisions)
222 resolve_deps(target, level + 1, self_update=False, overrideroots=overrideroo ts, skipdependencies=skipdependencies) 263 resolve_deps(target, level + 1, self_update=False, overrideroots=overrideroo ts, skipdependencies=skipdependencies)
223 264
224 if self_update and "_self" in config and "*" in config["_self"]: 265 if self_update and "_self" in config and "*" in config["_self"]:
225 source = safe_join(repodir, config["_self"]["*"]) 266 source = safe_join(repodir, config["_self"]["*"])
226 try: 267 try:
227 with io.open(source, "rb") as handle: 268 with io.open(source, "rb") as handle:
228 sourcedata = handle.read() 269 sourcedata = handle.read()
229 except IOError, e: 270 except IOError, e:
230 if e.errno != errno.ENOENT: 271 if e.errno != errno.ENOENT:
(...skipping 15 matching lines...) Expand all
246 else: 287 else:
247 logging.warning("Cannot restart %s automatically, please rerun" % target ) 288 logging.warning("Cannot restart %s automatically, please rerun" % target )
248 289
249 if __name__ == "__main__": 290 if __name__ == "__main__":
250 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) 291 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
251 repos = sys.argv[1:] 292 repos = sys.argv[1:]
252 if not len(repos): 293 if not len(repos):
253 repos = [os.getcwd()] 294 repos = [os.getcwd()]
254 for repo in repos: 295 for repo in repos:
255 resolve_deps(repo) 296 resolve_deps(repo)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld