-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
204 lines (168 loc) · 6.66 KB
/
main.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
import re
import subprocess
import requests
import zipfile
def unzip_starter_project_repo():
"""
Unzip a starter project repository.
Returns:
None
"""
# Get the name of the zip file from the user
zip_file_name = input("Enter the name of the zip file: ")
# Create a ZipFile object
with zipfile.ZipFile(zip_file_name, 'r') as zip_ref:
# Extract all the contents of the zip file in the current directory
zip_ref.extractall()
zip_file_name = zip_file_name[:-4]
return zip_file_name
def parse_requirements(pathtorequirements):
requirements = []
with open(f'{pathtorequirements}/requirements.txt', 'r') as file:
for line in file:
line = line.strip()
if line and not line.startswith('#'): # ignore empty lines and comments
match = re.match(r'([a-zA-Z0-9_.-]+\[?[a-zA-Z0-9_.-]*\]?)([<=>]+)?([\d.a-zA-Z-]*)?', line)
if match:
module, version_sign, version = match.groups()
requirements.append((module, version_sign, version))
else:
# handle packages without version specification
requirements.append((line, None, None))
return requirements
def get_envs():
result = subprocess.run(['conda', 'env', 'list'], stdout=subprocess.PIPE)
envs = result.stdout.decode('utf-8').splitlines()
env_names = [line.split()[0] for line in envs if not line.startswith('#') and line.split()]
return env_names
def get_channels():
result = subprocess.run(['conda', 'config', '--show', 'channels'], stdout=subprocess.PIPE)
# Decode the output to a string and split into lines
channels_output = result.stdout.decode('utf-8').splitlines()
# Initialize an empty list to hold channel names
channels_names = []
# Iterate through each line in the output
for line in channels_output:
# Check if the line starts with " - " which indicates a channel name
if line.strip().startswith('-'):
# Split the line by spaces and take the last element as the channel name
channel_name = line.split()[-1]
# Append the channel name to the list
channels_names.append(channel_name)
return channels_names
def get_package_versions(env, package):
result = subprocess.run(['conda', 'list', '-n', env, package], stdout=subprocess.PIPE)
versions = result.stdout.decode('utf-8')
return versions
def save_versions(package):
envs = get_envs()
with open('conda_versions_all_envs.txt', 'w') as f:
for env in envs:
f.write(f'Environment: {env}\n')
f.write(get_package_versions(env, package))
f.write('\n')
def create_conda_env(env_name):
"""
Create a new conda environment.
Parameters:
env_name (str): The name of the conda environment.
Returns:
None
"""
# Create a new conda environment
subprocess.run(["conda", "create", "--name", env_name, "--yes"], check=True)
def search_pypi(package, package_sign, package_version):
"""
Search for a package on PyPI.
Parameters:
package (str): The name of the package.
Returns:
bool: True if the package was found, False otherwise.
"""
response = requests.get(f"https://pypi.org/pypi/requests/json")
print(response)
if "releases" in response:
# Return the list of keys in the "releases" dictionary, which are the version numbers
return list(response["releases"].keys())
else:
# If "releases" key is not found, return an empty list
return []
"""
data = response.json()
if data.get("message"):
if data["message"] != "Not Found":
return True
else:
return True
return False
"""
def search_package(package, package_sign, package_version, channels_list):
"""
Search for a package in Conda Forge, Anaconda, and PIP repositories.
Parameters:
package (str): The name of the package.
Returns:
str: The repository where the package was found, or None if it wasn't found.
"""
# Loop through the channels
for channel in channels_list:
if channel == "defaults":
channel = "anaconda"
result = subprocess.run(["conda", "search", "-c", channel, package, package_sign, package_version], stdout=subprocess.PIPE)
if package in result.stdout.decode():
return channel
# Search in PIP
if search_pypi(package, package_sign, package_version):
return "pip"
return None
"""
# Separate packages into Conda and pip lists
conda_packages = []
pip_packages = []
for package in packages:
repo = search_package(package)
if repo is None:
print(f"Package {package} not found in any repository.")
continue
if repo == "pip":
pip_packages.append(package)
else:
conda_packages.append(package)
# Install Conda packages
for package in conda_packages:
subprocess.run(["conda", "install", "--yes", "-c", "conda-forge", package], check=True)
# Install pip packages
for package in pip_packages:
subprocess.run(["pip", "install", package], check=True)
"""
if __name__ == "__main__":
#project_folder_name = unzip_starter_project_repo()
#requirements = parse_requirements(project_folder_name)
#channels_list = get_channels()
#print(channels_list)
#search_pypi(package, package_sign, package_version)
releases_list = search_pypi("requests", "==", "2.31.0")
print(releases_list)
"""
env_list = get_envs()
while True:
env_name = input("Enter Environment Name: ")
if env_name not in env_list:
create_conda_env(str(env_name))
print(f"Environment '{env_name}' has been created.")
break # Exit the loop
else:
print(f"Environment '{env_name}' already exists. Please enter a different environment name.")
"""
"""
for requirement in requirements:
package, package_sign, package_version = requirement
repo = search_package(package, package_sign, package_version)
if repo is None:
print(f"Package {package} not found in any repository.")
continue
if repo == "pip":
subprocess.run(["pip", "install", package], check=True)
else:
subprocess.run(["conda", "install", "--yes", "-c", "conda-forge", package], check=True)
"""