-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconda_env_backup.pyw
58 lines (48 loc) · 1.97 KB
/
conda_env_backup.pyw
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
import os
import subprocess
import logging
from config_loader import read_config_byconfigparser
backup_directory = read_config_byconfigparser('PATH','backup_directory')
log_directory = read_config_byconfigparser('PATH','log_directory')
filename = os.path.basename(__file__)
sub_id = read_config_byconfigparser(filename,'sub_id')
backup_directory = os.path.join(backup_directory, sub_id)
if not os.path.exists(log_directory):
os.makedirs(log_directory)
log_file = os.path.join(log_directory, sub_id)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename=log_file
)
if not os.path.exists(backup_directory):
os.makedirs(backup_directory)
logging.info(f"create directory '{backup_directory}' for backup")
env_list = subprocess.check_output(
["conda", "env", "list"], universal_newlines=True)
env_list_effective = [line.split()
for line in env_list.splitlines()
if line.strip() and not line.startswith("#")]
if not env_list_effective:
logging.info("No conda environments found. Skipping backup.")
else:
for env_li in env_list_effective:
env_path = env_li[1]
if env_path == "*":
env_path = env_li[2]
if not env_li:
logging.warning(
"Empty environment path encountered. Skipping backup for this environment.")
continue
env_name = env_li[0]
backup_file = os.path.join(
backup_directory, f"{env_name}_environment.yml")
activate_env_command = f"conda activate {env_name}"
export_env_command = f"conda env export -n {env_name} > {backup_file}"
logging.info(f"conda env '{env_name}' has backup to '{backup_file}'")
try:
subprocess.run(activate_env_command, shell=True, check=True)
subprocess.run(export_env_command, shell=True, check=True)
except subprocess.CalledProcessError as e:
logging.error(f"Error: {e}")
logging.shutdown()