Renamed python script; changed comment convention; moved printing into

loop instead of accumulating and printing at the end; changed
yaml.load() to safe_load().

Fixed echo via subshell.
This commit is contained in:
Cody Shepherd 2018-10-17 14:02:04 -07:00
parent 3c3be06f27
commit d8af25cf65
No known key found for this signature in database
GPG Key ID: 0DB7E5F05C3FAB5F
4 changed files with 19 additions and 23 deletions

View File

@ -551,7 +551,7 @@ fi
# '--initramfs none' produces different manifest names. # '--initramfs none' produces different manifest names.
if [ -e "binary/$INITFS/filesystem.packages" ]; then if [ -e "binary/$INITFS/filesystem.packages" ]; then
/build/config/snap_seed_parse "chroot/" >> "binary/${INITFS}/filesystem.packages" /build/config/snap-seed-parse "chroot/" >> "binary/${INITFS}/filesystem.packages"
ln "binary/$INITFS/filesystem.packages" "$PREFIX.manifest" ln "binary/$INITFS/filesystem.packages" "$PREFIX.manifest"
chmod 644 "$PREFIX.manifest" chmod 644 "$PREFIX.manifest"
fi fi

View File

@ -33,7 +33,7 @@ fi
mkdir -p config mkdir -p config
cp -af /usr/share/livecd-rootfs/live-build/functions config/functions cp -af /usr/share/livecd-rootfs/live-build/functions config/functions
cp -af /usr/share/livecd-rootfs/live-build/snap_seed_parse.py config/snap_seed_parse cp -af /usr/share/livecd-rootfs/live-build/snap-seed-parse.py config/snap-seed-parse
mkdir -p config/package-lists mkdir -p config/package-lists

View File

@ -46,12 +46,12 @@ create_empty_disk_image() {
create_manifest() { create_manifest() {
local chroot_root=${1} local chroot_root=${1}
local target_file=${2} local target_file=${2}
(echo "create_manifest chroot_root: ${chroot_root}") echo "create_manifest chroot_root: ${chroot_root}"
dpkg-query --show --admindir="${chroot_root}/var/lib/dpkg" > ${target_file} dpkg-query --show --admindir="${chroot_root}/var/lib/dpkg" > ${target_file}
(echo "create_manifest call to dpkg-query finished.") echo "create_manifest call to dpkg-query finished."
/build/config/snap_seed_parse "${chroot_root}" >> ${target_file} /build/config/snap_seed_parse "${chroot_root}" >> ${target_file}
(echo "create_manifest call to snap_seed_parse finished.") echo "create_manifest call to snap_seed_parse finished."
(echo "create_manifest finished") echo "create_manifest finished"
} }
make_ext4_partition() { make_ext4_partition() {

View File

@ -1,8 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
""" """
This file parses the seed.yaml file for the chroot and appends it to the Usage: snap-seed-parse ${chroot_dir} > somefile.manifest
given manifest file.
This script looks for a seed.yaml path in the given root directory, parsing
it and printing generated manifest lines to stdout for easy redirection.
""" """
import re import re
@ -12,49 +14,43 @@ import os.path
def log(msg): def log(msg):
sys.stderr.write("snap_seed_parse: {}\n".format(msg)) sys.stderr.write("snap-seed-parse: {}\n".format(msg))
log("Parsing seed.yaml") log("Parsing seed.yaml")
CHROOT_ROOT = sys.argv[1] if len(sys.argv) > 1 and len(sys.argv[1]) > 0 \ CHROOT_ROOT = sys.argv[1] if len(sys.argv) > 1 and len(sys.argv[1]) > 0 \
else '' else ''
'''The chroot rooth path should be passed in by the caller'''
# Trim any trailing slashes for correct appending
log("CHROOT_ROOT: {}".format(CHROOT_ROOT)) log("CHROOT_ROOT: {}".format(CHROOT_ROOT))
if len(CHROOT_ROOT) > 0 and CHROOT_ROOT[-1] == '/': if len(CHROOT_ROOT) > 0 and CHROOT_ROOT[-1] == '/':
CHROOT_ROOT = CHROOT_ROOT[:-1] CHROOT_ROOT = CHROOT_ROOT[:-1]
'''Trim any trailing slashes for correct appending'''
# This is where we expect to find the seed.yaml file
YAML_PATH = CHROOT_ROOT + '/var/lib/snapd/seed/seed.yaml' YAML_PATH = CHROOT_ROOT + '/var/lib/snapd/seed/seed.yaml'
'''This is where we expect to find the seed.yaml file'''
# Snaps are prepended with this string in the manifest
LINE_PREFIX = 'snap:' LINE_PREFIX = 'snap:'
'''Snaps are prepended with this string in the manifest'''
log("yaml path: {}".format(YAML_PATH)) log("yaml path: {}".format(YAML_PATH))
if not os.path.isfile(YAML_PATH): if not os.path.isfile(YAML_PATH):
'''Exit gracefully and issue a warning if seeded snaps not found'''
log("WARNING: yaml path not found; no seeded snaps found.") log("WARNING: yaml path not found; no seeded snaps found.")
exit(0) exit(0)
else: else:
log("yaml path found.") log("yaml path found.")
with open(YAML_PATH, 'r') as fh: with open(YAML_PATH, 'r') as fh:
'''Open the seed.yaml and ingest its contents''' yaml_lines = yaml.safe_load(fh)['snaps']
yaml_lines = yaml.load(fh)['snaps']
to_write = '' # Loop over dict items, outputting one manifest line from each triplet
for item in yaml_lines: for item in yaml_lines:
'''Loop over dict items, generating one manifest line from each triplet'''
filestring = item['file'] filestring = item['file']
# Pull the revision number off the file name
revision = filestring[filestring.rindex('_')+1:] revision = filestring[filestring.rindex('_')+1:]
revision = re.sub(r'[^0-9]', '', revision) revision = re.sub(r'[^0-9]', '', revision)
to_write += "{}{}\t{}\t{}\n".format(LINE_PREFIX, print("{}{}\t{}\t{}".format(LINE_PREFIX,
item['name'], item['name'],
item['channel'], item['channel'],
revision, revision,
) ))
print(to_write.strip('\n'))
'''write accumulated text to stdout for redirection'''