diskimage-builder/diskimage_builder/elements/package-installs/bin/package-installs-squash
Ian Wienand 97c01e48ed Move elements & lib relative to diskimage_builder package
Currently we have all our elements and library files in a top-level
directory and install them into
<root>/share/diskimage-builder/[elements|lib] (where root is either /
or the root of a virtualenv).

The problem with this is that editable/development installs (pip -e)
do *not* install data_files.  Thus we have no canonical location to
look for elements -- leading to the various odd things we do such as a
whole bunch of guessing at the top of disk-image-create and having a
special test-loader in tests/test_elements.py so we can run python
unit tests on those elements that have it.

data_files is really the wrong thing to use for what are essentially
assets of the program.  data_files install works well for things like
config-files, init.d files or dropping documentation files.

By moving the elements under the diskimage_builder package, we always
know where they are relative to where we import from.  In fact,
pkg_resources has an api for this which we wrap in the new
diskimage_builder/paths.py helper [1].

We use this helper to find the correct path in the couple of places we
need to find the base-elements dir, and for the paths to import the
library shell functions.

Elements such as svc-map and pkg-map include python unit-tests, which
we do not need tests/test_elements.py to special-case load any more.
They just get found automatically by the normal subunit loader.

I have a follow-on change (I69ca3d26fede0506a6353c077c69f735c8d84d28)
to move disk-image-create to a regular python entry-point.

Unfortunately, this has to move to work with setuptools.  You'd think
a symlink under diskimage_builder/[elements|lib] would work, but it
doesn't.

[1] this API handles stuff like getting files out of .zip archive
modules, which we don't do.  Essentially for us it's returning
__file__.

Change-Id: I5e3e3c97f385b1a4ff2031a161a55b231895df5b
2016-11-01 17:27:41 -07:00

125 lines
4.4 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import collections
import functools
import json
import os
import sys
import yaml
def get_element_installtype(element_name):
default = os.environ.get("DIB_DEFAULT_INSTALLTYPE", "source")
return os.environ.get(
"DIB_INSTALLTYPE_%s" % element_name.replace('-', '_'),
default)
def _is_arch_in_list(strlist):
"""Checks if os.environ['ARCH'] is in comma separated strlist"""
strlist = strlist.split(',')
map(str.strip, strlist)
return os.environ['ARCH'] in strlist
def _valid_for_arch(pkg_name, arch, not_arch):
"""Filter out incorrect ARCH versions"""
if arch is None and not_arch is None:
# nothing specified; always OK
return True
if arch and not_arch:
print("package-installs configuration error: arch and not_arch "
"given for package [%s]" % pkg_name)
sys.exit(1)
# if we have an arch list, our current arch must be in it
# to install.
if arch:
return _is_arch_in_list(arch)
# if we don't have an explicit arch list, we should
# install unless we are in the not-arch list.
return not _is_arch_in_list(not_arch)
def collect_data(data, filename, element_name):
try:
objs = json.load(open(filename))
except ValueError:
objs = yaml.load(open(filename))
for pkg_name, params in objs.items():
if not params:
params = {}
phase = params.get('phase', 'install.d')
install = "install"
if 'uninstall' in params:
install = "uninstall"
# Filter out incorrect installtypes
installtype = params.get('installtype', None)
elem_installtype = get_element_installtype(element_name)
valid_installtype = (installtype is None or
installtype == elem_installtype)
valid_arch = _valid_for_arch(pkg_name, params.get('arch', None),
params.get('not-arch', None))
if valid_installtype and valid_arch:
data[phase][install].append((pkg_name, element_name))
return data
def main():
parser = argparse.ArgumentParser(
description="Produce a single packages-installs file from all of"
" the available package-installs files")
parser.add_argument('--elements', required=True,
help="Which elements to squash")
parser.add_argument('--path', required=True,
help="Elements path to search for elements")
parser.add_argument('outfile', help="Location of the output file")
args = parser.parse_args()
# Replicate the logic of finding the first element, because we can't
# operate on the post-copied hooks dir, since we lose element context
element_dirs = list()
for element_name in args.elements.split():
for elements_dir in args.path.split(':'):
potential_path = os.path.join(elements_dir, element_name)
if os.path.exists(potential_path):
element_dirs.append((elements_dir, element_name))
# Collect the merge of all of the existing install files in the elements
# that are the first on the ELEMENT_PATH
final_dict = collections.defaultdict(
functools.partial(collections.defaultdict, list))
for (elements_dir, element_name) in element_dirs:
for file_type in ('json', 'yaml'):
target_file = os.path.join(
elements_dir, element_name, "package-installs.%s" % file_type)
if not os.path.exists(target_file):
continue
final_dict = collect_data(final_dict, target_file, element_name)
# Write the resulting file
with open(args.outfile, 'w') as outfile:
json.dump(
final_dict, outfile,
indent=True, separators=(',', ': '), sort_keys=False)
if __name__ == '__main__':
main()