#import re, os, sys

from S3MockedForStaticTest import RemoteProvider as S3RemoteProvider

S3 = S3RemoteProvider()

# remote dynamic file test
# This makes use of a special provider that mocks up S3 using the moto
# library so that boto calls hit local "buckets"
rule all:
    input:
        # only keeping the files so we can copy them out to the cwd
        S3.remote("test-static-remote-bucket/out1.txt", keep_local=True),
        S3.remote("test-static-remote-bucket/out2.txt", keep_local=True)
    run:
        shell("for f in {input}; do mv $f ./; done")

# This rule should run
rule run_no_static:
    input: S3.remote('test-static-remote-bucket/test.txt', static=False)
    output: S3.remote('test-static-remote-bucket/out1.txt'),
    run:
        shell('''echo '{input} > {output}'; cat {input} | tee {output}''')

# This rule should NOT run because Snakemake should assume that the
# input file is static and has not changed.
rule run_static:
    input: S3.remote('test-static-remote-bucket/test.txt', static=True)
    output: S3.remote('test-static-remote-bucket/out2.txt'),
    run:
        shell('''echo '{input} > {output}'; cat {input} | tee {output}''')
        raise Exception("This rule should never run")

# after we finish, we need to remove the pickle storing
# the local moto "buckets" so we are starting fresh
# next time this test is run. This file is created by
# the moto wrapper defined in S3Mocked.py
onsuccess:
    shell("rm ./motoState.p")

onerror:
    shell("rm ./motoState.p")
