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(StrPath | Iterable[StrPath], default:()) –One or more paths to declare as static, relative to the current working directory. Arguments may also be iterables of strings. Each string must refer to an existing file or directory and can be one of:
- A file: declared immediately as a static path.
- A directory: registered as a static tree; files within it are lazily declared static the first time they are used as step inputs.
Raises:
-
ValueError–When a path does not exist, when an environment variable in a path is undefined, or when a path contains an invalid variable identifier.
Notes
Environment variables in paths are substituted immediately,
and the variables referenced are added to the calling step’s env_deps list.
These substitutions are based on the state of os.environ in the calling script,
at the time this function is called, not when the step is executed.
Source code in stepup/core/api.py
stepup.core.api.glob(*patterns, **subs)
¶
Declare static files through glob patterns and return the matches.
All matched files are declared static with the director, and the returned object can be iterated in the calling script.
Parameters:
-
*patterns(StrPath, default:()) –One or more glob patterns relative to the current working directory. Patterns may contain anonymous wildcards (
*,**) and named wildcards (${*name}). -
**subs(str, default:{}) –Override the sub-pattern matched by each named wildcard. By default every named wildcard matches
*.
Returns:
-
ngm–An
NGlobMultiinstance with all matched paths. Iteration yieldsNGlobMatchobjects when named wildcards are present, orPathobjects when only anonymous wildcards are used. Usengm.matches()orngm.files()to force either mode. Usengm.single()to assert and return exactly one matched path. Evaluates toTruein a boolean context when at least one match exists.
Notes
Multiple patterns are matched jointly: only combinations of files whose
named wildcard substitutions are mutually consistent are returned.
For independent patterns, separate glob calls are more efficient.
Environment variables in patterns are substituted before matching,
and the variables referenced are added to the calling step’s env_deps list.
These substitutions are based on the state of os.environ in the calling script,
at the time this function is called, not when the step is executed.
Source code in stepup/core/api.py
stepup.core.api.step(command, *, inp=(), env=(), out=(), vol=(), workdir='.', need=Need.DEFAULT, resources=None, shell=False, _add_exe=False, _need_relative_exe=False)
¶
Add a step to the build graph.
Parameters:
-
command(StrPath) –Command to execute (in the given working directory). When
shell=False, the command may start with one or moreVAR=valueassignments, e.g.OMP_NUM_THREADS=4 ./run.py. These are stripped from the command and applied as step-specific environment variable overrides when the step runs. This is the only way to set environment variables for a step withshell=False, as there is no shell to interpret such assignments. Note that these overrides (the variable values for the child process) are distinct fromenv(the variable names the step is sensitive to): a variable may not appear in both, otherwise aValueErroris raised. Withshell=True, assignments are left in the command for the shell to interpret. In that case, putting the same variables inenvalso invalid, but not detected by StepUp. -
inp(Collection[StrPath] | StrPath, default:()) –File(s) required by the step. Relative paths are assumed to be relative to
workdir. Directory inputs are not supported. -
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[StrPath] | StrPath, default:()) –File(s) created by the step. Relative paths are assumed to be relative to
workdir. Directory outputs are not supported. -
vol(Collection[StrPath] | StrPath, default:()) –Volatile file(s) created by the step. Relative paths are assumed to be relative to
workdir. Directory outputs are not supported. -
workdir(StrPath, default:'.') –The directory where the action must be executed. The path is normalized before further processing. If this is a relative path, it is relative to the work directory of the caller. (The default is the current directory.)
-
need(Need, default:DEFAULT) –The level of necessity for the step. Three values are allowed: -
Need.OPTIONAL= only execute the step if some of its outputs are (indirectly) needed by a non-optional step. -Need.DEFAULT= execute the step unless the user specifies targets. -Need.PLAN= always execute the step because it is part of the plan. -
resources(dict[str, int] | str | None, default:None) –Named resources required to run this step, e.g.
{"gpu": 1}. One may also provide the resources as a string, e.g."gpu:1,memgb:4". The step will not be scheduled until the required units are available, taking into account the units already held by other running steps. Resources not listed in--resources/STEPUP_RESOURCESare treated as unavailable. The required units must be strictly positive and default to 1 when not given, e.g."gpu"is equivalent to"gpu:1". -
_add_exe(bool, default:False) –(Internal.) When
True, if the first word of the command is a local relative executable (it contains a slash and is not absolute), it is added as an input. -
_need_relative_exe(bool, default:False) –(Internal.) When
True, require the first word of the command to be such a local relative executable, raising aValueErrorotherwise.
Returns:
-
step_info–Holds relevant information of the step, useful for defining follow-up steps.
Notes
Environment variables in inp, out, vol, and workdir are substituted immediately,
and the variables referenced are added to the calling step’s env_deps list.
These substitutions are based on the state of os.environ in the calling script,
at the time this function is called, not when the step is executed.
Before sending the step to the director, the variables ${inp}, ${out}, and ${vol}
in the command are substituted by the space-separated 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
220 221 222 223 224 225 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
stepup.core.api.amend(*, inp=(), env=(), out=(), vol=())
¶
Declare additional inputs, outputs, and environment dependencies from within a running step.
Parameters:
-
inp(Collection[StrPath] | StrPath, default:()) –Files required by the step. Relative paths are relative to the step’s working directory. Directory inputs are not supported.
-
env(Collection[str] | str, default:()) –Environment variables 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[StrPath] | StrPath, default:()) –Files created by the step. Relative paths are relative to the step’s working directory. Directory outputs are not supported.
-
vol(Collection[StrPath] | StrPath, default:()) –Volatile files created by the step. Relative paths are relative to the step’s working directory. Directory outputs are not supported.
Raises:
-
InputNotFoundError–When amended inputs are not yet available. Let this exception propagate — do not catch it. The director reschedules the step once the missing inputs become available.
Notes
Environment variables in inp, out, and vol are substituted immediately,
and the variables referenced are added to the calling step’s env_deps list.
These substitutions are based on the state of os.environ in the calling script,
at the time this function is called, not when the step is executed.
Always call amend() before reading input files and before writing output or volatile files.
Repeated calls are safe: items already amended in prior calls are silently skipped.
Source code in stepup/core/api.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 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 | |
stepup.core.api.getinfo()
¶
Get the information of the current step.
Returns:
-
step_info–Holds relevant information of the current step, useful for defining follow-up steps. For consistency with other functions in this module, the
inp,outandvolpaths 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.run(command, *, inp=(), env=(), out=(), vol=(), workdir='.', optional=False, shell=False, resources=None)
¶
Add a command to the build graph.
Parameters:
-
command(StrPath) –The command to execute, optionally followed by arguments. The execution method is selected automatically at run time:
- If
shell=True: the command is passed to a shell. Shell features like pipes and redirections are supported. - If
shell=Falseand the first word ends in.py: the script is executed via a Python wrapper that auto-detects local imports. Shell features are not available in this mode. - If
shell=Falseand the first word is a bare command name (no slashes) that matches aconsole_scriptsentry point in the current Python environment: the entry point is called in-process via the forkserver when available, avoiding subprocess overhead. If the entry point belongs to a different Python environment, a warning is logged and the command falls back to direct subprocess execution. - Otherwise: the command is executed directly without a shell. This is faster and safer than the shell mode.
When the first word contains a
/and is not an absolute path (e.g../script.py,subdir/tool), it is automatically added as an input dependency. Bare command names likeechoor absolute paths like/usr/bin/gccare not added.Python detection uses the
.pyfile extension only, so it works even when the script does not yet exist (e.g. it is an output of another step).shell=Truetakes precedence and disables Python auto-detection. - If
-
inp(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
env(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
out(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
vol(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
workdir(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
optional(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
resources(Collection[StrPath] | StrPath, default:()) –See
step()for more information.
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.plan(command, *, inp=(), env=(), out=(), vol=(), workdir='.', resources=None)
¶
Run a planning script.
The main difference with run() is that the step is flagged
as planner internally, which will give it higher priority than non-planner steps.
This results in earlier knowledge of the workflow, which improves scheduling efficiency.
Compared to the run() function, this function imposes optional=False and shell=False.
Parameters:
-
command(StrPath) –The command to execute, optionally followed by arguments. The execution method is selected automatically at run time:
- If the first word ends in
.py: the script is executed via a Python wrapper that auto-detects local imports. - Otherwise the command is executed directly without a shell. This scenario is highly unlikely but supported just for completeness.
Bare command names like
echoor absolute paths like/usr/bin/gccare not allowed. The command must always be a relative path to a local executable script. - If the first word ends in
-
inp(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
env(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
out(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
vol(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
workdir(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
resources(Collection[StrPath] | StrPath, default:()) –See
step()for more information.
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, resources=None)
¶
Add a step that copies a file.
Parameters:
-
src(StrPath) –This must be a file. Environment variables are substituted.
-
dst(StrPath) –This can be a file or a directory. Environment variables are substituted. If
dstdenotes a directory, it must have a trailing slash andsrcwill be copied inside it with its original name. Note that the trailing slash is not supported bypathlib.Path. It is recommended to use a string orpath.Pathfordstin this case. -
optional(bool, default:False) –See
step()for more information. -
resources(bool, default:False) –See
step()for more information.
Returns:
-
step_info–Holds relevant information of the step, useful for defining follow-up steps.
Notes
Environment variables in src and dst are substituted immediately,
and the variables referenced are added to the calling step’s env_deps list with amend().
These substitutions are based on the state of os.environ in the calling script,
at the time this function is called, not when the copy is actually made.
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(StrPath | 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
Pathinstances. This impliespath=True.
Returns:
-
value–The value of the environment variable. If
pathis set toTrue, this is aPathinstance. Ifbackis set toTrue, this is a translatedPathinstance. Ifmultiis set toTrue, this is a list ofPathinstances. Otherwise, the result is a string. All path variables are normalized.
Source code in stepup/core/api.py
stepup.core.api.call(executable_, function_, *, inp=(), env=(), out=(), vol=(), workdir='.', optional=False, planning=False, resources=None, args_file=None, **kwargs)
¶
Register a step that calls a named function in an executable.
Parameters:
-
executable_(StrPath) –Path to the script or binary to invoke. Must contain a path separator (e.g.
./script.pyorsub/script.py) and must not be an absolute path. Environment variables in the path are substituted. -
function_(str) –Name of the function to invoke (first positional CLI argument).
-
inp(Collection[StrPath] | StrPath, default:()) –Files declared as inputs to this step. Normalized to
list[str]. Also forwarded to the function asinp. -
env(Collection[str] | str, default:()) –Environment variables tracked by this step.
-
out(Collection[StrPath] | StrPath, default:()) –Files declared as outputs of this step. Normalized to
list[str]. Also forwarded to the function asout. -
vol(Collection[StrPath] | StrPath, default:()) –Volatile outputs of this step.
-
workdir(StrPath, default:'.') –Working directory for the step. Defaults to
".". -
optional(bool, default:False) –When
True, the step only runs if its outputs are (indirectly) needed by a non-optional step (Need.OPTIONAL). Mutually exclusive withplanning. -
planning(bool, default:False) –When
True, the step is scheduled as a planner (Need.PLAN). Use this when the called function registers further steps. Mutually exclusive withoptional. -
resources(dict[str, int] | str | None, default:None) –Resource constraints for this step.
-
args_file(StrPath | None, default:None) –Full filename for the serialized arguments. When given, arguments are written to this file (format inferred from extension) and passed via
--inp=<args_file>; when absent, a JSON string is embedded directly in the command. -
**kwargs–Additional keyword arguments forwarded to the function. Must be serializable to JSON by the
cattrsJSON converter.
Returns:
-
step_info–Holds relevant information of the registered step.
Raises:
-
ValueError–When
optionalandplanningare bothTrue. -
ValueError–When
executable_does not contain a path separator or is absolute. -
ValueError–When
function_is not a valid Python function name. -
ValueError–When the inline JSON string exceeds 128 KiB (use
args_fileinstead). -
ValueError–When
args_filehas an unrecognized extension.
Source code in stepup/core/api.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
stepup.core.api.script(executable, *, step_info=None, inp=(), env=(), out=(), vol=(), workdir='.', optional=False, resources=None)
¶
Run the executable with a single argument plan in a working directory.
Warning
The script interface for calling user Python scripts from plan.py has been deprecated
in favor of the new Call interface.
You are encouraged to migrate your plan.py files to the new API.
See the migration guide for a step-by-step walkthrough.
Parameters:
-
executable(StrPath) –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. Environment variables in the path are substituted. -
step_info(StrPath | None, default:None) –When given, the steps generated in the plan part of the executable are written to this
step_infofile. (See stepup.core.stepinfo module for the file format.) This filename is relative to the work directory. -
inp(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
env(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
out(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
vol(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
workdir(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
optional(Collection[StrPath] | StrPath, default:()) –See
step()for more information. -
resources(Collection[StrPath] | StrPath, default:()) –See
step()for more information.
Returns:
-
step_info–Holds relevant information of the step, useful for defining follow-up steps.
Notes
- The arguments
inp,env,outandvolare rarely needed for script steps. They only apply to the plan stage of the script, not the run stage. - The
inpargument 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
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 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | |
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(StrPath, 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. Environment variables in path names are substituted.
-
dir_out(StrPath | 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
dir_out. 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 SimpleNamespace instance with the variables, which can be accessed as attributes.
Source code in stepup/core/api.py
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 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 | |
stepup.core.api.dumpns(path, data, *, do_amend=True)
¶
Write variables to a JSON or YAML file.
Parameters:
-
path(StrPath) –Destination file path. The format is inferred from the extension:
.jsonfor JSON,.yamlor.ymlfor YAML. Environment variables in the path are substituted. -
data(dict | SimpleNamespace) –A
dictorSimpleNamespaceof variables to write.cattrs-supported types (attrs classes, dataclasses) are unstructured automatically. -
do_amend(bool, default:True) –If
True, the file is amended as an output of the current step before writing.
Raises:
-
ValueError–When the file extension is not
.json,.yaml, or.yml.
Source code in stepup/core/api.py
stepup.core.api.render_jinja(*args, mode='auto', optional=False, resources=None)
¶
Render the template with Jinja2.
Parameters:
-
args(StrPath | 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
plainorlatex, based on the extension of the output file. - The
plainformat is the default Jinja style with curly brackets:{{ }}etc. - The
latexstyle replaces curly brackets by angle brackets:<< >>etc.
- The default (auto) selects either
-
optional(bool, default:False) –See
step()for more information. -
resources(bool, default:False) –See
step()for more information.
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
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 | |