Skip to content

Commit

Permalink
Unify file (zip, jar) download (#59)
Browse files Browse the repository at this point in the history
* Unify file download

* Unify file download
  • Loading branch information
ismailsimsek authored Aug 28, 2023
1 parent cc18ba5 commit 6f12740
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
36 changes: 19 additions & 17 deletions pyliquibase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, defaultsFile: str,
if logLevel:
self.args.append("--log-level=%s" % logLevel)

self.additional_classpath: str = additionalClasspath
self.additional_classpath: str = additionalClasspath.rstrip('/') if additionalClasspath else None

# if liquibaseDir is provided then switch to user provided liquibase.
if liquibaseDir:
Expand Down Expand Up @@ -95,15 +95,16 @@ def _cli(self):
self.liquibase_internal_lib_dir + "/*"]

if self.jdbc_drivers_dir:
LIQUIBASE_CLASSPATH.append(self.jdbc_drivers_dir)
LIQUIBASE_CLASSPATH.append(self.jdbc_drivers_dir + "/*")

if self.additional_classpath:
LIQUIBASE_CLASSPATH.append(self.additional_classpath)
LIQUIBASE_CLASSPATH.append(self.additional_classpath + "/*")

if not jnius_config.vm_running:
jnius_config.add_classpath(*LIQUIBASE_CLASSPATH)
else:
log.warning("VM is already running, can't set classpath/options! classpath: %s" % jnius_config.get_classpath())
log.warning(
"VM is already running, can't set classpath/options! classpath: %s" % jnius_config.get_classpath())

log.debug("classpath: %s" % jnius_config.get_classpath())

Expand Down Expand Up @@ -219,24 +220,25 @@ def download_additional_java_library(self, url: str, destination_dir: str = None
"""
_url = urlparse(url)
lib_file_name: str = os.path.basename(_url.path)

if not (lib_file_name.lower().endswith('.zip') or lib_file_name.lower().endswith('.jar')):
raise RuntimeError("Unexpected url, Expecting link to a `**.jar` or `**.zip` file!")

destination_dir = destination_dir if destination_dir else self.liquibase_lib_dir
if lib_file_name.lower().endswith(".zip"):
self._download_zipfile(url=url, destination=destination_dir)
elif lib_file_name.lower().endswith(".jar"):
destination_file = "%s/%s" % (destination_dir, lib_file_name)
if pathlib.Path(destination_file).exists():
log.info("Java lib already available skipping download: %s", destination_file)
else:
log.info("Downloading java lib: %s to %s", url, destination_file)
self._download_file(url=url, destination=destination_file)
destination_file = "%s/%s" % (destination_dir, lib_file_name)
if pathlib.Path(destination_file).exists():
log.info("File already available skipping download: %s", destination_file)
else:
raise RuntimeError("Unexpected url, Expecting link to a `**.jar` or `**.zip` file!")
log.info("Downloading file: %s to %s", url, destination_file)
self._download_file(url=url, destination=destination_file)
with zipfile.ZipFile(destination_file, 'r') as zip_ref:
zip_ref.extractall(destination_dir)

def _download_zipfile(self, url: str, destination: str) -> None:
"""downloads zip file from given url and extract to destination folder
:param url:
:param destination:
:return:
:param url:
:param destination:
:return:
"""
with tempfile.NamedTemporaryFile(suffix="_liquibase.zip", delete=False) as tmpfile:
log.info("Downloading %s to %s" % (url, destination))
Expand Down
13 changes: 10 additions & 3 deletions tests/test_pyliquibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,22 @@ def test_exception(self):
lb.status()
except Exception as e:
self.assertTrue("Liquibase execution failed" in str(e))

def test_download_additional_java_library(self):
lb = Pyliquibase(defaultsFile=os.path.dirname(os.path.realpath(__file__)) + "/resources/liquibase.properties")
lb.download_additional_java_library(url="https://github.com/liquibase/liquibase-snowflake/releases/download/liquibase-snowflake-4.11.0/liquibase-snowflake-4.11.0.jar")
# test re downloading succeeds
lb.download_additional_java_library(url="https://github.com/liquibase/liquibase-snowflake/releases/download/liquibase-snowflake-4.11.0/liquibase-snowflake-4.11.0.jar")
# download Bigquery jdbc
lb.download_additional_java_library(url="https://storage.googleapis.com/simba-bq-release/jdbc/SimbaJDBCDriverforGoogleBigQuery42_1.3.3.1004.zip")
# download redshift jdbc
lb.download_additional_java_library(url="https://repo1.maven.org/maven2/com/amazon/redshift/redshift-jdbc42/2.1.0.16/redshift-jdbc42-2.1.0.16.jar")
# download snowflake jdbc
lb.download_additional_java_library(url="https://repo1.maven.org/maven2/net/snowflake/snowflake-jdbc/3.13.33/snowflake-jdbc-3.13.33.jar")

def test_download_additional_zip_library(self):
lb = Pyliquibase(defaultsFile=os.path.dirname(os.path.realpath(__file__)) + "/resources/liquibase.properties")
# download Bigquery jdbc
lb.download_additional_java_library(
url="https://storage.googleapis.com/simba-bq-release/jdbc/SimbaJDBCDriverforGoogleBigQuery42_1.3.3.1004.zip")
# download Bigquery jdbc
lb.download_additional_java_library(
url="https://storage.googleapis.com/simba-bq-release/jdbc/SimbaJDBCDriverforGoogleBigQuery42_1.3.3.1004.zip")

0 comments on commit 6f12740

Please sign in to comment.