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

# setup Azure Storage for remote access
# for testing, set AZ_AZURE_ACCOUNT and AZ_ACCOUNT_KEY or AZ_SAS_TOKEN as env vars to CircleCI
AS = AzureRemoteProvider()


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")
