2424
2525import apt
2626import apt_pkg
27+ import argparse
2728import collections
2829import logging
2930import os
@@ -60,9 +61,11 @@ def _convert_candidates_to_upgrade_infos(candidates):
6061 return changes_list
6162
6263
63- def _write_pending_upgrades (registry , cache , logger ):
64+ def _write_pending_upgrades (registry , cache , logger , cli_args ):
6465 candidates = {
65- p .candidate for p in cache if p .is_upgradable and not p .phasing_applied
66+ p .candidate
67+ for p in cache
68+ if p .is_upgradable and not p .phasing_applied and p .name not in cli_args .exclude
6669 }
6770 for candidate in candidates :
6871 logger .debug (
@@ -79,13 +82,14 @@ def _write_pending_upgrades(registry, cache, logger):
7982 g .labels (change .labels ['origin' ], change .labels ['arch' ]).set (change .count )
8083
8184
82- def _write_held_upgrades (registry , cache , logger ):
85+ def _write_held_upgrades (registry , cache , logger , cli_args ):
8386 held_candidates = {
8487 p .candidate for p in cache
8588 if (
8689 p .is_upgradable
8790 and p ._pkg .selected_state == apt_pkg .SELSTATE_HOLD
8891 and not p .phasing_applied
92+ and p .name not in cli_args .exclude
8993 )
9094 }
9195 for candidate in held_candidates :
@@ -103,13 +107,14 @@ def _write_held_upgrades(registry, cache, logger):
103107 g .labels (change .labels ['origin' ], change .labels ['arch' ]).set (change .count )
104108
105109
106- def _write_obsolete_packages (registry , cache , logger ):
110+ def _write_obsolete_packages (registry , cache , logger , cli_args ):
107111 # This corresponds to the apt filter "?obsolete"
108112 obsoletes = [p for p in cache if p .is_installed and (
109113 p .candidate is None or
110114 not p .candidate .origins or
111115 (len (p .candidate .origins ) == 1 and
112116 p .candidate .origins [0 ].origin in ['' , "/var/lib/dpkg/status" ])
117+ and p .name not in cli_args .exclude
113118 )]
114119 for package in obsoletes :
115120 if package .candidate is None :
@@ -126,8 +131,12 @@ def _write_obsolete_packages(registry, cache, logger):
126131 g .set (len (obsoletes ))
127132
128133
129- def _write_autoremove_pending (registry , cache , logger ):
130- autoremovable_packages = {p .candidate for p in cache if p .is_auto_removable }
134+ def _write_autoremove_pending (registry , cache , logger , cli_args ):
135+ autoremovable_packages = {
136+ p .candidate
137+ for p in cache
138+ if p .is_auto_removable and p .name not in cli_args .exclude
139+ }
131140 for candidate in autoremovable_packages :
132141 logger .debug (
133142 "autoremovable package: %s / %s" ,
@@ -181,13 +190,17 @@ def _main():
181190 if os .environ .get ("DEBUG" ):
182191 logger .setLevel (logging .DEBUG )
183192
193+ parser = argparse .ArgumentParser ()
194+ parser .add_argument ("--exclude" , nargs = '*' , default = [])
195+ args = parser .parse_args (sys .argv [1 :])
196+
184197 cache = apt .cache .Cache ()
185198
186199 registry = CollectorRegistry ()
187- _write_pending_upgrades (registry , cache , logger )
188- _write_held_upgrades (registry , cache , logger )
189- _write_obsolete_packages (registry , cache , logger )
190- _write_autoremove_pending (registry , cache , logger )
200+ _write_pending_upgrades (registry , cache , logger , args )
201+ _write_held_upgrades (registry , cache , logger , args )
202+ _write_obsolete_packages (registry , cache , logger , args )
203+ _write_autoremove_pending (registry , cache , logger , args )
191204 _write_installed_packages_per_origin (registry , cache )
192205 _write_cache_timestamps (registry )
193206 _write_reboot_required (registry )
0 commit comments