-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (55 loc) · 1.97 KB
/
utils.py
File metadata and controls
64 lines (55 loc) · 1.97 KB
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
import os
import re
import subprocess
import requests
TYPE_TAIL_RE = re.compile(".*/([^/]*).json.?(.*)")
REPO_RE = re.compile(".*/([^/ ]+).git")
def get_repo(directory):
"""
Get the repository for a given directory
@param directory: directory to check git repo for
@return: git repo of supplied directory
"""
sto, ste = subprocess.Popen("cd {}; git remote -v".format(
directory), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
for line in sto.split("\n"):
match = REPO_RE.match(line)
if match:
return match.group(1)
raise Exception(f"Failed to determine git repo of: {directory}.{ste}")
def get_product_id(specification, version):
"""
Get the product id from the specification and version.
@param specification: specification name to create product id from
@param version: version to used to create the product
@return: product id
"""
match = TYPE_TAIL_RE.match(specification)
if not match or not match.group(1) in ["job-spec", "hysds-io"]:
raise Exception("Invalid specification path")
ptype = "job" if match.group(1) == "job-spec" else "hysds-io"
name = match.group(2)
if name == "":
name = get_repo(os.path.basename(specification))
return f"{ptype}-{name}:{version}"
def check_exists(item, rest_url):
"""
Checks the existence of item in ES
@param item: item to check
@param rest_url: rest API endpoint
@return: True if item exists
"""
ptype = "container"
if item.startswith("job"):
ptype = "job_spec"
elif item.startswith("hysds_io"):
ptype = "hysds_io"
url = os.path.join(rest_url, "{}/{}?id={}".format(
ptype, "info" if item.startswith("container") else "type", item))
try:
r = requests.get(url, verify=False)
r.raise_for_status()
return True
except Exception as e:
print(f"Failed to find {item} because of {type(e)}.{e}")
return False