diff options
Diffstat (limited to 'src/debputy/analysis/debian_dir.py')
-rw-r--r-- | src/debputy/analysis/debian_dir.py | 54 |
1 files changed, 50 insertions, 4 deletions
diff --git a/src/debputy/analysis/debian_dir.py b/src/debputy/analysis/debian_dir.py index 68a5c1b..d63b0c9 100644 --- a/src/debputy/analysis/debian_dir.py +++ b/src/debputy/analysis/debian_dir.py @@ -15,6 +15,7 @@ from typing import ( Iterator, TypedDict, NotRequired, + FrozenSet, ) from debputy.analysis import REFERENCE_DATA_TABLE @@ -277,12 +278,49 @@ def _kpf_install_pattern( return ppkpf.info.get("install_pattern") +def _parse_dh_cmd_list( + cmd_list: Optional[List[Union[Mapping[str, Any], object]]] +) -> Iterable[str]: + if not isinstance(cmd_list, list): + return + + for command in cmd_list: + if not isinstance(command, dict): + continue + command_name = command.get("command") + if isinstance(command_name, str): + yield command_name + + +def _resolve_active_and_inactive_dh_commands( + dh_rules_addons: Iterable[str], +) -> Tuple[FrozenSet[str], FrozenSet[str]]: + cmd = ["dh_assistant", "list-commands", "--output-format=json"] + if dh_rules_addons: + addons = ",".join(dh_rules_addons) + cmd.append(f"--with={addons}") + try: + output = subprocess.check_output( + cmd, + stderr=subprocess.DEVNULL, + ) + except (subprocess.CalledProcessError, FileNotFoundError): + return frozenset(), frozenset() + else: + result = json.loads(output) + active_commands = frozenset(_parse_dh_cmd_list(result.get("commands"))) + disabled_commands = frozenset( + _parse_dh_cmd_list(result.get("disabled-commands")) + ) + return active_commands, disabled_commands + + def _resolve_debhelper_config_files( debian_dir: VirtualPath, binary_packages: Mapping[str, BinaryPackage], debputy_plugin_metadata: DebputyPluginMetadata, dh_ppf_docs: Dict[str, PluginProvidedKnownPackagingFile], - dh_rules_addons: Iterable[str], + dh_rules_addons: Sequence[str], dh_compat_level: int, saw_dh: bool, ) -> Tuple[List[PackagerProvidedFile], Optional[object], int]: @@ -311,6 +349,7 @@ def _resolve_debhelper_config_files( "config-files", [] ) issues = result.get("issues") + active_commands, _ = _resolve_active_and_inactive_dh_commands(dh_rules_addons) for config_file in config_files: if not isinstance(config_file, dict): continue @@ -369,6 +408,8 @@ def _resolve_debhelper_config_files( else: continue is_active = command.get("is-active", True) + if is_active is None and command_name in active_commands: + is_active = True if not isinstance(is_active, bool): continue if is_active: @@ -396,6 +437,13 @@ def _resolve_debhelper_config_files( "packageless_is_fallback_for_all_packages", False, ) + has_active_command = ( + ppkpf.info.get("has_active_command", False) if saw_dh else False + ) + if not has_active_command: + dh_cmds = ppkpf.info.get("debhelper_commands") + if dh_cmds: + has_active_command = any(c in active_commands for c in dh_cmds) dh_ppfs[stem] = _fake_PPFClassSpec( debputy_plugin_metadata, stem, @@ -404,9 +452,7 @@ def _resolve_debhelper_config_files( default_priority=default_priority, post_formatting_rewrite=post_formatting_rewrite, packageless_is_fallback_for_all_packages=packageless_is_fallback_for_all_packages, - has_active_command=( - ppkpf.info.get("has_active_command", False) if saw_dh else False - ), + has_active_command=has_active_command, ) all_dh_ppfs = list( flatten_ppfs( |