-
Notifications
You must be signed in to change notification settings - Fork 9
/
oras.py
184 lines (153 loc) · 6.95 KB
/
oras.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
import logging
import subprocess
from os import chdir
from pathlib import Path
from get_latest_conda_package import get_version_file
def get_latest_pkg(some_dir):
sort_key = lambda f: f.stat().st_mtime
directory = Path(some_dir)
files = directory.glob("*.bz2")
sorted_files = sorted(files, key=sort_key, reverse=True)
if len(sorted_files) > 0:
file = sorted_files[0]
else:
file = "empty"
return file
def getName_andTag(pkg):
logging.warning(f"Pkg is: <<{pkg}>>")
name, version, a_hash = pkg.rsplit("-", 2)
tag = version + "-" + a_hash
tag_resized = tag.rpartition(".tar")[0]
tag_resized = tag_resized.replace("_", "-")
return name, tag_resized
def split_name(data, host):
logging.warning(f"Data1!!! is: <<{data}>>")
sep = "/"
strData = str(data)
if "win" in host:
sep = "\\"
pkg = str(data).rsplit(sep, 1)[-1]
length = len(strData) - len(pkg)
path = strData[:length]
pkg_name, tag = getName_andTag(pkg)
origin = pkg
return pkg_name, tag, path, origin, pkg
def write_version(some_dict, data, host):
some_dir = data
logging.warning(f"Data is: <<{data}>>")
whole_path = get_latest_pkg(some_dir)
logging.warning(f"File is: {whole_path}")
if whole_path == "empty":
return some_dict
else:
pkg_name, _, _, _, _ = split_name(whole_path, host)
version = get_version_file(some_dir, pkg_name)[1]
if pkg_name in some_dict.keys():
old_ver = some_dict[pkg_name]
logging.warning(f"Comparing <<{old_ver}>> and <<{version}>>")
if version > old_ver:
some_dict[pkg_name] = version
else:
some_dict[pkg_name] = version
return some_dict
def install_on_OS(sys):
logging.warning(f"Installing oras on {sys}...")
if "os" in sys:
subprocess.run(
"curl -LO https://github.com/oras-project/oras/releases/download/v0.12.0/oras_0.12.0_darwin_amd64.tar.gz",
shell=True,
)
location = Path("oras-install")
location.mkdir(mode=511, parents=False, exist_ok=True)
subprocess.run("tar -zxf oras_0.12.0_*.tar.gz -C oras-install/", shell=True)
subprocess.run("mv oras-install/oras /usr/local/bin/", shell=True)
subprocess.run("rm -rf oras_0.12.0_*.tar.gz oras-install/", shell=True)
elif "win" in sys:
subprocess.run(
"curl.exe -sLO https://github.com/oras-project/oras/releases/download/v0.12.0/oras_0.12.0_windows_amd64.tar.gz",
shell=True,
)
subprocess.run("tar.exe -xvzf oras_0.12.0_windows_amd64.tar.gz", shell=True)
mdir_cmd = "mkdir -p %USERPROFILE%\\bin\\"
subprocess.run(mdir_cmd, shell=True)
copy_cmd = "copy oras.exe %USERPROFILE%\\bin\\"
subprocess.run(copy_cmd, shell=True)
setPath_cmd = "set PATH=%USERPROFILE%\\bin\\;%PATH%"
subprocess.run(setPath_cmd, shell=True)
class Oras:
def __init__(self, github_owner, user_token, origin, system):
self.owner = github_owner
self.conda_prefix = origin
self.token = user_token
if "win" in str(system):
self.strSys = "win-64"
else:
self.strSys = str(system)
logging.warning(f"Host is <<{self.strSys}>>")
if "osx" in self.strSys or "win" in self.strSys:
install_on_OS(system)
def login(self):
loginStr = f"echo {self.token} | oras login https://ghcr.io -u {self.owner} --password-stdin"
subprocess.run(loginStr, shell=True)
def push_repodata(self, the_dir):
# create the repodata file
logging.warning("Build the repodata file using conda index command")
subprocess.run(f"conda index {str(the_dir)}", shell=True)
repo = "repodata.json"
upload_cmd = f"oras push ghcr.io/{self.owner}/samples/{self.strSys}/repodata.json:latest {repo}:application/json"
logging.warning(f"upload the json file using following command: <<{upload_cmd}")
subprocess.run(upload_cmd, shell=True)
def push(self, target, data, versions_dict):
logging.warning(f"current dic is: {versions_dict}")
pkg_name, tag, path, origin, pkg = split_name(data, self.strSys)
# upload the tar_bz2 file to the right url
push_bz2 = f"oras push ghcr.io/{self.owner}/samples/{target}/{pkg_name}:{tag} {origin}:application/octet-stream"
push_bz2_latest = f"oras push ghcr.io/{self.owner}/samples/{target}/{pkg_name}:latest {origin}:application/octet-stream"
upload_url = f"ghcr.io/{self.owner}/samples/{target}/{pkg_name}:{tag}"
upload_url_lat = f"ghcr.io/{self.owner}/samples/{target}/{pkg_name}:latest"
logging.warning(f"Cmd <<{push_bz2}>>")
logging.warning(f"Latest Cmd <<{push_bz2_latest}>>")
chdir(path)
logging.warning(
f"Uploading <<{pkg}>>. path <<{origin} (from dir: << {self.conda_prefix} >> to link: <<{upload_url}>>"
)
subprocess.run(push_bz2, shell=True)
logging.warning(f"Package <<{pkg_name}>> uploaded to: <<{upload_url}>>")
can_be_pushed = False
current_version = get_version_file(path, pkg_name)[1]
if pkg_name in versions_dict.keys():
old_version = versions_dict[pkg_name]
logging.warning(
f"Two versions of package <<{pkg_name}>> found: current version: <<{current_version}>> old version:<<{old_version}>>"
)
if current_version != old_version:
logging.warning(f"new version found: <<{current_version}>>")
can_be_pushed = True
else:
can_be_pushed = True
if can_be_pushed:
logging.warning(
f"Uploading <<{pkg}>> with tag latest with cmd <<{push_bz2_latest}>>"
)
subprocess.run(push_bz2_latest, shell=True)
versions_dict[pkg_name] = current_version
# versions_dict = write_version(versions_dict, data)
logging.warning(f"Package <<{pkg_name}>> uploaded to: <<{upload_url_lat}>>")
else:
logging.warning(
f"This Version {current_version} of <<{pkg_name}>> cannot be tagged as latest, because there is a newer version"
)
return versions_dict
def pull(self, pkg, tag, a_dir, versions_dict):
pullCmd = f'oras pull ghcr.io/{self.owner}/samples/{self.strSys}/{pkg}:{tag} --output {a_dir} -t "application/octet-stream"'
logging.warning(f"Pulling lattest of <<{pkg}>>. with command: <<{pullCmd}>>")
try:
subprocess.run(pullCmd, shell=True)
except subprocess.CalledProcessError:
logging.warning(f"Package <<{pkg}>> did not exist on the registry")
logging.warning("Upload aborted!")
return versions_dict
else:
logging.warning(f"Latest version of <<{pkg}>> pulled")
versions_dict = write_version(versions_dict, a_dir, self.strSys)
return versions_dict