This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
builder.py
394 lines (320 loc) · 13.6 KB
/
builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import subprocess
import tempfile
import os
import sys
import shutil
import logging
import traceback
import time
import stat
import platform
import glob
logger = logging.getLogger("Builder")
logger.setLevel(logging.DEBUG)
logging_handler = logging.StreamHandler()
logger.addHandler(logging_handler)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logging_handler.setFormatter(formatter)
class Builder(object):
phases = []
builders = {}
@classmethod
def register(cls, **kwargs):
def sub_register(builder):
for key in kwargs:
def platform_setter(sub_builder, platform):
def sub_constructor(*sub_args, **sub_kwargs):
b = sub_builder(*sub_args, **sub_kwargs)
b.platform = platform
return b
return sub_constructor
if kwargs[key]:
cls.builders[key] = platform_setter(builder, key)
return builder
return sub_register
def __init__(self, *args, **kwargs):
self.logger = logging.getLogger("Builder")
tempfile_options = {}
if 'tempdir' in kwargs:
tempfile_options['dir'] = kwargs['tempdir']
self.remote_repo = kwargs.get("repo", "git://github.com/brownan/Minecraft-Overviewer.git")
self.logger.debug('using remote repo: %s', self.remote_repo)
self.temp_area = tempfile.mkdtemp(prefix="mco_build_temp", **tempfile_options)
self.logger.debug("making temp_area: %s", self.temp_area)
self.original_dir = os.getcwd()
os.chdir(self.temp_area)
self.git = kwargs.get("git", "git")
self.python = kwargs.get("python", sys.executable)
self.logger.debug('using git (%s) and python (%s)', self.git, self.python)
self.stderr_log = tempfile.mkstemp(prefix="mco_log_", **tempfile_options)
self.stdout_log = tempfile.mkstemp(prefix="mco_log_", **tempfile_options)
self.logs_closed = False
def forceDeleter(self, func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
if os.path.isdir(path):
try:
shutil.rmtree(path)
except:
self.logger.warn('Can\'t delete path: %s', path)
elif os.path.isfile(path):
try:
os.unlink(path)
except:
self.logger.warn('Can\'t delete path: %s', path)
def __del__(self):
try:
self.logger.debug("deleting temp_area: %s", self.temp_area)
os.chdir(self.original_dir)
shutil.rmtree(self.temp_area, onerror=self.forceDeleter)
self.close_logs()
os.remove(self.stderr_log[1])
os.remove(self.stdout_log[1])
except:
self.logger.exception('Failed to delete temp area')
time.sleep(4)
try:
shutil.rmtree(self.temp_area)
except:
self.logger.error('Failed to delete temp area again')
def close_logs(self):
if self.logs_closed:
return
os.close(self.stderr_log[0])
os.close(self.stdout_log[0])
self.logs_closed = True
# helper for doing commands, and redirecting to our logs
def popen(self, action, cmd):
self.logger.info("running command [%s]" % action)
os.write(self.stdout_log[0], "> [%s]: %s\n" % (action, cmd))
os.write(self.stderr_log[0], "> [%s]: %s\n" % (action, cmd))
p = subprocess.Popen(cmd, stdout=self.stdout_log[0], stderr=self.stderr_log[0])
p.wait()
if p.returncode != 0:
self.logger.error("Error during [%s]" % action)
raise Exception()
def fetch(self, checkout=None):
"Clones a remote repo into a local directory"
self.popen("clone", [self.git,"clone", self.remote_repo, self.temp_area])
self.logger.info("Cloned %s into %s", self.remote_repo, self.temp_area)
if checkout:
self.popen("checkout", [self.git, "checkout", checkout])
return 0
def getDesc(self):
cmd = [self.git, "describe", "--tags"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p.wait()
return p.stdout.read().strip()
def getCommit(self):
cmd = [self.git, "rev-parse", "HEAD"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p.wait()
return p.stdout.read().strip()
def getVersion(self):
cmd = [self.python, "setup.py", "--version"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p.wait()
return p.stdout.read().strip()
# called for each phase in self.phases
def build(self, phase="build"):
self.popen("build " + phase, [self.python, "setup.py", phase])
# Do some extra stuff after the build, if necessary
def post_build(self):
pass
# returns the filename on the (eventual) server
def filename(self):
raise NotYetImplemented()
# called after all the phases are done, returns a filename to upload
def package(self):
raise NotYetImplemented()
@Builder.register(win86_32 = platform.system() == 'Windows' and '32bit' in platform.architecture(),
win86_64 = platform.system() == 'Windows' and '64bit' in platform.architecture())
class WindowsBuilder(Builder):
phases = ["clean", "build", "py2exe"]
def __init__(self, *args, **kwargs):
#self.logger = logging.getLogger("Builder.WindowsBuilder")
kwargs['tempdir'] = "C:\\temp\\"
kwargs['git'] = "git.cmd"
Builder.__init__(self, *args, **kwargs)
self.zipper = r"C:\Program Files\7-Zip\7z.exe"
self._checkBuildTools()
os.environ['PIL_INCLUDE_DIR'] = r"C:\devel\PIL-1.1.7\libImaging"
os.environ['DISTUTILS_USE_SDK'] = "1"
os.environ['MSSdk'] = "1"
def findExe(self, exe, path=None):
if path and type(path) == str:
path = path.split(os.pathsep)
if not path:
path = os.environ['PATH'].split(os.pathsep)
l = map(lambda x: os.path.join(x, exe), path)
return filter(os.path.exists, l)
def _checkBuildTools(self):
"Makes sure that all of the build tools are ready to go"
if not self.findExe(exe="cl.exe"):
raise Exception("Don't know where cl.exe is")
if not self.findExe(exe="link.exe"):
raise Exception("Don't know where link.exe is")
#if not self.findExe(exe="7z.exe"):
# os.environ['PATH'] += os.pathsep + self.zipper
# raise Exception("Don't know where 7z is")
def filename(self):
desc = self.getDesc()
zipname = "%s-%s.zip" % (self.platform, desc)
return zipname
def package(self):
zipname = self.filename()
return self.zip(root="dist", archive=zipname)
def zip(self, root, archive):
old_cwd = os.getcwd()
self.logger.debug('old_cwd: %s', old_cwd)
os.chdir(root)
try:
self.popen("zip", [self.zipper, "a", archive, "."])
if not os.path.exists(archive):
self.logger.error("something went wrong, the archive doesn't exist")
raise Exception("something went wrong. the archive doesn't exist")
finally:
os.chdir(old_cwd)
return os.path.abspath(os.path.join(root,archive))
def post_build(self):
# build docs
doc_src="docs"
doc_dest="dist\\docs"
cmd = [self.python, r"c:\devel\Sphinx-1.0.8\sphinx-build.py", "-b", "html", doc_src, doc_dest]
self.logger.info('building docs with: %r', cmd)
self.logger.debug('cwd: %s', os.getcwd())
p = subprocess.Popen(cmd)
p.wait()
if (p.returncode != 0):
self.logger.error("Failed to build docs")
raise Exception("Failed to build docs")
else:
self.logger.info('docs built OK')
class LinuxBuilder(Builder):
pass
@Builder.register(osx_app = platform.system() == 'Darwin')
class OSXBuilder(Builder):
phases = ['clean', 'py2app']
def __init__(self, *args, **kwargs):
Builder.__init__(self, *args, **kwargs)
os.environ['PIL_INCLUDE_DIR'] = r"/home/agrif/devel/mc-overviewer"
def filename(self):
desc = self.getDesc()
return "%s-%s.dmg" % (self.platform, desc)
def package(self):
dmgname = self.filename()
self.popen("dmgcreate", ['hdiutil', 'create', dmgname, '-srcfolder', './dist/'])
return dmgname
@Builder.register(deb86_32 = platform.system() == 'Linux' and ('debian' in platform.dist() or 'Ubuntu' in platform.dist()) and '32bit' in platform.architecture(),
deb86_64 = platform.system() == 'Linux' and ('debian' in platform.dist() or 'Ubuntu' in platform.dist()) and '64bit' in platform.architecture())
class DebBuilder(Builder):
phases = ['clean', 'debuild']
def fetch(self, *args, **kwargs):
ret = Builder.fetch(self, *args, **kwargs)
debsrc = os.path.split(sys.argv[0])[0]
debsrc = os.path.join(self.original_dir, debsrc, 'debian')
shutil.copytree(debsrc, 'debian')
clog = open('debian/changelog', 'r').read()
clog = clog.replace("{VERSION}", self.getVersion())
clog = clog.replace("{DESC}", self.getDesc())
clog = clog.replace("{DATE}", time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))
open('debian/changelog', 'w').write(clog)
return ret
def build(self, phase='build'):
if phase != 'debuild':
return Builder.build(self, phase)
# run debuild!
self.popen("debuild", ['debuild', '-i', '-us', '-uc', '-b'])
# move build products into current dir
for fname in glob.glob("../minecraft-overviewer_%s-0~overviewer1_*" % self.getVersion()):
newname = os.path.split(fname)[1]
shutil.move(fname, newname)
def filename(self):
desc = self.getDesc()
return "%s-%s.deb" % (self.platform, desc)
def package(self):
return glob.glob("minecraft-overviewer_%s-0~overviewer1_*.deb" % self.getVersion())[0]
class ELBuilderBase(Builder):
phases = ['build']
_package_name = 'Minecraft-Overviewer'
_specfile = '%s.spec' % _package_name
def fetch(self, *args, **kwargs):
ret = Builder.fetch(self, *args, **kwargs)
self._make_source_tarball()
spec = os.path.join(self.original_dir, os.path.split(sys.argv[0])[0],
self._base, self._specfile)
spec = open(spec, 'r').read()
spec = spec.replace( '{VERSION}', self.getVersion())
open(self._specfile, 'w').write(spec)
return ret
def build(self, phase='build'):
self._build_srpm()
self._build_rpm()
def _make_source_tarball(self):
self.popen('tarball',
['cp', '-a', self.temp_area,
os.path.join(os.path.dirname(self.temp_area), self._package_name)])
self.popen('tarball',
['tar', '-czf', os.path.expanduser(
'~/rpmbuild/SOURCES/%s-%s.tar.gz' % \
(self._package_name, self.getVersion())),
'-C', os.path.dirname(self.temp_area), self._package_name])
shutil.rmtree(
os.path.join(os.path.dirname(self.temp_area), self._package_name),
onerror=self.forceDeleter)
def _get_arch(self):
return 'x86_64' if self.platform[-2:] == '64' else 'i386'
def _get_mock_config(self):
return '%s-%s' % (self._mock_config, self._get_arch())
def _get_rpm_name(self):
return '/var/lib/mock/%s/result/%s' % \
(self._get_mock_config(), self.filename())
def _get_srpm_name(self):
return os.path.expanduser(
'~/rpmbuild/SRPMS/%s-%s-1.%s.src.rpm' %
(self._package_name, self.getVersion(), self._base))
def _build_srpm(self):
self.popen('buildsrpm',
['rpmbuild', '-bs', '--define', 'dist .%s' % self._base, self._specfile])
def _build_rpm(self):
self.popen('mock',
['mock', '-r', self._get_mock_config(), self._get_srpm_name()])
def filename(self):
return '%s-%s-1.%s.%s.rpm' % \
(self._package_name, self.getVersion(), self._base, self._get_arch())
def package(self):
return self._get_rpm_name()
@Builder.register(
el6_86_32 = platform.system() == 'Linux' and \
'fedora' in platform.dist(),
el6_86_64 = platform.system() == 'Linux' and \
'fedora' in platform.dist() and \
'64bit' in platform.architecture())
class EL6Builder(ELBuilderBase):
_base = 'el6'
_mock_config = 'epel-6'
def _get_rpm_name(self):
return '/var/lib/mock/%s/result/%s' % \
(self._get_mock_config(), self.filename().replace('i386', 'i686'))
@Builder.register(
el5_86_32 = platform.system() == 'Linux' and \
'fedora' in platform.dist(),
el5_86_64 = platform.system() == 'Linux' and \
'fedora' in platform.dist() and \
'64bit' in platform.architecture())
class EL5Builder(ELBuilderBase):
_base = 'el5'
_mock_config = 'epel-5'
def _build_srpm(self):
#el6+ use sha<something> which older versions of rpm don't understand
self.popen('buildsrpm',
['rpmbuild', '-bs', '--define', 'dist .%s' % self._base,
'--define', '_source_filedigest_algorithm md5', self._specfile])
@Builder.register(
fedora_86_32 = platform.system() == 'Linux' and \
'fedora' in platform.dist(),
fedora_86_64 = platform.system() == 'Linux' and \
'fedora' in platform.dist() and \
'64bit' in platform.architecture())
class FedoraBuilder(EL6Builder):
_base = 'fc16'
_mock_config = 'fedora-16'