MCPcopy Create free account
hub / github.com/bleachbit/bleachbit / run_cleaner_cmd

Function run_cleaner_cmd

bleachbit/Unix.py:577–601  ·  view source on GitHub ↗

Runs a specified command and returns how much space was (reportedly) freed. The subprocess shouldn't need any user input and the user should have the necessary rights. freed_space_regex gets applied to every output line, if the re matches, add values captured by the single group in t

(cmd, args, freed_space_regex=r'[\d.]+[kMGTE]?B?', error_line_regexes=None)

Source from the content-addressed store, hash-verified

575
576
577def run_cleaner_cmd(cmd, args, freed_space_regex=r'[\d.]+[kMGTE]?B?', error_line_regexes=None):
578 """Runs a specified command and returns how much space was (reportedly) freed.
579 The subprocess shouldn't need any user input and the user should have the
580 necessary rights.
581 freed_space_regex gets applied to every output line, if the re matches,
582 add values captured by the single group in the regex"""
583 if not FileUtilities.exe_exists(cmd):
584 raise RuntimeError(_('Executable not found: %s') % cmd)
585 freed_space_regex = re.compile(freed_space_regex)
586 error_line_regexes = [re.compile(regex)
587 for regex in error_line_regexes or []]
588
589 env = {'LC_ALL': 'C', 'PATH': os.getenv('PATH')}
590 output = subprocess.check_output([cmd] + args, stderr=subprocess.STDOUT,
591 universal_newlines=True, env=env)
592 freed_space = 0
593 for line in output.split('\n'):
594 m = freed_space_regex.match(line)
595 if m is not None:
596 freed_space += FileUtilities.human_to_bytes(m.group(1))
597 for error_re in error_line_regexes:
598 if error_re.search(line):
599 raise RuntimeError('Invalid output from %s: %s' % (cmd, line))
600
601 return freed_space
602
603
604def journald_clean():

Callers 7

test_run_cleaner_cmdMethod · 0.90
journald_cleanFunction · 0.85
apt_autoremoveFunction · 0.85
apt_autocleanFunction · 0.85
apt_cleanFunction · 0.85
yum_cleanFunction · 0.85
dnf_cleanFunction · 0.85

Calls 1

matchMethod · 0.80

Tested by 1

test_run_cleaner_cmdMethod · 0.72