source-hooks 3.1 KB
Newer Older
1
2
3
#!/usr/bin/python3

## live-build(7) - Live System Build Components
4
## Copyright (C) 2006-2014 Daniel Baumann <mail@daniel-baumann.ch>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


import argparse
import configparser
import glob
import os
import shutil
import subprocess
import sys


# TODO:
#   * logfile output
#   * lockfile handling
#   * use gettext for i18n

def main():
	## Parsing Arguments
	arguments = argparse.ArgumentParser(
28
		prog            = 'lb source-hooks',
29
30
		usage           = '%(prog)s [arguments]',
		description     = '''live-build contains the components to build a live system from a configuration directory.
31
32
		                     The source-hooks command executes hook files after the source stage.''',
		epilog          = 'See \'man lb-source-hooks\' for more information.',
33
34
35
36
37
38
39
40
41
42
43
		formatter_class = argparse.ArgumentDefaultsHelpFormatter
	)

	arguments.add_argument('--version',     help='show program\'s version number and exit', action='version', version='live-build 4')
	arguments.add_argument('--verbose',     help='set verbose option',                      action='store_true')

	args = arguments.parse_args()

	# --verbose
	verbose = args.verbose

44
	## Calling source hooks
45
46

	# stagefile
47
	if os.path.isfile('.build/source-hooks'):
48
		if verbose:
49
			print('I: source-hooks already done - nothing to do')
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

		sys.exit(0)

	# dependencies
	if not os.path.isfile('.build/source'):
		print('E: source stage missing - aborting', file=sys.stderr)

		if verbose:
			print('I: use \'lb source\' to source system')

		sys.exit(1)

	# hooks
	if not glob.glob('config/hooks/*.hook') and not glob.glob('config/hooks/*.hook.source'):
		if verbose:
			print ('I: no source hooks found at config/hooks/*.hook{,.source} - nothing to do')

		sys.exit(0)

	# bind mount configuration directory
	if verbose:
		print('I: Mounting config to source/live-build/config')

	os.makedirs('source/live-build/config', exist_ok=True)

	mount = subprocess.call('mount -o bind config source/live-build/config', shell=True)
	remount = subprocess.call('mount -o remount,ro,bind source/live-build/config', shell=True)

	# process hooks
	os.makedirs('source/live-build', exist_ok=True)

	hooks = glob.glob('config/hooks/*.hook') + glob.glob('config/hooks/*.hook.source')

83
	for hook in sorted(hooks):
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
		if verbose:
			print('I: Copying config/hooks/*.hook.source to source/live-build')

		shutil.copy(hook, os.path.join('source/live-build/' + os.path.basename(hook)), follow_symlinks=True)

		if verbose:
			print('I: Executing \' ' + hook + '\'')

		os.chmod(hook, 0o755)
		exec_hook = subprocess.call('cd source && live-build/' + os.path.basename(hook), shell=True)
		os.remove('source/live-build/' + os.path.basename(hook))

	# unmount coniguration directory
	umount = subprocess.call('umount source/live-build/config', shell=True)

	os.rmdir('source/live-build/config')
	os.rmdir('source/live-build')

102
	# stagefile
103
	os.makedirs('.build', exist_ok=True)
104
	open('.build/source-hooks', 'w').close()
105
106
107
108


if __name__ == '__main__':
	main()