Last active
September 24, 2020 15:15
-
-
Save obenshaindw/a3f509b9b5040b47bf8311da28e6d223 to your computer and use it in GitHub Desktop.
Python script for interactively resuming an NDA submission.This script allows the user to select local file for each remaining file to be uploaded.This has an advantage over using nda-tools vtcmd command-line client, which will attempt to find the file and can be quite inefficient with multiple directories to scan.
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
import boto3 | |
from botocore.exceptions import ClientError | |
import requests | |
import getpass | |
import os | |
import time | |
username = input('Username:') | |
password = getpass.getpass() | |
auth = (username, password) | |
submission_id = input('Enter submission_id to resume:') | |
submission_files = requests.get(f'https://nda.nih.gov/api/submission/{submission_id}/files?retrieveFilesToUpload=true', auth=auth).json() | |
for file in submission_files: | |
try: | |
credentials = requests.get(file['_links']['multipartUploadCredentials']['href'], auth=auth) | |
credentials.raise_for_status() | |
credentials = credentials.json() | |
except requests.exceptions.HTTPError as err: | |
print(credentials.text) | |
raise SystemExit(err) | |
submission_file_id = file['id'] | |
file_user_path = file['file_user_path'] | |
file_remote_path = file['file_remote_path'] | |
bucket = file_remote_path.split("/", 3)[2] | |
key = file_remote_path.split("/", 3)[3] | |
full_local_path = input(f'Enter the full path to the local file {file_user_path}:') | |
if os.path.isfile(full_local_path): | |
local_size = os.path.getsize(full_local_path) | |
print(f'Attempting to upload {full_local_path} to s3://{bucket}/{key}') | |
try: | |
session = boto3.session.Session(aws_access_key_id=credentials['access_key'], | |
aws_secret_access_key=credentials['secret_key'], | |
aws_session_token=credentials['session_token']) | |
s3_client = session.client(service_name='s3') | |
s3_client.upload_file(full_local_path, bucket, key) | |
print('Upload completed.') | |
except ClientError as e: | |
print(f'There was an error uploading the file {e}') | |
else: | |
print(f'The file {full_local_path} could not be found.') | |
requests.put(file['_links']['self']['href'], params={'submissionFileStatus': 'Complete', | |
'fileSize': local_size}, auth=auth) | |
for i in range(5): | |
submission_status = requests.get(f'https://nda.nih.gov/api/submission/{submission_id}', auth=auth).json()['submission_status'] | |
if submission_status in (['Submitted', 'Upload Completed', 'Submitted_Prototype', 'Processing']): | |
print('Submission Completed') | |
break | |
else: | |
print(f'Submission is not yet complete, current status is {submission_files}, retrying in 30 seconds ...') | |
time.sleep(30) |
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
botocore | |
requests | |
boto3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment