Last active
December 26, 2018 23:00
-
-
Save chdsbd/32b861eeec4009757ef6a170456d985e to your computer and use it in GitHub Desktop.
find conflicting file names between two folders. For use with JS-style import systems. (I didn't want to just delete this, so it's here for posterity)
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
#!/usr/bin/env python3 | |
""" | |
Find conflicting files between folders. | |
When using absolute imports, it is possible to have conflicts where a module | |
is shadowed by a dependency in node_modules. This will detect such an issue | |
and warn. We compare without file extensions, to behave as Node imports do. | |
""" | |
import argparse | |
from pathlib import Path | |
import sys | |
from typing import Set | |
parser = argparse.ArgumentParser( | |
description="given directories, find duplicate files/folders, ignoring their extensions" | |
) | |
parser.add_argument("dir", nargs="*", help="directories to compare") | |
def find_names(dir: str) -> Set[str]: | |
"""Normalize files and dirs, removing their extensions""" | |
return set(f.stem for f in Path(dir).iterdir()) | |
if __name__ == '__main__': | |
args = parser.parse_args() | |
problems: Set[str] = set() | |
total: Set[str] = set() | |
for dirname in args.dir: | |
new = find_names(dirname) | |
# see if our new file stems are in the running total and store them with any | |
# existing problems | |
problems = problems | (total & new) | |
total = total.union(new) | |
if len(problems) > 0: | |
print( | |
"Conflicting file(s) found. Try moving your file to a different namespace:", | |
file=sys.stderr, | |
) | |
for filename in problems: | |
print(filename, file=sys.stderr) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment