forked from hysds/container-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
62 lines (56 loc) · 2.11 KB
/
utils.py
File metadata and controls
62 lines (56 loc) · 2.11 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
from __future__ import print_function
import os
import re
import subprocess
import hysds_commons.request_utils
from hysds.celery import app
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 {0}; 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("Failed to determine git repo of: {0}.{1}".format(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.basename(specification))
return "{0}-{1}:{2}".format(ptype,name,version)
def check_exists(item,tosca=False):
'''
Checks the existence of item in ES
@param item: item to check
@return: True if item exists
'''
es = app.conf["MOZART_REST_URL"]
if tosca:
es = app.conf["TOSCA_REST_URL"]
ptype = "container"
if item.startswith("job"):
ptype = "job_spec"
elif item.startswith("hysds_io"):
ptype = "hysds_io"
es = os.path.join(es,"{0}/{1}?id={2}".format(ptype,"info" if item.startswith("container") else "type",item))
try:
hysds_commons.request_utils.requests_json_response("GET",es,verify=False)
return True
except Exception as e:
print("Failed to find {0} because of {1}.{2}".format(item,type(e),e))
return False