import os import re import pathlib import time import click import pysftp import shutil import tempfile @click.command() @click.argument("input-file", type=click.File(mode='r', encoding='utf8')) @click.option("--username", prompt=True, envvar='ASSET_FETCH_USERNAME', show_envvar=True) @click.option("--password", prompt=True, envvar='ASSET_FETCH_PASSWORD', show_envvar=True) @click.option("--host", prompt=True, envvar='ASSET_FETCH_HOST', show_envvar=True) @click.option("--archive", type=click.Path(), default=lambda: "assets-{}".format(int(time.time()))) def asset_fetch(username, password, host, input_file, archive): with tempfile.TemporaryDirectory() as tmpdir: click.echo("tmpdir: {}".format(tmpdir)) with pysftp.Connection(host, username=username, password=password) as sftp: for line in input_file.readlines(): remote_path = re.sub(r"\\", "/", re.sub(r"\\\\[0-9.]*\\", "", line.strip())); if (len(remote_path) > 0): click.echo("fetching {}".format(remote_path)) try: sftp.get_r(remote_path, localdir=tmpdir) except FileNotFoundError: click.echo("not found, skipping") zip_file = shutil.make_archive(archive, 'zip', tmpdir) click.echo("Files written to archive {}".format(zip_file)) if __name__ == '__main__': asset_fetch()