| 1 |
import yaml |
| 2 |
import subprocess |
| 3 |
|
| 4 |
# we use this script to format the dependencies for the pyproject.toml and recipe.yml files |
| 5 |
|
| 6 |
# Function to run a shell command and capture the output |
| 7 |
def run_command(command): |
| 8 |
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True) |
| 9 |
if result.returncode != 0: |
| 10 |
print(f"Error running command: {command}\n{result.stderr}") |
| 11 |
raise Exception(result.stderr) |
| 12 |
return result.stdout |
| 13 |
|
| 14 |
# Function to get the installed version of a package |
| 15 |
def get_installed_version(package_name): |
| 16 |
try: |
| 17 |
output = run_command(f"micromamba list ^{package_name}$") |
| 18 |
line = output.strip().split('\n')[4] # Skip the header lines |
| 19 |
parts = line.split() |
| 20 |
if len(parts) > 1: |
| 21 |
return parts[1] |
| 22 |
except Exception as e: |
| 23 |
print(f"Could not find installed version for {package_name}: {e}") |
| 24 |
return None |
| 25 |
|
| 26 |
def get_installed_version_pip(package_name): |
| 27 |
try: |
| 28 |
output = run_command(f"pip show {package_name}") |
| 29 |
line = output.strip().split('\n')[1] # Skip the first line |
| 30 |
parts = line.split() |
| 31 |
if len(parts) > 1: |
| 32 |
return parts[1] |
| 33 |
except Exception as e: |
| 34 |
print(f"Could not find installed version for {package_name}: {e}") |
| 35 |
return None |
| 36 |
|
| 37 |
# Function to parse the environment.yml file and extract package information with versions |
| 38 |
def parse_environment_file(env_file): |
| 39 |
with open(env_file, 'r') as f: |
| 40 |
env_data = yaml.safe_load(f) |
| 41 |
|
| 42 |
conda_packages = [] |
| 43 |
pip_packages = [] |
| 44 |
|
| 45 |
for dep in env_data['dependencies']: |
| 46 |
if isinstance(dep, str): |
| 47 |
package_name = dep.split('=')[0].lower() |
| 48 |
version = get_installed_version(package_name) |
| 49 |
print(package_name, version) |
| 50 |
if version: |
| 51 |
conda_packages.append(f"{package_name}=={version}") |
| 52 |
else: |
| 53 |
conda_packages.append(package_name) |
| 54 |
elif isinstance(dep, dict) and 'pip' in dep: |
| 55 |
for pip_dep in dep['pip']: |
| 56 |
package_name = pip_dep.split('==')[0] |
| 57 |
version = get_installed_version_pip(package_name) |
| 58 |
print(package_name, version) |
| 59 |
if version: |
| 60 |
pip_packages.append(f"{package_name}=={version}") |
| 61 |
else: |
| 62 |
pip_packages.append(pip_dep) |
| 63 |
|
| 64 |
return conda_packages, pip_packages |
| 65 |
|
| 66 |
# Function to generate the pyproject.toml |
| 67 |
def generate_deps_list(conda_packages, pip_packages): |
| 68 |
dependencies = conda_packages + pip_packages |
| 69 |
|
| 70 |
deps_toml = [f"\"{d}\"" for d in dependencies] |
| 71 |
deps_recipe = [" ==".join(d.split("==")) for d in dependencies] |
| 72 |
|
| 73 |
|
| 74 |
# print(dependencies) |
| 75 |
file_toml = "./deps_toml.txt" |
| 76 |
file_recipe = "./deps_recipe.txt" |
| 77 |
with open(file_toml, 'w') as f: |
| 78 |
f.write(", ".join(deps_toml)) |
| 79 |
with open(file_recipe, 'w') as f: |
| 80 |
f.write("\n".join([f"- {d}" for d in deps_recipe])) |
| 81 |
|
| 82 |
print(f"Dependency files {file_toml} and {file_recipe} have been generated.") |
| 83 |
|
| 84 |
if __name__ == "__main__": |
| 85 |
original_env_file = 'environment.yaml' |
| 86 |
# original_env_file = 'environment-cf.yaml' |
| 87 |
conda_packages, pip_packages = parse_environment_file(original_env_file) |
| 88 |
generate_deps_list(conda_packages, pip_packages) |