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. 9, 2014, 2:37 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".
(...skipping 21 matching lines...) Expand all
60 # should simply return an empty string. 62 # should simply return an empty string.
61 result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess .PIPE).communicate()[0] 63 result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess .PIPE).communicate()[0]
62 return result.strip() 64 return result.strip()
63 65
64 def pull(self, repo): 66 def pull(self, repo):
65 subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"]) 67 subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"])
66 68
67 def update(self, repo, rev): 69 def update(self, repo, rev):
68 subprocess.check_call(["hg", "update", "--repository", repo, "--quiet", "--c heck", "--rev", rev]) 70 subprocess.check_call(["hg", "update", "--repository", repo, "--quiet", "--c heck", "--rev", rev])
69 71
72 def ignore(self, target, repo):
73
74 if not self.istype(target):
75
76 config_path = os.path.join(repo, ".hg", "hgrc")
77 ignore_path = os.path.abspath(os.path.join(repo, ".hg", "dependencies"))
78
79 config = RawConfigParser()
80 config.read(config_path)
81
82 if not config.has_section("ui"):
83 config.add_section("ui")
84
85 config.set("ui", "ignore.dependencies", ignore_path)
86 with open(config_path, "w") as stream:
87 config.write(stream)
88
89 module = os.path.relpath(target, repo)
90 _ensure_line_exists(ignore_path, module)
91
70 class Git(): 92 class Git():
71 def istype(self, repodir): 93 def istype(self, repodir):
72 return os.path.exists(os.path.join(repodir, ".git")) 94 return os.path.exists(os.path.join(repodir, ".git"))
73 95
74 def clone(self, source, target): 96 def clone(self, source, target):
75 source = source.rstrip("/") 97 source = source.rstrip("/")
76 if not source.endswith(".git"): 98 if not source.endswith(".git"):
77 source += ".git" 99 source += ".git"
78 subprocess.check_call(["git", "clone", "--quiet", source, target]) 100 subprocess.check_call(["git", "clone", "--quiet", source, target])
79 101
80 def get_revision_id(self, repo, rev="HEAD"): 102 def get_revision_id(self, repo, rev="HEAD"):
81 command = ["git", "rev-parse", "--revs-only", rev] 103 command = ["git", "rev-parse", "--revs-only", rev]
82 return subprocess.check_output(command, cwd=repo).strip() 104 return subprocess.check_output(command, cwd=repo).strip()
83 105
84 def pull(self, repo): 106 def pull(self, repo):
85 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po) 107 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po)
86 108
87 def update(self, repo, rev): 109 def update(self, repo, rev):
88 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo) 110 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo)
89 111
112 def ignore(self, target, repo):
113 module = os.path.relpath(target, repo)
114 exclude_file = os.path.join(repo, ".git", "info", "exclude")
115 _ensure_line_exists(exclude_file, module)
116
90 repo_types = OrderedDict(( 117 repo_types = OrderedDict((
91 ("hg", Mercurial()), 118 ("hg", Mercurial()),
92 ("git", Git()), 119 ("git", Git()),
93 )) 120 ))
94 121
95 def parse_spec(path, line): 122 def parse_spec(path, line):
96 if "=" not in line: 123 if "=" not in line:
97 logging.warning("Invalid line in file %s: %s" % (path, line)) 124 logging.warning("Invalid line in file %s: %s" % (path, line))
98 return None, None 125 return None, None
99 126
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 for key in roots: 194 for key in roots:
168 if key == parenttype or (key in repo_types and type is None): 195 if key == parenttype or (key in repo_types and type is None):
169 type = key 196 type = key
170 if type is None: 197 if type is None:
171 raise Exception("No valid source found to create %s" % target) 198 raise Exception("No valid source found to create %s" % target)
172 199
173 url = urlparse.urljoin(roots[type], sourcename) 200 url = urlparse.urljoin(roots[type], sourcename)
174 logging.info("Cloning repository %s into %s" % (url, target)) 201 logging.info("Cloning repository %s into %s" % (url, target))
175 repo_types[type].clone(url, target) 202 repo_types[type].clone(url, target)
176 203
204 for repo in repo_types.itervalues():
205 if repo.istype(parentrepo):
206 repo.ignore(target, parentrepo)
207
177 def update_repo(target, revisions): 208 def update_repo(target, revisions):
178 type = get_repo_type(target) 209 type = get_repo_type(target)
179 if type is None: 210 if type is None:
180 logging.warning("Type of repository %s unknown, skipping update" % target) 211 logging.warning("Type of repository %s unknown, skipping update" % target)
181 return 212 return
182 213
183 if type in revisions: 214 if type in revisions:
184 revision = revisions[type] 215 revision = revisions[type]
185 elif "*" in revisions: 216 elif "*" in revisions:
186 revision = revisions["*"] 217 revision = revisions["*"]
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 if sourcedata != targetdata: 270 if sourcedata != targetdata:
240 logging.info("Updating %s from %s, don't forget to commit" % (source, targ et)) 271 logging.info("Updating %s from %s, don't forget to commit" % (source, targ et))
241 with io.open(target, "wb") as handle: 272 with io.open(target, "wb") as handle:
242 handle.write(sourcedata) 273 handle.write(sourcedata)
243 if __name__ == "__main__": 274 if __name__ == "__main__":
244 logging.info("Restarting %s" % target) 275 logging.info("Restarting %s" % target)
245 os.execv(sys.executable, [sys.executable, target] + sys.argv[1:]) 276 os.execv(sys.executable, [sys.executable, target] + sys.argv[1:])
246 else: 277 else:
247 logging.warning("Cannot restart %s automatically, please rerun" % target ) 278 logging.warning("Cannot restart %s automatically, please rerun" % target )
248 279
280 def _ensure_line_exists(path, pattern):
281 with open(path, 'a+') as f:
282 file_content = [l.strip() for l in f.readlines()]
283 if not pattern in file_content:
284 file_content.append(pattern)
285 f.seek(0, os.SEEK_SET)
286 f.truncate()
287 for l in file_content:
288 print >>f, l
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