1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#!/usr/bin/env python
from __future__ import print_function
"""
test_softlink_uptodate.py
"""
import os
tempdir = os.path.relpath(os.path.abspath(os.path.splitext(__file__)[0])) + "/"
import sys
# add grandparent to search path for testing
grandparent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
sys.path.insert(0, grandparent_dir)
# module name = script name without extension
module_name = os.path.splitext(os.path.basename(__file__))[0]
# funky code to import by file name
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
import ruffus
from ruffus import originate, transform, Pipeline, pipeline_run, suffix
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Tasks
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
#
# First task
#
@originate(["a.1", "b.1"])
def start_task(output_file_name):
with open(output_file_name, "w") as f:
pass
#
# Forwards file names, is always as up to date as its input files...
#
@transform(start_task, suffix(".1"), ".1")
def same_file_name_task(input_file_name, output_file_name):
pass
#
# Links file names, is always as up to date if links are not missing
#
@transform(start_task, suffix(".1"), ".linked.1")
def linked_file_name_task(input_file_name, output_file_name):
try:
os.symlink(input_file_name, output_file_name)
except:
print (input_file_name, output_file_name)
raise
#
# Final task linking everything
#
@transform([linked_file_name_task, same_file_name_task], suffix(".1"), ".3")
def final_task (input_file_name, output_file_name):
with open(output_file_name, "w") as f:
pass
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# Run pipeline
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
import unittest, shutil
try:
from StringIO import StringIO
except:
from io import StringIO
class Test_ruffus(unittest.TestCase):
def setUp(self):
for f in ["a.1", "b.1", "a.linked.1", "b.linked.1", "a.3", "b.3", "a.linked.3", "b.linked.3"]:
try:
if os.path.exists(f):
os.unlink(f)
except:
print (" !!!!OOPs. Can't unlink %s" % f, file = sys.stderr)
pass
def tearDown(self):
for f in ["a.1", "b.1", "a.linked.1", "b.linked.1", "a.3", "b.3", "a.linked.3", "b.linked.3"]:
if os.path.lexists(f):
os.unlink(f)
else:
raise Exception("Expected %s missing" % f)
def test_ruffus (self):
pipeline_run(log_exceptions = True, verbose = 0, pipeline= "main")
def test_newstyle_ruffus (self):
test_pipeline = Pipeline("test")
test_pipeline.originate(start_task, ["a.1", "b.1"])
test_pipeline.transform(same_file_name_task, start_task, suffix(".1"), ".1")
test_pipeline.transform(linked_file_name_task, start_task, suffix(".1"), ".linked.1")
test_pipeline.transform(final_task, [linked_file_name_task, same_file_name_task], suffix(".1"), ".3")
test_pipeline.run(log_exceptions = True, verbose = 0)
if __name__ == '__main__':
unittest.main()
|