-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_create_environments.py
80 lines (67 loc) · 3.47 KB
/
02_create_environments.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
import os
import subprocess
import json
# Define the directory where repositories are cloned
repositories_dir = "projects" # Replace with the actual directory
data_dir = "dataset" # Replace with the actual directory
# Function to create a virtual environment and install packages
def create_venv_and_install_requirements(repo_dir):
venv_dir = os.path.join(repo_dir, "venv")
requirements_file = os.path.join(repo_dir, "requirements.txt")
if not os.path.exists(venv_dir):
# Create a virtual environment
try:
subprocess.run(["python", "-m", "venv", venv_dir], check=True)
print(f"Successfully created venv for {repo_dir}")
except subprocess.CalledProcessError as e:
print(f"Error while creating venv for {repo_dir}: {e}")
raise e
if os.path.exists(requirements_file):
# Activate the virtual environment
activate_script = os.path.join(venv_dir, "bin", "activate")
activate_cmd = f". {activate_script}" if os.name != "nt" else f"{activate_script}"
try:
subprocess.run(activate_cmd, shell=True, check=True)
print(f"Activated venv for {repo_dir}")
# print current active environment name
subprocess.run("echo $VIRTUAL_ENV", shell=True, check=True)
except subprocess.CalledProcessError as e:
print(f"Error while activating venv for {repo_dir}: {e}")
raise e
# Install packages using pip
try:
print("__"*50)
subprocess.run(["pip", "install", "-r", requirements_file], check=True)
print(f"Successfully installed requirements for {repo_dir}")
except subprocess.CalledProcessError as e:
print(f"Error while installing requirements for {repo_dir}: {e}")
raise e
# Deactivate the virtual environment
subprocess.run("deactivate", shell=True, check=True)
# Iterate through the cloned repositories and check if repo_name is a directory not file
# for repo_name in os.listdir(repositories_dir):
repo_name ="tensorflow_docs"
repo_dir = os.path.join(repositories_dir, repo_name)
# Check if repo_name is a directory not file
if os.path.isdir(repo_dir):
# Check if requirements.txt exists
requirements_file = os.path.join(repo_dir, "requirements.txt")
# add status key to git_metadata.json file
with open(os.path.join(data_dir, repo_name, "git_metadata.json"), "r") as git_metadata_file:
git_metadata = json.load(git_metadata_file)
if os.path.exists(requirements_file):
print(f"Setting up venv and installing requirements for {repo_name}")
try:
create_venv_and_install_requirements(repo_dir)
git_metadata["status"] = "venv_and_requirements_installed"
print(f"Successfully set up venv and installed requirements for {repo_name}")
except subprocess.CalledProcessError as e:
print(f"Error while setting up venv and installing requirements for {repo_name}: {e}")
git_metadata["status"] = "error_while_setting_up_venv_and_installing_requirements"
else:
print(f"No requirements.txt found for {repo_name}. You can install dependencies manually.")
git_metadata["status"] = "no_requirements_file"
# add git_metadata.json file to dataset folder
with open(os.path.join(data_dir, repo_name, "git_metadata.json"), "w") as git_metadata_file:
json.dump(git_metadata, git_metadata_file)
print("Setup completed.")