57def download(package=None, projectid=None, version=".", **kwargs):
59 Download all resources for a package from the default registry
62 if kwargs.get(
"datacheck",
False):
return datacheck(**kwargs)
63 else: base_url, auth =
datacheck(**kwargs)
69 from bs4
import BeautifulSoup
72 if not projectid: projectid = kwargs.get(
"identifier",
None)
75 if not package
or not projectid:
raise ValueError
78 api_v4_url = posixpath.join(base_url,
"projects",projectid)
79 r = requests.get(posixpath.join(api_v4_url,
"packages",
"pypi",
"simple",package),headers=auth);
82 path = os.path.abspath(kwargs.get(
"output",os.getcwd()))
84 os.makedirs(path, exist_ok=
True)
87 data = [x[
"href"]
for x
in BeautifulSoup(r.text,
'html.parser').find_all(
'a', {
'href': re.compile(api_v4_url)})
if version
in x[
"href"]]
92 with requests.get(url, stream=
True, headers=auth)
as r:
94 file_name = Utility.PathLeaf(url.split(
"#")[0])
95 with open(file_name,
'wb')
as f:
96 for chunk
in r.iter_content(chunk_size=8192):
97 if chunk: f.write(chunk)
100 return os.listdir(path)
102def pipeline(token=None, projectid=str(12702), **kwargs):
104 Main function to execute the script if main cannot be imported.
105 Otherwise, run a named pipeline for a given project with the given credentials.
106 Defaults to running a remote install script on CARA.
109 settings = copy.deepcopy(kwargs); settings.update({
"token":token})
110 if kwargs.get(
"datacheck",
False):
return datacheck(**settings)
111 else: base_url, auth =
datacheck(**settings)
117 api_v4_url = posixpath.join(base_url,
"projects",projectid)
120 job = kwargs.get(
"job_name",
None)
121 data= {
"ref": kwargs.get(
"branch",
"master")}
122 variables = kwargs.get(
"api_variables",{})
125 if job
and job
in [
"stm_cara"]:
128 cara_login_user = kwargs.get(
"cara_login_user",getpass.getuser())
129 cara_login_credentials = kwargs.get(
"cara_login_credentials", getpass.getpass())
130 install_directory = kwargs.get(
"cara_install_directory",posixpath.join(
"/home",cara_login_user,
"software"))
131 variables = kwargs.get(
"api_variables",
132 {
"USER":cara_login_user,
"CREDENTIALS":cara_login_credentials,
"directory":install_directory,
"feature":kwargs.get(
"package",
"all")})
133 query =
"&".join([
"variables[][key]="+str(x)+
"&variables[][value]="+str(y)
for x, y
in variables.items()])
136 r = requests.post(api_v4_url+
"/pipeline?"+query, data=data, headers=auth);
142 r = requests.post(api_v4_url+
"/pipelines/%s/cancel" % r.json()[
"id"], headers=auth)
143 r = requests.get(api_v4_url+
"/jobs", headers=auth)
144 r = requests.post(api_v4_url+
"/jobs/%s/play" % [x
for x
in r.json()
if x[
"name"]
in [job]][0][
"id"], headers=auth)
145 r = requests.get(api_v4_url+
"/jobs", headers=auth)
148 JobID = [x
for x
in r.json()
if x[
"name"]
in [job]][0][
"id"]
150 r = requests.get(api_v4_url+
"/jobs/%s" % JobID, headers=auth)
152 if r.json()[
"status"]
in [
"pending",
"running"]
and False:
break
153 if r.json()[
"status"]
in [
"success",
"failure"]:
154 PipeID = requests.get(api_v4_url+
"/jobs/%s" % r.json()[
"id"], headers=auth).json()[
"pipeline"][
"id"];
155 r = requests.get(api_v4_url+
"/jobs/%s/trace" % r.json()[
"id"], headers=auth);
160 try: requests.delete(api_v4_url+
"/pipelines/%s" % PipeID, headers=auth)
164 try: result = r.json()
165 except: result = {
"status_code":r.status_code,
"content":r.text}