Commit 6942f144 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Merge branch 'issue-73-cannot-push-a-cxx-algorithm' into 'master'

Two-step process for pushing cxx algorithms

Closes #73

See merge request !98
parents 8ecb434a 57e95378
Pipeline #41424 failed with stages
in 3 minutes and 5 seconds
......@@ -42,7 +42,6 @@ import fnmatch
import glob
import logging
import os
from enum import Enum
from enum import unique
......@@ -863,7 +862,25 @@ def pull(webapi, prefix, asset_type, names, fields, force, indentation):
}
storage.save(declaration)
else:
if asset_type == "algorithm" and storage.language == "cxx":
try:
file_data = webapi.download(
"/api/v1/%s/%s/%s/%s/file/"
% (
TYPE_PLURAL[asset_type],
webapi.user,
storage.name,
storage.version,
)
)
data["code"] = file_data
except Exception as e:
logger.error(e)
status += 1
continue
storage.save(**data)
available.add(name)
return status, list(available)
......@@ -1469,6 +1486,10 @@ def push(
url = "/api/v1/%s/%s/" % (TYPE_PLURAL[asset_type], webapi.user)
# C++ algorithms must be sent in two steps (see below)
if asset_type == "algorithm" and storage.language == "cxx":
message.pop("code")
# sends the data
if (
modification_status == ModificationStatus.LOCAL_ONLY_AVAILABLE
......@@ -1506,6 +1527,26 @@ def push(
else:
logger.info("Successfuly updated `%s'", asset_name)
# C++ algorithms must be sent in two steps, we send the binary file now
if asset_type == "algorithm" and storage.language == "cxx":
try:
with open(storage.code.path, "rb") as f:
webapi.upload(
"/api/v1/%s/%s/%s/%s/file/"
% (
TYPE_PLURAL[asset_type],
webapi.user,
storage.name,
storage.version,
),
{"binary": ("%s.so" % storage.version, f)},
)
except RuntimeError as e:
logger.error(e)
return 1
else:
logger.info("Successfuly uploaded binary blob `%s'", asset_name)
return 0
......
......@@ -69,12 +69,15 @@ class WebAPI(object):
def __exit__(self, *exc):
return
def _make_authorization(self):
return "Token {}".format(self.token)
def _make_headers(self):
if self.token is None:
return None
return {
"Authorization": "Token %s" % (self.token),
"Authorization": self._make_authorization(),
"Content-Type": "application/json",
}
......@@ -111,6 +114,31 @@ class WebAPI(object):
except json.JSONDecodeError:
return answer.text
def upload(self, path, data):
url = self.__build_url(path)
answer = requests.post(
url, files=data, headers={"Authorization": self._make_authorization()}
)
if answer.status_code not in [200, 201, 204]:
raise WebAPIError("POST", url, answer)
try:
return answer.json()
except json.JSONDecodeError:
return answer.text
def download(self, path):
url = self.__build_url(path)
answer = requests.get(
url, headers={"Authorization": self._make_authorization()}
)
if answer.status_code not in [200, 204]:
raise WebAPIError("GET", url, answer)
return answer.content
def put(self, path, data=None):
url = self.__build_url(path)
answer = requests.put(url, json=data, headers=self._make_headers())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment