import os
import fnmatch
import snakemake
from snakemake.exceptions import MissingInputException
from snakemake.remote.AzureStorage import RemoteProvider as AzureRemoteProvider

# setup Azure Storage for remote access
# for testing these variable can be added to CircleCI
account_name=os.environ['AZURE_ACCOUNT']
account_key=os.environ.get('AZURE_KEY')
sas_token=os.environ.get('SAS_TOKEN')
assert sas_token or account_key, ("Either SAS_TOKEN or AZURE_KEY have to be set")
AS = AzureRemoteProvider(account_name=account_name,
    account_key=account_key, sas_token=sas_token)


rule upload_to_azure_storage:
    input:
        "test.txt.gz"
    output:
        AS.remote("snakemake-test/data_upload/test.txt.gz")
    run:
        shell("cp {input} {output}")

rule download_from_azure_storage:
    input:
        AS.remote("snakemake-test/data_upload/test.txt.gz")
    output:
        "test.txt.gz"
    run:
        shell("cp {input} {output}")

rule test_globbing:
    input:
        AS.remote("snakemake-test/data_upload/test.txt.gz")
    output:
        touch("globbing.done")
    run:
        basenames, = AS.glob_wildcards("snakemake-test/data_upload/{base}.txt.gz")
        assert "test" in basenames

rule test_download:
    input:
        "globbing.done",
        "test.txt.gz"

rule test_upload:
    input:
        AS.remote("snakemake-test/data_upload/test.txt.gz")
