#!/usr/bin/env python import argparse import importlib.util import logging import os from pathlib import Path import sys import time import types import pandas as pd import requests def main(username: str, password: str): dataset_root_dir = Path(__file__).resolve().parent.parent # import dataset loader script to access its functions phytozome_genomes_spec = importlib.util.spec_from_file_location('phytozome_genomes', dataset_root_dir / 'phytozome_genomes.py') phytozome_genomes = importlib.util.module_from_spec(phytozome_genomes_spec) sys.modules['phytozome_genomes'] = phytozome_genomes phytozome_genomes_spec.loader.exec_module(phytozome_genomes) genomes = pd.read_table( dataset_root_dir / 'genomes.tsv', index_col = 'id' ) jgi_session = phytozome_genomes.create_JGI_session(username = username, password = password) for jamo_id in genomes.index.to_list(): time.sleep(0.5) with jgi_session.get(f"https://files.jgi.doe.gov/download_files/{jamo_id}", stream = True) as response: if response.status_code == requests.codes.conflict: print(f"File '{jamo_id}' has been requested from the tape archive; please check again in 24 hours.") else: print(f"File '{jamo_id}' is already restored, nothing to be done.") jgi_session.close() if __name__ == '__main__': parser = argparse.ArgumentParser( description = 'Requests all files in dataset to be pulled from Phytozome tape archives if they are not already restored' ) parser.add_argument( '--username', help = 'JGI username (overrides JGI_USERNAME environment variable)' ) parser.add_argument( '--password', help = 'JGI password (overrides JGI_PASSWORD environment variable)' ) args = parser.parse_args() if args.username is None: if 'JGI_USERNAME' in os.environ: args.username = os.environ['JGI_USERNAME'] else: logging.error('Either pass a JGI username with --username or by setting the JGI_USERNAME environment variable') parser.print_usage(sys.stderr) sys.exit(1) if args.password is None: if 'JGI_PASSWORD' in os.environ: args.password = os.environ['JGI_PASSWORD'] else: logging.error('Either set the JGI_PASSWORD environment variable or pass a JGI password using --password (not recommended)') parser.print_usage(sys.stderr) sys.exit(1) main(username = args.username, password = args.password)