Package: argrecord
argrecordAn extension to argparse to automate the generation of logfiles and self-describing output files, and provide a Make-like functionality to re-run a script based on those automatically generated logfiles.IntroductionThis library can be used in the place ofargparse. It provides additional functionality to create and read logfiles or datafile headers to document the command that created them and re-run the same command.Additional decorators such asGooeycan still be used.It works with Python 3.The source code can be found atargrecord.UsageRecording script argumentsSimply replaceargparsewithargrecordand the classArgumentParserwithArgumentRecorder.TheArgumentRecorderclass provides three new methods:build_commentsreturns a multi-line string that represents the current script invocation; that is, the name of the script and the list arguments to it. We call these comments because they are designed to be included as the header of an output file and treated as a comment by whichever program subsequently treats the output file.write_commentswrites the comments to a file. The file can be specified either as a filename or a file object of an output file. Additional arguments specify whether additional comments (for example from an input file) or comments in the already existing file should be appended to the comments generated from the argument parser, and whether an already existing file should be backed by by appending a suffix to its name.Appending multiple sets of commments in a single logfile or output file allows the entire chain of commands the produced that file to be recorded.replay_requiredreturnsTrueorFalseindicating whether the script needs to be re-run. This is calculated by determining whether any of the input files to the script are newer than any of the currently existing output files.The methodadd_argumenttakes three additional arguments.inputandoutputindicate whether the argument represents the name of a file that is an input or output of the script.privateindicates that the argument should not be included in the comments.Replaying script argumentsDefault behaviourRun the scriptargreplayto re-run the commands that produced a logfile (or initial section of an output file). The default behaviour is to read a series ofrecipesfrom a logfile, detecting the name of the script and the input and output file(s) of each. Once it has read all the recipes, it processes them in reverse order. When it finds a command that needs to be re-run (because one or more of its input files is younger than one or more of its output files) it re-runs that command, then proceeds to the previous recipe. When a command is re-run, it typically creates a new output file that is an input file for the previous recipe, so that will in turn need to be re-run. The process continues until all the recipes have been processed, or a command returns an error.Pipesargreplayhas some special features that allow it to replay sequences of commands in which the output of one is piped to the input of the next. When recording script arguments, if it encounters an input argument (one that was flagged withinput) but no argument value, it assumes that the input came from standard input. Likewise for output arguments and standard output. When replaying a sequence in which a command writing to standard output is followed by one reading from standard input, a pipe is established between those two commands. Such a sequence may be arbitrarily long.Variable substitutionA recipe may include variables in its command arguments. For example,–year ${year}In this case,argreplaymust be given an argument--substitutethat contains the variables to be substituted and the values with which to substitute them, with a colon as separator. For example,argreplay--substituteyear:2019....Other options--dry-runsimply prints the commands that would be run. Note that since the command is not actually run, output files are not touched and subsequent commands that would be run will not be listed.--forcemeans that the commands are run regardless of the timestamps on input and output file(s).--depthindicates how many recipes to read from a logfile. The default is to read all the recipes.--removemeans that the logfile should be removed after it has been read but before any commands are run. This can help prevent the logfile growing too long if the commands will cause it to be extended.--guicausesGooeyto be invoked if it is available.