-
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.
Merge pull request #240 from ASFHyP3/develop
Release v0.14.0
- Loading branch information
Showing
7 changed files
with
126 additions
and
8 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
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
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
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,30 @@ | ||
import netrc | ||
import os | ||
from pathlib import Path | ||
from platform import system | ||
from typing import Tuple | ||
|
||
|
||
ESA_HOST = 'dataspace.copernicus.eu' | ||
|
||
|
||
def get_esa_credentials() -> Tuple[str, str]: | ||
netrc_name = '_netrc' if system().lower() == 'windows' else '.netrc' | ||
netrc_file = Path.home() / netrc_name | ||
|
||
if "ESA_USERNAME" in os.environ and "ESA_PASSWORD" in os.environ: | ||
username = os.environ["ESA_USERNAME"] | ||
password = os.environ["ESA_PASSWORD"] | ||
return username, password | ||
|
||
if netrc_file.exists(): | ||
netrc_credentials = netrc.netrc(netrc_file) | ||
if ESA_HOST in netrc_credentials.hosts: | ||
username = netrc_credentials.hosts[ESA_HOST][0] | ||
password = netrc_credentials.hosts[ESA_HOST][2] | ||
return username, password | ||
|
||
raise ValueError( | ||
"Please provide Copernicus Data Space Ecosystem (CDSE) credentials via the " | ||
"ESA_USERNAME and ESA_PASSWORD environment variables, or your netrc 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pytest | ||
|
||
from hyp3_autorift.utils import ESA_HOST, get_esa_credentials | ||
|
||
|
||
def test_get_esa_credentials_env(tmp_path, monkeypatch): | ||
with monkeypatch.context() as m: | ||
m.setenv('ESA_USERNAME', 'foo') | ||
m.setenv('ESA_PASSWORD', 'bar') | ||
m.setenv('HOME', str(tmp_path)) | ||
(tmp_path / '.netrc').write_text(f'machine {ESA_HOST} login netrc_username password netrc_password') | ||
|
||
username, password = get_esa_credentials() | ||
assert username == 'foo' | ||
assert password == 'bar' | ||
|
||
|
||
def test_get_esa_credentials_netrc(tmp_path, monkeypatch): | ||
with monkeypatch.context() as m: | ||
m.delenv('ESA_USERNAME', raising=False) | ||
m.delenv('ESA_PASSWORD', raising=False) | ||
m.setenv('HOME', str(tmp_path)) | ||
(tmp_path / '.netrc').write_text(f'machine {ESA_HOST} login foo password bar') | ||
|
||
username, password = get_esa_credentials() | ||
assert username == 'foo' | ||
assert password == 'bar' | ||
|
||
|
||
def test_get_esa_credentials_missing(tmp_path, monkeypatch): | ||
with monkeypatch.context() as m: | ||
m.delenv('ESA_USERNAME', raising=False) | ||
m.setenv('ESA_PASSWORD', 'env_password') | ||
m.setenv('HOME', str(tmp_path)) | ||
(tmp_path / '.netrc').write_text('') | ||
msg = 'Please provide.*' | ||
with pytest.raises(ValueError, match=msg): | ||
get_esa_credentials() | ||
|
||
with monkeypatch.context() as m: | ||
m.setenv('ESA_USERNAME', 'env_username') | ||
m.delenv('ESA_PASSWORD', raising=False) | ||
m.setenv('HOME', str(tmp_path)) | ||
(tmp_path / '.netrc').write_text('') | ||
msg = 'Please provide.*' | ||
with pytest.raises(ValueError, match=msg): | ||
get_esa_credentials() |