"""
Author: TC:
Affiliation: IP
Aim: cleanup all files from cleanup rules

""" 
rule cleanup:
    """Recursive cleanup"""
    run:
        # ideally, we'd like to know the name of this file dynamically

        import snakemake
        s = snakemake.Workflow('Snakefile')
        s.include('Snakefile')
        import glob
        from sequana import snaketools as sm
        cleanup_rules = sm.get_cleanup_rules("Snakefile")
        for this_rule_name in cleanup_rules:
            this_rule = s.get_rule(this_rule_name)
            if "wkdir" in this_rule.params.keys():
                for filename in glob.glob(this_rule.params['wkdir']+"/*"):
                    print("Removing %s " % filename)
                    os.remove(filename)
            if "cleanup" in this_rule.params.keys():
                for filename in this_rule.params['cleanup']:
                    print("Removing %s " % filename)
                    try:
                        os.remove(filename)
                    except:
                        pass


