generated from linz/template-python-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: add standardising script * chore: organize imports * fix: set the AWS credentials in the command environment rather than in the global (os) environment * fix: modify the way the source path is modified for gdal to allow local files
- Loading branch information
1 parent
ab2efc4
commit fd60e53
Showing
7 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import argparse | ||
import os | ||
import subprocess | ||
import tempfile | ||
|
||
from linz_logger import get_log | ||
|
||
import aws_helper | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--source',dest='source', required=True) | ||
parser.add_argument('--destination', dest='destination', required=True) | ||
arguments = parser.parse_args() | ||
source = arguments.source | ||
destination = arguments.destination | ||
#TODO if destination needs write permission we have to handle this | ||
|
||
get_log().info("standardising", source=source, destination=destination) | ||
|
||
src_bucket_name, src_file_path = aws_helper.parse_path(source) | ||
dst_bucket_name, dst_path = aws_helper.parse_path(destination) | ||
get_log().debug("source", bucket=src_bucket_name, file_path=src_file_path) | ||
get_log().debug("destination", bucket=dst_bucket_name, file_path=dst_path) | ||
dst_bucket = aws_helper.get_bucket(dst_bucket_name) | ||
|
||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
standardized_file_name = f"standardized_{os.path.basename(src_file_path)}" | ||
tmp_file_path = os.path.join(tmp_dir, standardized_file_name) | ||
src_gdal_path = source.replace('s3://', '/vsis3/') | ||
|
||
# Set the credentials for GDAL to be able to read the source file | ||
credentials = aws_helper.get_credentials(src_bucket_name) | ||
gdal_env = os.environ.copy() | ||
gdal_env["AWS_ACCESS_KEY_ID"] = credentials.access_key | ||
gdal_env["AWS_SECRET_ACCESS_KEY"] = credentials.secret_key | ||
gdal_env["AWS_SESSION_TOKEN"] = credentials.token | ||
|
||
# Run GDAL to standardized the file | ||
get_log().debug("run_gdal_translate", src=src_gdal_path, output=tmp_file_path) | ||
command = ["gdal_translate", "-q", "-scale", "0", "255", "0", "254", "-a_srs", "EPSG:2193", "-a_nodata", "255", "-b", "1", "-b", "2", "-b", "3", "-co", "compress=lzw", src_gdal_path, tmp_file_path] | ||
proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=gdal_env) | ||
if proc.returncode != 0: | ||
get_log().error("run_gdal_translate_failed", command=" ".join(command)) | ||
raise Exception(proc.stderr.decode()) | ||
get_log().debug("run_gdal_translate_succeded", command=" ".join(command)) | ||
|
||
# Upload the standardized file to destination | ||
dst_file_path = os.path.join(dst_path, standardized_file_name).strip("/") | ||
get_log().debug("upload_file", path=dst_file_path) | ||
dst_bucket.upload_file(tmp_file_path, dst_file_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pytest | ||
|
||
from scripts.aws_helper import parse_path | ||
|
||
|
||
def test_parse_path() -> None: | ||
s3_path = "s3://bucket-name/path/to/the/file.test" | ||
bucket_name, file_path = parse_path(s3_path) | ||
|
||
assert bucket_name == "bucket-name" | ||
assert file_path == "/path/to/the/file.test" |