-
Notifications
You must be signed in to change notification settings - Fork 12
/
clean_up.py
35 lines (28 loc) · 1.16 KB
/
clean_up.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python2
""" Clean up script after the previous segments. Deletes whatever files are given
in `input_files`, and empty parent directories of these files.
USERS BEWARE: really deletes everything without confirmation!
"""
from logging import getLogger
import os
logger = getLogger("pijp.cleanup")
def main(input_files, really_delete):
if not really_delete:
logger.info("Running clean_up in 'demo' mode - not really deleting anything")
# Go over the supplied filenames and delete them.
for filename in input_files:
logger.info("Deleting %s", filename)
if really_delete:
try:
os.remove(filename)
except OSError as e:
logger.error("Error deleting %s: %s", filename, e.strerror)
# Go over the parent dirs and delete them.
# Recursively deletes empty parents.
for dirname in set(os.path.dirname(x) for x in input_files):
logger.info("Trying to delete %s and empty parent dirs", dirname)
if really_delete:
try:
os.removedirs(dirname)
except OSError:
pass # the dir is probabily not empty.