Skip to content
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

Added automatic creation of .netrc if none exists to address issue #93 #172

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]
### Fixed
### Added
- Added error messages to inform user if .harmony file is formatted incorrectly or missing a key
- Added error message if .netrc file is not found and prompt for Earthdata login credentials to create a .netrc in the user's home directory
- Added note about how to change permission of .netrc file for mac and linux
### Changed

## [1.15.2]
### Fixed
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ machine urs.earthdata.nasa.gov
password podaacIsAwesome
```

**If the script cannot find the netrc file, you will be prompted to enter the username and password and the script wont be able to generate the CMR token**
**If the script cannot find the netrc file, you will be prompted to enter the username and password to create one automatically. Without a netrc file the script won't be able to generate the CMR token**

**If using MacOS or Linux you will need to change the permissions of the netrc file by running chmod og-rw .netrc**


## Advanced Usage
Expand Down
48 changes: 43 additions & 5 deletions subscriber/podaac_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import requests
import tenacity
from datetime import datetime
import getpass
import platform

__version__ = "1.15.2"
extensions = ["\\.nc", "\\.h5", "\\.zip", "\\.tar.gz", "\\.tiff"]
Expand Down Expand Up @@ -64,6 +66,37 @@
# notebook client in your browser.*


def create_netrc_file(response):
"""
Ask the user if they have created an Earthdata login
if so:
Prompt the user for their username and password
Use the credentials to create a .netrc file in the users home directory
if not:
Prompt the user to create an Earthdata login at the appropriate url
"""
if response.lower() == 'y':
login = input('Enter your Earthdata username: ')
password = getpass.getpass('Enter your Earthdata password: ')
netrc_content = f"machine urs.earthdata.nasa.gov\n" \
f" login {login}\n" \
f" password {password}\n"

home_dir = os.path.expanduser("~")
file_path = os.path.join(home_dir, ".netrc")

with open(file_path, "w") as file:
file.write(netrc_content)

# running this on windows will cause the program to crash and is not necessary
if platform.system() != 'Windows':
# change .netrc permissions
subprocess.run(["chmod", "og-rw", file_path])

if response.lower() == 'n':
logging.info('Go to https://urs.earthdata.nasa.gov/users/new to create an Earthdata login')
exit()

def setup_earthdata_login_auth(endpoint):
"""
Set up the request library so that it authenticates against the given
Expand All @@ -76,11 +109,16 @@ def setup_earthdata_login_auth(endpoint):
"""
try:
username, _, password = netrc.netrc().authenticators(endpoint)
except (FileNotFoundError, TypeError):
# FileNotFound = There's no .netrc file
# TypeError = The endpoint isn't in the netrc file,
# causing the above to try unpacking None
logging.warning("There's no .netrc file or the The endpoint isn't in the netrc file")
# FileNotFound = There's no .netrc file
except FileNotFoundError:
logging.warning("No .netrc file can be found. One will be attempted to be created.")
create_netrc_file(input('Do you have an Earthdata login? (y/n): '))
username, _, password = netrc.netrc().authenticators(endpoint)
# TypeError = The endpoint isn't in the netrc file,
# causing the below to try unpacking None
except TypeError:
logging.warning("The endpoint isn't in the netrc file or is incorrect")


manager = request.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, endpoint, username, password)
Expand Down
Loading