nzbToMedia/cleanup.py
Labrys of Knossos d3d1868021 Fix IOError on cleanup when git not found
On posix systems IOError is raised when git is not found

In Python 3 IOError is an alias for OSError.
In Python 2 IOError is not caught by OSError so we must catch both.

Fixes #1460
2018-12-27 11:07:39 -05:00

100 lines
2.7 KiB
Python

from __future__ import print_function
import subprocess
import sys
def git_clean(remove_directories=False, force=False, dry_run=False, interactive=False, quiet=False, exclude=None,
ignore_rules=False, clean_ignored=False, paths=None):
"""Execute git clean commands."""
command = ['git', 'clean']
if remove_directories:
command.append('-d')
if force:
command.append('--force')
if interactive:
command.append('--interactive')
if quiet:
command.append('--quiet')
if dry_run:
command.append('--dry-run')
if exclude:
try:
exclude = exclude.split(' ')
except AttributeError:
pass
for exclusion in exclude:
command.append('--exclude={pattern}'.format(pattern=exclusion))
if ignore_rules:
command.append('-x')
if clean_ignored:
command.append('-X')
if paths:
try:
paths = paths.split(' ')
except AttributeError:
pass
command.extend(paths)
return subprocess.check_output(command)
def clean_bytecode():
"""Clean bytecode files."""
try:
result = git_clean(
remove_directories=True,
force=True,
exclude=[
'*.*', # exclude everything
'!*.py[co]', # except bytecode
'!**/__pycache__/', # and __pycache__ folders
],
)
print(result)
except subprocess.CalledProcessError as error:
sys.exit('Error Code: {}'.format(error.returncode))
except (IOError, OSError) as error:
sys.exit('Error: {}'.format(error))
else:
return result
def clean_folders(*paths):
"""Clean obsolete folders."""
try:
result = git_clean(
remove_directories=True,
force=True,
ignore_rules=True,
paths=paths,
)
except subprocess.CalledProcessError as error:
sys.exit('Error Code: {}'.format(error.returncode))
except (IOError, OSError) as error:
sys.exit('Error: {}'.format(error))
else:
return result
def clean(*paths):
"""Clean up bytecode and obsolete folders."""
print('-- Cleaning bytecode --')
try:
result = clean_bytecode()
except SystemExit as error:
print(error)
else:
print(result or 'No bytecode to clean\n')
if paths:
print('-- Cleaning folders: {} --'.format(paths))
try:
result = clean_folders(*paths)
except SystemExit as error:
print(error)
else:
print(result or 'No folders to clean\n')
if __name__ == '__main__':
clean('libs', 'core')