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
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 220 221 222 223 224 225 226 227 228 229 230 231 |
|
stepup.core.api.step(action, *, inp=(), env=(), out=(), vol=(), workdir='./', optional=False, pool=None, block=False)
¶
Add a step to the build graph.
Parameters:
-
action
(str
) –Action to execute (in the given working directory).
-
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 action 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 action 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
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 334 335 336 337 338 339 340 341 |
|
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
stepup.core.api.graph(prefix)
¶
Composite API¶
stepup.core.api.runsh(command, *, inp=(), env=(), out=(), vol=(), workdir='./', optional=False, pool=None, block=False)
¶
Add a shell command to the build graph.
See step()
for the documentation of all optional arguments
and the return value.
Parameters:
-
command
(str
) –The command to execute, which will be interpreted by the shell.
Source code in stepup/core/api.py
stepup.core.api.runpy(command, *, inp=(), env=(), out=(), vol=(), workdir='./', optional=False, pool=None, block=False)
¶
Add a Python command to the build graph.
See step()
for the documentation of all optional arguments
and the return value.
Parameters:
-
command
(str
) –The path of the script and its command line arguments. Local imports will be detected and amended as inputs to the script.
Source code in stepup/core/api.py
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
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 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 |
|
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
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 |
|
stepup.core.api.loadns(*paths_variables, dir_out=None, do_amend=True)
¶
Load variable from Python, JSON, TOML or YAML files and put them in a namespace.
Parameters:
-
paths_variables
(str
, default:()
) –paths of Python, JSON, TOML or YAML files containing variable definitions. They are loaded in the given order, so later variable definitions may overrule earlier ones.
-
dir_out
(str | None
, default:None
) –This is used to translate paths defined in the variables files (relative to parent of the variable file) to paths relative to the parent of the output of the rendering task. If not given, the current working directory is used. This is only relevant for variables loaded from Python files.
-
do_amend
(bool
, default:True
) –If
True
, All loaded files are amended as inputs to the current step.
Returns:
-
variables
–A namespace with the variables.
Source code in stepup/core/api.py
stepup.core.api.render_jinja(*args, mode='auto', optional=False, block=False)
¶
Render the template with Jinja2.
Parameters:
-
args
(str | dict
, default:()
) –The first argument is the path to the template file. All the following position arguments can be one of the following two types:
- Paths to Python, JSON, TOML or YAML files with variable definitions. Variables defined in later files take precedence.
- A dictionary with additional variables. These will be JSON-serialized and passed on the command-line to the Jinja renderer. Variables in dictionaries take precedence over variables from files. When multiple dictionaries are given, later ones take precedence.
The very last argument is an output destination (directory or file).
-
mode
(str
, default:'auto'
) –The format of the Jinja placeholders:
- The default (auto) selects either
plain
orlatex
, based on the extension of the output file. - The
plain
format is the default Jinja style with curly brackets:{{ }}
etc. - The
latex
style replaces curly brackets by angle brackets:<< >>
etc.
- The default (auto) selects either
-
optional
(bool
, default:False
) –If
True
, the step is only executed when needed by other steps. -
block
(bool
, default:False
) –If
True
, the step will always remain pending.
Returns:
-
step_info
–Holds relevant information of the step, useful for defining follow-up steps.
Notes
At least some variables must be given, either as a file containing variables or as a dictionary.
Source code in stepup/core/api.py
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 |
|
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.