packagelister.whouses
1import argparse 2 3from pathier import Pathier 4 5from packagelister import scan 6 7 8def get_args() -> argparse.Namespace: 9 parser = argparse.ArgumentParser() 10 11 parser.add_argument( 12 "package", 13 type=str, 14 help=""" Scan the current working directory for project folders that use this package.""", 15 ) 16 17 parser.add_argument( 18 "-i", 19 "--ignore", 20 nargs="*", 21 default=["pkgs", "envs"], 22 type=str, 23 help=""" Ignore these folders. """, 24 ) 25 args = parser.parse_args() 26 27 return args 28 29 30def find(root: Pathier, package: str, ignore: list[str] = []) -> list[str]: 31 """Find what sub-folders of `root`, excluding those in `ignore`, have files that use `package`.""" 32 package_users = [] 33 scan_fails = {} # Error message: [projects] 34 for project in root.iterdir(): 35 if project.is_dir() and project.stem not in ignore: 36 try: 37 if package in scan(project): 38 package_users.append(project.stem) 39 except Exception as e: 40 err = str(e) 41 if err not in scan_fails: 42 scan_fails[err] = [project] 43 else: 44 scan_fails[err].append(project) 45 print() 46 print("The following errors occured during the scan:") 47 for fail in scan_fails: 48 print(f"ERROR: {fail}:") 49 print(*scan_fails[fail], sep="\n") 50 print() 51 return package_users 52 53 54def main(args: argparse.Namespace = None): 55 if not args: 56 args = get_args() 57 package_users = find(Pathier.cwd(), args.package, args.ignore) 58 print(f"The following folders have files that use {args.package}:") 59 print(*package_users, sep="\n") 60 61 62if __name__ == "__main__": 63 main(get_args())
def
get_args() -> argparse.Namespace:
9def get_args() -> argparse.Namespace: 10 parser = argparse.ArgumentParser() 11 12 parser.add_argument( 13 "package", 14 type=str, 15 help=""" Scan the current working directory for project folders that use this package.""", 16 ) 17 18 parser.add_argument( 19 "-i", 20 "--ignore", 21 nargs="*", 22 default=["pkgs", "envs"], 23 type=str, 24 help=""" Ignore these folders. """, 25 ) 26 args = parser.parse_args() 27 28 return args
def
find( root: pathier.pathier.Pathier, package: str, ignore: list[str] = []) -> list[str]:
31def find(root: Pathier, package: str, ignore: list[str] = []) -> list[str]: 32 """Find what sub-folders of `root`, excluding those in `ignore`, have files that use `package`.""" 33 package_users = [] 34 scan_fails = {} # Error message: [projects] 35 for project in root.iterdir(): 36 if project.is_dir() and project.stem not in ignore: 37 try: 38 if package in scan(project): 39 package_users.append(project.stem) 40 except Exception as e: 41 err = str(e) 42 if err not in scan_fails: 43 scan_fails[err] = [project] 44 else: 45 scan_fails[err].append(project) 46 print() 47 print("The following errors occured during the scan:") 48 for fail in scan_fails: 49 print(f"ERROR: {fail}:") 50 print(*scan_fails[fail], sep="\n") 51 print() 52 return package_users
Find what sub-folders of root, excluding those in ignore, have files that use package.
def
main(args: argparse.Namespace = None):