stepup.core.api¶
You can expect reasonable stability of the API documented here over the future releases of StepUp. (No hard promises, since StepUp is still very young.) Other parts of StepUp, not documented here, may undergo larger changes and are not intended to be API stable.
Basic API¶
stepup.core.api.static(*paths)
¶
Declare static paths.
Parameters:
-
*paths
(str | Iterable[str]
, default:()
) –One or more static paths (files or directories), relative to the current working directory. Arguments may also be lists of strings.
Raises:
-
ValueError
–When a file does not exist or there is an error with the trailing separator.
Notes
Environment variables in the paths
will be
substituted directly and amend the current step’s env_vars list, if needed.
These substitutions will ignore changes to os.environ
made in the calling script.
Source code in stepup/core/api.py
stepup.core.api.glob(*patterns, _required=False, _defer=False, **subs)
¶
Declare static paths through pattern matching.
Parameters:
-
*patterns
(str
, default:()
) –One or more patterns for static files or directories, relative to the current working directory. The patterns may contain (named) wildcards and one may specify the pattern for each named wildcard with the keyword arguments.
-
_required
(bool
, default:False
) –When True, an error will be raised when there are no matches.
-
_defer
(bool
, default:False
) –When True, static files are not added yet. Instead, the glob is installed in the workflow as a deferred glob. As soon as any file is needed as input and matches the pattern, it will be made static. This is not compatible with
_required=True
. Named wildcards are not supported in deferred globs. -
**subs
(str
, default:{}
) –When using named wildcards, they will match the pattern
*
by default. Through the subs argument each name can be associated with another glob pattern. Names starting with underscores are not allowed.
Raises:
-
FileNotFoundError
–when no matches were found and _required is True.
Returns:
-
ngm
–An
NGlobMulti
instance holding all the matched (combinations of) paths. This object acts as an iterator. When named wildcards are used, it iterates overNGlobMatch
instances. When using only anonymous wildcards, it iterates over unique paths. It also featuresngm.matches()
andngm.files()
iterators, with which the type of iterator can be overruled. Finally, one may also use ngm in conditional expressions: It evaluates to True if and only if it contains some matches.None
is returned when_defer=True
.
Notes
The combinatorics allow one to construct nested loops easily in one call.
For unrelated patterns, it may be more efficient to use separate glob
calls.
Environment variables in the patterns
will be
substituted directly and amend the current step’s env_vars list, if needed.
These substitutions will ignore changes to os.environ
made in the calling script.
Source code in stepup/core/api.py
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 212 213 214 215 216 217 218 219 |
|
stepup.core.api.step(command, *, inp=(), env=(), out=(), vol=(), workdir='./', optional=False, pool=None, block=False)
¶
Add a step to the build graph.
Parameters:
-
command
(str
) –Command to execute (in the working directory of the director).
-
inp
(Collection[str] | str
, default:()
) –File(s) required by the step. Relative paths are assumed to be relative to
workdir
. Can be files or directories (trailing slash). -
env
(Collection[str] | str
, default:()
) –Environment variable(s) to which the step is sensitive. If they change, or when they are (un)defined, the step digest will change, such that the step cannot be skipped.
-
out
(Collection[str] | str
, default:()
) –File(s) created by the step. Relative paths are assumed to be relative to
workdir
. These can be files or directories (trailing slash). -
vol
(Collection[str] | str
, default:()
) –Volatile file(s) created by the step Relative paths are assumed to be relative to
workdir
. Directories are not allowed. -
workdir
(str
, default:'./'
) –The directory where the command must be executed. A trailing slash is added when not present. If this is a relative path, it is relative to the work directory of the caller. (The default is the current directory.)
-
optional
(bool
, default:False
) –When set to True, the step is only executed when required by other mandatory steps.
-
pool
(str | None
, default:None
) –If given, the execution of this step is restricted to the pool with the given name. The maximum number of parallel steps running in this pool is determined by the pool size.
-
block
(bool
, default:False
) –When set to True, the step will always remain pending. This can be used to temporarily prevent part of the workflow from being executed.
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps.
Notes
Environment variables in the workdir
, inp
, out
and vol
paths and workdir will be
substituted directly and amend the current step’s env_vars list, if needed.
These substitutions will ignore changes to os.environ
made in the calling script.
Before sending the step to the director the variables ${inp}
, ${out}
and ${vol}
in the command are substituted by white-space concatenated list of inp
, out
and
vol
, respectively.
Relative paths in inp
, out
and vol
are relative to the working directory of the new step.
Source code in stepup/core/api.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
stepup.core.api.pool(name, size)
¶
Define a pool with given size or change an existing pool size.
Parameters:
-
name
(str
) –The name of the pool.
-
size
(int
) –The pool size.
stepup.core.api.amend(*, inp=(), env=(), out=(), vol=())
¶
Specify additional inputs and outputs from within a running step.
Parameters:
-
inp
(Collection[str] | str
, default:()
) –Files required by the step. Can be files or directories (trailing slash).
-
env
(Collection[str] | str
, default:()
) –Environment variables to which the step is sensitive. If the change, or when they are (un)defined, the step digest will change, such that the step is not skipped when these variables change.
-
out
(Collection[str] | str
, default:()
) –Files created by the step. Can be files or directories (trailing slash).
-
vol
(Collection[str] | str
, default:()
) –Volatile files created by the step. Can be files or directories (trailing slash).
Raises:
-
InputNotFoundError
–When amended inputs are not available yet. There is no need to catch this exception. Instead, just let it fail the calling script, so that it can be rescheduled for later execution. The director has been informed that some of the amended inputs were not available yet.
Notes
Environment variables in the inp
, out
and vol
paths are substituted in the same way
as in the step()
function. The used variables are added to the env_vars argument.
Always call amend before using the input files and before creating the output and volatile files.
Source code in stepup/core/api.py
stepup.core.api.getinfo()
¶
Get the information of the current step.
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps. For consistency with other functions in this module, the
inp
,out
andvol
paths are relative to the working directory of the step.
Source code in stepup/core/api.py
Composite API¶
stepup.core.api.plan(subdir, *, inp=(), env=(), out=(), vol=(), optional=False, pool=None, block=False)
¶
Run a plan.py
script in a subdirectory.
Parameters:
-
subdir
(str
) –The subdirectory in which another
plan.py
script can be found. The file must be executable and have#!/usr/bin/env python3
as its first line. A trailing slash is added when not present. -
inp
(Collection[str] | str
, default:()
) –See the
step()
function for more information. (Rarely needed for planning steps.) -
env
(Collection[str] | str
, default:()
) –See the
step()
function for more information. (Rarely needed for planning steps.) -
out
(Collection[str] | str
, default:()
) –See the
step()
function for more information. (Rarely needed for planning steps.) -
vol
(Collection[str] | str
, default:()
) –See the
step()
function for more information. (Rarely needed for planning steps.) -
optional
(bool
, default:False
) –See the
step()
function for more information. (Rarely needed for planning steps.) Use with care, since the nodes created by plan script will be unknown upfront and cannot therefore imply the necessity of an optional plan step. -
pool
(str | None
, default:None
) –See the
step()
function for more information. (Rarely needed for planning steps.) -
block
(bool
, default:False
) –See the
step()
function for more information. (Rarely needed for planning steps.)
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps.
Source code in stepup/core/api.py
stepup.core.api.copy(src, dst, *, optional=False, block=False)
¶
Add a step that copies a file.
Parameters:
-
src
(str
) –This must be a file. Environment variables are substituted.
-
dst
(str
) –This can be a file or a directory. Environment variables are substituted. If it is a directory, it must have a trailing slash.
-
optional
(bool
, default:False
) –When True, the file is only copied when needed as input for another step.
-
block
(bool
, default:False
) –When True, the step will always remain pending.
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps.
Source code in stepup/core/api.py
stepup.core.api.mkdir(dirname, *, optional=False, block=False)
¶
Make a directory.
Parameters:
-
dirname
(str
) –The director to create. A trailing slash is added when not present. Environment variables are substituted.
-
optional
(bool
, default:False
) –When True, the directory is only created when needed by other steps.
-
block
(bool
, default:False
) –When True, the step will always remain pending.
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps.
Source code in stepup/core/api.py
stepup.core.api.getenv(name, default=None, *, path=False, back=False, multi=False)
¶
Get an environment variable and amend the current step with the variable name.
Parameters:
-
name
(str
) –The name of the environment variable, which is retrieved with
os.getenv
. -
default
(Path | str | None
, default:None
) –The value to return when the environment variable is unset.
-
path
(bool
, default:False
) –Set to True if the variable taken from the environment is assumed to be a path. A Path instance will be returned. Shell variables are substituted (once) in such paths.
-
back
(bool
, default:False
) –Set to True to translate the path back to the working directory of the caller. If the path is relative, it is assumed to be relative to the StepUp’s working directory. It will be translated to become relative to the working directory of the caller. This implies
path=True
. -
multi
(bool
, default:False
) –Set to True if the variable is a list of paths. The paths are split on the colon character and returned as a list of
Path
instances. This impliespath=True
.
Returns:
-
value
–The value of the environment variable. If
path
is set toTrue
, this is aPath
instance. Ifback
is set toTrue
, this is a translatedPath
instance. Ifmulti
is set toTrue
, this is a list ofPath
instances. Otherwise, the result is a string.
Notes
The optional arguments of this function have changed in StepUp 2.0.0.
Source code in stepup/core/api.py
stepup.core.api.call(executable, *, prefix=None, fmt='auto', inp=None, env=(), out=None, vol=(), workdir='./', optional=False, pool=None, block=False, pars=None, **kwargs)
¶
Call an executable with a set of serialized arguments.
This function assumes that the executable implements StepUp’s call protocol.
Parameters:
-
executable
(str
) –The path of a local executable script to call. Environment variables are substituted. The path of the executable is assumed to be relative to this directory.
-
prefix
(str | None
, default:None
) –The prefix used to construct filenames of the input (serialized arguments) and optionally output file (serialized return value). If absent, the prefix is the stem of the executable.
-
fmt
(str
, default:'auto'
) –The format used for serialization of arguments (and optionally return values). Can be
"auto"
,"json"
or"pickle"
. In case"auto"
, the"json"
format is used, unless that fails, then"pickle"
is used as the fallback option. If input or output files are given, the format is deduced from their extension. -
inp
(Sequence | str | bool | None
, default:None
) –The path of the input file:
- If
None
: The arguments are JSON serialized and passed to the script on the command line. If the types of the keyword arguments are incompatible with JSON, a pickle file is created whose filename is derived fromprefix
. - If
True
: an input file is always written to a path derived fromprefix
andfmt
, even if no keyword arguments are given. - If
str
: an input file is written if some extra**kwargs
are given, andfmt
is deduced from the extension. Without keyword arguments, the input file is assumed to be the output of another step. - If
Sequence
, the first item is used according to one of the previous points, depending on its type. Remaining items are add to theinp
argument of thestep()
function, and are added tokwargs['inp']
.
- If
-
env
(Collection[str] | str
, default:()
) –See the
step()
function for more information. -
out
(Sequence | str | bool | None
, default:None
) –The path of the output file:
- If
None
: the script may write an output file. (This is the most flexible option.) The output path is derived fromprefix
andfmt
. The script is called with arguments--out={path_out}
and--amend-out
, so the script can decide whether to write the output file. - If
str
: the script is called with the argument--out={path_out}
and is expected to create this output file unconditionally. (Noamend(out=path_out)
is needed.) - If
True
, similar to the previous, except that the output path is derived fromprefix
andfmt
. - If
False
, the script is not called with--out
and is not expected to write an output file. (This is useful to keep things minimal.) - If
Sequence
, the first item is used according one of the previous points, depending on its type. Remaining items are add to theout
argument of thestep()
function, and are added tokwargs['out']
.
- If
-
vol
(Collection[str] | str
, default:()
) –See the
step()
function for more information. -
workdir
(str
, default:'./'
) –See the
step()
function for more information. -
optional
(bool
, default:False
) –See the
step()
function for more information. -
pool
(str | None
, default:None
) –See the
step()
function for more information. -
block
(bool
, default:False
) –See the
step()
function for more information. -
pars
(dict[str] | None
, default:None
) –A dictionary with additional parameters for the script. They will be merged with the arguments in
kwargs
. (This can be useful to pass arguments whose name coincide with the arguments above.) -
kwargs
–If given, these are serialized to the input file. If absent, no input file is written unless
inp
isTrue
.
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps.
Notes
This is an experimental feature introduced in StepUp 2.0.0. It may undergo significant revisions in future 2.x releases.
When the inp
, env
, out
and vol
arguments contain items,
they are also included in the keyword arguments passed to the script.
However, they do not count as extra keyword arguments to determine if an input file
must be written when inp
is a string or a sequence of strings.
When using the call protocol, it is recommended to add the following lines to .gitignore
:
Source code in stepup/core/api.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
|
stepup.core.api.script(executable, *, step_info=None, inp=(), env=(), out=(), vol=(), workdir='./', optional=False, pool=None, block=False)
¶
Run the executable with a single argument plan
in a working directory.
This function assumes that the executable implements StepUp’s script protocol.
Parameters:
-
executable
(str
) –The path of a local executable that will be called with the argument
plan
. The file must be executable. The path of the script is assumed to be relative to this directory. -
step_info
(str | None
, default:None
) –When given, the steps generated in the plan part of the executable are written to this
step_info
file. (See stepup.core.stepinfo module for the file format.) This filename is relative to the work directory. -
inp
(Collection[str] | str
, default:()
) –See the
step()
function for more information. -
env
(Collection[str] | str
, default:()
) –See the
step()
function for more information. -
out
(Collection[str] | str
, default:()
) –See the
step()
function for more information. -
vol
(Collection[str] | str
, default:()
) –See the
step()
function for more information. -
workdir
(str
, default:'./'
) –See the
step()
function for more information. -
optional
(bool
, default:False
) –See the
step()
function for more information. -
pool
(str | None
, default:None
) –See the
step()
function for more information. -
block
(bool
, default:False
) –See the
step()
function for more information.
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps.
Notes
- The arguments
inp
,env
,out
,vol
andpool
are rarely needed for script steps. They only apply to the plan stage of the script, not the run stage. - The
inp
argument may be useful when the planning is configured by some input files. - The optional argument never applies to the plan stage, and is passed on the the run stage.
Source code in stepup/core/api.py
Utilities for API extensions¶
stepup.core.api.subs_env_vars()
¶
A context manager for substituting environment variables and tracking the used variables.
The context manager yields a function, subs
, which takes a string with variables and
returns the substituted form.
All used variables are recorded and sent to the director with amend(env=...)
.
For example:
This function may be used in other API functions to substitute environment variables in all relevant paths.