Skip to content

Commit

Permalink
Log to stderr and fix Oauth2Client inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mackey22 committed Aug 17, 2017
1 parent 51cb8bb commit 3d2696d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
6 changes: 3 additions & 3 deletions hca/added_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def _get_access_token(cls, args, retry):
logging.info("The access token stored in {} is not valid."
" Attempting with refresh token.".format(config.config_files[-1]))
credentials = OAuth2Credentials(
access_token=None,
token=None,
client_id=config.client_id,
client_secret=config.client_secret,
scopes=["https://www.googleapis.com/auth/userinfo.email"],
Expand All @@ -261,8 +261,8 @@ def _get_access_token(cls, args, retry):
credentials.refresh(r)
r.session.close()

config.access_token = credentials.access_token
access_token = credentials.access_token
config.access_token = credentials.token
access_token = credentials.token
# No refresh token
elif retry:
logging.info("The access token stored in {} is not valid and there is"
Expand Down
24 changes: 12 additions & 12 deletions hca/api/composite_commands/download.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import os
import logging
import sys

from io import open
from . import get_bundles, get_files
Expand Down Expand Up @@ -50,25 +50,25 @@ def _download_files(cls, files, folder, replica):
file_uuid = file_['uuid']
filename = file_.get("name", file_uuid)

logging.info("File {}: Retrieving...".format(filename))
sys.stderr.write("\nFile {}: Retrieving...".format(filename))

response = get_files(file_uuid, replica=replica, stream=True)

if response.ok:
file_path = os.path.join(folder, filename)
logging.info("File {}: GET SUCCEEDED. Writing to disk.".format(filename, file_uuid))
sys.stderr.write("\nFile {}: GET SUCCEEDED. Writing to disk.".format(filename, file_uuid))
with open(file_path, "wb") as fh:
# Process taken from
# https://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py
for chunk in response.iter_content(chunk_size=1024):
if chunk:
fh.write(chunk)
response.close()
logging.info("File {}: GET SUCCEEDED. Stored at {}.".format(filename, file_path))
sys.stderr.write("\nFile {}: GET SUCCEEDED. Stored at {}.".format(filename, file_path))

else:
logging.info("File {}: GET FAILED. This uuid is neither a bundle nor a file.".format(filename))
logging.info(response.text)
sys.stderr.write("\nFile {}: GET FAILED. This uuid is neither a bundle nor a file.".format(filename))
sys.stderr.write("\n{}".format(response.text))
response.close()

return {"completed": True}
Expand All @@ -80,7 +80,7 @@ def _download_bundle(cls, args):
bundle_name = args.get("name", bundle_uuid)
replica = args["replica"]

logging.info("Bundle {}: Retrieving...".format(bundle_uuid))
sys.stderr.write("\nBundle {}: Retrieving...".format(bundle_uuid))

response = get_bundles(bundle_uuid, replica=replica)

Expand All @@ -92,13 +92,13 @@ def _download_bundle(cls, args):
folder = bundle_name
if not os.path.isdir(folder):
os.makedirs(folder)
logging.info("Bundle {}: GET SUCCEEDED. {} files to download".format(bundle_uuid, len(files)))
logging.info("Request response:")
logging.info("{}\n".format(response.content.decode()))
sys.stderr.write("\nBundle {}: GET SUCCEEDED. {} files to download".format(bundle_uuid, len(files)))
sys.stderr.write("\nRequest response:")
sys.stderr.write("\n{}\n".format(response.content.decode()))

else:
logging.info("Bundle {}: GET FAILED. Checking if uuid is a file.".format(bundle_uuid))
logging.info(response.text)
sys.stderr.write("\nBundle {}: GET FAILED. Checking if uuid is a file.".format(bundle_uuid))
sys.stderr.write("\n{}".format(response.text))
return files, folder

@classmethod
Expand Down
39 changes: 18 additions & 21 deletions hca/api/composite_commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import pprint
import sys
import uuid
import logging
from io import open

import boto3

from ...upload_to_cloud import upload_to_cloud
from . import put_bundles, put_files
from ...added_command import AddedCommand
Expand Down Expand Up @@ -101,14 +98,14 @@ def _upload_files(cls, args, staging_bucket):
filenames = list(map(os.path.basename, uploaded_keys))

# Print to stderr, upload the files to s3 and return a list of tuples: (filename, filekey)
logging.info("Uploading the following keys to aws:")
sys.stderr.write("\nUploading the following keys to aws:")
for file_ in filenames:
logging.info(file_)
sys.stderr.write("\n{}".format(file_))

filename_key_list = list(zip(filenames, file_uuids, uploaded_keys))
logging.info("\nThe following keys were uploaded successfully:")
sys.stderr.write("\nThe following keys were uploaded successfully:")
for filename, file_uuid, key in filename_key_list:
logging.info('{:<12} {:<12}'.format(filename, key))
sys.stderr.write('\n{:<12} {:<12}'.format(filename, key))
return filename_key_list

@classmethod
Expand All @@ -117,14 +114,14 @@ def _put_files(cls, filename_key_list, staging_bucket):
bundle_uuid = str(uuid.uuid4())
files = []
for filename, file_uuid, key in filename_key_list:
logging.info("File {}: registering...".format(filename))
sys.stderr.write("\nFile {}: registering...".format(filename))

# Generating file data
creator_uid = os.environ.get(cls.CREATOR_ID_ENVIRONMENT_VARIABLE, 1)
source_url = "s3://{}/{}".format(staging_bucket, key)
logging.info(source_url)
sys.stderr.write("\n{}".format(source_url))
# file_uuid = key[:key.find("/")]
logging.info("File {}: assigned uuid {}".format(filename, file_uuid))
sys.stderr.write("\nFile {}: assigned uuid {}".format(filename, file_uuid))

response = put_files(file_uuid,
bundle_uuid=bundle_uuid,
Expand All @@ -134,7 +131,7 @@ def _put_files(cls, filename_key_list, staging_bucket):

if response.ok:
version = response.json().get('version', "blank")
logging.info("File {}: registered with uuid {}".format(filename, file_uuid))
sys.stderr.write("\nFile {}: registered with uuid {}".format(filename, file_uuid))
files.append({
'name': filename,
'version': version,
Expand All @@ -144,13 +141,13 @@ def _put_files(cls, filename_key_list, staging_bucket):
response.close()

else:
logging.info("File {}: registration FAILED".format(filename))
logging.info(response.text)
sys.stderr.write("\nFile {}: registration FAILED".format(filename))
sys.stderr.write("\n{}".format(response.text))
response.close()
response.raise_for_status()

logging.info("Request response")
logging.info("{}".format(response.content.decode()))
sys.stderr.write("\nRequest response")
sys.stderr.write("\n{}".format(response.content.decode()))
return bundle_uuid, files

@classmethod
Expand All @@ -162,23 +159,23 @@ def _put_bundle(cls, bundle_uuid, files, replica):
'version': file_['version'],
'uuid': file_['uuid']} for file_ in files]

logging.info("Bundle {}: registering...".format(bundle_uuid))
sys.stderr.write("\nBundle {}: registering...".format(bundle_uuid))

response = put_bundles(bundle_uuid, replica=replica, creator_uid=creator_uid, files=file_args)

version = None

if response.ok:
version = response.json().get('version', None)
logging.info("Bundle {}: registered successfully".format(bundle_uuid))
sys.stderr.write("\nBundle {}: registered successfully".format(bundle_uuid))

else:
logging.info("Bundle {}: registration FAILED".format(bundle_uuid))
logging.info(response.text)
sys.stderr.write("\nBundle {}: registration FAILED".format(bundle_uuid))
sys.stderr.write("\n{}".format(response.text))
response.raise_for_status()

logging.info("Request response:")
logging.info("{}\n".format(response.content.decode()))
sys.stderr.write("\nRequest response:")
sys.stderr.write("\n{}".format(response.content.decode()))
final_return = {
"bundle_uuid": bundle_uuid,
"creator_uid": creator_uid,
Expand Down

0 comments on commit 3d2696d

Please sign in to comment.