Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of --maxdepth to limit recursion of input directory #104

Merged
merged 11 commits into from
Mar 30, 2021
11 changes: 11 additions & 0 deletions phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ def main():
help="Don't move any files, just show which changes would be done.",
)

parser.add_argument(
"--maxdepth",
type=int,
default=-1,
choices=range(0, 255),
metavar="1-255",
help="Descend at most 'maxdepth' levels (a non-negative integer) of directories",
)

parser.add_argument(
"-r",
"--regex",
Expand Down Expand Up @@ -143,6 +152,7 @@ def main():
""",
)


parser.add_argument(
"-q",
"--quiet",
Expand Down Expand Up @@ -176,6 +186,7 @@ def main():
date_field=args.date_field,
dry_run=args.dry_run,
quiet=args.quiet,
max_depth=args.maxdepth
)


Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ If you want phockup to run without any changes (don't copy/move any files) but j
### Quiet run
If you want phockup to run without any output (displaying only error messages, and muting all progress messages) use the flag `-q | --quiet`.

### Limit directory traversal depth
If you would like to limit how deep the directories are traversed, you can use the `--maxdepth` option to specify the maximum number of levels below the input directory to process. In order to process only the input directory, you can disable sub-directory processing with:
`--maxdepth=0` The current implementation is limited to a maximum depth of 255.

## Development

### Running tests
Expand Down
8 changes: 6 additions & 2 deletions src/phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def __init__(self, input_dir, output_dir, **args):
self.timestamp = args.get('timestamp', False)
self.date_field = args.get('date_field', False)
self.dry_run = args.get('dry_run', False)
self.max_depth = args.get('max_depth', -1)
self.stop_depth = self.input_dir.count(os.sep) + self.max_depth if self.max_depth > -1 else sys.maxsize
self.quiet = args.get('quiet', False)

printer.should_print(self.quiet)

self.check_directories()
self.walk_directory()

Expand All @@ -61,14 +63,16 @@ def walk_directory(self):
"""
Walk input directory recursively and call process_file for each file except the ignored ones
"""
for root, _, files in os.walk(self.input_dir):
for root, dirnames, files in os.walk(self.input_dir):
files.sort()
for filename in files:
if filename in ignored_files:
continue

filepath = os.path.join(root, filename)
self.process_file(filepath)
if root.count(os.sep) >= self.stop_depth:
del dirnames[:]

def checksum(self, filename):
"""
Expand Down