From 258f506b9672e86c8a14cf3bd622a01ee6e81650 Mon Sep 17 00:00:00 2001
From: Daniel Baumann <mail@daniel-baumann.ch>
Date: Mon, 28 Oct 2013 08:27:03 +0100
Subject: [PATCH] Rewriting bootstrap_hooks in python.

---
 scripts/build/bootstrap_hooks | 134 ++++++++++++++++++++--------------
 1 file changed, 81 insertions(+), 53 deletions(-)

diff --git a/scripts/build/bootstrap_hooks b/scripts/build/bootstrap_hooks
index 02fdfad5a..b8d144020 100755
--- a/scripts/build/bootstrap_hooks
+++ b/scripts/build/bootstrap_hooks
@@ -1,6 +1,6 @@
-#!/bin/sh
+#!/usr/bin/python3
 
-## live-build(7) - System Build Scripts
+## live-build(7) - Live System Build Components
 ## Copyright (C) 2006-2013 Daniel Baumann <mail@daniel-baumann.ch>
 ##
 ## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
@@ -8,71 +8,99 @@
 ## under certain conditions; see COPYING for details.
 
 
-set -e
+import argparse
+import configparser
+import glob
+import os
+import shutil
+import subprocess
+import sys
 
-# Including common functions
-[ -e "${LIVE_BUILD}/scripts/build.sh" ] && . "${LIVE_BUILD}/scripts/build.sh" || . /usr/lib/live/build.sh
 
-# Setting static variables
-DESCRIPTION="$(Echo 'execute hooks in bootstrap')"
-HELP=""
-USAGE="${PROGRAM} [--force]"
+# TODO:
+#   * logfile output
+#   * lockfile handling
+#   * use gettext for i18n
 
-Arguments "${@}"
+def main():
+	## Parsing Arguments
+	arguments = argparse.ArgumentParser(
+		prog            = 'lb bootstrap_hooks',
+		usage           = '%(prog)s [arguments]',
+		description     = '''live-build contains the components to build a live system from a configuration directory.
+		                     The bootstrap_hooks command executes hooks after the bootstrap stage.''',
+		epilog          = 'See \'man lb_bootstrap_hooks\' for more information.',
+		version         = 'live-build 4',
+		formatter_class = argparse.ArgumentDefaultsHelpFormatter
+	)
 
-# Reading configuration files
-Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source
-Set_defaults
+	arguments.add_argument('--verbose',     help='set verbose option',         action='store_true')
 
-Echo_message "Begin executing hooks..."
+	args = arguments.parse_args()
 
-# Requiring stage file
-Require_stagefile .build/config .build/bootstrap
+	# --verbose
+	verbose = args.verbose
 
-# Checking stage file
-Check_stagefile .build/bootstrap_hooks
+	## Executing hooks
 
-# Checking lock file
-Check_lockfile .lock
+	# stagefile
+	if os.path.isfile('.build/bootstrap_hooks'):
+		if verbose:
+			print('I: bootstrap_hooks already done - nothing to do')
 
-# Creating lock file
-Create_lockfile .lock
+		sys.exit(0)
 
-## Processing distribution hooks
+	# dependencies
+	if not os.path.isfile('.build/bootstrap'):
+		print('E: bootstrap stage missing - aborting', file=sys.stderr)
 
-# Make build config available to bootstrap hooks. First, make the bind
-# mount and then make it read-only. This can't happen in one mount
-# command, then the resulting mount will be rw (see mount(8)). Making it
-# ro prevents modifications and prevents accidentally removing the
-# contents of the config directory when removing the chroot.
-mkdir -p chroot/root/config
-mount -o bind config chroot/root/config
-mount -o remount,ro,bind config chroot/root/config
+		if verbose:
+			print('I: use \'lb bootstrap\' to bootstrap system')
 
-if Find_files config/hooks/*.bootstrap
-then
-	for _HOOK in config/hooks/*.bootstrap
-	do
-		# Copying hook
-		cp "${_HOOK}" chroot/root
+		sys.exit(1)
 
-		# Making hook executable
-		if [ ! -x chroot/root/"$(basename ${_HOOK})" ]
-		then
-			chmod +x chroot/root/"$(basename ${_HOOK})"
-		fi
+	# hooks
+	if not glob.glob('config/hooks/*.hook.bootstrap'):
+		if verbose:
+			print ('I: no bootstrap hooks found at config/hooks/*.hook.bootstrap - nothing to do')
 
-		# Executing hook
-		Chroot chroot "/root/$(basename ${_HOOK})" || { Echo_error "${_HOOK} failed (exit non-zero). You should check for errors."; exit 1 ;}
+		sys.exit(0)
 
-		# Removing hook
-		rm -f chroot/root/"$(basename ${_HOOK})"
-	done
+	# bind mount configuration directory
+	if verbose:
+		print('I: Mounting config to chroot/live-build/config')
 
-	# Creating stage file
-	Create_stagefile .build/bootstrap_hooks
-fi
+	os.makedirs('chroot/live-build/config', exist_ok=True)
 
-# Remove bind mount of build config inside chroot.
-umount chroot/root/config
-rmdir chroot/root/config
+	mount = subprocess.call('mount -o bind config chroot/live-build/config', shell=True)
+	remount = subprocess.call('mount -o remount,ro,bind chroot/live-build/config', shell=True)
+
+	# process hooks
+	os.makedirs('chroot/live-build', exist_ok=True)
+
+	for hook in glob.glob('config/hooks/*.hook.bootstrap'):
+		if verbose:
+			print('I: Copying config/hooks/*.hook.bootstrap to chroot/live-build')
+
+		os.link(hook, os.path.join('chroot/live-build/' + os.path.basename(hook)), follow_symlinks=True)
+
+		if verbose:
+			print('I: Executing \' ' + hook + '\'')
+
+		os.chmod(hook, 0o755)
+		exec_hook = subprocess.call('chroot chroot /live-build/' + os.path.basename(hook), shell=True)
+		os.remove('chroot/live-build/' + os.path.basename(hook))
+
+	# unmount coniguration directory
+	umount = subprocess.call('umount chroot/live-build/config', shell=True)
+
+	os.rmdir('chroot/live-build/config')
+	os.rmdir('chroot/live-build')
+
+	## stagefile
+	os.makedirs('.build', exist_ok=True)
+	open('.build/bootstrap_hooks', 'w').close()
+
+
+if __name__ == '__main__':
+	main()