Exclude lib and non-main sections as tasksel does (Closes: #758218).
[debian-live-build.git] / components / binary-hooks
blob20c5c0190ed8fe8f7bce5a3a16c3d62ee1bb45c1
1 #!/usr/bin/python3
3 ## live-build(7) - Live System Build Components
4 ## Copyright (C) 2006-2014 Daniel Baumann <mail@daniel-baumann.ch>
5 ##
6 ## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
7 ## This is free software, and you are welcome to redistribute it
8 ## under certain conditions; see COPYING for details.
11 import argparse
12 import configparser
13 import glob
14 import os
15 import shutil
16 import subprocess
17 import sys
20 # TODO:
21 #   * logfile output
22 #   * lockfile handling
23 #   * use gettext for i18n
25 def main():
26         ## Parsing Arguments
27         arguments = argparse.ArgumentParser(
28                 prog            = 'lb binary-hooks',
29                 usage           = '%(prog)s [arguments]',
30                 description     = '''live-build contains the components to build a live system from a configuration directory.
31                                      The binary-hooks command executes hook files after the binary stage.''',
32                 epilog          = 'See \'man lb-binary-hooks\' for more information.',
33                 formatter_class = argparse.ArgumentDefaultsHelpFormatter
34         )
36         arguments.add_argument('--version',     help='show program\'s version number and exit', action='version', version='live-build 4')
37         arguments.add_argument('--verbose',     help='set verbose option',                      action='store_true')
39         args = arguments.parse_args()
41         # --verbose
42         verbose = args.verbose
44         ## Calling binary hooks
46         # stagefile
47         if os.path.isfile('.build/binary-hooks'):
48                 if verbose:
49                         print('I: binary-hooks already done - nothing to do')
51                 sys.exit(0)
53         # dependencies
54         if not os.path.isfile('.build/bootstrap'):
55                 print('E: bootstrap stage missing - aborting', file=sys.stderr)
57                 if verbose:
58                         print('I: use \'lb bootstrap\' to bootstrap system')
60                 sys.exit(1)
62         # hooks
63         if not glob.glob('config/hooks/*.hook') and not glob.glob('config/hooks/*.hook.binary'):
64                 if verbose:
65                         print ('I: no binary hooks found at config/hooks/*.hook{,.binary} - nothing to do')
67                 sys.exit(0)
69         # bind mount configuration directory
70         if verbose:
71                 print('I: Mounting config to binary/live-build/config')
73         os.makedirs('binary/live-build/config', exist_ok=True)
75         mount = subprocess.check_call('mount -o bind config binary/live-build/config', shell=True)
76         remount = subprocess.check_call('mount -o remount,ro,bind binary/live-build/config', shell=True)
78         # process hooks
79         os.makedirs('binary/live-build', exist_ok=True)
81         hooks = glob.glob('config/hooks/*.hook') + glob.glob('config/hooks/*.hook.binary')
83         for hook in sorted(hooks):
84                 if verbose:
85                         print('I: Copying config/hooks/*.hook.binary to binary/live-build')
87                 shutil.copy(hook, os.path.join('binary/live-build/' + os.path.basename(hook)), follow_symlinks=True)
89                 if verbose:
90                         print('I: Executing \' ' + hook + '\'')
92                 os.chmod(hook, 0o755)
93                 exec_hook = subprocess.check_call('cd binary && live-build/' + os.path.basename(hook), shell=True)
94                 os.remove('binary/live-build/' + os.path.basename(hook))
96         # unmount coniguration directory
97         umount = subprocess.check_call('umount binary/live-build/config', shell=True)
99         os.rmdir('binary/live-build/config')
100         os.rmdir('binary/live-build')
102         ## stagefile
103         os.makedirs('.build', exist_ok=True)
104         open('.build/binary-hooks', 'w').close()
107 if __name__ == '__main__':
108         main()