easyreqtxt 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
easyreqtxt/__init__.py ADDED
File without changes
easyreqtxt/main.py ADDED
@@ -0,0 +1,124 @@
1
+ import importlib.metadata
2
+ import ast
3
+ import logging
4
+ import sys
5
+ from pathlib import Path
6
+ import argparse
7
+
8
+ package_map = importlib.metadata.packages_distributions()
9
+
10
+ logging.basicConfig(
11
+ level=logging.INFO,
12
+ format="%(levelname)s: %(message)s"
13
+ )
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ modules = []
19
+
20
+
21
+ def parse_args():
22
+ parser = argparse.ArgumentParser(
23
+ description="Scan a folder for Python imports and resolve their versions, and output them to a requirements.txt"
24
+ )
25
+ parser.add_argument(
26
+ "folder",
27
+ type=str,
28
+ help="Path to the folder to scan."
29
+ )
30
+ parser.add_argument(
31
+ "-o", "--output",
32
+ type=str,
33
+ default="requirements.txt",
34
+ help="Output filename (default: requirements.txt)"
35
+ )
36
+ return parser.parse_args()
37
+
38
+
39
+
40
+ def walkfile(filepath: Path):
41
+ filename = filepath.name
42
+ module_names = []
43
+ logger.info(f"Beginning walk on {filename}")
44
+ with open(filepath, 'r',encoding='utf-8') as f:
45
+ source = f.read()
46
+ tree = ast.parse(source)
47
+ walk = (ast.walk(tree))
48
+ for node in walk:
49
+ if isinstance(node, ast.Import):
50
+ for alias in node.names:
51
+ nm = alias.name.split(".")[0]
52
+ if nm in module_names:
53
+ continue
54
+ module_names.append(nm)
55
+ logger.info(f"Found {nm} in {filename}")
56
+ else:
57
+ if isinstance(node, ast.ImportFrom):
58
+ name = node.module.split(".")[0]
59
+ if name in module_names:
60
+ continue
61
+ module_names.append(name)
62
+ logger.info(f"Found {name} in {filename}")
63
+
64
+ logger.info(f"Finished dependency walk on {filename}!")
65
+ logger.info(f"Beginning dist name corrections")
66
+ for module in module_names:
67
+ if module in sys.stdlib_module_names:
68
+ logger.info(f"Skipping baselib module: {module}")
69
+ continue
70
+ if module in package_map:
71
+ distnames = package_map[module]
72
+ if len(distnames) > 1:
73
+ logger.warning(f"Module {module} maps to multiple packages. I shall use the first one.")
74
+ distname = distnames[0]
75
+ logger.debug(f"{module} -> {distname}")
76
+ if any(distname in m for m in modules):
77
+ logger.info(f"Skipping repeated module: {distname}")
78
+ continue
79
+ try:
80
+ version = importlib.metadata.version(distname)
81
+ except importlib.metadata.PackageNotFoundError as e:
82
+ logging.warning(f"Unable to obtain a version for module {module}. Reason: {e}")
83
+ version = None
84
+
85
+ modules.append({distname: version})
86
+ else:
87
+ if any(module in m for m in modules):
88
+ logger.info(f"Skipping repeated module: {module}")
89
+ continue
90
+ try:
91
+ version = importlib.metadata.version(module)
92
+ except importlib.metadata.PackageNotFoundError as e:
93
+ logging.warning(f"Unable to obtain a version for module {module}. Reason: {e}. Version number will equal None. This can happen if the module isn't installed.")
94
+ version = None
95
+ modules.append({module: version})
96
+
97
+ logger.info(f"Walk of file {filename} finished!")
98
+
99
+
100
+
101
+ def output(filename: str):
102
+ with open(filename, 'w', encoding='utf-8') as f:
103
+ for m in modules:
104
+ for name, ver in m.items():
105
+ if ver == None:
106
+ string = f"{name}"
107
+ else:
108
+ string = f"{name}=={ver}"
109
+ f.write(string + "\n")
110
+ logger.info(f"Outputted depedency list at {filename}")
111
+
112
+
113
+ def main():
114
+ args = parse_args()
115
+ for f in Path(args.folder).rglob("*.py"):
116
+ walkfile(f)
117
+ output(args.output)
118
+
119
+
120
+ if __name__ == "__main__":
121
+ main()
122
+
123
+
124
+
@@ -0,0 +1,86 @@
1
+ Metadata-Version: 2.5
2
+ Name: easyreqtxt
3
+ Version: 0.1.0
4
+ Summary: Easily make requirements.txt files for entire python projects.
5
+ License-File: LICENSE
6
+ Requires-Python: >=3.14
7
+ Description-Content-Type: text/markdown
8
+
9
+ # easyreqtxt
10
+
11
+ A Python command-line tool that creates `requirements.txt` files by scanning a Python project for its imports.
12
+
13
+ ## Installation
14
+
15
+ Clone the repository:
16
+
17
+ ```bash
18
+ git clone https://github.com/cayrrs/easyreqtxt.git
19
+ cd easyreqtxt
20
+ ```
21
+
22
+ Install easyreqtxt:
23
+
24
+ ```bash
25
+ pip install -e .
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Provide the directory you want to scan:
31
+
32
+ ```bash
33
+ easyreqtxt <folder>
34
+ ```
35
+
36
+ For example:
37
+
38
+ ```bash
39
+ easyreqtxt .
40
+ ```
41
+
42
+ This recursively scans the directory for Python files and creates a `requirements.txt` file.
43
+
44
+ ### Custom Output File
45
+
46
+ Use `-o` or `--output` to specify a different output file:
47
+
48
+ ```bash
49
+ easyreqtxt . -o requirements-dev.txt
50
+ ```
51
+
52
+ ## How It Works
53
+
54
+ easyreqtxt:
55
+
56
+ 1. Recursively finds Python files in the specified directory.
57
+ 2. Finds imported modules using Python's AST parser.
58
+ 3. Ignores Python standard-library modules.
59
+ 4. Resolves imported modules to installed packages.
60
+ 5. Retrieves the installed package versions.
61
+ 6. Writes the dependencies to the output file.
62
+
63
+ For example, a project containing:
64
+
65
+ ```python
66
+ import requests
67
+ import numpy
68
+ import json
69
+ ```
70
+
71
+ could produce:
72
+
73
+ ```text
74
+ requests==2.32.5
75
+ numpy==2.3.3
76
+ ```
77
+
78
+ The `json` module is excluded because it is part of Python's standard library.
79
+
80
+ ## Requirements
81
+
82
+ Python 3.14 or newer.
83
+
84
+ ## License
85
+
86
+ This project is licensed under the Unlicense (do whatever you want) License. See the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,7 @@
1
+ easyreqtxt/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ easyreqtxt/main.py,sha256=2iLIDUSRuTTapoeJrQLxiOTlF0KbKzBShfiXdsr0w_E,3937
3
+ easyreqtxt-0.1.0.dist-info/METADATA,sha256=OlKEL6gvjOLeSRijrumkbuODGPBhlbBurk_umVhlvZM,1598
4
+ easyreqtxt-0.1.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
5
+ easyreqtxt-0.1.0.dist-info/entry_points.txt,sha256=IYRg6xWLEXFobcn7WJ8NMhi7WIbG3CXz2s3UQVB78es,52
6
+ easyreqtxt-0.1.0.dist-info/licenses/LICENSE,sha256=awOCsWJ58m_2kBQwBUGWejVqZm6wuRtCL2hi9rfa0X4,1211
7
+ easyreqtxt-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ easyreqtxt = easyreqtxt.main:main
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>