Skip to content

Commit

Permalink
add option to clean iters to not remove all files by default
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Nov 26, 2024
1 parent 453e767 commit ff6fb7e
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions matsim/cli/clean_iters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import os
import re
import shutil
import glob

from argparse import ArgumentParser

METADATA = "clean-iters", "Remove all iterations directories except the last one"
Expand All @@ -18,6 +20,8 @@ def setup(parser: ArgumentParser):
parser.add_argument("-f", "--force", action='store_true', default=False, help="Don't ask for confirmation")
parser.add_argument("-d", "--dry-run", action='store_true', default=False,
help="Don't remove anything, only print for information")
parser.add_argument( "--full-clean", action='store_true', default=False,
help="Remove the whole iteration directory, otherwise only remove large files")


def main(args):
Expand Down Expand Up @@ -50,14 +54,33 @@ def main(args):
print("Unknown answer, don't delete")

if delete:

# Delete all except last
for iter, n in iters[:-1]:
path = os.path.join(dirpath, iter)
try:
shutil.rmtree(path)
except Exception as e:
print("Error removing %s:" % path, e)

if args.full_clean:
path = os.path.join(dirpath, iter)
try:
shutil.rmtree(path)
except Exception as e:
print("Error removing %s:" % path, e)

else:

# Delete large files
rm = []
rm.extend(glob.glob(os.path.join(dirpath, iter, "*.events.xml*")))
rm.extend(glob.glob(os.path.join(dirpath, iter, "*.plans.xml*")))
rm.extend(glob.glob(os.path.join(dirpath, iter, "*.legs.csv*")))
rm.extend(glob.glob(os.path.join(dirpath, iter, "*.activities.csv*")))

if rm:
print("Removing files:", rm)

for f in rm:
try:
os.remove(f)
except Exception as e:
print("Error removing %s:" % f, e)


if __name__ == "__main__":
Expand Down

0 comments on commit ff6fb7e

Please sign in to comment.