packagelister.packagelister_cli API documentation

# packagelister.packagelister_cli

View Source

     1import argparse
     2
     3from pathier import Pathier
     4
     5from packagelister import packagelister
     6
     7
     8def get_args() -> argparse.Namespace:
     9    parser = argparse.ArgumentParser(
    10        prog="packagelister",
    11        description=""" Scan the current directory for imported packages. """,
    12    )
    13
    14    parser.add_argument(
    15        "-f",
    16        "--files",
    17        action="store_true",
    18        help=""" Show which files imported each of the packages. """,
    19    )
    20
    21    parser.add_argument(
    22        "-g",
    23        "--generate_requirements",
    24        action="store_true",
    25        help=""" Generate a requirements.txt file in the current directory. """,
    26    )
    27
    28    parser.add_argument(
    29        "-v",
    30        "--versions",
    31        type=str,
    32        default=None,
    33        choices=["==", "<", "<=", ">", ">=", "~="],
    34        help=""" When generating a requirements.txt file, include the versions of the packages using this relation.
    35            (You may need to put quotes around some of the options.)""",
    36    )
    37
    38    parser.add_argument(
    39        "-b",
    40        "--builtins",
    41        action="store_true",
    42        help=""" Include built in standard library modules in terminal display. """,
    43    )
    44
    45    parser.add_argument(
    46        "-d",
    47        "--debug",
    48        action="store_true",
    49        help=""" Print the Package objects found during the scan. """,
    50    )
    51
    52    args = parser.parse_args()
    53
    54    return args
    55
    56
    57def main(args: argparse.Namespace | None = None):
    58    if not args:
    59        args = get_args()
    60    project = packagelister.scan_dir(Pathier.cwd())
    61    print(f"Packages imported by {Pathier.cwd().stem}:")
    62    print(
    63        *(
    64            project.get_formatted_requirements(" v")
    65            + ([] if not args.builtins else project.packages.builtin.names)
    66        ),
    67        sep="\n",
    68    )
    69    if args.generate_requirements:
    70        print("Generating `requirements.txt`.")
    71        (Pathier.cwd() / "requirements.txt").join(
    72            project.get_formatted_requirements(args.versions)
    73        )
    74    if args.files:
    75        print("Files importing each package:")
    76        files_by_package = project.get_files_by_package()
    77        if not args.builtins:
    78            files_by_package = {
    79                k: v
    80                for k, v in files_by_package.items()
    81                if k in project.packages.third_party.names
    82            }
    83        for package, files in files_by_package.items():
    84            print(f"{package}:")
    85            print(*[f"  {file}" for file in files], sep="\n")
    86    if args.debug:
    87        print(*project.packages, sep="\n")
    88
    89
    90if __name__ == "__main__":
    91    main(get_args())

def get_args() -> argparse.Namespace: View Source

     9def get_args() -> argparse.Namespace:
    10    parser = argparse.ArgumentParser(
    11        prog="packagelister",
    12        description=""" Scan the current directory for imported packages. """,
    13    )
    14
    15    parser.add_argument(
    16        "-f",
    17        "--files",
    18        action="store_true",
    19        help=""" Show which files imported each of the packages. """,
    20    )
    21
    22    parser.add_argument(
    23        "-g",
    24        "--generate_requirements",
    25        action="store_true",
    26        help=""" Generate a requirements.txt file in the current directory. """,
    27    )
    28
    29    parser.add_argument(
    30        "-v",
    31        "--versions",
    32        type=str,
    33        default=None,
    34        choices=["==", "<", "<=", ">", ">=", "~="],
    35        help=""" When generating a requirements.txt file, include the versions of the packages using this relation.
    36            (You may need to put quotes around some of the options.)""",
    37    )
    38
    39    parser.add_argument(
    40        "-b",
    41        "--builtins",
    42        action="store_true",
    43        help=""" Include built in standard library modules in terminal display. """,
    44    )
    45
    46    parser.add_argument(
    47        "-d",
    48        "--debug",
    49        action="store_true",
    50        help=""" Print the Package objects found during the scan. """,
    51    )
    52
    53    args = parser.parse_args()
    54
    55    return args

def main(args: argparse.Namespace | None = None): View Source

    58def main(args: argparse.Namespace | None = None):
    59    if not args:
    60        args = get_args()
    61    project = packagelister.scan_dir(Pathier.cwd())
    62    print(f"Packages imported by {Pathier.cwd().stem}:")
    63    print(
    64        *(
    65            project.get_formatted_requirements(" v")
    66            + ([] if not args.builtins else project.packages.builtin.names)
    67        ),
    68        sep="\n",
    69    )
    70    if args.generate_requirements:
    71        print("Generating `requirements.txt`.")
    72        (Pathier.cwd() / "requirements.txt").join(
    73            project.get_formatted_requirements(args.versions)
    74        )
    75    if args.files:
    76        print("Files importing each package:")
    77        files_by_package = project.get_files_by_package()
    78        if not args.builtins:
    79            files_by_package = {
    80                k: v
    81                for k, v in files_by_package.items()
    82                if k in project.packages.third_party.names
    83            }
    84        for package, files in files_by_package.items():
    85            print(f"{package}:")
    86            print(*[f"  {file}" for file in files], sep="\n")
    87    if args.debug:
    88        print(*project.packages, sep="\n")