Last active
October 15, 2021 19:13
-
-
Save vladimirgamalyan/c90177c26041e38fde76034aeefbe838 to your computer and use it in GitHub Desktop.
Enchanted file globbing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from wcmatch import pathlib | |
def glob_files(base_dir, glob_list): | |
file_list = set() | |
for file_glob in glob_list: | |
if file_glob.startswith('!'): | |
p = base_dir.as_posix() + '/' + file_glob[1:] | |
file_list = {i for i in file_list if not i.match(p, flags=pathlib.GLOBSTAR + pathlib.BRACE)} | |
else: | |
file_list.update(base_dir.glob(file_glob, flags=pathlib.GLOBSTAR + pathlib.BRACE)) | |
return file_list | |
def process_json(json_file): | |
with open(json_file) as f: | |
data = json.load(f) | |
base_dir = pathlib.Path(data['base']) | |
result = set() | |
for glob_list in data['files']: | |
result.update(glob_files(base_dir, glob_list)) | |
result = sorted(str(p.relative_to(base_dir).as_posix()) for p in result) | |
with open(data['output'], 'w') as f: | |
for i in result: | |
f.write("%s\n" % i) | |
if __name__ == '__main__': | |
process_json('source_list.json') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"base": "D:/projects", | |
"output": "D:/source_list.txt", | |
"files": [ | |
["foo/src/**/*.{h,cpp}"], | |
["**/*.{h,cpp}", "!**/*tests.*"] | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment