-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequirementsBuilder.py
377 lines (311 loc) · 14.1 KB
/
requirementsBuilder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python3
"""
Script to generate requirements.txt by scanning Python files for imports.
Capabilities:
- Scans all Python files in the specified directory and its subdirectories.
- Detects non-standard imports, including those in conditional blocks and inside functions, using AST parsing.
- Checks installed package versions via pip to include version info in requirements.txt.
- Supports choosing the output location for the requirements file (absolute path if provided, or in the current directory).
- Offers a CLI interface and prompts whether to install packages via pip after generating the file.
- Recursively generates requirements.txt for subdirectories when enabled.
Limitations:
- Dynamic imports or imports generated at runtime may not be detected.
- Analysis is static and only covers patterns parsed by Python's AST.
- Only standard libraries available in Python 3.13 and below are used.
"""
import sys
import subprocess
import argparse
import ast
from pathlib import Path
from typing import Set, List, Optional
import fnmatch
# Configuration variables
STANDARD_LIBRARIES = {
'abc', 'aifc', 'argparse', 'array', 'ast', 'asyncio', 'base64', 'bdb',
'binascii', 'bisect', 'builtins', 'bz2', 'calendar', 'cgi', 'cgitb',
'chunk', 'cmath', 'cmd', 'code', 'codecs', 'codeop', 'collections',
'colorsys', 'compileall', 'concurrent', 'configparser', 'contextlib',
'contextvars', 'copy', 'copyreg', 'cProfile', 'crypt', 'csv', 'ctypes',
'curses', 'dataclasses', 'datetime', 'dbm', 'decimal', 'difflib', 'dis',
'distutils', 'doctest', 'email', 'encodings', 'ensurepip', 'enum',
'errno', 'faulthandler', 'fcntl', 'filecmp', 'fileinput', 'fnmatch',
'formatter', 'fractions', 'ftplib', 'functools', 'gc', 'getopt', 'getpass',
'gettext', 'glob', 'graphlib', 'grp', 'gzip', 'hashlib', 'heapq', 'hmac',
'html', 'http', 'idlelib', 'imaplib', 'imghdr', 'imp', 'importlib',
'inspect', 'io', 'ipaddress', 'itertools', 'json', 'keyword', 'lib2to3',
'linecache', 'locale', 'logging', 'lzma', 'macpath', 'mailbox', 'mailcap',
'marshal', 'math', 'mimetypes', 'mmap', 'modulefinder', 'msilib', 'msvcrt',
'multiprocessing', 'netrc', 'nis', 'nntplib', 'numbers', 'operator', 'optparse',
'os', 'ossaudiodev', 'parser', 'pathlib', 'pdb', 'pickle', 'pickletools',
'pipes', 'pkgutil', 'platform', 'plistlib', 'poplib', 'posix', 'pprint',
'profile', 'pstats', 'pty', 'pwd', 'py_compile', 'pyclbr', 'pydoc', 'queue',
'quopri', 'random', 're', 'readline', 'reprlib', 'resource', 'rlcompleter',
'runpy', 'sched', 'secrets', 'select', 'selectors', 'shelve', 'shlex', 'shutil',
'signal', 'site', 'smtpd', 'smtplib', 'sndhdr', 'socket', 'socketserver',
'spwd', 'sqlite3', 'ssl', 'stat', 'statistics', 'string', 'stringprep',
'struct', 'subprocess', 'sunau', 'symbol', 'symtable', 'sys', 'sysconfig',
'syslog', 'tabnanny', 'tarfile', 'telnetlib', 'tempfile', 'termios', 'test',
'textwrap', 'threading', 'time', 'timeit', 'tkinter', 'token', 'tokenize',
'trace', 'traceback', 'tracemalloc', 'tty', 'turtle', 'turtledemo', 'types',
'typing', 'unicodedata', 'unittest', 'urllib', 'uu', 'uuid', 'venv', 'warnings',
'wave', 'weakref', 'webbrowser', 'winreg', 'winsound', 'wsgiref', 'xdrlib',
'xml', 'xmlrpc', 'zipapp', 'zipfile', 'zipimport', 'zlib',
'setuptools', 'distutils', 'pytest', 'unittest', 'test', 'typing_extensions'
}
def get_installed_package_version(package_name: str) -> Optional[str]:
"""
Retrieves the installed version of a package using pip show command.
Args:
package_name (str): Name of the package to check.
Returns:
Optional[str]: Version of the package if installed, None otherwise.
"""
try:
result = subprocess.run(
[sys.executable, '-m', 'pip', 'show', package_name],
capture_output=True, text=True, check=True
)
for line in result.stdout.splitlines():
if line.startswith('Version:'):
return line.split(':', 1)[1].strip()
except subprocess.CalledProcessError:
return None
return None
def get_installed_package_location(package_name: str) -> Optional[str]:
"""
Retrieves the installation location of a package using pip show command.
Args:
package_name (str): Name of the package to locate.
Returns:
Optional[str]: Installation path of the package if installed, None otherwise.
"""
try:
result = subprocess.run(
[sys.executable, '-m', 'pip', 'show', package_name],
capture_output=True, text=True, check=True
)
for line in result.stdout.splitlines():
if line.startswith('Location:'):
return line.split(':', 1)[1].strip()
except subprocess.CalledProcessError:
return None
return None
def find_imports_in_file_ast(file_path: Path) -> Set[str]:
"""
Parses a Python file using Abstract Syntax Tree (AST) to find non-standard imported modules.
Detects imports in all scopes including conditional imports and within functions.
Skips standard library and relative imports.
Args:
file_path (Path): Path to the Python file to analyze.
Returns:
Set[str]: Set of non-standard imported module names.
Warns:
Prints a warning message if the file cannot be processed.
"""
third_party_imports = set()
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
tree = ast.parse(content, filename=str(file_path))
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
module = alias.name.split('.')[0]
if module and module not in STANDARD_LIBRARIES:
third_party_imports.add(module)
elif isinstance(node, ast.ImportFrom):
if node.module and not node.module.startswith('.'):
module = node.module.split('.')[0]
if module and module not in STANDARD_LIBRARIES:
third_party_imports.add(module)
except (SyntaxError, UnicodeDecodeError, IOError) as e:
print(f"Warning: Could not process {file_path}: {e}")
return third_party_imports
def load_gitignore(directory: Path) -> List[str]:
"""
Loads .gitignore file from the given directory and generates ignore patterns.
If .gitignore does not exist, prompts user to create one with .venv exclusion.
Always ensures '.venv' is included in ignore patterns.
Args:
directory (Path): Directory to search for .gitignore file.
Returns:
List[str]: List of ignore patterns, always including '.venv'.
Warns:
Prints warning messages if .gitignore cannot be read or created.
"""
gitignore_file = directory / '.gitignore'
patterns = ['.venv']
if not gitignore_file.exists():
response = input(".gitignore not found. Create one with '.venv' exclusion? (y/n): ").strip().lower()
if response in ['y', 'yes']:
try:
gitignore_file.write_text(".venv\n", encoding='utf-8')
print(f"Created .gitignore in {directory}")
except Exception as e:
print(f"Warning: Could not create .gitignore: {e}")
else:
try:
with open(gitignore_file, 'r', encoding='utf-8') as f:
patterns.extend(line.strip() for line in f if line.strip() and not line.startswith('#'))
except Exception as e:
print(f"Warning: Could not read .gitignore: {e}")
return patterns
def is_ignored(file_path: Path, ignore_patterns: List[str], base_dir: Path) -> bool:
"""
Checks if a file path matches any of the ignore patterns.
Determines if a file should be ignored based on its relative path and ignore patterns.
Args:
file_path (Path): Path to the file to check.
ignore_patterns (List[str]): List of patterns to ignore.
base_dir (Path): Base directory for relative path calculation.
Returns:
bool: True if the file should be ignored, False otherwise.
"""
try:
relative = file_path.relative_to(base_dir).as_posix()
except ValueError:
return False
if any(fnmatch.fnmatch(relative, pattern) for pattern in ignore_patterns):
return True
for part in file_path.parts:
if part in ignore_patterns:
return True
return False
def find_python_files(directory: Path) -> List[Path]:
"""
Recursively finds all Python files in the given directory.
Excludes files and directories that match patterns in .gitignore.
Args:
directory (Path): Directory to search for Python files.
Returns:
List[Path]: List of Python file paths that are not ignored.
"""
ignore_patterns = load_gitignore(directory)
return [
f for f in directory.glob('**/*.py')
if not is_ignored(f, ignore_patterns, directory)
]
def generate_requirements(
directory: Path,
output_path: Optional[Path] = None,
show_location: bool = False,
interactive: bool = True,
recursive: bool = False
) -> Path:
"""
Generates a requirements.txt file by scanning Python files for non-standard imports.
Scans all Python files, finds third-party imports, checks their installed versions,
and optionally includes installation locations. Can process subdirectories recursively.
Args:
directory (Path): Directory to scan for Python files.
output_path (Optional[Path], optional): Path to save requirements.txt.
Defaults to 'requirements.txt' in the scan directory.
show_location (bool, optional): Whether to include package installation locations.
Defaults to False.
interactive (bool, optional): Whether to prompt for package installation.
Defaults to True.
recursive (bool, optional): Whether to process subdirectories.
Defaults to False.
Returns:
Path: Path to the generated requirements.txt file.
Warns:
Prints messages about file generation and potential issues.
Prompts:
Asks user whether to install the discovered requirements if interactive is True.
"""
if output_path is None:
output_path = directory / 'requirements.txt'
python_files = find_python_files(directory)
all_imports = set()
print(f"Scanning {len(python_files)} Python files in {directory}...")
for file_path in python_files:
all_imports.update(find_imports_in_file_ast(file_path))
sorted_imports = sorted(all_imports)
if not sorted_imports:
print(f"No third-party imports found in {directory}. Skipping requirements.txt.")
if output_path.exists():
try:
output_path.unlink()
print(f"Removed empty {output_path}")
except Exception as e:
print(f"Warning: Could not remove {output_path}: {e}")
return output_path
with open(output_path, 'w', encoding='utf-8') as f:
for module in sorted_imports:
version = get_installed_package_version(module)
line = f"{module}=={version}" if version else module
f.write(line + "\n")
if show_location:
location = get_installed_package_location(module)
if location:
f.write(f"# {location}\n")
print(f"Generated {output_path}")
if interactive and sorted_imports:
while True:
choice = input("Install requirements now? (y/n): ").lower()
if choice in ['y', 'yes']:
install_requirements(output_path)
break
elif choice in ['n', 'no']:
break
if recursive:
ignore_patterns = load_gitignore(directory)
for subdir in [d for d in directory.iterdir() if d.is_dir()]:
if not is_ignored(subdir, ignore_patterns, directory):
generate_requirements(
subdir, subdir / 'requirements.txt',
show_location, interactive=False, recursive=True
)
return output_path
def install_requirements(requirements_path: Path) -> None:
"""
Installs packages from the generated requirements file using pip.
Args:
requirements_path (Path): Path to the requirements.txt file.
Warns:
Prints error message if package installation fails.
"""
try:
subprocess.run(
[sys.executable, '-m', 'pip', 'install', '-r', str(requirements_path)],
check=True
)
print("Requirements installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error installing requirements: {e}")
def main() -> int:
"""
Main function to parse CLI arguments and run the requirements generation process.
Handles command-line arguments for directory scanning, output path,
location display, and recursive processing.
Returns:
int: Exit code (0 for success, 1 for directory error, 130 for keyboard interrupt).
Warns:
Prints error message for invalid directory.
Raises:
KeyboardInterrupt: Handled to provide a clean exit on user interruption.
"""
try:
parser = argparse.ArgumentParser(description="Generate requirements.txt from Python imports")
parser.add_argument('-d', '--dir', default='.', help="Directory to scan")
parser.add_argument('-o', '--output', help="Output file path")
parser.add_argument('-l', '--show-location', action='store_true', help="Show package locations")
parser.add_argument('-r', '--recursive', action='store_true', help="Process subdirectories recursively")
args = parser.parse_args()
scan_dir = Path(args.dir).resolve()
if not scan_dir.is_dir():
print(f"Error: {scan_dir} is not a valid directory")
return 1
generate_requirements(
scan_dir,
Path(args.output) if args.output else None,
args.show_location,
recursive=args.recursive
)
return 0
except KeyboardInterrupt:
print("\nOperation cancelled by user")
return 130
if __name__ == '__main__':
sys.exit(main())