Skip to content
Snippets Groups Projects
Commit ba3795f6 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add archive option for update step

parent 8cf78914
No related branches found
No related tags found
1 merge request!26Resolve "Local (non-sudo) CMake 21 setup"
import os
import requests
import dataclasses as dc
......@@ -88,16 +89,101 @@ class Git(Hook):
raise e
@dc.dataclass
class Archive(Hook):
url: Optional[str] = None
extract: Optional[bool] = None
@property
def archive_filename(self) -> str:
return os.path.basename(self.url)
@property
def archive_filepath(self) -> str:
return os.path.join(self.ctx.path, self.archive_filename)
def update(self, **kwargs):
os.makedirs(self.ctx.path, exist_ok=True)
summary = None
actions = []
refresh_extract = False
if not os.path.isfile(self.archive_filepath):
self.download()
actions.append("downloaded")
refresh_extract = True
if self.extract:
try:
done = self.do_extract(refresh=refresh_extract)
except error.ArmarXSetupError as e:
summary = str(e)
else:
if done:
actions.append("extracted")
if summary is None:
if actions:
summary = f"{(' and '.join(actions)).capitalize()} {self.archive_filename}."
else:
summary = "Nothing to do."
assert summary is not None
return summary, ""
def download(self):
dl_file = requests.get(self.url)
with open(self.archive_filepath, 'wb') as file:
file.write(dl_file.content)
assert os.path.isfile(self.archive_filepath)
def do_extract(self, refresh: bool):
def extract_tar():
import tarfile
with tarfile.open(self.archive_filepath) as zip:
zip.extractall(self.ctx.path)
def extract_zip():
import zipfile
with zipfile.ZipFile(self.archive_filepath) as zip:
zip.extractall(self.ctx.path)
extractors = {
".tar.gz": extract_tar,
".zip": extract_zip
}
for ext, fn in extractors.items():
if self.archive_filename.endswith(ext):
target_dir = self.archive_filepath[:-len(ext)]
if refresh or not os.path.isdir(target_dir):
fn()
assert os.path.isdir(target_dir)
return True
else:
return False
raise error.ArmarXSetupError(
f"Unknown archive type: '{os.path.splitext(self.archive_filename)}'.\n"
f"Known are: " + ", ".join(extractors))
@dc.dataclass
class Update(Hook):
git: Optional[Git] = None
archive: Optional[Archive] = None
def __post_init__(self):
from armarx_setup.core.util.dataclass import sanitize_type
self.git = sanitize_type(self.git, Git, ctx=self.ctx)
self.archive = sanitize_type(self.archive, Archive, ctx=self.ctx)
def __call__(self, **kwargs):
if self.git is not None:
return self.git.update(**kwargs)
if self.archive is not None:
return self.archive.update(**kwargs)
......@@ -20,6 +20,7 @@ docutils = "^0.17.1"
# numpy = "~1.19"
thefuzz = "^0.19.0"
python-Levenshtein = "^0.12.2"
requests = "^2.26.0"
# pygraphviz = "~1.6"
# python-igraph = "^0.9.6"
......
......@@ -2,6 +2,7 @@ rich==10.9
click==8.0
networkx==2.5
GitPython==3.1.20
requests==2.26.0
thefuzz==0.19.0
python-levenshtein==0.12.2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment