Skip to content

Commit 6a51091

Browse files
authored
🏗️ maintenance scripts: improves error message and some cleanup (ITISFoundation#2355)
* Improves error message and some cleanup * adds password inline
1 parent 222d342 commit 6a51091

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

scripts/maintenance/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ install:
88
# activating python virtual environment
99
@source .venv/bin/activate
1010
# installing python dependencies
11+
@.venv/bin/pip install --upgrade pip setuptools wheel
1112
@.venv/bin/pip install -r requirements.txt
1213
# now you can call the maintenance scripts
1314
# e.g. ./clean_projects_of_user https://osparc-master.speag.com USEREMAIL USERPASSWORD

scripts/maintenance/import_projects_as_template.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
#! /usr/bin/env python3
22

33
import asyncio
4+
import os
5+
from contextlib import suppress
46
from pathlib import Path
7+
from pprint import pformat
58
from typing import Optional
69

710
import typer
8-
from httpx import URL, AsyncClient
11+
from httpx import URL, AsyncClient, HTTPStatusError
912
from pydantic.networks import EmailStr
1013
from pydantic.types import SecretStr
1114

15+
EVERYONE_GROUP_ID = 1
16+
17+
18+
def create_human_readable_message(exc: HTTPStatusError) -> str:
19+
msg = str(exc)
20+
with suppress(Exception):
21+
error = exc.response.json()["error"]
22+
for err in error["errors"]:
23+
msg += "\n{code}: {message}".format(**err)
24+
return msg
25+
1226

1327
async def login_user(client: AsyncClient, email: EmailStr, password: SecretStr):
1428
typer.secho(
@@ -100,7 +114,7 @@ async def import_project_as_template(
100114
password: SecretStr,
101115
project_file: Path,
102116
project_name: str,
103-
share_with_gid: int,
117+
share_with_gid: int, # TODO: not used!?
104118
) -> int:
105119
try:
106120
async with AsyncClient(base_url=endpoint.join("v0")) as client:
@@ -113,20 +127,26 @@ async def import_project_as_template(
113127
fg=typer.colors.BRIGHT_WHITE,
114128
)
115129

130+
except HTTPStatusError as exc:
131+
typer.secho(create_human_readable_message(exc), fg=typer.colors.RED, err=True)
132+
return os.EX_SOFTWARE
133+
116134
except Exception as exc: # pylint: disable=broad-except
117135
typer.secho(f"Unexpected issue: {exc}", fg=typer.colors.RED, err=True)
118-
return 1
136+
return os.EX_SOFTWARE
119137

120-
return 0
138+
return os.EX_OK
121139

122140

123141
def main(
124142
endpoint: str,
125143
username: str,
126-
password: str,
127144
project_file: Path,
128145
project_name: Optional[str] = None,
129-
share_with_gid: Optional[int] = 1,
146+
share_with_gid: int = EVERYONE_GROUP_ID,
147+
password: str = typer.Option(
148+
..., prompt=True, confirmation_prompt=True, hide_input=True
149+
),
130150
) -> int:
131151

132152
if project_name is None:

scripts/maintenance/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ black
22
httpx
33
pydantic[email,dotenv]
44
pylint
5-
typer[colorama]
5+
typer[all]

0 commit comments

Comments
 (0)