From 740c6ab42b123ba293c565647a8ec978e1e8d1fc Mon Sep 17 00:00:00 2001 From: Robert C Jennings Date: Fri, 19 Jul 2019 13:40:32 -0500 Subject: [PATCH] ubuntu-cpc: rework image artifact cleanup The livecd.ubuntu-cpc.ext4 that is present in each build (plus kernel and initrd) are not renamed from /build/binary/boot/filsystem.ext4 and friends until after the binary hooks are run, so this patch moves from trying to perform this cleanup in a binary hook. Now the cleanup will be run at the end of live-build/binary for the ubuntu-cpc project. --- live-build/auto/build | 5 ++ .../ubuntu-cpc/hooks.d/base/final.binary | 47 ------------------- live-build/ubuntu-cpc/hooks.d/make-hooks | 18 ++++--- .../hooks.d/remove-implicit-artifacts | 40 ++++++++++++++++ 4 files changed, 53 insertions(+), 57 deletions(-) delete mode 100755 live-build/ubuntu-cpc/hooks.d/base/final.binary create mode 100755 live-build/ubuntu-cpc/hooks.d/remove-implicit-artifacts diff --git a/live-build/auto/build b/live-build/auto/build index 96811213..e87a4953 100755 --- a/live-build/auto/build +++ b/live-build/auto/build @@ -972,3 +972,8 @@ if [ -f "config/magic-proxy.pid" ]; then iptables -t nat -D OUTPUT -p tcp --dport 80 -m owner ! --uid-owner daemon \ -j REDIRECT --to 8080 fi + +case $PROJECT in + ubuntu-cpc) + config/hooks.d/remove-implicit-artifacts +esac diff --git a/live-build/ubuntu-cpc/hooks.d/base/final.binary b/live-build/ubuntu-cpc/hooks.d/base/final.binary deleted file mode 100755 index d1d3803f..00000000 --- a/live-build/ubuntu-cpc/hooks.d/base/final.binary +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python3 -#-*- encoding: utf-8 -*- -""" -The final hook is run after all other binary hooks. -""" -import glob -import os - - -def remove_implicit_files(): - """ - Remove output files not created by explicitly specified image targets - - This uses the 'explicit_provides' file generated by the 'make-hooks' - script. If the file is empty, all output will be saved. - """ - explicit = set() - with open('./config/hooks/explicit_provides', 'r', encoding='utf-8') as fp: - for filename in fp: - explicit.add(filename.rstrip()) - - if not explicit: - print('remove_implicit_files: explicit_provides is empty ' - 'all binary output will be included') - quit() - - all = set(glob.glob('livecd.ubuntu-cpc.*')) - implicit = all - explicit - - print('remove_implicit_files: all artifacts considered: {}'.format(all)) - print('remove_implicit_files: explict artifacts to keep: ' - '{}'.format(explicit)) - print('remove_implicit_files: implicit artifacts to remove: ' - '{}'.format(implicit)) - - for file in implicit: - print('remove_implicit_files: removing {} ' - '{} bytes'.format(file, os.stat(file).st_size)) - if os.path.islink(file): - os.unlink(file) - elif os.path.isfile(file): - os.remove(file) - - -if __name__ == "__main__": - print('Running {}'.format(__file__)) - remove_implicit_files() diff --git a/live-build/ubuntu-cpc/hooks.d/make-hooks b/live-build/ubuntu-cpc/hooks.d/make-hooks index 360e0cbe..c1fe0e5d 100755 --- a/live-build/ubuntu-cpc/hooks.d/make-hooks +++ b/live-build/ubuntu-cpc/hooks.d/make-hooks @@ -42,11 +42,10 @@ specified multiple times. The field is used by this script to generate a list of output files created explicitly by the named image targets. The list is saved to the "explicit_provides" file in the hooks output directory. In the case of the "all" target this list would be empty. This list is -consumed by the "final.binary" hook file. +consumed by the "remove-implicit-artifacts" which is run at the end of the build. The final.binary hook is always included as the last hook(s) if it exists, -it should not be specified in series files. It can be included from "base" -and/or "extra" directories with the final hook in "exta" running last. +it should not be specified in series files. ACHTUNG: live build runs scripts with the suffix ".chroot" in a batch separate from scripts ending in ".binary". Even if you arrange them interleaved in your @@ -215,11 +214,10 @@ class MakeHooks: sys.stderr.write("WARNING: Hooks directory exists and is not empty.\n") os.makedirs(self._hooks_dir, exist_ok=True) - # Always add final.binary hooks if they exist - for subdir in ["base", "extra"]: - final_hook = os.path.join(self._script_dir, subdir, "final.binary") - if os.path.exists(final_hook) and os.path.isfile(final_hook): - self._hooks_list.append("base/final.binary") + # Always add final.binary hook if it exists + final_hook = os.path.join(self._script_dir, "base/final.binary") + if os.path.exists(final_hook) and os.path.isfile(final_hook): + self._hooks_list.append("base/final.binary") for counter, hook in enumerate(self._hooks_list, start=1): hook_basename = os.path.basename(hook) @@ -239,13 +237,13 @@ class MakeHooks: def create_explicit_provides(self): """ - Create a file named "explicit_provides" in self._hooks_dir + Create a file named "explicit_provides" in self._script_dir listing all files named on "provides" in the series files of targets explicitly named by the user. The file is created but left empty if there are no explict "provides" keywords in the targets (this is the case for 'all') """ - with open(os.path.join(self._hooks_dir, "explicit_provides"), "w", + with open(os.path.join(self._script_dir, "explicit_provides"), "w", encoding="utf-8") as fp: empty = True for provides in self._provides: diff --git a/live-build/ubuntu-cpc/hooks.d/remove-implicit-artifacts b/live-build/ubuntu-cpc/hooks.d/remove-implicit-artifacts new file mode 100755 index 00000000..e1ea3728 --- /dev/null +++ b/live-build/ubuntu-cpc/hooks.d/remove-implicit-artifacts @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +#-*- encoding: utf-8 -*- +""" +Remove output files not created by explicitly specified image targets + +This uses the 'explicit_provides' file generated by the 'make-hooks' +script. If the file is empty, all output will be saved. +""" +import glob +import os + +if __name__ == "__main__": + print('Running {}'.format(__file__)) + scriptname = os.path.basename(__file__) + explicit = set() + with open('./config/hooks.d/explicit_provides', 'r', + encoding='utf-8') as fp: + for filename in fp: + explicit.add(filename.rstrip()) + + if not explicit: + print('{}: explicit_provides is empty. ' + 'All binary output will be included'.format(scriptname)) + quit() + + all = set(glob.glob('livecd.ubuntu-cpc.*')) + implicit = all - explicit + + print('{}: all artifacts considered: {}'.format(scriptname, all)) + print('{}: explict artifacts to keep: {}'.format(scriptname, explicit)) + print('{}: implicit artifacts to remove: {}'.format(scriptname, implicit)) + + for file in implicit: + if os.path.islink(file): + print('{}: unlinking {}'.format(scriptname, file)) + os.unlink(file) + elif os.path.isfile(file): + print('{}: removing {} ' + '{} bytes'.format(scriptname, file, os.stat(file).st_size)) + os.remove(file)