-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c95bee
commit c458c92
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import os | ||
import sys | ||
import subprocess | ||
|
||
def find_python_files(root_dir): | ||
"""Recursively find all Python files starting from the given directory.""" | ||
py_files = [] | ||
for root, _, files in os.walk(root_dir): | ||
py_files.extend([os.path.join(root, f) for f in files if f.endswith(".py")]) | ||
return py_files | ||
|
||
def extract_imported_packages(py_files): | ||
"""Extract a list of imported packages from Python files.""" | ||
imported_packages = set() | ||
for file in py_files: | ||
try: | ||
with open(file, "r", encoding="utf-8") as f: | ||
for line in f: | ||
# Detect `import` and `from ... import ...` statements | ||
if line.startswith("import ") or line.startswith("from "): | ||
package = line.split()[1].split(".")[0] | ||
imported_packages.add(package) | ||
except UnicodeDecodeError: | ||
print(f"Skipping file with encoding issue: {file}") | ||
return imported_packages | ||
|
||
def check_missing_packages(imported_packages): | ||
"""Check which imported packages are missing.""" | ||
missing_packages = [] | ||
for package in imported_packages: | ||
try: | ||
__import__(package) | ||
except ImportError: | ||
missing_packages.append(package) | ||
return missing_packages | ||
|
||
def write_requirements(imported_packages): | ||
"""Write all imported packages to requirements.txt.""" | ||
with open("requirements.txt", "w", encoding="utf-8") as req_file: | ||
req_file.write("\n".join(sorted(imported_packages))) | ||
print("All detected packages written to 'requirements.txt'.") | ||
|
||
def install_packages(missing_packages): | ||
"""Install missing packages using pip.""" | ||
for package in missing_packages: | ||
print(f"Installing {package}...") | ||
subprocess.check_call([sys.executable, "-m", "pip", "install", package]) | ||
print("All missing packages installed.") | ||
|
||
if __name__ == "__main__": | ||
# Root directory for your project | ||
root_directory = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
# Step 1: Find all Python files | ||
python_files = find_python_files(root_directory) | ||
|
||
# Step 2: Extract imported packages | ||
imported = extract_imported_packages(python_files) | ||
|
||
# Step 3: Check for missing packages | ||
missing = check_missing_packages(imported) | ||
|
||
# Step 4: Write all detected packages to requirements.txt | ||
write_requirements(imported) | ||
|
||
# Step 5: Install missing packages | ||
if missing: | ||
print(f"Missing packages detected: {missing}") | ||
install_packages(missing) | ||
else: | ||
print("No missing packages detected. All packages are installed.") |