2025-11-16 18:05:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
from argparse import Namespace, ArgumentParser
|
2026-03-15 12:56:03 +01:00
|
|
|
from enum import Enum, auto
|
2025-11-16 18:05:10 +01:00
|
|
|
|
2026-01-25 15:18:27 +01:00
|
|
|
from ...lib.log import *
|
2026-03-15 14:19:32 +01:00
|
|
|
from ...App import Scope
|
2025-11-16 18:05:10 +01:00
|
|
|
from ..Cmd import Cmd
|
2026-01-27 10:22:16 +01:00
|
|
|
from ..CmdProjects import CmdProjects
|
2025-11-16 18:05:10 +01:00
|
|
|
|
|
|
|
|
class BaseCmdPkgRelations(Cmd):
|
|
|
|
|
|
2026-03-15 12:56:03 +01:00
|
|
|
class Syntax(Enum):
|
|
|
|
|
semver = auto()
|
|
|
|
|
debian = auto()
|
|
|
|
|
names_only = auto()
|
2026-03-15 10:00:56 +01:00
|
|
|
|
2026-03-15 12:56:03 +01:00
|
|
|
def pkg_relations_list(
|
|
|
|
|
self,
|
|
|
|
|
rel_type: str,
|
|
|
|
|
flavours: list[str],
|
2026-04-06 13:49:34 +02:00
|
|
|
seed_pkgs: list[str],
|
2026-03-15 12:56:03 +01:00
|
|
|
subsections: list[str]|None=None,
|
|
|
|
|
delimiter: str=' ',
|
|
|
|
|
no_subpackages: bool=False,
|
|
|
|
|
dont_strip_revision: bool=False,
|
|
|
|
|
expand_semver_revision_range: bool=False,
|
|
|
|
|
syntax: Syntax=Syntax.semver,
|
|
|
|
|
recursive: bool=False,
|
|
|
|
|
dont_expand_version_macros: bool=False,
|
|
|
|
|
ignore: set[str] = set(),
|
2026-03-15 13:05:15 +01:00
|
|
|
quote: bool = False,
|
2026-03-15 14:19:32 +01:00
|
|
|
skip_excluded: bool = False,
|
2026-03-15 16:34:30 +01:00
|
|
|
hide_self = False,
|
2026-04-06 13:57:20 +02:00
|
|
|
hide_jw_pkg = False
|
2026-03-15 12:56:03 +01:00
|
|
|
) -> list[str]:
|
|
|
|
|
|
|
|
|
|
if subsections is None:
|
2026-04-18 12:08:00 +02:00
|
|
|
subsections = self.app.distro.os_cascade
|
2026-04-06 13:49:34 +02:00
|
|
|
subsections.append('jw')
|
2026-03-15 10:00:56 +01:00
|
|
|
|
2026-03-15 12:56:03 +01:00
|
|
|
expand_semver_revision_range = expand_semver_revision_range
|
|
|
|
|
if syntax == self.Syntax.debian:
|
2026-03-15 10:13:26 +01:00
|
|
|
expand_semver_revision_range = True
|
2026-03-15 12:31:21 +01:00
|
|
|
|
2026-03-15 14:19:32 +01:00
|
|
|
if skip_excluded:
|
2026-04-06 13:49:34 +02:00
|
|
|
excluded = self.app.get_project_refs(seed_pkgs, ['build'], 'exclude',
|
2026-03-15 14:19:32 +01:00
|
|
|
scope = Scope.One, add_self=False, names_only=True)
|
|
|
|
|
ignore |= set(excluded)
|
|
|
|
|
|
2026-04-06 13:49:34 +02:00
|
|
|
log(DEBUG, f'flavours="{", ".join(flavours)}", subsections="{", ".join(subsections)}", "ignore="{", ".join(ignore)}"')
|
2026-03-15 10:00:56 +01:00
|
|
|
|
|
|
|
|
version_pattern = re.compile("[0-9-.]*")
|
2026-04-06 13:49:34 +02:00
|
|
|
ret: list[str] = []
|
|
|
|
|
for flavour in flavours: # build / release / run / devel
|
2026-04-06 15:35:18 +02:00
|
|
|
cur_pkgs = seed_pkgs.copy()
|
|
|
|
|
visited = set()
|
|
|
|
|
while len(cur_pkgs):
|
|
|
|
|
cur_pkg = cur_pkgs.pop(0)
|
|
|
|
|
if cur_pkg in visited or cur_pkg in ignore:
|
|
|
|
|
continue
|
|
|
|
|
for subsec in subsections:
|
|
|
|
|
section = 'pkg.' + rel_type + '.' + subsec
|
2026-04-06 13:49:34 +02:00
|
|
|
visited.add(cur_pkg)
|
|
|
|
|
value = self.app.get_value(cur_pkg, section, flavour)
|
2025-11-16 18:05:10 +01:00
|
|
|
if not value:
|
|
|
|
|
continue
|
|
|
|
|
deps = value.split(',')
|
|
|
|
|
for spec in deps:
|
|
|
|
|
dep = re.split('([=><]+)', spec)
|
2026-03-15 12:56:03 +01:00
|
|
|
if syntax == self.Syntax.names_only:
|
2025-11-16 18:05:10 +01:00
|
|
|
dep = dep[:1]
|
|
|
|
|
dep = list(map(str.strip, dep))
|
|
|
|
|
dep_name = re.sub('-dev$|-devel$|-run$', '', dep[0])
|
|
|
|
|
if dep_name in ignore or dep[0] in ignore:
|
|
|
|
|
continue
|
2026-03-15 12:56:03 +01:00
|
|
|
if no_subpackages:
|
2025-11-16 18:05:10 +01:00
|
|
|
dep[0] = dep_name
|
|
|
|
|
for i, item in enumerate(dep):
|
|
|
|
|
dep[i] = item.strip()
|
2026-04-06 13:49:34 +02:00
|
|
|
if subsec == 'jw':
|
|
|
|
|
if recursive and not dep_name in visited and not dep_name in cur_pkgs:
|
|
|
|
|
cur_pkgs.append(dep_name)
|
2026-04-06 13:57:20 +02:00
|
|
|
if hide_jw_pkg:
|
|
|
|
|
continue
|
2025-11-16 18:05:10 +01:00
|
|
|
if len(dep) == 3:
|
2026-04-06 13:49:34 +02:00
|
|
|
if dont_expand_version_macros and dep_name in cur_pkgs:
|
2025-11-16 18:05:10 +01:00
|
|
|
version = dep[2]
|
|
|
|
|
else:
|
|
|
|
|
version = self.app.get_value(dep_name, 'version', '')
|
|
|
|
|
if dep[2] == 'VERSION':
|
2026-03-15 12:56:03 +01:00
|
|
|
if dont_strip_revision:
|
2025-11-16 18:05:10 +01:00
|
|
|
dep[2] = version
|
|
|
|
|
else:
|
|
|
|
|
dep[2] = version.split('-')[0]
|
|
|
|
|
elif dep[2] == 'VERSION-REVISION':
|
|
|
|
|
dep[2] = version
|
|
|
|
|
elif version_pattern.match(dep[2]):
|
|
|
|
|
# dep[2] = dep[2]
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
raise Exception("Unknown version specifier in " + spec)
|
2026-03-16 06:02:48 +01:00
|
|
|
if len(dep) != 3 or not expand_semver_revision_range:
|
2026-03-15 10:00:56 +01:00
|
|
|
expanded_deps = [dep]
|
|
|
|
|
else:
|
|
|
|
|
expanded_deps = []
|
2026-03-16 06:02:48 +01:00
|
|
|
semver = re.split(r'[.-]', version)
|
|
|
|
|
if len(semver) != 4:
|
|
|
|
|
expanded_deps = [dep]
|
|
|
|
|
else:
|
|
|
|
|
release = int(semver[2])
|
|
|
|
|
major_minor = f'{semver[0]}.{semver[1]}'
|
|
|
|
|
match dep[1]:
|
|
|
|
|
case '>' | '>=':
|
|
|
|
|
expanded_deps.append([dep[0], dep[1], dep[2]])
|
|
|
|
|
expanded_deps.append([dep[0], '<', f'{major_minor}.{release + 1}'])
|
|
|
|
|
case '<' | '<=':
|
|
|
|
|
expanded_deps.append([dep[0], dep[1], dep[2]])
|
|
|
|
|
case '=':
|
|
|
|
|
expanded_deps.append([dep[0], '>=', f'{major_minor}.{release}'])
|
|
|
|
|
expanded_deps.append([dep[0], '<', f'{major_minor}.{release + 1}'])
|
|
|
|
|
case _:
|
|
|
|
|
raise NotImplementedError(f'Expanding SemVer range "{dep[0]} {dep[1]} {dep[3]}" is not yet implemented')
|
2026-03-15 10:00:56 +01:00
|
|
|
for expanded_dep in expanded_deps:
|
2026-04-06 13:49:34 +02:00
|
|
|
if hide_self and dep_name in seed_pkgs:
|
2026-03-15 16:34:30 +01:00
|
|
|
continue
|
2026-03-15 12:56:03 +01:00
|
|
|
match syntax:
|
|
|
|
|
case self.Syntax.semver:
|
2026-03-15 10:13:26 +01:00
|
|
|
pass
|
2026-03-15 12:56:03 +01:00
|
|
|
case self.Syntax.debian:
|
2026-03-25 09:35:48 +00:00
|
|
|
if len(expanded_dep) == 3:
|
|
|
|
|
match expanded_dep[1]:
|
|
|
|
|
case '<':
|
|
|
|
|
expanded_dep[1] = '<<'
|
|
|
|
|
case '>':
|
|
|
|
|
expanded_dep[1] = '>>'
|
2026-03-15 10:13:26 +01:00
|
|
|
case '_':
|
2026-03-15 12:56:03 +01:00
|
|
|
raise NotImplementedError(f'Unknown dependency syntax "{syntax}" for dependency "{dep[0]} {dep[1]} {dep[3]}"')
|
2026-03-15 10:00:56 +01:00
|
|
|
dep_str = ' '.join(expanded_dep)
|
2026-03-15 13:05:15 +01:00
|
|
|
if quote:
|
|
|
|
|
dep_str = '"' + dep_str + '"'
|
2026-04-06 13:49:34 +02:00
|
|
|
if not dep_str in ret:
|
2026-03-15 13:05:15 +01:00
|
|
|
log(DEBUG, f'Appending dependency >{dep_str}<')
|
2026-04-06 13:49:34 +02:00
|
|
|
ret.append(dep_str)
|
|
|
|
|
return ret
|
2025-11-16 18:05:10 +01:00
|
|
|
|
2026-03-15 12:56:03 +01:00
|
|
|
def pkg_relations(self, rel_type: str, args: Namespace) -> str:
|
|
|
|
|
|
|
|
|
|
return args.delimiter.join(
|
|
|
|
|
self.pkg_relations_list(
|
|
|
|
|
rel_type=rel_type,
|
|
|
|
|
flavours = args.flavours.split(','),
|
2026-04-06 13:49:34 +02:00
|
|
|
seed_pkgs = args.modules,
|
2026-03-15 12:56:03 +01:00
|
|
|
subsections = None if args.subsections is None else args.subsections.split(','),
|
|
|
|
|
no_subpackages = args.no_subpackages,
|
|
|
|
|
dont_strip_revision = args.dont_strip_revision,
|
|
|
|
|
expand_semver_revision_range = args.expand_semver_revision_range,
|
|
|
|
|
syntax = self.Syntax[args.syntax.replace('-', '_')],
|
|
|
|
|
recursive = args.recursive,
|
|
|
|
|
dont_expand_version_macros = args.dont_expand_version_macros,
|
|
|
|
|
ignore = set(args.ignore.split(',')),
|
2026-03-15 13:05:15 +01:00
|
|
|
quote = args.quote,
|
2026-03-15 14:19:32 +01:00
|
|
|
skip_excluded = args.skip_excluded,
|
2026-03-15 16:34:30 +01:00
|
|
|
hide_self = args.hide_self,
|
2026-04-06 13:57:20 +02:00
|
|
|
hide_jw_pkg = args.hide_jw_pkg,
|
2026-03-15 12:56:03 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-15 12:31:21 +01:00
|
|
|
def print_pkg_relations(self, rel_type: str, args: Namespace) -> None:
|
|
|
|
|
print(self.pkg_relations(rel_type, args))
|
2025-11-16 18:05:10 +01:00
|
|
|
|
2026-01-27 10:22:16 +01:00
|
|
|
def __init__(self, parent: CmdProjects, relation: str, help: str) -> None:
|
|
|
|
|
super().__init__(parent, 'pkg-' + relation, help=help)
|
2025-11-16 18:05:10 +01:00
|
|
|
self.relation = relation
|
|
|
|
|
|
|
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
|
|
|
|
super().add_arguments(parser)
|
|
|
|
|
parser.add_argument('-S', '--subsections', nargs='?', default=None, help='Subsections to consider, comma-separated')
|
|
|
|
|
parser.add_argument('-d', '--delimiter', nargs='?', default=', ', help='Output words delimiter')
|
2026-03-15 12:56:03 +01:00
|
|
|
parser.add_argument('flavours', help='Dependency flavours (run, build, devel, release)')
|
|
|
|
|
parser.add_argument('modules', nargs='*', help='Modules')
|
2025-11-16 18:05:10 +01:00
|
|
|
parser.add_argument('-p', '--no-subpackages', action='store_true',
|
|
|
|
|
default=False, help='Cut -run and -devel from package names')
|
|
|
|
|
parser.add_argument('--dont-strip-revision', action='store_true',
|
|
|
|
|
default=False, help='Always treat VERSION macro as VERSION-REVISION')
|
2026-03-15 10:00:56 +01:00
|
|
|
parser.add_argument('--expand-semver-revision-range', action='store_true',
|
|
|
|
|
default=False, help='Always treat =VERSION macro as >= VERSION-0 and < (VERSION+1)-0')
|
2026-03-15 12:25:11 +01:00
|
|
|
parser.add_argument('--syntax', choices=['semver', 'debian', 'names-only'],
|
2026-03-15 10:13:26 +01:00
|
|
|
default='semver', help='Output syntax')
|
2025-11-16 18:05:10 +01:00
|
|
|
parser.add_argument('--recursive', action='store_true',
|
|
|
|
|
default=False, help='Find dependencies recursively')
|
|
|
|
|
parser.add_argument('--dont-expand-version-macros', action='store_true',
|
|
|
|
|
default=False, help='Don\'t expand VERSION and REVISION macros')
|
|
|
|
|
parser.add_argument('--ignore', nargs='?', default='', help='Packages that '
|
|
|
|
|
'should be ignored together with their dependencies')
|
2026-03-15 14:19:32 +01:00
|
|
|
parser.add_argument('--skip-excluded', action='store_true', default=False,
|
|
|
|
|
help='Don\'t consider or output modules matching the os cascade in their [build].exclude config')
|
2026-03-15 16:34:30 +01:00
|
|
|
parser.add_argument('--hide-self', action='store_true', default=False,
|
|
|
|
|
help='Don\'t include any of the projects listed in <modules> in the output')
|
2026-04-06 13:57:20 +02:00
|
|
|
parser.add_argument('--hide-jw-pkg', action='store_true', default=False,
|
|
|
|
|
help='Don\'t include packages from requires.jw in the output')
|
2026-03-15 13:05:15 +01:00
|
|
|
parser.add_argument('--quote', action='store_true', default=False,
|
|
|
|
|
help='Put double quotes around each listed dependency')
|
2025-11-16 18:05:10 +01:00
|
|
|
|
2026-01-27 14:31:25 +01:00
|
|
|
async def _run(self, args: Namespace) -> None:
|
2025-11-16 18:05:10 +01:00
|
|
|
return self.print_pkg_relations(self.relation, args)
|