Skip to content

Commit

Permalink
Make lint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Shmuma committed Jan 8, 2024
1 parent bd9d111 commit de728a8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
11 changes: 6 additions & 5 deletions exasol/bfs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
_logger = logging.getLogger(__name__)


def put_file(bucket: bfs.Bucket, file_path: pathlib.Path, skip_if_exists: bool = True) -> pathlib.Path:
def put_file(bucket: bfs.Bucket, file_path: pathlib.Path,
skip_if_exists: bool = True) -> pathlib.Path:
"""
Uploads given file into bucketfs
:param bucket: bucket to use
Expand All @@ -21,9 +22,9 @@ def put_file(bucket: bfs.Bucket, file_path: pathlib.Path, skip_if_exists: bool =
raise ValueError(f"Local file doesn't exist: {file_path}")
local_name = file_path.name
if skip_if_exists and local_name in list(bucket):
_logger.info(f"File {local_name} is already present in the bucketfs")
_logger.info("File %s is already present in the bucketfs", local_name)
else:
_logger.info(f"Uploading file {local_name} to bucketfs")
with file_path.open("rb") as fd:
bucket.upload(local_name, fd)
_logger.info("Uploading file %s to bucketfs", local_name)
with file_path.open("rb") as file:
bucket.upload(local_name, file)
return pathlib.Path("/buckets/bfsdefault/") / bucket.name / local_name
35 changes: 21 additions & 14 deletions exasol/github.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Github-related utility functions - check for latest release of project, retrieval of artefacts, etc.
Github-related utility functions - check for latest release of
project, retrieval of artefacts, etc.
"""
import enum
import requests
Expand All @@ -12,7 +13,8 @@

class Project(enum.Enum):
"""
Names of github projects to be retrieved. Values have to match github project names.
Names of github projects to be retrieved. Values have to
match github project names.
"""
CLOUD_STORAGE_EXTENSION = "cloud-storage-extension"
KAFKA_CONNECTOR_EXTENSION = "kafka-connector-extension"
Expand All @@ -26,13 +28,16 @@ def get_latest_version_and_jar_url(project: Project) -> Tuple[str, str]:
:param project: name of the project
:return: tuple with version and url to retrieve the artefact.
"""
r = requests.get(f"https://api.github.com/repos/exasol/{project.value}/releases/latest")
if r.status_code != 200:
raise RuntimeError("Error sending request to the github api, code: %d" % r.status_code)
data = r.json()
req = requests.get(f"https://api.github.com/repos/exasol/{project.value}"
f"/releases/latest", timeout=10)
if req.status_code != 200:
raise RuntimeError("Error sending request to the github, code: %d" %
req.status_code)
data = req.json()
version = data.get('tag_name')
if version is None:
raise RuntimeError(f"The latest version of {project.value} has no tag, something is wrong")
raise RuntimeError(f"The latest version of {project.value} "
f"has no tag, something is wrong")
for asset in data.get('assets', []):
name = asset['name']
if name.endswith(f"{version}.jar"):
Expand All @@ -47,7 +52,8 @@ def retrieve_jar(project: Project, use_local_cache: bool = True,
Returns latest jar file for the project, possibly using local cache.
:param project: project to be used
:param use_local_cache: should local cache be used or file always retrieved anew
:param storage_path: path to be used for downloading. If None, current directory will be used.
:param storage_path: path to be used for downloading.
If None, current directory will be used.
:return: path to the jar file on the local filesystem
"""
version, jar_url = get_latest_version_and_jar_url(project)
Expand All @@ -59,13 +65,14 @@ def retrieve_jar(project: Project, use_local_cache: bool = True,
local_jar_path = storage_path / local_jar_path

if use_local_cache and local_jar_path.exists():
_logger.info(f"Jar for version {version} already exists in {local_jar_path}, skip downloading")
_logger.info("Jar for version %s already exists in %s, skip downloading",
version, local_jar_path)
else:
_logger.info(f"Fetching jar for version {version} from {jar_url}...")
r = requests.get(jar_url, stream=True)
_logger.info("Fetching jar for version %s from %s...", version, jar_url)
req = requests.get(jar_url, stream=True, timeout=10)
try:
count_bytes = local_jar_path.write_bytes(r.content)
_logger.info(f"Saved {count_bytes} bytes in {local_jar_path}")
count_bytes = local_jar_path.write_bytes(req.content)
_logger.info("Saved %d bytes in %s", count_bytes, local_jar_path)
finally:
r.close()
req.close()
return local_jar_path
16 changes: 8 additions & 8 deletions test/unit/test_bfs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ def bucket_with_file(bfs_bucket: mock.MagicMock):

@pytest.fixture
def temp_file(tmp_path) -> Generator[pathlib.Path, None, None]:
p = pathlib.Path(tmp_path) / MOCKED_FILE_NAME
p.write_text("data")
yield p
p.unlink()
path = pathlib.Path(tmp_path) / MOCKED_FILE_NAME
path.write_text("data")
yield path
path.unlink()


def test_put_file_exists(caplog, bucket_with_file, temp_file):
caplog.set_level("INFO")
p = bfs_utils.put_file(bucket_with_file, temp_file)
assert str(p) == f"/buckets/bfsdefault/{MOCKED_BUCKET}/{MOCKED_FILE_NAME}"
path = bfs_utils.put_file(bucket_with_file, temp_file)
assert str(path) == f"/buckets/bfsdefault/{MOCKED_BUCKET}/{MOCKED_FILE_NAME}"
assert "already present in the bucketfs" in caplog.text
assert not bucket_with_file.upload.called

caplog.clear()
p = bfs_utils.put_file(bucket_with_file, temp_file, skip_if_exists=False)
assert str(p) == f"/buckets/bfsdefault/{MOCKED_BUCKET}/{MOCKED_FILE_NAME}"
path = bfs_utils.put_file(bucket_with_file, temp_file, skip_if_exists=False)
assert str(path) == f"/buckets/bfsdefault/{MOCKED_BUCKET}/{MOCKED_FILE_NAME}"
assert bucket_with_file.upload.called
assert "Uploading file" in caplog.text
6 changes: 3 additions & 3 deletions test/unit/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}


def mocked_requests_get(*args, **kwargs):
def mocked_requests_get(*args, **_):
res = mock.create_autospec(requests.Response)
res.status_code = 404
url = args[0]
Expand All @@ -40,7 +40,7 @@ def mocked_requests_get(*args, **kwargs):


@mock.patch("requests.get", side_effect=mocked_requests_get)
def test_get_latest_version_and_jar_url(get_mock: mock.MagicMock):
def test_get_latest_version_and_jar_url(_):
res = github.get_latest_version_and_jar_url(github.Project.CLOUD_STORAGE_EXTENSION)
assert res == ("2.7.8", CSE_MOCK_URL)

Expand All @@ -49,7 +49,7 @@ def test_get_latest_version_and_jar_url(get_mock: mock.MagicMock):


@mock.patch("requests.get", side_effect=mocked_requests_get)
def test_retrieve_jar(get_mock: mock.MagicMock, tmpdir, caplog):
def test_retrieve_jar(_, tmpdir, caplog):
# need this as retrieve_jar works with current directory in some cases
os.chdir(tmpdir)

Expand Down

0 comments on commit de728a8

Please sign in to comment.