Coverage for r11k/util.py: 91%
16 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-13 23:29 +0100
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-13 23:29 +0100
1"""Minor utilities which didn't fit anywhere else."""
3import logging
4import os
5import requests
8logger = logging.getLogger(__name__)
11def ensure_directory(path: str) -> None: # pragma: no cover
12 """Mkdir, but don't fail on existing directory."""
13 try:
14 os.mkdir(path)
15 except FileExistsError:
16 pass
19def download_file(url: str, outfile: str) -> None:
20 """Fetch file from url, save it to outfile."""
21 logger.info('Downloading', outfile)
22 r = requests.get(url, stream=True)
23 if r.status_code != requests.codes.ok: 23 ↛ 24line 23 didn't jump to line 24, because the condition on line 23 was never true
24 r.raise_for_status()
25 with open(outfile, 'wb') as fd:
26 for chunk in r.iter_content(chunk_size=128):
27 fd.write(chunk)
30def unfix_name(name: str) -> str:
31 """Transform <namespace>/<name> into <namespace>-<name>."""
32 return '-'.join(name.split('/', 1))
35def fix_name(name: str) -> str:
36 """Transform <namespace>-<name> into <namespace>/<name>."""
37 return '/'.join(name.split('-', 1))