Skip to content
Open
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
2 changes: 1 addition & 1 deletion subscriber/podaac_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
# You can log in manually by executing the cell below when running in the
# notebook client in your browser.*


def setup_earthdata_login_auth(endpoint):
"""
Set up the request library so that it authenticates against the given
Expand All @@ -82,6 +81,7 @@ def setup_earthdata_login_auth(endpoint):
# causing the above to try unpacking None
logging.warning("There's no .netrc file or the The endpoint isn't in the netrc file")


manager = request.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, endpoint, username, password)
auth = request.HTTPBasicAuthHandler(manager)
Expand Down
37 changes: 37 additions & 0 deletions subscriber/podaac_data_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from os import makedirs
from os.path import isdir, basename, join, exists
from urllib.error import HTTPError
import getpass

from subscriber import podaac_access as pa
from subscriber import subsetting
Expand Down Expand Up @@ -120,6 +121,35 @@ def create_parser():

return parser

def netrc_file_exists():
# get the path for .netrc file
home_dir = os.path.expanduser("~")
netrc_path = os.path.join(home_dir, ".netrc")

return os.path.isfile(netrc_path)

def create_netrc_file():
"""
Prompt the user for their username and password
Use the credentials to create a .netrc file in the users home directory
"""
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"

# get the path for .netrc file
home_dir = os.path.expanduser("~")
netrc_path = os.path.join(home_dir, ".netrc")

# os.open netrc permissions
netrc_permissions = 0o600

# Create and open the .netrc file with the correct permissions
fd = os.open(netrc_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, netrc_permissions)
with os.fdopen(fd, 'w') as file:
file.write(netrc_content)

def run(args=None):
if args is None:
Expand All @@ -138,6 +168,13 @@ def run(args=None):
logging.error(str(v))
exit(1)

if not netrc_file_exists():
if input('Do you have an Earthdata login? (y/n): ').lower() == 'y':
create_netrc_file()
else:
logging.info('Go to https://urs.earthdata.nasa.gov/users/new to create an Earthdata login')
exit()

pa.setup_earthdata_login_auth(edl)
token = pa.get_token(token_url)

Expand Down
36 changes: 36 additions & 0 deletions subscriber/podaac_data_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from os import makedirs
from os.path import isdir, basename, join, isfile, exists
from urllib.error import HTTPError
import getpass

from subscriber import podaac_access as pa
from subscriber import subsetting
Expand Down Expand Up @@ -110,7 +111,35 @@ def create_parser():

return parser

def netrc_file_exists():
# get the path for .netrc file
home_dir = os.path.expanduser("~")
netrc_path = os.path.join(home_dir, ".netrc")

return os.path.isfile(netrc_path)

def create_netrc_file():
"""
Prompt the user for their username and password
Use the credentials to create a .netrc file in the users home directory
"""
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"

# get the path for .netrc file
home_dir = os.path.expanduser("~")
netrc_path = os.path.join(home_dir, ".netrc")

# os.open netrc permissions
netrc_permissions = 0o600

# Create and open the .netrc file with the correct permissions
fd = os.open(netrc_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, netrc_permissions)
with os.fdopen(fd, 'w') as file:
file.write(netrc_content)

def run(args=None):
if args is None:
Expand All @@ -125,6 +154,13 @@ def run(args=None):
logging.error(str(v))
exit(1)

if not netrc_file_exists():
if input('Do you have an Earthdata login? (y/n): ').lower() == 'y':
create_netrc_file()
else:
logging.info('Go to https://urs.earthdata.nasa.gov/users/new to create an Earthdata login')
exit()

pa.setup_earthdata_login_auth(edl)
token = pa.get_token(token_url)

Expand Down