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)
| 575 | |
| 576 | |
| 577 | def 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 | |
| 604 | def journald_clean(): |