packagelister.packagelister_cli

 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            "-v",
35            "--versions",
36            type=str,
37            default=None,
38            choices=["==", "<", "<=", ">", ">=", "~="],
39            help=""" When generating a requirements.txt file,
40            include the versions of the packages using this
41            relation.""",
42        )
43
44        parser.add_argument(
45            "-i",
46            "--include_builtins",
47            action="store_true",
48            help=""" Include built in standard library modules. """,
49        )
50
51        args = parser.parse_args()
52
53        if not args.project_path:
54            args.project_path = Path.cwd()
55        else:
56            args.project_path = Path(args.project_path)
57        if not args.project_path.is_absolute():
58            args.project_path = args.project_path.absolute()
59
60        return args
61
62    args = get_args()
63    packages = scan(args.project_path, args.include_builtins)
64    if args.generate_requirements:
65        req_path = args.project_path / "requirements.txt"
66        req_path.write_text(
67            "\n".join(
68                f"{package}{args.versions}{packages[package]['version']}"
69                if args.versions
70                else f"{package}"
71                if packages[package]["version"]
72                else package
73                for package in sorted(packages)
74            )
75        )
76    packages = {
77        f"{package}=={packages[package]['version']}": packages[package]["files"]
78        for package in sorted(packages)
79    }
80
81    if args.show_files:
82        longest_key = max(len(package) for package in packages)
83        packages = [
84            f"{package}{' '*(longest_key-len(package)+4)}{', '.join(str(Path(file).relative_to(args.project_path)) for file in packages[package])}"
85            for package in packages
86        ]
87
88    print(f"Packages used in {args.project_path.stem}:")
89    print(
90        *packages,
91        sep="\n",
92    )
93
94
95if __name__ == "__main__":
96    main()
def main():
 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            "-v",
36            "--versions",
37            type=str,
38            default=None,
39            choices=["==", "<", "<=", ">", ">=", "~="],
40            help=""" When generating a requirements.txt file,
41            include the versions of the packages using this
42            relation.""",
43        )
44
45        parser.add_argument(
46            "-i",
47            "--include_builtins",
48            action="store_true",
49            help=""" Include built in standard library modules. """,
50        )
51
52        args = parser.parse_args()
53
54        if not args.project_path:
55            args.project_path = Path.cwd()
56        else:
57            args.project_path = Path(args.project_path)
58        if not args.project_path.is_absolute():
59            args.project_path = args.project_path.absolute()
60
61        return args
62
63    args = get_args()
64    packages = scan(args.project_path, args.include_builtins)
65    if args.generate_requirements:
66        req_path = args.project_path / "requirements.txt"
67        req_path.write_text(
68            "\n".join(
69                f"{package}{args.versions}{packages[package]['version']}"
70                if args.versions
71                else f"{package}"
72                if packages[package]["version"]
73                else package
74                for package in sorted(packages)
75            )
76        )
77    packages = {
78        f"{package}=={packages[package]['version']}": packages[package]["files"]
79        for package in sorted(packages)
80    }
81
82    if args.show_files:
83        longest_key = max(len(package) for package in packages)
84        packages = [
85            f"{package}{' '*(longest_key-len(package)+4)}{', '.join(str(Path(file).relative_to(args.project_path)) for file in packages[package])}"
86            for package in packages
87        ]
88
89    print(f"Packages used in {args.project_path.stem}:")
90    print(
91        *packages,
92        sep="\n",
93    )