#import re, os, sys

from snakemake.remote.S3Mocked 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 file so we can copy it out to the cwd
        S3.remote("test-remote-bucket/out.txt", keep_local=True)
    run:
        shell("mv test-remote-bucket/out.txt ./")

rule split:
    input: S3.remote('test-remote-bucket/test.txt')
    output: S3.remote(dynamic('test-remote-bucket/prefix{split_id}.txt'))
    run:
        shell('split -l 2 {input} test-remote-bucket/prefix')
        for f in os.listdir(os.getcwd()+"/test-remote-bucket"):
            if re.search('prefix[a-z][a-z]', f):
                os.rename("test-remote-bucket/"+f, "test-remote-bucket/"+f + '.txt')

rule cut:
    input: S3.remote('test-remote-bucket/prefix{split_id,[a-z][a-z]}.txt')
    output:
        S3.remote('test-remote-bucket/{split_id}_cut.txt')
    shell: 'cut -f 1,2 {input} > {output}'

rule merge:
    input:
        S3.remote(dynamic('test-remote-bucket/{split_id}_cut.txt'))
    output:
        S3.remote('test-remote-bucket/out.txt'),
    run:
        shell('echo {input}; cat {input} > {output}')

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

# or if you prefer to not instantiate a RemoteProvider object, and rely on the module
# import S3Mocked as S3Mocked
# # 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 file so we can copy it out to the cwd
#         remote("test-remote-bucket/out.txt", keep_local=True, provider=S3Mocked, additional_kwargs={})
#     run:
#         shell("mv test-remote-bucket/out.txt ./")

# rule split:
#     input: remote('test-remote-bucket/test.txt', keep_local=False, provider=S3Mocked, additional_kwargs={})
#     output: remote(dynamic('test-remote-bucket/prefix{split_id}.txt'), provider=S3Mocked, additional_kwargs={})
#     run:
#         shell('split -l 2 {input} test-remote-bucket/prefix')
#         for f in os.listdir(os.getcwd()+"/test-remote-bucket"):
#             if re.search('prefix[a-z][a-z]', f):
#                 os.rename("test-remote-bucket/"+f, "test-remote-bucket/"+f + '.txt')

# rule cut:
#     input: remote('test-remote-bucket/prefix{split_id,[a-z][a-z]}.txt', provider=S3Mocked, additional_kwargs={})
#     output:
#         remote('test-remote-bucket/{split_id}_cut.txt', provider=S3Mocked, additional_kwargs={})
#     shell: 'cut -f 1,2 {input} > {output}'

# rule merge:
#     input:
#         remote(dynamic('test-remote-bucket/{split_id}_cut.txt'), provider=S3Mocked, additional_kwargs={})
#     output:
#         remote('test-remote-bucket/out.txt', provider=S3Mocked, additional_kwargs={}),
#     run:
#         shell('echo {input}; cat {input} > {output}')
