Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 # |
(...skipping 26 matching lines...) Expand all Loading... | |
37 # File to update this script from (optional) | 37 # File to update this script from (optional) |
38 _self = buildtools/ensure_dependencies.py | 38 _self = buildtools/ensure_dependencies.py |
39 # Check out elemhidehelper repository into extensions/elemhidehelper directory | 39 # Check out elemhidehelper repository into extensions/elemhidehelper directory |
40 # at tag "1.2". | 40 # at tag "1.2". |
41 extensions/elemhidehelper = elemhidehelper 1.2 | 41 extensions/elemhidehelper = elemhidehelper 1.2 |
42 # Check out buildtools repository into buildtools directory at VCS-specific | 42 # Check out buildtools repository into buildtools directory at VCS-specific |
43 # revision IDs. | 43 # revision IDs. |
44 buildtools = buildtools hg:016d16f7137b git:f3f8692f82e5 | 44 buildtools = buildtools hg:016d16f7137b git:f3f8692f82e5 |
45 """ | 45 """ |
46 | 46 |
47 class SCM(object): | 47 class Mercurial(): |
48 def _ignore(self, target, file_name): | |
Wladimir Palant
2014/10/07 22:30:14
That should simply be a helper function _ensure_li
aalvz
2014/10/08 13:49:18
Done.
Wladimir Palant
2014/10/08 21:40:30
And the same file format? That's merely a coincide
| |
49 with open(file_name, 'a+') as f: | |
50 file_content = [l.strip() for l in f.readlines()] | |
51 if not target in file_content: | |
52 if len(file_content): | |
53 f.seek(-1, os.SEEK_CUR) | |
54 last_character = f.read(1) | |
55 if not last_character in "\r\n": | |
Wladimir Palant
2014/10/07 22:30:14
No need to overcomplicate this:
file_content.appe
aalvz
2014/10/08 13:49:18
I think the solution you are suggesting and the cu
mathias
2014/10/08 17:37:55
While the first point is not really true (though i
| |
56 f.write(os.linesep) | |
57 f.write(target) | |
58 f.write(os.linesep) | |
59 | |
60 class Mercurial(SCM): | |
61 def istype(self, repodir): | 48 def istype(self, repodir): |
62 return os.path.exists(os.path.join(repodir, ".hg")) | 49 return os.path.exists(os.path.join(repodir, ".hg")) |
63 | 50 |
64 def clone(self, source, target): | 51 def clone(self, source, target): |
65 if not source.endswith("/"): | 52 if not source.endswith("/"): |
66 source += "/" | 53 source += "/" |
67 subprocess.check_call(["hg", "clone", "--quiet", "--noupdate", source, targe t]) | 54 subprocess.check_call(["hg", "clone", "--quiet", "--noupdate", source, targe t]) |
68 | 55 |
69 def get_revision_id(self, repo, rev=None): | 56 def get_revision_id(self, repo, rev=None): |
70 command = ["hg", "id", "--repository", repo, "--id"] | 57 command = ["hg", "id", "--repository", repo, "--id"] |
71 if rev: | 58 if rev: |
72 command.extend(["--rev", rev]) | 59 command.extend(["--rev", rev]) |
73 | 60 |
74 # Ignore stderr output and return code here: if revision lookup failed we | 61 # Ignore stderr output and return code here: if revision lookup failed we |
75 # should simply return an empty string. | 62 # should simply return an empty string. |
76 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] |
77 return result.strip() | 64 return result.strip() |
78 | 65 |
79 def pull(self, repo): | 66 def pull(self, repo): |
80 subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"]) | 67 subprocess.check_call(["hg", "pull", "--repository", repo, "--quiet"]) |
81 | 68 |
82 def update(self, repo, rev): | 69 def update(self, repo, rev): |
83 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]) |
84 | 71 |
85 def ignore(self, target, repo): | 72 def ignore(self, target, repo): |
86 | 73 |
87 if not self.istype(target): | 74 if not self.istype(target): |
88 | 75 |
89 config_path = os.path.join(repo, ".hg", "hgrc") | 76 config_path = os.path.join(repo, ".hg", "hgrc") |
90 ignore_path = os.path.join(".", ".hg", "dependencies") | 77 ignore_path = os.path.abspath(os.path.join(repo, ".hg", "dependencies")) |
91 | 78 |
92 config = RawConfigParser() | 79 config = RawConfigParser() |
93 config.read(config_path) | 80 config.read(config_path) |
94 | 81 |
95 if not config.has_section("ui"): | 82 if not config.has_section("ui"): |
96 config.add_section("ui") | 83 config.add_section("ui") |
97 | 84 |
98 if not config.has_option("ui", "ignore.dependencies"): | 85 config.set("ui", "ignore.dependencies", ignore_path) |
99 config.set("ui", "ignore.dependencies", ignore_path) | 86 with open(config_path, "w") as stream: |
100 with open(config_path, "w") as stream: | 87 config.write(stream) |
101 config.write(stream) | |
102 else: | |
103 ignore_path = config.get("ui", "ignore.dependencies") | |
Wladimir Palant
2014/10/07 22:30:14
Please set that option regardless of whether it is
mathias
2014/10/08 16:32:11
Agreed, this is necessary due to the issue above,
| |
104 | 88 |
105 module = os.path.relpath(target, repo) | 89 module = os.path.relpath(target, repo) |
106 self._ignore(module, ignore_path) | 90 _ensure_line_exists(ignore_path, module) |
107 | 91 |
108 class Git(SCM): | 92 class Git(): |
109 def istype(self, repodir): | 93 def istype(self, repodir): |
110 return os.path.exists(os.path.join(repodir, ".git")) | 94 return os.path.exists(os.path.join(repodir, ".git")) |
111 | 95 |
112 def clone(self, source, target): | 96 def clone(self, source, target): |
113 source = source.rstrip("/") | 97 source = source.rstrip("/") |
114 if not source.endswith(".git"): | 98 if not source.endswith(".git"): |
115 source += ".git" | 99 source += ".git" |
116 subprocess.check_call(["git", "clone", "--quiet", source, target]) | 100 subprocess.check_call(["git", "clone", "--quiet", source, target]) |
117 | 101 |
118 def get_revision_id(self, repo, rev="HEAD"): | 102 def get_revision_id(self, repo, rev="HEAD"): |
119 command = ["git", "rev-parse", "--revs-only", rev] | 103 command = ["git", "rev-parse", "--revs-only", rev] |
120 return subprocess.check_output(command, cwd=repo).strip() | 104 return subprocess.check_output(command, cwd=repo).strip() |
121 | 105 |
122 def pull(self, repo): | 106 def pull(self, repo): |
123 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po) | 107 subprocess.check_call(["git", "fetch", "--quiet", "--all", "--tags"], cwd=re po) |
124 | 108 |
125 def update(self, repo, rev): | 109 def update(self, repo, rev): |
126 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo) | 110 subprocess.check_call(["git", "checkout", "--quiet", rev], cwd=repo) |
127 | 111 |
128 def ignore(self, target, repo): | 112 def ignore(self, target, repo): |
129 module = os.path.relpath(target, repo) | 113 module = os.path.relpath(target, repo) |
130 exclude_file = os.path.join(repo, ".git", "info", "exclude") | 114 exclude_file = os.path.join(repo, ".git", "info", "exclude") |
131 self._ignore(module, exclude_file) | 115 _ensure_line_exists(exclude_file, module) |
132 | 116 |
133 repo_types = { | 117 repo_types = OrderedDict(( |
134 "hg": Mercurial(), | 118 ("hg", Mercurial()), |
135 "git": Git(), | 119 ("git", Git()), |
136 } | 120 )) |
137 | 121 |
138 def parse_spec(path, line): | 122 def parse_spec(path, line): |
139 if "=" not in line: | 123 if "=" not in line: |
140 logging.warning("Invalid line in file %s: %s" % (path, line)) | 124 logging.warning("Invalid line in file %s: %s" % (path, line)) |
141 return None, None | 125 return None, None |
142 | 126 |
143 key, value = line.split("=", 1) | 127 key, value = line.split("=", 1) |
144 key = key.strip() | 128 key = key.strip() |
145 items = value.split() | 129 items = value.split() |
146 if not len(items): | 130 if not len(items): |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 | 181 |
198 def get_repo_type(repo): | 182 def get_repo_type(repo): |
199 for name, repotype in repo_types.iteritems(): | 183 for name, repotype in repo_types.iteritems(): |
200 if repotype.istype(repo): | 184 if repotype.istype(repo): |
201 return name | 185 return name |
202 return None | 186 return None |
203 | 187 |
204 def ensure_repo(parentrepo, target, roots, sourcename): | 188 def ensure_repo(parentrepo, target, roots, sourcename): |
205 if os.path.exists(target): | 189 if os.path.exists(target): |
206 return | 190 return |
191 | |
207 parenttype = get_repo_type(parentrepo) | 192 parenttype = get_repo_type(parentrepo) |
208 type = None | 193 type = None |
209 for key in roots: | 194 for key in roots: |
210 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): |
211 type = key | 196 type = key |
212 if type is None: | 197 if type is None: |
213 raise Exception("No valid source found to create %s" % target) | 198 raise Exception("No valid source found to create %s" % target) |
214 | 199 |
215 url = urlparse.urljoin(roots[type], sourcename) | 200 url = urlparse.urljoin(roots[type], sourcename) |
216 logging.info("Cloning repository %s into %s" % (url, target)) | 201 logging.info("Cloning repository %s into %s" % (url, target)) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 if sourcedata != targetdata: | 270 if sourcedata != targetdata: |
286 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)) |
287 with io.open(target, "wb") as handle: | 272 with io.open(target, "wb") as handle: |
288 handle.write(sourcedata) | 273 handle.write(sourcedata) |
289 if __name__ == "__main__": | 274 if __name__ == "__main__": |
290 logging.info("Restarting %s" % target) | 275 logging.info("Restarting %s" % target) |
291 os.execv(sys.executable, [sys.executable, target] + sys.argv[1:]) | 276 os.execv(sys.executable, [sys.executable, target] + sys.argv[1:]) |
292 else: | 277 else: |
293 logging.warning("Cannot restart %s automatically, please rerun" % target ) | 278 logging.warning("Cannot restart %s automatically, please rerun" % target ) |
294 | 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 | |
295 if __name__ == "__main__": | 290 if __name__ == "__main__": |
296 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) | 291 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) |
297 repos = sys.argv[1:] | 292 repos = sys.argv[1:] |
298 if not len(repos): | 293 if not len(repos): |
299 repos = [os.getcwd()] | 294 repos = [os.getcwd()] |
300 for repo in repos: | 295 for repo in repos: |
301 resolve_deps(repo) | 296 resolve_deps(repo) |
LEFT | RIGHT |