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 ):
64+ def  _write_pending_upgrades (registry , cache ,  exclusions ):
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   exclusions 
6669    }
6770    for  candidate  in  candidates :
6871        logging .debug (
@@ -79,13 +82,14 @@ def _write_pending_upgrades(registry, cache):
7982            g .labels (change .labels ['origin' ], change .labels ['arch' ]).set (change .count )
8083
8184
82- def  _write_held_upgrades (registry , cache ):
85+ def  _write_held_upgrades (registry , cache ,  exclusions ):
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   exclusions 
8993        )
9094    }
9195    for  candidate  in  held_candidates :
@@ -103,13 +107,14 @@ def _write_held_upgrades(registry, cache):
103107            g .labels (change .labels ['origin' ], change .labels ['arch' ]).set (change .count )
104108
105109
106- def  _write_obsolete_packages (registry , cache ):
110+ def  _write_obsolete_packages (registry , cache ,  exclusions ):
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   exclusions 
113118                )]
114119    for  package  in  obsoletes :
115120        if  package .candidate  is  None :
@@ -126,9 +131,11 @@ def _write_obsolete_packages(registry, cache):
126131    g .set (len (obsoletes ))
127132
128133
129- def  _write_autoremove_pending (registry , cache ):
134+ def  _write_autoremove_pending (registry , cache ,  exclusions ):
130135    autoremovable_packages  =  {
131-         p .candidate  for  p  in  cache  if  p .is_auto_removable 
136+         p .candidate 
137+         for  p  in  cache 
138+         if  p .is_auto_removable  and  p .name  not  in   exclusions 
132139    }
133140    for  candidate  in  autoremovable_packages :
134141        logging .debug (
@@ -181,13 +188,17 @@ def _main():
181188    if  os .getenv ('DEBUG' ):
182189        logging .basicConfig (level = logging .DEBUG )
183190
191+     parser  =  argparse .ArgumentParser ()
192+     parser .add_argument ("--exclude" , nargs = '*' , default = [])
193+     args  =  parser .parse_args (sys .argv [1 :])
194+ 
184195    cache  =  apt .cache .Cache ()
185196
186197    registry  =  CollectorRegistry ()
187-     _write_pending_upgrades (registry , cache )
188-     _write_held_upgrades (registry , cache )
189-     _write_obsolete_packages (registry , cache )
190-     _write_autoremove_pending (registry , cache )
198+     _write_pending_upgrades (registry , cache ,  args . exclude )
199+     _write_held_upgrades (registry , cache ,  args . exclude )
200+     _write_obsolete_packages (registry , cache ,  args . exclude )
201+     _write_autoremove_pending (registry , cache ,  args . exclude )
191202    _write_installed_packages_per_origin (registry , cache )
192203    _write_cache_timestamps (registry )
193204    _write_reboot_required (registry )
0 commit comments