File: test_runtime_data.py

package info (click to toggle)
python-ruffus 2.6.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,828 kB
  • ctags: 2,843
  • sloc: python: 15,745; makefile: 180; sh: 14
file content (194 lines) | stat: -rwxr-xr-x 4,456 bytes parent folder | download | duplicates (2)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
from __future__ import print_function
"""

    test_runtime_data.py

        Tests having inputs depend on a run time parameter
        (named element in the dict runtime_data passed to pipeline_run)

"""


import os
tempdir = os.path.relpath(os.path.abspath(os.path.splitext(__file__)[0])) + "/"
runtime_files = [tempdir + "a.3"]
import sys
import shutil


# 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 pipeline_run, pipeline_printout, Pipeline, originate, transform, suffix, follows, runtime_parameter, mkdir








#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   imports


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

import unittest
import shutil

import json
# use simplejson in place of json for python < 2.6
#try:
#    import json
#except ImportError:
#    import simplejson
#    json = simplejson




#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Tasks


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


#
#    task1
#
@originate([tempdir + 'a.1'] + runtime_files)
def task1(outfile):
    """
    First task
    """
    output_text  = ""
    output_text += "    -> " + json.dumps(outfile) + "\n"
    with open(outfile, "w") as oo:
        oo.write(output_text)



#
#    task2
#
@transform(task1, suffix(".1"), ".2")
def task2(infile, outfile):
    """
    Second task
    """
    if infile:
        with open(infile) as ii:
            output_text  = ii.read()
    else:
        output_text = ""
    output_text += json.dumps(infile) + " -> " + json.dumps(outfile) + "\n"
    with open(outfile, "w") as oo:
        oo.write(output_text)



#
#    task3
#
@transform(task2, suffix(".2"), ".3")
def task3(infile, outfile):
    """
    Third task
    """
    if infile:
        with open(infile) as ii:
            output_text  = ii.read()
    else:
        output_text = ""
    output_text += json.dumps(infile) + " -> " + json.dumps(outfile) + "\n"
    with open(outfile, "w") as oo:
        oo.write(output_text)



#
#    task4
#
@follows(task3)
@transform(runtime_parameter("a"), suffix(".3"), ".4")
def task4(infile, outfile):
    """
    Fourth task
    """
    if infile:
        with open(infile) as ii:
            output_text  = ii.read()
    else:
        output_text = ""
    output_text += json.dumps(infile) + " -> " + json.dumps(outfile) + "\n"
    with open(outfile, "w") as oo:
        oo.write(output_text)








class Test_ruffus(unittest.TestCase):
    def setUp(self):
        for f in ["a.1", "a.2","a.3","a.4"]:
            f = os.path.join(tempdir, f)
            if os.path.exists(f):
                os.unlink(f)
        try:
            os.makedirs(tempdir)
        except:
            pass


    def tearDown(self):
        for f in ["a.1", "a.2","a.3","a.4"]:
            f = os.path.join(tempdir, f)
            if os.path.exists(f):
                os.unlink(f)
            else:
                raise Exception("%s is missing" % f)

        shutil.rmtree(tempdir)

    def test_ruffus (self):
        pipeline_run(verbose = 0, runtime_data = {"a": runtime_files}, pipeline= "main")


    def test_newstyle_ruffus (self):


        test_pipeline = Pipeline("test")
        test_pipeline.originate(task_func = task1,
                                output = [tempdir + 'a.1'] + runtime_files)
        test_pipeline.transform(task2, task1, suffix(".1"), ".2")
        test_pipeline.transform(task_func = task3,
                                   input = task2,
                                   filter = suffix(".2"),
                                   output = ".3")
        test_pipeline.transform(task_func = task4,
                                input = runtime_parameter("a"),
                                filter = suffix(".3"),
                                output = ".4").follows(task3)
        test_pipeline.run(verbose = 0, runtime_data = {"a": runtime_files})



if __name__ == '__main__':
    unittest.main()