Marcel Telka
2024-04-02 327b05574f0dc1b1046c72401256ce5afd3e3247
tools/userland-mapping
@@ -1,4 +1,4 @@
#!/usr/bin/python2.7
#!/usr/bin/python3.9
#
# This file and its contents are supplied under the terms of the
@@ -20,8 +20,6 @@
# e.g. checking for outdated pacakges, vulnerable packages etc.
#
from __future__ import print_function, absolute_import
import argparse
import json
import os
@@ -29,7 +27,8 @@
import logging
import subprocess
import multiprocessing
import sys
from bass.component import Component
try:
    from scandir import walk
@@ -38,7 +37,7 @@
logger = logging.getLogger('userland-mapping')
COMONENT_MAPPING_FILENAME = 'mapping.json'
COMPONENT_MAPPING_FILENAME = 'mapping.json'
def find_component_paths(path, subdir='components', debug=False):
@@ -50,32 +49,28 @@
    for dirpath, dirnames, filenames in walk(workspace_path):
        for name in filenames:
            if expression.match(name):
                paths.append(dirpath)
                if not os.path.isfile(os.path.join( dirpath, 'pkg5.ignore')):
                    paths.append(dirpath)
                del dirnames[:]
                break
    return paths
def generate_component_data(component_path):
def generate_component_data(component_path, subdir='components'):
    result = []
    component = Component(path=component_path)
    component_name = component.name
    if not component_name:
        raise ValueError('Component name is empty for path ' + component_path + '.')
    component_fmris = component.supplied_packages
    proc = subprocess.Popen(['gmake', 'print-value-COMPONENT_NAME', 'print-package-names'],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            cwd=component_path)
    for out in proc.stdout:
        result.append(out.rstrip())
    component_name = result[0]
    component_fmris = result[1:]
    component_relative_path = component_path.split(os.environ['WS_TOP'])[-1].replace('/', '', 1)
    component_relative_path = component_path.split(os.path.join(os.environ['WS_TOP'], subdir))[-1].replace('/', '', 1)
    return component_fmris, component_name, component_relative_path
def generate_userland_mapping(workspace_path, subdir='components'):
def generate_userland_mapping(workspace_path, subdir='components', repo='userland', repo_map=[]):
    mapping = []
    paths = find_component_paths(path=workspace_path, subdir=subdir)
@@ -84,11 +79,17 @@
    for component_fmris, component_name, component_relative_path in results:
        for component_fmri in component_fmris:
            mapping.append({'component_name': component_name,
                            'component_fmri': component_fmri,
                            'component_path': component_relative_path})
            component_repo = repo
            for rm in repo_map:
                if component_relative_path.startswith(rm['pfx']):
                    component_repo = rm['repo']
    component_mapping_file = os.path.join(workspace_path, subdir, COMONENT_MAPPING_FILENAME)
            mapping.append({'name': component_name,
                            'fmri': component_fmri,
                            'path': component_relative_path,
                            'repo': component_repo})
    component_mapping_file = os.path.join(workspace_path, subdir, COMPONENT_MAPPING_FILENAME)
    with open(component_mapping_file, 'w') as f:
        f.write(json.dumps(mapping, sort_keys=True, indent=4))
@@ -98,14 +99,24 @@
    parser.add_argument('-w', '--workspace', default=os.getenv('WS_TOP'), help='Path to workspace')
    parser.add_argument('--subdir', default='components', help='Directory holding components')
    parser.add_argument('--repo', default='userland', help='Default target repository')
    parser.add_argument('--repo-map', help='Target repository for this directory; e.g., encumbered/=userland-encumbered', action='append')
    args = parser.parse_args()
    workspace = args.workspace
    subdir = args.subdir
    generate_userland_mapping(workspace_path=workspace, subdir=subdir)
    repo = args.repo
    repo_map = []
    if args.repo_map:
        for rm in args.repo_map:
            l = rm.split("=")
            if len(l) != 2:
                raise ValueError('invalid --repo-map: ' + rm)
            repo_map.append({'pfx': l[0], 'repo': l[1]})
    generate_userland_mapping(workspace_path=workspace, subdir=subdir, repo=repo, repo_map=repo_map)
if __name__ == '__main__':
    main()