cmd/directory_discrepancy.py

59 lines
1.6 KiB
Python
Raw Normal View History

2021-05-27 04:35:29 +00:00
import argparse
2020-08-20 17:58:09 +00:00
import sys
2021-05-27 04:35:29 +00:00
from voussoirkit import betterhelp
from voussoirkit import pathclass
2022-01-20 03:35:29 +00:00
from voussoirkit import niceprints
2021-05-27 04:35:29 +00:00
from voussoirkit import operatornotify
from voussoirkit import spinal
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'directory_discrepancy')
def helper(root, recurse):
root = pathclass.Path(root)
walk = spinal.walk(root, recurse=recurse, yield_directories=True, yield_files=True)
for path in walk:
relative = path.relative_to(root)
yield relative
def directory_discrepancy_argparse(args):
files1 = set(helper(args.dir1, recurse=args.recurse))
files2 = set(helper(args.dir2, recurse=args.recurse))
2022-01-20 03:35:29 +00:00
print(niceprints.equals_header(f'In "{args.dir1}" but not in "{args.dir2}"'))
2021-05-27 04:35:29 +00:00
for discrepancy in sorted(files1.difference(files2)):
print(discrepancy)
print()
2022-01-20 03:35:29 +00:00
print(niceprints.equals_header(f'In "{args.dir2}" but not in "{args.dir1}"'))
2021-05-27 04:35:29 +00:00
for discrepancy in sorted(files2.difference(files1)):
print(discrepancy)
return 0
2021-08-10 18:54:22 +00:00
@vlogging.main_decorator
2021-05-27 04:35:29 +00:00
def main(argv):
2022-02-13 03:50:00 +00:00
parser = argparse.ArgumentParser(
description='''
This program compares two directory and shows which files exist in each
directory that do not exist in the other.
''',
)
2021-05-27 04:35:29 +00:00
parser.add_argument('dir1')
parser.add_argument('dir2')
2022-02-13 03:50:00 +00:00
parser.add_argument(
'--recurse',
action='store_true',
help='''
Also check subdirectories.
''',
)
2021-05-27 04:35:29 +00:00
parser.set_defaults(func=directory_discrepancy_argparse)
2020-08-20 17:58:09 +00:00
2022-02-13 03:50:00 +00:00
return betterhelp.go(parser, argv)
2020-08-20 17:58:09 +00:00
2021-05-27 04:35:29 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))