-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpush_env_packages.py
151 lines (133 loc) · 5.03 KB
/
push_env_packages.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
from __future__ import print_function
from subprocess import Popen, PIPE
import os
import sys
import shlex
import logging
import yaml
import click
from plumbum import local
from plumbum import TEE
from plumbum import BG
from plumbum.commands import ProcessExecutionError
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
def call_binary(binary_name, arguments=None, silent=False, conda_env=None):
'''Uses plumbum to make a call to a CLI binary. The arguments should be passed as a list of strings'''
RETURN_CODE = 0
STDOUT = 1
STDERR = 2
logger.debug("binary_name: %s", binary_name)
logger.debug("arguments: %s", arguments)
if conda_env is not None:
if arguments is not None:
arguments = [conda_env, binary_name] + arguments
else:
arguments = [conda_env, binary_name]
binary_name = os.path.join(os.path.dirname(__file__), "run_in_env.sh")
try:
command = local[binary_name]
except ProcessExecutionError:
logger.error("Could not find %s executable", binary_name)
raise
for var in os.environ:
local.env[var] = os.environ[var]
if silent:
if arguments is not None:
cmd_future = command.__getitem__(arguments) & BG
else:
cmd_future = command.run_bg()
cmd_future.wait()
output = [cmd_future.returncode, cmd_future.stdout, cmd_future.stderr]
else:
if arguments is not None:
output = command.__getitem__(arguments) & TEE
else:
output = command.run_tee()
#Check for non-zero return code
if output[RETURN_CODE] != 0:
logger.error("Error occurred when executing %s %s", binary_name, " ".join(arguments))
logger.error("STDERR: %s", output[STDERR])
raise ProcessExecutionError
else:
print("output:", output)
print("output type:", type(output))
return output[STDOUT]
def check_anaconda_login():
login_info = call_binary("anaconda", ["whoami"])
print("login_info:", login_info)
@click.command()
## TODO: add --from-channel option
# TODO: add --to-channel option
@click.option('--env', default=None, help='Name of the conda environment for which to upload the packages it contains')
def main(env):
if env is None:
env_file_name = input("Enter the name of the conda environment file to parse:")
else:
env_file_name = "{}_env.yml".format(env)
with open(os.path.join(os.path.dirname(__file__), env_file_name), 'r') as config_file:
config = yaml.load(config_file)
# print("config:", config)
pip_dependencies = config["dependencies"][-1]
print("pip_dependencies:", pip_dependencies)
dependencies = config["dependencies"][:-1]
print("dependencies:", dependencies)
if sys.platform == "darwin":
conda_os = "osx-64"
else:
conda_os = "linux-64"
# sys.path.append("/anaconda2/bin")
conda_pkgs = os.path.abspath(os.path.join(os.environ.get("CONDA_EXE"),"..","..","pkgs"))
print("conda_pkgs:", conda_pkgs)
check_anaconda_login()
# Get list of package we are using
call_binary("python", ["--version"])
pkgs = call_binary("conda", ["list", "-n", env_file_name])
# print("test:", test)
# pkgs, err = run_cmd("conda list", verbose=True)
failed = []
success = []
# print("pkgs:", pkgs)
# for l in pkgs.decode("utf8").split("\n")[2:-1]:
print("Uploading packages for environment: {}".format(env_file_name))
for dependency in dependencies:
# print("l:", l)
# sp = l.split()
# name = sp[0]
# # print("name:", name)
# version = sp[1]
# build = sp[2]
name, version = dependency.split("=")
print("name:", name)
print("version:", version)
# sys.exit(1)
if name == "#":
continue
# tarname = "{}-{}-{}.tar.bz2".format(name,version,build)
resource_location = "conda-forge/{}/{}".format(name, version)
print("Copying {} version {}".format(name, version))
try:
output = call_binary("anaconda", ["copy", resource_location, "--to-owner", "esgf"])
success.append((name, version))
except ProcessExecutionError:
failed.append((name, version))
print("Successfully copied the following packages to the esgf conda channel")
for package in success:
print(package)
print("The following packages failed to be copied.")
for package in failed:
print(package)
# print("tarname:", tarname)
# tarball = os.path.join(conda_pkgs,tarname)
# print("looking at:",tarball,os.path.exists(tarball))
# if os.path.exists(tarball):
# o,e = run_cmd("anaconda upload {} -u esgf".format(tarball))
# print("OUT:",o.decode("utf8"))
# print("Err:",e.decode("utf8"))
# else:
# missing.append(tarball)
# print(sys.prefix)
# print(conda_pkgs)
# print("Error on:",missing)
if __name__ == '__main__':
main()