Mercurial > repos > tduigou > data_manager_cache
comparison data_manager/cache_fetcher.py @ 0:e0b92d203870 draft
"planemo upload commit f40274f6b9f6a15eb4022aab21286d4c96cd8475-dirty"
| author | tduigou |
|---|---|
| date | Mon, 04 Jul 2022 13:28:30 +0000 |
| parents | |
| children | 35c33747b9e3 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e0b92d203870 |
|---|---|
| 1 from os import ( | |
| 2 path as os_path, | |
| 3 mkdir as os_mkdir | |
| 4 ) | |
| 5 | |
| 6 from requests import get as r_get | |
| 7 | |
| 8 from tempfile import ( | |
| 9 NamedTemporaryFile | |
| 10 ) | |
| 11 | |
| 12 import argparse | |
| 13 | |
| 14 def download( | |
| 15 url: str, | |
| 16 file: str = "" | |
| 17 ) -> str: | |
| 18 """ | |
| 19 Download a file from 'url' and save it as 'file'. | |
| 20 Parameters: | |
| 21 url -- URL the file is downloaded from | |
| 22 file -- (Optional) filename the downloaded file is saved into (default: "") | |
| 23 Returns: | |
| 24 A filename where the downloaded file has stored into | |
| 25 """ | |
| 26 r = r_get(url) | |
| 27 if not file: | |
| 28 f = NamedTemporaryFile( | |
| 29 mode='wb', | |
| 30 delete=False | |
| 31 ) | |
| 32 file = f.name | |
| 33 else: | |
| 34 f = open(file, 'wb') | |
| 35 f.write(r.content) | |
| 36 f.close() | |
| 37 return file | |
| 38 | |
| 39 parser = argparse.ArgumentParser(description="Download a cache file") | |
| 40 parser.add_argument('-u','--url', required=True, default=None, type=str, help="URL the file is downloaded from") | |
| 41 parser.add_argument('-o','--outfile', required=True, default=None, type=str, help="A filename where the downloaded file has stored into") | |
| 42 | |
| 43 args = parser.parse_args() | |
| 44 | |
| 45 url= args.url #"https://gitlab.com/breakthewall/rrCache-data/-/raw/master/" | |
| 46 filename= os_path.basename(args.outfile) #"cid_strc.json.gz" | |
| 47 cache_dir=os_path.dirname(args.outfile) #'${GALAXY_DATA_MANAGER_DATA_PATH}'+'/rpextractsink/cache/' | |
| 48 full_filename=os_path.join(cache_dir,filename) #'${GALAXY_DATA_MANAGER_DATA_PATH}'+'/rpextractsink/cache/cid_strc.json.gz' | |
| 49 | |
| 50 if not os_path.isdir(cache_dir): | |
| 51 os_mkdir(cache_dir) | |
| 52 | |
| 53 download(url+filename, full_filename) |
