Skip to content

Commit

Permalink
[elixir-cloud-aai#12] FileTransput.download_file()
Browse files Browse the repository at this point in the history
TODO It's still using the host path. This has to be translated to a path inside the container.
  • Loading branch information
tfga committed Jan 23, 2019
1 parent d5aa985 commit 68e83fe
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
36 changes: 23 additions & 13 deletions tesk_core/transput_filer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import logging
import requests
from exception import UnknownProtocol
import shutil

try:
from urllib.parse import urlparse
Expand Down Expand Up @@ -127,21 +128,25 @@ def download_dir(self):
return 1


def getPath(url):

parsed_url = urlparse(url)

return parsed_url.path


class FileTransput(Transput):
def __init__(self, path, url, ftype):
Transput.__init__(self, path, url, ftype)

def download_file(self):
req = requests.get(self.url)

src = getPath(self.url)
dst = self.path

if req.status_code < 200 or req.status_code >= 300:
logging.error('Got status code: %d', req.status_code)
logging.error(req.text)
return 1
logging.debug('OK, got status code: %d', req.status_code)
logging.debug(f"Copying {src} to {dst}")
shutil.copy(src, dst)

with open(self.path, 'wb') as file:
file.write(req.content)
return 0

def upload_file(self):
Expand Down Expand Up @@ -449,7 +454,7 @@ def process_file(ttype, filedata):
logging.error('Could not determine protocol for url: "%s"', filedata['url'])
return 1

trans = newTransput(filedata, scheme)
trans = newTransput(scheme)

with trans(filedata['path'], filedata['url'], Type(filedata['type'])) as transfer:
if ttype == 'inputs':
Expand All @@ -461,6 +466,14 @@ def process_file(ttype, filedata):
return 0



def logConfig(loglevel):

logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S',
level=loglevel,
stream=sys.stdout)

def main():
parser = argparse.ArgumentParser(
description='Filer script for down- and uploading files')
Expand All @@ -482,10 +495,7 @@ def main():
else:
loglevel = logging.ERROR

logging.basicConfig(
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S',
level=loglevel)
logConfig(loglevel)

logging.info('Starting %s filer...', args.transputtype)

Expand Down
41 changes: 40 additions & 1 deletion tests/FilerTest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
import unittest
from transput_filer import newTransput, FTPTransput, HTTPTransput, FileTransput
from transput_filer import newTransput, FTPTransput, HTTPTransput, FileTransput,\
process_file, logConfig, getPath
from exception import UnknownProtocol
from assertThrows import AssertThrowsMixin
import logging
from unittest.mock import patch



class FilerTest(unittest.TestCase, AssertThrowsMixin):


@classmethod
def setUpClass(cls):

logConfig(logging.DEBUG) # Doesn't work...


@patch('transput_filer.shutil.copy')
def test_process_file(self, copyMock):

filedata = {

"url": "file:///home/tfga/workspace/cwl-tes/tmphrtip1o8/md5",
"path": "/var/lib/cwl/stgda974802-fa81-4f0b-8fe4-341d5655af4b/md5",

"type": "FILE", # File = 'FILE'
# Directory = 'DIRECTORY'

"name": "md5",
"description": "cwl_input:md5"
}

process_file('inputs', filedata)

copyMock.assert_called_once_with( '/home/tfga/workspace/cwl-tes/tmphrtip1o8/md5'
, '/var/lib/cwl/stgda974802-fa81-4f0b-8fe4-341d5655af4b/md5')


def test_getPath(self):

self.assertEquals( getPath('file:///home/tfga/workspace/cwl-tes/tmphrtip1o8/md5')
, '/home/tfga/workspace/cwl-tes/tmphrtip1o8/md5')



def test_newTransput(self):

self.assertEquals(newTransput('ftp') , FTPTransput)
Expand Down

0 comments on commit 68e83fe

Please sign in to comment.