packagelister.packagelister_cli API documentation

# packagelister.packagelister_cli

View Source

     1import argparse
     2from pathlib import Path
     3
     4from packagelister import scan
     5
     6
     7def main():
     8    def get_args() -> argparse.Namespace:
     9        parser = argparse.ArgumentParser()
    10
    11        parser.add_argument(
    12            "project_path",
    13            nargs="?",
    14            type=str,
    15            default=None,
    16            help=""" The project directory path to scan. """,
    17        )
    18
    19        parser.add_argument(
    20            "-s",
    21            "--show_files",
    22            action="store_true",
    23            help=""" Show which files imported each of the packages. """,
    24        )
    25
    26        parser.add_argument(
    27            "-g",
    28            "--generate_requirements",
    29            action="store_true",
    30            help=""" Generate a requirements.txt file in --project_path. """,
    31        )
    32
    33        parser.add_argument(
    34            "-i",
    35            "--include_builtins",
    36            action="store_true",
    37            help=""" Include built in standard library modules. """,
    38        )
    39
    40        args = parser.parse_args()
    41
    42        if not args.project_path:
    43            args.project_path = Path.cwd()
    44        else:
    45            args.project_path = Path(args.project_path)
    46        if not args.project_path.is_absolute():
    47            args.project_path = args.project_path.absolute()
    48
    49        return args
    50
    51    args = get_args()
    52    packages = scan(args.project_path, args.include_builtins)
    53    if args.generate_requirements:
    54        req_path = args.project_path / "requirements.txt"
    55        req_path.write_text(
    56            "\n".join(
    57                f"{package}~={packages[package]['version']}"
    58                if packages[package]["version"]
    59                else package
    60                for package in sorted(packages)
    61            )
    62        )
    63    packages = {
    64        f"{package}=={packages[package]['version']}": packages[package]["files"]
    65        for package in sorted(packages)
    66    }
    67
    68    if args.show_files:
    69        longest_key = max(len(package) for package in packages)
    70        packages = [
    71            f"{package}{' '*(longest_key-len(package)+4)}{', '.join(str(Path(file).relative_to(args.project_path)) for file in packages[package])}"
    72            for package in packages
    73        ]
    74
    75    print(f"Packages used in {args.project_path.stem}:")
    76    print(
    77        *packages,
    78        sep="\n",
    79    )
    80
    81
    82if __name__ == "__main__":
    83    main()

def main(): View Source

     8def main():
     9    def get_args() -> argparse.Namespace:
    10        parser = argparse.ArgumentParser()
    11
    12        parser.add_argument(
    13            "project_path",
    14            nargs="?",
    15            type=str,
    16            default=None,
    17            help=""" The project directory path to scan. """,
    18        )
    19
    20        parser.add_argument(
    21            "-s",
    22            "--show_files",
    23            action="store_true",
    24            help=""" Show which files imported each of the packages. """,
    25        )
    26
    27        parser.add_argument(
    28            "-g",
    29            "--generate_requirements",
    30            action="store_true",
    31            help=""" Generate a requirements.txt file in --project_path. """,
    32        )
    33
    34        parser.add_argument(
    35            "-i",
    36            "--include_builtins",
    37            action="store_true",
    38            help=""" Include built in standard library modules. """,
    39        )
    40
    41        args = parser.parse_args()
    42
    43        if not args.project_path:
    44            args.project_path = Path.cwd()
    45        else:
    46            args.project_path = Path(args.project_path)
    47        if not args.project_path.is_absolute():
    48            args.project_path = args.project_path.absolute()
    49
    50        return args
    51
    52    args = get_args()
    53    packages = scan(args.project_path, args.include_builtins)
    54    if args.generate_requirements:
    55        req_path = args.project_path / "requirements.txt"
    56        req_path.write_text(
    57            "\n".join(
    58                f"{package}~={packages[package]['version']}"
    59                if packages[package]["version"]
    60                else package
    61                for package in sorted(packages)
    62            )
    63        )
    64    packages = {
    65        f"{package}=={packages[package]['version']}": packages[package]["files"]
    66        for package in sorted(packages)
    67    }
    68
    69    if args.show_files:
    70        longest_key = max(len(package) for package in packages)
    71        packages = [
    72            f"{package}{' '*(longest_key-len(package)+4)}{', '.join(str(Path(file).relative_to(args.project_path)) for file in packages[package])}"
    73            for package in packages
    74        ]
    75
    76    print(f"Packages used in {args.project_path.stem}:")
    77    print(
    78        *packages,
    79        sep="\n",
    80    )