# HG changeset patch # User Jeff Hammel # Date 1321380827 28800 # Node ID 1c963875e6cd3323435321d01754be167aca7790 # Parent 5ecb6507931b7b883cad18a004387bfa9daf20f8 add a test for manifest and fix resulting bugs diff -r 5ecb6507931b -r 1c963875e6cd fetch.py --- a/fetch.py Mon Nov 14 22:12:49 2011 -0800 +++ b/fetch.py Tue Nov 15 10:13:47 2011 -0800 @@ -60,7 +60,7 @@ class FileFetcher(Fetcher): """fetch a single file""" # Note: subpath and clobber for single files are ignored - + type = 'file' @classmethod @@ -130,7 +130,7 @@ # can only export with a subpath self.export(dest, subpath=self.subpath) return - + if os.path.exists(dest): assert os.path.isdir(dest) else: @@ -179,7 +179,7 @@ def versioned(self, directory): return os.path.exists(os.path.join(directory, self.vcs_dir)) - + if which('hg'): class HgFetcher(VCSFetcher): @@ -195,7 +195,7 @@ if os.path.exists(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) @@ -232,7 +232,7 @@ __all__ += [i.__name__ for i in fetchers] class Fetch(object): - + def __init__(self, fetchers=fetchers[:], relative_to=None, strict=True): self.fetchers = fetchers self.relative_to = relative_to @@ -256,7 +256,8 @@ # ensure all the required fetchers are available types = set([i['type'] for i in items]) assert not [i for i in types - if [True for fetcher in fetchers if fetcher.match(i)]] + if not [True for fetcher in fetchers + if fetcher.match(i)]] for item in items: @@ -274,18 +275,21 @@ def read_manifests(*manifests): """ read some manifests and return the items - + Format: %s """ % format_string - # sanity check - assert not [i for i in manifests if not os.path.exists(i)] - retval = [] for manifest in manifests: - for line in file(i).readlines(): + if isinstance(manifest, basestring): + assert os.path.exists(manifest), "manifest '%s' not found" % manifest + f = file(manifest) + else: + f = manifest + + for line in f.readlines(): line = line.strip() if line.startswith('#') or not line: continue @@ -316,7 +320,7 @@ return description + '\n' else: return '' - + parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) parser.add_option('-o', '--output', help="output relative to this location vs. the manifest location") @@ -345,6 +349,9 @@ parser.print_help() parser.exit() + # sanity check + assert not [i for i in args if not os.path.exists(i)] + items = read_manifests(*args) fetch = Fetch(fetchers, strict=options.strict) @@ -352,5 +359,5 @@ fetch.fetch(*items) if __name__ == '__main__': - main() + main() diff -r 5ecb6507931b -r 1c963875e6cd tests/doctest.txt --- a/tests/doctest.txt Mon Nov 14 22:12:49 2011 -0800 +++ b/tests/doctest.txt Tue Nov 15 10:13:47 2011 -0800 @@ -7,6 +7,7 @@ >>> import os >>> import shutil >>> import tempfile + >>> from StringIO import StringIO Create a staging directory:: @@ -15,14 +16,31 @@ Create a Fetch object:: >>> f = fetch.Fetch(relative_to=stage, strict=True) - + Call Fetch directly:: >>> def url(*args): ... return 'file://' + os.path.join(*([here] + list(args))) >>> f(url=url('sample1.txt'), destination=stage, type='file') - >>> file(os.path.join(stage, 'sample1.txt')).read().strip() + >>> dest = os.path.join(stage, 'sample1.txt') + >>> file(dest).read().strip() 'sample1' + >>> os.remove(dest) + +Parse a Fetch "manifest":: + + >>> dest = os.path.join(stage, 'example1.txt') + >>> manifest = '%s %s file' % (url('sample1.txt'), 'example1.txt') # SOURCE DEST TYPE + >>> buffer = StringIO() + >>> buffer.write(manifest) + >>> buffer.seek(0) + >>> contents = fetch.read_manifests(buffer) + >>> len(contents) + 1 + >>> f.fetch(*contents) + >>> file(dest).read().strip() + 'sample1' + >>> os.remove(dest) Cleanup::