packagelister.whouses
1import argparse 2 3from pathier import Pathier 4from printbuddies import ProgBar 5 6from packagelister import packagelister 7 8 9def get_args() -> argparse.Namespace: 10 parser = argparse.ArgumentParser( 11 prog="whouses", 12 description=""" Determine what sub-folders in the current directory use the specified package. 13 Useful for knowing which projects need to be updated when upgrading an installed package.""", 14 ) 15 16 parser.add_argument( 17 "package", 18 type=str, 19 help=""" Scan the current working directory for project folders that use this package.""", 20 ) 21 22 parser.add_argument( 23 "-i", 24 "--ignore", 25 nargs="*", 26 default=[], 27 type=str, 28 help=""" Ignore these folders. """, 29 ) 30 args = parser.parse_args() 31 32 return args 33 34 35def find(root: Pathier, package: str, ignore: list[str] = []) -> list[str]: 36 """Find what sub-folders of `root`, excluding those in `ignore`, have files that use `package`.""" 37 package_users = [] 38 scan_fails = {} # Error message: [projects] 39 projects = [ 40 path for path in root.iterdir() if path.is_dir() and path.stem not in ignore 41 ] 42 num_projects = len(projects) 43 with ProgBar(num_projects, width_ratio=0.3) as bar: 44 for project in projects: 45 try: 46 if package in packagelister.scan_dir(project, True).packages.names: 47 package_users.append(project.stem) 48 except Exception as e: 49 err = str(e) 50 if err not in scan_fails: 51 scan_fails[err] = [project] 52 else: 53 scan_fails[err].append(project) 54 bar.display(suffix=f"Scanning {project.stem}...") 55 print() 56 if scan_fails: 57 print("The following errors occured during the scan:") 58 for fail in scan_fails: 59 print(f"ERROR: {fail}:") 60 print(*scan_fails[fail], sep="\n") 61 print() 62 return package_users 63 64 65def main(args: argparse.Namespace | None = None): 66 if not args: 67 args = get_args() 68 package_users = find( 69 Pathier.cwd(), args.package, [".pytest_cache", "__pycache__"] + args.ignore 70 ) 71 print(f"The following folders have files that use `{args.package}`:") 72 print(*package_users, sep="\n") 73 74 75if __name__ == "__main__": 76 main(get_args())
def
get_args() -> argparse.Namespace:
10def get_args() -> argparse.Namespace: 11 parser = argparse.ArgumentParser( 12 prog="whouses", 13 description=""" Determine what sub-folders in the current directory use the specified package. 14 Useful for knowing which projects need to be updated when upgrading an installed package.""", 15 ) 16 17 parser.add_argument( 18 "package", 19 type=str, 20 help=""" Scan the current working directory for project folders that use this package.""", 21 ) 22 23 parser.add_argument( 24 "-i", 25 "--ignore", 26 nargs="*", 27 default=[], 28 type=str, 29 help=""" Ignore these folders. """, 30 ) 31 args = parser.parse_args() 32 33 return args
def
find( root: pathier.pathier.Pathier, package: str, ignore: list[str] = []) -> list[str]:
36def find(root: Pathier, package: str, ignore: list[str] = []) -> list[str]: 37 """Find what sub-folders of `root`, excluding those in `ignore`, have files that use `package`.""" 38 package_users = [] 39 scan_fails = {} # Error message: [projects] 40 projects = [ 41 path for path in root.iterdir() if path.is_dir() and path.stem not in ignore 42 ] 43 num_projects = len(projects) 44 with ProgBar(num_projects, width_ratio=0.3) as bar: 45 for project in projects: 46 try: 47 if package in packagelister.scan_dir(project, True).packages.names: 48 package_users.append(project.stem) 49 except Exception as e: 50 err = str(e) 51 if err not in scan_fails: 52 scan_fails[err] = [project] 53 else: 54 scan_fails[err].append(project) 55 bar.display(suffix=f"Scanning {project.stem}...") 56 print() 57 if scan_fails: 58 print("The following errors occured during the scan:") 59 for fail in scan_fails: 60 print(f"ERROR: {fail}:") 61 print(*scan_fails[fail], sep="\n") 62 print() 63 return package_users
Find what sub-folders of root
, excluding those in ignore
, have files that use package
.
def
main(args: argparse.Namespace | None = None):
66def main(args: argparse.Namespace | None = None): 67 if not args: 68 args = get_args() 69 package_users = find( 70 Pathier.cwd(), args.package, [".pytest_cache", "__pycache__"] + args.ignore 71 ) 72 print(f"The following folders have files that use `{args.package}`:") 73 print(*package_users, sep="\n")