diff options
author | David Pursehouse <david.pursehouse@sonymobile.com> | 2015-06-09 00:14:13 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-06-09 00:14:13 +0000 |
commit | 185307d1dd1e63a8cf139c55f26895a6b378d43b (patch) | |
tree | 2945070b5db6ae3379e7e7ce5c7625ea8e7e1606 /project.py | |
parent | c116f94261cf0e6d33b3910b68d08d3b0e4bccdd (diff) | |
parent | 4c426ef1d4c1399feb170447ca3090810ab3c02e (diff) | |
download | git-repo-185307d1dd1e63a8cf139c55f26895a6b378d43b.tar.gz |
Merge "Teach _LinkFile._Link to handle globs."
Diffstat (limited to 'project.py')
-rw-r--r-- | project.py | 55 |
1 files changed, 44 insertions, 11 deletions
@@ -16,6 +16,7 @@ from __future__ import print_function | |||
16 | import contextlib | 16 | import contextlib |
17 | import errno | 17 | import errno |
18 | import filecmp | 18 | import filecmp |
19 | import glob | ||
19 | import os | 20 | import os |
20 | import random | 21 | import random |
21 | import re | 22 | import re |
@@ -233,28 +234,60 @@ class _CopyFile(object): | |||
233 | _error('Cannot copy file %s to %s', src, dest) | 234 | _error('Cannot copy file %s to %s', src, dest) |
234 | 235 | ||
235 | class _LinkFile(object): | 236 | class _LinkFile(object): |
236 | def __init__(self, src, dest, relsrc, absdest): | 237 | def __init__(self, git_worktree, src, dest, relsrc, absdest): |
238 | self.git_worktree = git_worktree | ||
237 | self.src = src | 239 | self.src = src |
238 | self.dest = dest | 240 | self.dest = dest |
239 | self.src_rel_to_dest = relsrc | 241 | self.src_rel_to_dest = relsrc |
240 | self.abs_dest = absdest | 242 | self.abs_dest = absdest |
241 | 243 | ||
242 | def _Link(self): | 244 | def __linkIt(self, relSrc, absDest): |
243 | src = self.src_rel_to_dest | ||
244 | dest = self.abs_dest | ||
245 | # link file if it does not exist or is out of date | 245 | # link file if it does not exist or is out of date |
246 | if not os.path.islink(dest) or os.readlink(dest) != src: | 246 | if not os.path.islink(absDest) or (os.readlink(absDest) != relSrc): |
247 | try: | 247 | try: |
248 | # remove existing file first, since it might be read-only | 248 | # remove existing file first, since it might be read-only |
249 | if os.path.exists(dest): | 249 | if os.path.exists(absDest): |
250 | os.remove(dest) | 250 | os.remove(absDest) |
251 | else: | 251 | else: |
252 | dest_dir = os.path.dirname(dest) | 252 | dest_dir = os.path.dirname(absDest) |
253 | if not os.path.isdir(dest_dir): | 253 | if not os.path.isdir(dest_dir): |
254 | os.makedirs(dest_dir) | 254 | os.makedirs(dest_dir) |
255 | os.symlink(src, dest) | 255 | os.symlink(relSrc, absDest) |
256 | except IOError: | 256 | except IOError: |
257 | _error('Cannot link file %s to %s', src, dest) | 257 | _error('Cannot link file %s to %s', relSrc, absDest) |
258 | |||
259 | def _Link(self): | ||
260 | """Link the self.rel_src_to_dest and self.abs_dest. Handles wild cards | ||
261 | on the src linking all of the files in the source in to the destination | ||
262 | directory. | ||
263 | """ | ||
264 | # We use the absSrc to handle the situation where the current directory | ||
265 | # is not the root of the repo | ||
266 | absSrc = os.path.join(self.git_worktree, self.src) | ||
267 | if os.path.exists(absSrc): | ||
268 | # Entity exists so just a simple one to one link operation | ||
269 | self.__linkIt(self.src_rel_to_dest, self.abs_dest) | ||
270 | else: | ||
271 | # Entity doesn't exist assume there is a wild card | ||
272 | absDestDir = self.abs_dest | ||
273 | if os.path.exists(absDestDir) and not os.path.isdir(absDestDir): | ||
274 | _error('Link error: src with wildcard, %s must be a directory', | ||
275 | absDestDir) | ||
276 | else: | ||
277 | absSrcFiles = glob.glob(absSrc) | ||
278 | for absSrcFile in absSrcFiles: | ||
279 | # Create a releative path from source dir to destination dir | ||
280 | absSrcDir = os.path.dirname(absSrcFile) | ||
281 | relSrcDir = os.path.relpath(absSrcDir, absDestDir) | ||
282 | |||
283 | # Get the source file name | ||
284 | srcFile = os.path.basename(absSrcFile) | ||
285 | |||
286 | # Now form the final full paths to srcFile. They will be | ||
287 | # absolute for the desintaiton and relative for the srouce. | ||
288 | absDest = os.path.join(absDestDir, srcFile) | ||
289 | relSrc = os.path.join(relSrcDir, srcFile) | ||
290 | self.__linkIt(relSrc, absDest) | ||
258 | 291 | ||
259 | class RemoteSpec(object): | 292 | class RemoteSpec(object): |
260 | def __init__(self, | 293 | def __init__(self, |
@@ -1362,7 +1395,7 @@ class Project(object): | |||
1362 | # make src relative path to dest | 1395 | # make src relative path to dest |
1363 | absdestdir = os.path.dirname(absdest) | 1396 | absdestdir = os.path.dirname(absdest) |
1364 | relsrc = os.path.relpath(os.path.join(self.worktree, src), absdestdir) | 1397 | relsrc = os.path.relpath(os.path.join(self.worktree, src), absdestdir) |
1365 | self.linkfiles.append(_LinkFile(src, dest, relsrc, absdest)) | 1398 | self.linkfiles.append(_LinkFile(self.worktree, src, dest, relsrc, absdest)) |
1366 | 1399 | ||
1367 | def AddAnnotation(self, name, value, keep): | 1400 | def AddAnnotation(self, name, value, keep): |
1368 | self.annotations.append(_Annotation(name, value, keep)) | 1401 | self.annotations.append(_Annotation(name, value, keep)) |