# HG changeset patch # User Jeff Hammel # Date 1321337569 28800 # Node ID 5ecb6507931b7b883cad18a004387bfa9daf20f8 # Parent 423b67ff451272e087b7df083640fdcb76c61437 fix vcs fetchers to almost follow a pattern diff -r 423b67ff4512 -r 5ecb6507931b fetch.py --- a/fetch.py Mon Nov 14 21:32:54 2011 -0800 +++ b/fetch.py Mon Nov 14 22:12:49 2011 -0800 @@ -83,8 +83,7 @@ type = 'tar' def __call__(self, dest): - if self.clobber: - shutil.rmtree(dest) + self.clobber() if os.path.exists(dest): assert os.path.isdir(dest), "Destination must be a directory" else: @@ -121,9 +120,12 @@ """ Fetcher.__init__(self, url) self.export = export + self.prog = self.type # name of app program + self.vcs_dir = '.' + self.type # subdirectory for version control def __call__(self, dest): + self.clobber(dest) if self.subpath or self.export: # can only export with a subpath self.export(dest, subpath=self.subpath) @@ -131,6 +133,7 @@ if os.path.exists(dest): assert os.path.isdir(dest) + else: self.clone(dest) def export(self, dest): @@ -172,8 +175,11 @@ updates a checkout """ raise NotImplementedError("Abstract base class") - + def versioned(self, directory): + return os.path.exists(os.path.join(directory, self.vcs_dir)) + + if which('hg'): class HgFetcher(VCSFetcher): @@ -181,23 +187,24 @@ type = 'hg' def __init__(self, url, export=True): - VCSFetcher.__init__(self, url, export=True) + VCSFetcher.__init__(self, url, export=export) self.hg = which('hg') assert self.hg, "'hg' command not found" - def __call__(self, dest): + def clone(self, dest): if os.path.exists(dest): - assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg')) - call([self.hg, 'pull', self.url], cwd=dest) - call([self.hg, 'update'], cwd=dest) - else: - if not os.path.exists(dest): - os.mkdirs(dest) - call([self.hg, 'clone', self.url, dest]) + assert os.path.isdir(dest) + call([self.hg, 'clone', self.url, dest]) + + def update(self, dest): + assert os.path.versioned(dest) + assert os.path.exists(dest) + call([self.hg, 'pull', self.url], cwd=dest) + call([self.hg, 'update', '-C'], cwd=dest) + fetchers.append(HgFetcher) - if which('git'): class GitFetcher(Fetcher): @@ -205,19 +212,20 @@ type = 'git' def __init__(self, url, export=True): - VCSFetcher.__init__(self, url, export=True) + VCSFetcher.__init__(self, url, export=export) self.git = which('git') + assert self.git, "'git' command not found" - def __call__(self, dest): - if os.path.exists(dest): - assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.git')) - call([self.git, 'pull', self.url], cwd=dest) - call([self.hg, 'update'], cwd=dest) - else: - if not os.path.exists(dest): - os.mkdirs(dest) - call([self.hg, 'clone', self.url, dest]) - + def update(self, dest): + assert os.path.isdir(dest) + assert os.path.versioned(dest) + call([self.git, 'pull', self.url], cwd=dest) + call([self.git, 'update'], cwd=dest) + + def clone(self, dest): + if not os.path.exists(dest): + os.makedirs(dest) + call([self.git, 'clone', self.url, dest]) fetchers.append(GitFetcher)