-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfind.py
executable file
·43 lines (30 loc) · 1.89 KB
/
find.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
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import os
import itertools
def find_files(suffix, first_dirs, first_files, last_files, last_dirs, root_dir, excludes):
excludes = set(excludes)
for root, dirs, files in os.walk(root_dir):
all_files = set(files) - excludes
ffiles = [i for i in first_files if i in all_files]
lfiles = [i for i in last_files if i in all_files]
rfiles = sorted((all_files - set(ffiles)) - set(lfiles))
for doc in itertools.chain(ffiles, rfiles, lfiles):
if doc.endswith(suffix):
yield os.path.join(root, doc)
all_dirs = set(dirs) - excludes
fdirs = [i for i in first_dirs if i in all_dirs]
ldirs = [i for i in last_dirs if i in all_dirs]
rdirs = sorted((all_dirs - set(fdirs)) - set(ldirs))
dirs[:] = itertools.chain(fdirs, rdirs, ldirs)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='find all relevant files to include in a hackpack')
parser.add_argument('-s', '--suffix', dest='suffix', default='.md', help='suffix to search for')
parser.add_argument('-e', '--exclude', action='append', dest='exclude', default=[], help='file to exclude')
parser.add_argument('-f', '--first', action='append', dest='first', default=[], help='folders to place first')
parser.add_argument('-g', '--first-file', action='append', dest='first_file', default=[], help='files to place first')
parser.add_argument('-l', '--last', action='append', dest='last', default=[], help='folders to place last')
parser.add_argument('-m', '--last-file', action='append', dest='last_file', default=[], help='files to place last')
parser.add_argument('dir', nargs='?', default='.', help='scan directory')
args = parser.parse_args()
print(' '.join(find_files(args.suffix, args.first, args.first_file, args.last_file, args.last, args.dir, args.exclude)))