Mercurial > hg > fetch
comparison fetch.py @ 25:e628ce3ae49f
more stubbing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 14 Nov 2011 20:19:57 -0800 |
parents | b1f65f3bd1bc |
children | d495b610046a |
comparison
equal
deleted
inserted
replaced
24:b1f65f3bd1bc | 25:e628ce3ae49f |
---|---|
27 | 27 |
28 def __init__(self, url, clobber=True): | 28 def __init__(self, url, clobber=True): |
29 self.subpath = None | 29 self.subpath = None |
30 if '#' in url: | 30 if '#' in url: |
31 url, self.subpath = url.rsplit('#') | 31 url, self.subpath = url.rsplit('#') |
32 if self.subpath: | |
33 self.subpath = self.subpath.split('/') | |
32 self.url = url | 34 self.url = url |
33 self.clobber = clobber | 35 self.clobber = clobber |
34 | 36 |
35 def __call__(self, dest): | 37 def __call__(self, dest): |
36 raise NotImplementedError("Should be called by implementing class") | 38 raise NotImplementedError("Should be called by implementing class") |
47 import urllib2 | 49 import urllib2 |
48 from StringIO import StringIO | 50 from StringIO import StringIO |
49 | 51 |
50 class FileFetcher(Fetcher): | 52 class FileFetcher(Fetcher): |
51 """fetch a single file""" | 53 """fetch a single file""" |
54 # Note: subpath for files is ignored | |
52 | 55 |
53 type = 'file' | 56 type = 'file' |
54 | 57 |
55 @classmethod | 58 @classmethod |
56 def download(cls, url): | 59 def download(cls, url): |
57 return urllib2.urlopen(url).read() | 60 return urllib2.urlopen(url).read() |
58 | 61 |
59 def __call__(self, dest): | 62 def __call__(self, dest): |
63 | |
60 if os.path.isdir(dest): | 64 if os.path.isdir(dest): |
61 filename = self.url.rsplit('/', 1)[-1] | 65 filename = self.url.rsplit('/', 1)[-1] |
62 dest = os.path.join(dest, filename) | 66 dest = os.path.join(dest, filename) |
63 f = file(dest, 'w') | 67 f = file(dest, 'w') |
64 f.write(self.download(self.url)) | 68 f.write(self.download(self.url)) |
107 """ | 111 """ |
108 Fetcher.__init__(self, url) | 112 Fetcher.__init__(self, url) |
109 self.export = export | 113 self.export = export |
110 | 114 |
111 def __call__(self, dest): | 115 def __call__(self, dest): |
116 | |
117 if self.subpath or self.export: | |
118 # can only export with a subpath | |
119 | |
120 | |
121 if os.path.exists(dest): | |
122 assert os.path.isdir(dest) | |
123 | |
124 def export(self, dest): | |
125 """ | |
126 export a clone of the directory | |
127 """ | |
128 tmpdir = tempfile.mkdtmp() | |
129 self.clone(tmpdir) | |
130 | |
131 shutil.rmtree(tmpdir) | |
132 | |
133 def clone(self, dest): | |
134 """ | |
135 clones into a directory | |
136 """ | |
112 raise NotImplementedError("Abstract base class") | 137 raise NotImplementedError("Abstract base class") |
138 | |
113 | 139 |
114 if which('hg'): | 140 if which('hg'): |
115 | 141 |
116 class HgFetcher(VCSFetcher): | 142 class HgFetcher(VCSFetcher): |
117 """checkout a mercurial repository""" | 143 """checkout a mercurial repository""" |
118 type = 'hg' | 144 type = 'hg' |
119 | 145 |
120 def __init__(self, url, export=True): | 146 def __init__(self, url, export=True): |
121 VCSFetcher.__init__(self, url, export=True) | 147 VCSFetcher.__init__(self, url, export=True) |
122 self.hg = which('hg') | 148 self.hg = which('hg') |
149 assert self.hg, "'hg' command not found" | |
123 | 150 |
124 def __call__(self, dest): | 151 def __call__(self, dest): |
125 if os.path.exists(dest): | 152 if os.path.exists(dest): |
126 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg')) | 153 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg')) |
127 call([self.hg, 'pull', self.url], cwd=dest) | 154 call([self.hg, 'pull', self.url], cwd=dest) |