comparison fetch.py @ 24:b1f65f3bd1bc

pretend to flesh out git fetcher
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 10 Nov 2011 08:16:48 -0800
parents 12ad9ab11860
children e628ce3ae49f
comparison
equal deleted inserted replaced
23:12ad9ab11860 24:b1f65f3bd1bc
69 """fetch and extract a tarball""" 69 """fetch and extract a tarball"""
70 70
71 type = 'tar' 71 type = 'tar'
72 72
73 def __call__(self, dest): 73 def __call__(self, dest):
74 assert os.path.isdir(dest) 74 if os.path.exists(dest):
75 assert os.path.isdir(dest)
76 else:
77 os.mkdirs(dest)
75 if self.subpath: 78 if self.subpath:
76 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet") 79 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet")
77 buffer = StringIO() 80 buffer = StringIO()
78 buffer.write(self.download(self.url)) 81 buffer.write(self.download(self.url))
79 buffer.seek(0) 82 buffer.seek(0)
80 tf = tarfile.open(mode='r', fileobj=buffer) 83 tf = tarfile.open(mode='r', fileobj=buffer)
81 tf.extract(dest) 84 tf.extract(dest)
82 85
83 fetchers = [FileFetcher, TarballFetcher] 86 fetchers = [FileFetcher, TarballFetcher]
84 87
85 ### VCS fetchers using executable 88 ### VCS fetchers
86 89
87 import subprocess 90 import subprocess
88 try: 91 try:
89 from subprocess import check_call as call 92 from subprocess import check_call as call
90 except ImportErorr: 93 except ImportErorr:
91 raise # we need check_call, kinda 94 raise # we need check_call, kinda
92 95
93 class VCSFetcher(Fetcher): 96 class VCSFetcher(Fetcher):
97
98 command = None
99
100 def call(*args, **kwargs):
101 assert command is not None, "Abstract base class"
102 call([self.command] + list(args), **kwargs)
103
94 def __init__(self, url, export=True): 104 def __init__(self, url, export=True):
95 """ 105 """
96 - export : whether to strip the versioning information 106 - export : whether to strip the versioning information
97 """ 107 """
98 Fetcher.__init__(self, url) 108 Fetcher.__init__(self, url)
99 self.export = export 109 self.export = export
110
111 def __call__(self, dest):
112 raise NotImplementedError("Abstract base class")
100 113
101 if which('hg'): 114 if which('hg'):
102 115
103 class HgFetcher(VCSFetcher): 116 class HgFetcher(VCSFetcher):
104 """checkout a mercurial repository""" 117 """checkout a mercurial repository"""
118 os.mkdirs(dest) 131 os.mkdirs(dest)
119 call([self.hg, 'clone', self.url, dest]) 132 call([self.hg, 'clone', self.url, dest])
120 133
121 fetchers.append(HgFetcher) 134 fetchers.append(HgFetcher)
122 135
136
123 if which('git'): 137 if which('git'):
124 138
125 class GitFetcher(Fetcher): 139 class GitFetcher(Fetcher):
126 """checkout a git repository""" 140 """checkout a git repository"""
127 type = 'git' 141 type = 'git'
128 142
129 def __init__(self, url, export=True): 143 def __init__(self, url, export=True):
130 VCSFetcher.__init__(self, url, export=True) 144 VCSFetcher.__init__(self, url, export=True)
131 self.git = which('git') 145 self.git = which('git')
132 146
133 def __call__(self, url 147 def __call__(self, dest):
148 if os.path.exists(dest):
149 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.git'))
150 call([self.git, 'pull', self.url], cwd=dest)
151 call([self.hg, 'update'], cwd=dest)
152 else:
153 if not os.path.exists(dest):
154 os.mkdirs(dest)
155 call([self.hg, 'clone', self.url, dest])
156
134 157
135 fetchers.append(GitFetcher) 158 fetchers.append(GitFetcher)
136 159
137 __all__ += [i.__name__ for i in fetchers] 160 __all__ += [i.__name__ for i in fetchers]
138 161