forked from sugarlabs/sugar-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osbuild
executable file
·489 lines (340 loc) · 12 KB
/
osbuild
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env python2.7
# Copyright 2013 Daniel Narvaez
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import fcntl
import json
import os
import shutil
import subprocess
import sys
import tarfile
import urllib2
import StringIO
base_dir = os.path.dirname(os.path.abspath(__file__))
virtualenv_version = "1.11.4"
broot_dir = "build"
commands_dir = os.path.join("build", "commands")
main_repo = "sugarlabs/sugar-build"
docs_url = "http://developer.sugarlabs.org/dev-environment.md.html"
buildbot_env = "SUGAR_BUILDBOT"
lock_file = None
MODE_NONE = 1
MODE_HOST = 2
MODE_BROOT = 3
_mode = None
_prefs = None
def get_mode():
global _mode
if _mode is not None:
return _mode
if not get_prefs().get("use_broot", True):
_mode = MODE_NONE
elif "BROOT" in os.environ:
_mode = MODE_BROOT
else:
_mode = MODE_HOST
return _mode
def get_name_for_mode(base, mode=None):
if mode is None:
mode = get_mode()
if mode == MODE_NONE:
return base
elif mode == MODE_HOST:
return "%s-%s" % (base, "host")
elif mode == MODE_BROOT:
return "%s-%s" % (base, "broot")
def get_lock_file_path(mode=None):
return os.path.join(get_base_dir(), get_name_for_mode(".lock", mode))
def get_lock_file(mode=None):
global lock_file
if lock_file is not None:
return lock_file
lock_file = open(get_lock_file_path(), "w")
return lock_file
def get_base_dir():
return base_dir
def get_broot_dir():
return os.path.join(get_base_dir(), broot_dir)
def get_sandbox_dir(mode=None):
return os.path.join(get_base_dir(), "build", "out",
get_name_for_mode("sandbox", mode))
def get_commands_dir(commands_group=None):
if commands_group is None:
return os.path.join(get_base_dir(), commands_dir)
else:
return os.path.join(get_base_dir(), commands_dir, commands_group)
def get_cache_dir():
return os.path.join(get_sandbox_dir(), "cache")
def get_virtualenv_dir(mode=None):
return os.path.join(get_sandbox_dir(mode=mode), "install")
def get_log_dir():
return os.path.join(get_base_dir(), "build", "logs")
def get_prefs_path():
return os.path.join(get_base_dir(), "prefs.json")
def get_prefs():
global _prefs
if _prefs is not None:
return _prefs
try:
with open(get_prefs_path()) as f:
_prefs = json.load(f)
except IOError:
_prefs = {}
return _prefs
def get_bin_path(name=None):
bin_path = os.path.join(get_virtualenv_dir(), "bin")
if name is None:
return bin_path
else:
return os.path.join(bin_path, name)
def create_virtualenv():
source_dir = os.path.join(get_cache_dir(),
"virtualenv-%s" % virtualenv_version)
if not os.path.exists(source_dir):
url = "https://pypi.python.org/packages/source/v/" \
"virtualenv/virtualenv-%s.tar.gz" % virtualenv_version
f = urllib2.urlopen(url)
buffer = StringIO.StringIO(f.read())
with tarfile.open(fileobj=buffer, mode="r:gz") as tar:
tar.extractall(get_cache_dir())
subprocess.check_call(["python2.7",
os.path.join(source_dir, "virtualenv.py"),
"-q", "--system-site-packages",
get_virtualenv_dir()])
def install_system_deps():
try:
with open("/etc/fedora-release") as f:
if not f.read().startswith("Fedora release 20"):
return
except IOError:
return
with open(os.path.join(get_broot_dir(), "root.json")) as f:
broot_config = json.load(f)
packages = []
for group in broot_config.get("packages", {}).values():
for package in group:
if package not in packages:
packages.append(package)
args = ["rpm", "-q", "--quiet"]
args.extend(packages)
if subprocess.call(args) != 0:
args = ["sudo", "yum", "install"]
args.extend(packages)
subprocess.check_call(args)
def delete_virtualenv(mode=None):
try:
shutil.rmtree(get_virtualenv_dir(mode=mode))
except OSError:
pass
def delete_sandbox(mode=None):
try:
shutil.rmtree(get_sandbox_dir(mode=mode))
except OSError:
pass
def broot_check_output(args):
os.chdir(get_broot_dir())
broot_args = ["sudo", get_bin_path("broot")]
broot_args.extend(args)
return subprocess.check_output(broot_args)
def broot_run(args, message_args=None):
if "XAUTHORITY" not in os.environ:
os.environ["XAUTHORITY"] = os.path.expanduser("~/.Xauthority")
os.chdir(get_broot_dir())
broot_args = ["sudo", "env"]
keep = ["DISPLAY", "XAUTHORITY", "XAUTHLOCALHOSTNAME", "http_proxy",
"https_proxy"]
for name in keep:
if name in os.environ:
broot_args.append("%s=%s" % (name, os.environ[name]))
broot_args.append(get_bin_path("broot"))
broot_args.extend(args)
if message_args is None:
message_args = args
print("\n$ sudo broot %s" % " ".join(message_args))
return subprocess.call(broot_args) == 0
def clean():
mode = get_mode()
try:
if mode == MODE_NONE:
os.unlink(get_lock_file_path(MODE_NONE))
elif mode == MODE_HOST:
os.unlink(get_lock_file_path(MODE_HOST))
os.unlink(get_lock_file_path(MODE_BROOT))
except OSError:
pass
if mode == MODE_NONE:
delete_sandbox(MODE_NONE)
elif mode == MODE_HOST:
delete_sandbox(MODE_HOST)
delete_sandbox(MODE_BROOT)
def is_buildbot():
return buildbot_env in os.environ
def get_virtualenv_exists():
return os.path.exists(get_virtualenv_dir())
def broot_state():
return broot_check_output(["state"]).strip()
def setup(command):
mode = get_mode()
full_pull = command == "pull" and is_full_pull()
build_virtualenv = not get_virtualenv_exists() or full_pull
if build_virtualenv or full_pull:
build_system = {MODE_NONE: "build system",
MODE_HOST: "host build system",
MODE_BROOT: "broot build system"}
print("\n= Setup the %s =\n" % build_system[mode])
if build_virtualenv:
delete_virtualenv()
try:
print("* Create the python virtualenv")
create_virtualenv()
print("* Install python packages")
packages = ["git+https://github.com/dnarvaez/osbuild"]
if mode == MODE_HOST:
packages.append("git+https://github.com/dnarvaez/broot")
else:
packages.append("sourcestamp")
args = [get_bin_path("pip"), "-q", "install"]
args.extend(packages)
subprocess.check_call(args)
except Exception, e:
delete_virtualenv()
raise e
if full_pull and not is_buildbot():
print("* Pull latest sugar-build")
os.chdir(get_base_dir())
subprocess.check_call(["git", "pull", "--ff-only", "-q"])
if full_pull:
if mode == MODE_NONE:
install_system_deps()
else:
print("* Setup the build root")
if not broot_run(["setup"]):
return False
# When we run the broot osbuild the virtualenv will be updated
if get_mode() == MODE_HOST and command == "pull":
delete_virtualenv(MODE_BROOT)
return True
def get_available_commands():
available_commands = []
commands_dirs = [get_commands_dir("broot")]
if get_mode() in (MODE_HOST, MODE_NONE):
commands_dirs.append(get_commands_dir("host"))
for command_dir in commands_dirs:
for command in os.listdir(command_dir):
available_commands.append(command)
return available_commands
def print_help():
available_commands = get_available_commands()
available_commands.remove("shell")
print("\n= Available commands =\n")
print("\n".join(available_commands))
print("\nSee also %s" % docs_url)
def get_command_path(command, commands_group):
command_path = os.path.join(get_commands_dir(commands_group), command)
if os.path.exists(command_path):
return command_path
return None
def run_command(command, args):
if command == "broot":
return broot_run(args)
command_path = get_command_path(command, "host")
if command_path is None:
is_broot_command = True
command_path = get_command_path(command, "broot")
else:
is_broot_command = False
if get_mode() == MODE_HOST and is_broot_command:
broot_args = ["run", "/home/broot/sugar-build/osbuild", command]
broot_message_args = ["run", "osbuild", command]
if args:
broot_args.extend(args)
broot_message_args.extend(args)
return broot_run(broot_args, broot_message_args)
else:
command_args = [get_bin_path("python2.7"), command_path]
if args:
command_args.extend(args)
os.environ["PYTHONPATH"] = get_commands_dir()
os.environ["PATH"] = os.path.expandvars("%s:$PATH" % get_bin_path())
os.environ["PYTHONUNBUFFERED"] = "yes"
return subprocess.call(command_args) == 0
def check_lock():
try:
fcntl.lockf(get_lock_file(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
return False
return True
def check_system_deps():
for bin_name in "tar", "xz", "chroot", "uname":
found = False
for bin_path in "/usr/bin", "/usr/sbin", "/bin", "/sbin":
if os.path.exists(os.path.join(bin_path, bin_name)):
found = True
if not found:
print ("Please install %s" % bin_name)
return found
def is_full_pull():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
pull_parser = subparsers.add_parser("pull")
pull_parser.add_argument("module", nargs="?")
pull_parser.add_argument("--sources")
options = parser.parse_args()
return options.module is None
def parse_known_args():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
commands = get_available_commands()
commands.append("broot")
for command in commands:
subparser = subparsers.add_parser(command)
if command == "clean":
subparser.add_argument("--broot", action="store_true",
help="Clean also the build root")
return parser.parse_known_args()
def main():
# Unbuffered stdout
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
if os.geteuid() == 0:
print("osbuild should not be run as root.")
return False
if not check_lock():
print("Another osbuild instance is running.")
return False
if "OSBUILD_SHELL" in os.environ:
print("Don't run osbuild inside an osbuild shell, you can just "
"run the commands directly.")
return False
if not check_system_deps():
return False
os.environ["PIP_DOWNLOAD_CACHE"] = get_cache_dir()
options, other_args = parse_known_args()
if not setup(options.command):
return False
if get_mode() in (MODE_NONE, MODE_BROOT) and options.command == "shell":
print_help()
if not run_command(options.command, other_args):
print("\n! Failed to run command %s" % options.command)
return False
if options.command == "clean":
if options.broot:
if not broot_run(["clean"]):
return False
clean()
return True
if __name__ == "__main__":
if not main():
sys.exit(1)