Coverage for src/debputy/dh_migration/dh_related_migrations.py: 10%

82 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-10-28 20:14 +0000

1import os.path 

2import subprocess 

3 

4from debian.debian_support import Version 

5 

6from debputy.analysis.debian_dir import resolve_debhelper_config_files 

7from debputy.dh.debhelper_emulation import dhe_pkgfile 

8from debputy.dh.dh_assistant import read_dh_addon_sequences, extract_dh_compat_level 

9from debputy.dh_migration.models import ( 

10 FeatureMigration, 

11 MigrationRequest, 

12) 

13from debputy.plugin.api.impl import plugin_metadata_for_debputys_own_plugin 

14from debputy.pydeb_compat import HAS_PY_DEB_1_2 

15 

16 

17def _installed_debhelper_version_is_at_least(desired_version: str) -> bool: 

18 try: 

19 output = subprocess.check_output( 

20 [ 

21 "dpkg-query", 

22 "-W", 

23 "--showformat=${Version} ${db:Status-Status}\n", 

24 "debhelper", 

25 ] 

26 ).decode("utf-8") 

27 except (FileNotFoundError, subprocess.CalledProcessError): 

28 return False 

29 else: 

30 parts = output.split() 

31 if len(parts) != 2: 

32 return False 

33 if parts[1] != "installed": 

34 return False 

35 return Version(parts[0]) >= Version(desired_version) 

36 

37 

38def dh_mbm_migrate_package_files( 

39 migration_request: MigrationRequest, 

40 feature_migration: FeatureMigration, 

41) -> None: 

42 feature_migration.tagline = "Add explicit package name to debhelper config files" 

43 debputy_plugin_metadata = plugin_metadata_for_debputys_own_plugin() 

44 dh_compat_level, _ = extract_dh_compat_level() 

45 r = read_dh_addon_sequences(migration_request.debian_dir) 

46 if r is not None: 

47 bd_sequences, dr_sequences, uses_dh_sequencer = r 

48 dh_sequences = bd_sequences | dr_sequences 

49 uses_dh_sequencer = True 

50 else: 

51 dh_sequences = set() 

52 uses_dh_sequencer = False 

53 

54 ( 

55 all_dh_ppfs, 

56 missing_introspection, 

57 _, 

58 _, 

59 ) = resolve_debhelper_config_files( 

60 migration_request.debian_dir, 

61 {p.name: p for p in migration_request.all_packages}, 

62 debputy_plugin_metadata, 

63 migration_request.manifest.plugin_provided_feature_set, 

64 dh_sequences, 

65 dh_compat_level, 

66 uses_dh_sequencer, 

67 match_arch=migration_request.all_arch_names(), 

68 match_arch_os=migration_request.all_arch_os_names(), 

69 multi_match=True, 

70 ) 

71 for ppf in all_dh_ppfs: 

72 if ( 

73 ppf.uses_explicit_package_name 

74 or ppf.definition.packageless_is_fallback_for_all_packages 

75 ): 

76 continue 

77 basename = ppf.path.name 

78 new_basename = f"{ppf.package_name}.{basename}" 

79 ppf_parent = ppf.path.parent_dir 

80 assert ppf_parent is not None 

81 parent_path = ppf_parent.path 

82 new_path = os.path.join(parent_path, new_basename) 

83 feature_migration.rename_on_success(ppf.path.path, new_path) 

84 

85 if not missing_introspection and not _installed_debhelper_version_is_at_least( 

86 "13.26~" 

87 ): 

88 feature_migration.warn( 

89 "Please upgrade to `debhelper/13.26` to ensure reliability. Some files might be silently missed." 

90 ) 

91 

92 for command in missing_introspection: 

93 feature_migration.warn( 

94 f"The command `{command}` is used but `dh_assistant` was not aware of whether it had configuration files." 

95 ) 

96 

97 if missing_introspection: 

98 feature_migration.warn( 

99 "Please ensure you have the latest versions of the above commands installed and try again." 

100 ) 

101 feature_migration.warn( 

102 "If the issue still persists, then please file a bug against the provider of the commands asking them to supply the relevant hints (see doc/PROGRAMMING.md in debhelper/13.26+) with the debhelper maintainers in CC." 

103 ) 

104 if HAS_PY_DEB_1_2: 

105 feature_migration.warn( 

106 "Any templating based files (such as debian/install.tmpl) may be missed. Please validate these manually." 

107 ) 

108 else: 

109 feature_migration.warn( 

110 "Any architecture restricted file based on OS (such `debian/install.linux`) and templating based files (such as debian/install.tmpl) may be missed. Please validate these manually." 

111 ) 

112 

113 

114def dh_mbm_migrate_manual_warnings( 

115 migration_request: MigrationRequest, 

116 feature_migration: FeatureMigration, 

117) -> None: 

118 feature_migration.tagline = "Manual parts" 

119 debian_dir = migration_request.debian_dir 

120 r = read_dh_addon_sequences(debian_dir) 

121 if r is not None: 

122 bd_sequences, dr_sequences, uses_dh_sequencer = r 

123 dh_sequences = bd_sequences | dr_sequences 

124 uses_dh_sequencer = True 

125 else: 

126 dh_sequences = set() 

127 uses_dh_sequencer = False 

128 drules = debian_dir.get("rules") 

129 if drules and drules.is_file: 

130 with drules.open() as fd: 

131 migrated_auto_install = 0 

132 for line in fd: 

133 line = line.lstrip() 

134 if not line or line.startswith("#"): 

135 continue 

136 if line.startswith("\t") and "dh_auto_install" in line: 

137 migrated_auto_install += 1 if "--destdir" in line else 0 

138 used_dh_install_files = 0 

139 for dctrl_bin in migration_request.all_packages: 

140 dh_config_file = dhe_pkgfile(debian_dir, dctrl_bin, "install") 

141 if dh_config_file is not None: 

142 used_dh_install_files += 1 

143 main_pkg = migration_request.main_binary 

144 if not migrated_auto_install and not used_dh_install_files: 

145 feature_migration.warn( 

146 f"Ensure `dh_auto_install --destdir debian/{main_pkg.name}` or `dh_install` is used. Remember Replaces/Breaks if files are moved" 

147 ) 

148 if uses_dh_sequencer and "single-binary" in dh_sequences: 

149 feature_migration.warn("Remove `single-binary` add-on")