-
Notifications
You must be signed in to change notification settings - Fork 2
Updates/asf netrc #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abradley60
wants to merge
2
commits into
main
Choose a base branch
from
updates/ASF_netrc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Updates/asf netrc #105
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import shapely | ||
| import ast | ||
| import re | ||
| import tempfile | ||
| from cdsetool.query import query_features, FeatureQuery | ||
| from cdsetool.credentials import Credentials | ||
| from cdsetool.download import download_features | ||
|
|
@@ -84,6 +85,50 @@ def query_scene_from_asf(scene: str) -> ASFSearchResults: | |
| return search_results | ||
|
|
||
|
|
||
| def create_asf_netrc_file(asf_login: str, asf_pass: str) -> Path: | ||
| """ | ||
| Create a temporary `.netrc` file containing ASF/Earthdata login credentials. | ||
|
|
||
| This function generates a temporary `.netrc` file to authenticate with the | ||
| Alaska Satellite Facility (ASF) or NASA Earthdata services. The file is | ||
| written to a secure temporary location, assigned appropriate file permissions | ||
| (`chmod 600`), and its path is set as the `NETRC` environment variable so that | ||
| tools using `requests` or other netrc-aware libraries can locate it. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| asf_login : str | ||
| The username associated with the ASF/NASA Earthdata account. | ||
| asf_pass : str | ||
| The password associated with the ASF/NASA Earthdata account. | ||
|
|
||
| Returns | ||
| ------- | ||
| Path | ||
| The filesystem path to the created temporary `.netrc` file. | ||
| """ | ||
|
|
||
| logger.info(f"Creating temporary .netrc file with ASF/Earthdata credentials") | ||
| with tempfile.NamedTemporaryFile("w", delete=False) as f: | ||
| f.write( | ||
| f"""machine urs.earthdata.nasa.gov | ||
| login {asf_login} | ||
| password {asf_pass} | ||
| """ | ||
| ) | ||
| netrc_path = f.name | ||
|
|
||
| logger.info(f"Temporary .netrc created at {netrc_path}") | ||
|
|
||
| # Set file permissions to 600 (required) | ||
| os.chmod(netrc_path, 0o600) | ||
|
|
||
| # Optional: tell requests/netrc-aware tools to use this file | ||
| os.environ["NETRC"] = netrc_path | ||
|
|
||
| return netrc_path | ||
|
|
||
|
|
||
| @log_timing | ||
| def download_scene_from_asf( | ||
| scene: str, | ||
|
|
@@ -151,8 +196,22 @@ def download_scene_from_asf( | |
| ) | ||
| MissingCredentialsError(err_string) | ||
|
|
||
| session = asf_search.ASFSession() | ||
| session.auth_with_creds(asf_login, asf_pass) | ||
| # create the NETRC file if it doesn't exist | ||
| # this is a fallback if auth_with_creds fails | ||
| if not os.getenv("NETRC"): | ||
| create_asf_netrc_file(asf_login, asf_pass) | ||
|
|
||
| try: | ||
| logger.info("Attempting to authenticate with ASF session") | ||
| session = asf_search.ASFSession() | ||
| session.auth_with_creds(asf_login, asf_pass) | ||
| use_session = True | ||
| except: | ||
| logger.error( | ||
| "ASF session authentication failed. Attempting fo fall back to .netrc file.", | ||
| exc_info=True, | ||
| ) | ||
| use_session = False | ||
|
|
||
| if make_folder: | ||
| os.makedirs(download_folder, exist_ok=True) | ||
|
|
@@ -167,7 +226,10 @@ def download_scene_from_asf( | |
| logger.info(f"Skipping download, zipped scene exists at : {scene_zip_path}") | ||
| else: | ||
| try: | ||
| asf_scene_metadata.download(path=download_folder, session=session) | ||
| if use_session: | ||
| asf_scene_metadata.download(path=download_folder, session=session) | ||
| else: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps add a comment to explain that asf_scene_metadata will default to looking for the .netrc file if no session is provided. |
||
| asf_scene_metadata.download(path=download_folder) | ||
| except: | ||
| logger.error( | ||
| "An error occurred while running the download command", | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR, but I'm wondering if eof.download.main could use a tidy up? It's a bit unintuitive that it takes both CDSE and ASF credentials at this level, although that is explained with the comment. For readability, I would find it more intuitive if it attempted to download with CDSE credentials at the time where you check
if source == "CDSE"-- however, I haven't looked at the function in detail, so there may be good reasons for having the downloader handle that logic.