File: mkdir.rst

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 (211 lines) | stat: -rw-r--r-- 8,901 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
.. include:: ../global.inc
.. _decorators.mkdir:
.. index::
    pair: @mkdir; Syntax

.. seealso::

    * :ref:`@mkdir <new_manual.mkdir>` in the **Ruffus** Manual
    * :ref:`@follows(mkdir("dir")) <decorators.follows>` specifies the creation of a *single* directory as a task pre-requisite.
    * :ref:`Decorators <decorators>` for more decorators

.. |input| replace:: `input`
.. _input: `decorators.mkdir.input`_
.. |output| replace:: `output`
.. _output: `decorators.mkdir.output`_
.. |filter| replace:: `filter`
.. _filter: `decorators.mkdir.filter`_
.. |matching_regex| replace:: `matching_regex`
.. _matching_regex: `decorators.mkdir.matching_regex`_
.. |matching_formatter| replace:: `matching_formatter`
.. _matching_formatter: `decorators.mkdir.matching_formatter`_
.. |suffix_string| replace:: `suffix_string`
.. _suffix_string: `decorators.mkdir.suffix_string`_

########################################################################
@mkdir( |input|_, |filter|_, |output|_ )
########################################################################
    **Purpose:**

        * Prepares directories to receive *Output* files
        * Used when *Output* path names are generated at runtime from *Inputs*. **mkdir** can make sure these runtime specified paths exist.
        * Directory names are generated from **Input** using string substitution via :ref:`formatter() <decorators.formatter>`,  :ref:`suffix() <decorators.suffix>` or  :ref:`regex() <decorators.regex>`.
        * Behaves essentially like ``@transform`` but with its own (internal) function which does the actual work of making a directory
        * Does *not* invoke the host task function to which it is attached
        * Makes specified directories using `os.makedirs  <http://docs.python.org/2/library/os.html#os.makedirs>`__
        * Multiple directories can be created in a list

        .. note::

            Only missing directories are created.

            In other words, the same directory can be specified multiple times safely without, for example, being recreated repeatedly.

            Sometimes, for pipelines with multiple entry points, this is the only way to make sure that certain working or output
            directories are always created or available *before* the pipeline runs.

    **Simple Example**

        Creates multiple directories per job to hold the results of :ref:`@transform<decorators.transform>`

            .. code-block:: python
                :emphasize-lines: 10,20

                from ruffus import *

                #   initial files
                @originate([ 'A.start',
                             'B.start'])
                def create_initial_files(output_file):
                    with open(output_file, "w") as oo: pass


                # create files without making directories -> ERROR
                @transform( create_initial_files,
                            formatter(),
                            ["{path[0]}/{basename[0]}/processed.txt",
                             "{path[0]}/{basename[0]}.tmp/tmp.processed.txt"])
                def create_files_without_mkdir(input_file, output_files):
                    open(output_files[0], "w")
                    open(output_files[1], "w")


                # create files after making corresponding directories
                @mkdir( create_initial_files,
                        formatter(),
                        ["{path[0]}/{basename[0]}",         # create directory
                         "{path[0]}/{basename[0]}.tmp"])    # create directory.tmp
                @transform( create_initial_files,
                            formatter(),
                            ["{path[0]}/{basename[0]}/processed.txt",
                             "{path[0]}/{basename[0]}.tmp/tmp.processed.txt"])
                def create_files_with_mkdir(input_file, output_files):
                    open(output_files[0], "w")
                    open(output_files[1], "w")

                pipeline_run([create_files_without_mkdir])
                pipeline_run([create_files_with_mkdir])

        Running without making the directories first gives errors:

            .. code-block:: python
                :emphasize-lines: 14-19

                >>> pipeline_run([create_files_without_mkdir])
                    Job  = [None -> A.start] completed
                    Job  = [None -> B.start] completed
                Completed Task = create_initial_files

                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                      File "/usr/local/lib/python2.7/dist-packages/ruffus/task.py", line 3738, in pipeline_run
                        raise job_errors
                    ruffus.ruffus_exceptions.RethrownJobError:

                    Original exception:

                >>> #    Exception #1
                >>> #      'exceptions.IOError([Errno 2] No such file or directory: 'A/processed.txt')' raised in ...
                >>> #       Task = def create_files_without_mkdir(...):
                >>> #       Job  = [A.start -> [processed.txt, tmp.processed.txt]]


        Running after making the directories first:

            .. code-block:: python
                :emphasize-lines: 15

                >>> pipeline_run([create_files_with_mkdir])
                    Job  = [None -> A.start] completed
                    Job  = [None -> B.start] completed
                Completed Task = create_initial_files
                    Make directories [A, A.tmp] completed
                    Make directories [B, B.tmp] completed
                Completed Task = (mkdir 1) before create_files_with_mkdir
                    Job  = [A.start -> [processed.txt, tmp.processed.txt]] completed
                    Job  = [B.start -> [processed.txt, tmp.processed.txt]] completed
                Completed Task = create_files_with_mkdir

    **Parameters:**

.. _decorators.mkdir.input:

    * **input** = *tasks_or_file_names*
       can be a:

       #.  Task / list of tasks (as in the example above).
            File names are taken from the |output|_ of the specified task(s)
       #.  (Nested) list of file name strings.
            File names containing ``*[]?`` will be expanded as a |glob|_.
             E.g.:``"a.*" => "a.1", "a.2"``

.. _decorators.mkdir.filter:

.. _decorators.mkdir.suffix_string:

    * **filter** = *suffix(suffix_string)*
       must be wrapped in a :ref:`suffix<decorators.suffix>` indicator object.
       The end of each |input|_ file name which matches ``suffix_string`` will be replaced by |output|_.

       Input file names which do not match suffix_string will be ignored


       The non-suffix part of the match can be referred to using the ``r"\1"`` pattern. This
       can be useful for putting the output in different directory, for example::


            @mkdir(["1.c", "2.c"], suffix(".c"), r"my_path/\1.o")
            def compile(infile, outfile):
                pass

       This results in the following function calls:

            ::

                # 1.c -> my_path/1.o
                # 2.c -> my_path/2.o
                compile("1.c", "my_path/1.o")
                compile("2.c", "my_path/2.o")

       For convenience and visual clarity, the  ``"\1"`` can be omitted from the output parameter.
       However, the ``"\1"`` is mandatory for string substitutions in additional parameters, ::


            @mkdir(["1.c", "2.c"], suffix(".c"), [r"\1.o", ".o"], "Compiling \1", "verbatim")
            def compile(infile, outfile):
                pass

       Results in the following function calls:

            ::

                compile("1.c", ["1.o", "1.o"], "Compiling 1", "verbatim")
                compile("2.c", ["2.o", "2.o"], "Compiling 2", "verbatim")

       Since r"\1" is optional for the output parameter, ``"\1.o"`` and ``".o"`` are equivalent.
       However, strings in other parameters which do not contain r"\1" will be included verbatim, much
       like the string ``"verbatim"`` in the above example.




.. _decorators.mkdir.matching_regex:

    * **filter** = *regex(matching_regex)*
       is a python regular expression string, which must be wrapped in
       a :ref:`regex<decorators.regex>`\  indicator object
       See python `regular expression (re) <http://docs.python.org/library/re.html>`_
       documentation for details of regular expression syntax
       Each output file name is created using regular expression substitution with ``output``

.. _decorators.mkdir.matching_formatter:

    * **filter** = *formatter(...)*
       a :ref:`formatter<decorators.formatter>` indicator object containing optionally
       a  python `regular expression (re) <http://docs.python.org/library/re.html>`_.

.. _decorators.mkdir.output:

    * **output** = *output*
        Specifies the directories to be created after string substitution