Package: repro-tarfile
repro-tarfileA tiny, zero-dependency replacement for Python'starfilestandard library for creating reproducible/deterministic tar archives."Reproducible" or "deterministic" in this context means that the binary content of the tar archive is identical if you add files with identical binary content in the same order. It means you can reliably check equality of the contents of two tar archives by simply comparing checksums of the archive using a hash function like MD5 or SHA-256.This Python package provides aReproducibleTarFileclass that works exactly liketarfile.TarFilefrom the Python standard library, except that certain archive metadata are set to fixed values. See"How does repro-tarfile work?"below for details.You can also optionally install a command-line program,rptar. See"rptar command line program"below for more information.Looking instead to create reproducible/deterministic ZIP archives? Check out our sister package,repro-zipfile!Installationrepro-tarfile is available from PyPI. To install, run:pipinstallrepro-tarfileIt is also available from conda-forge. To install, run:condainstallrepro-tarfile-cconda-forgeUsageSimplyimport repro_tarfileand use it in the same way you would use regulartarfilefrom the Python standard library.importrepro_tarfilewithrepro_tarfile.open("archive.tar.gz","w:gz")astar:tar.add("examples/data.txt",arcname="data.txt")Note that files must be written to the archive in the same order to reproduce an identical archive. Be aware that functions that likeos.listdir,os.glob,Path.iterdir, andPath.globreturn files in a nondeterministic order—you should callsortedon their returned values first.Seeexamples/usage.pyfor an example script that you can run, andexamples/demo_vs_tarfile.pyfor a demonstration in contrast with the standard library's tarfile module.For more advanced usage, such as customizing the fixed metadata values, see the subsections under"How does repro-tarfile work?".rptar command-line programYou can optionally install a lightweight command-line program,rptar. This includes an additional dependency on thetyperCLI framework. You can install it either directly or using thecliextra with repro-tarfile. We recommend you usepipxfor installing Python CLIs into isolated virtual environments. You can also install it with regular pip, too.pipxinstallrptar# orpipxinstallrepro-tarfile[cli]rptar is designed to a partial drop-in replacement ubiquitoustarprogram. Userptar --helpto see the documentation. Here are some usage examples:# Archive one filerptar-czvfarchive.tar.gzsome_file.txt# Archive two filesrptar-czvfarchive.tar.gzfile1.txtfile2.txt# Archive many files with globrptar-czvfarchive.tar.gzsome_dir/*.txt# Archive directory recursivelyrptar-czvfarchive.tar.gzsome_dir/In addition to the fixed metadata values that repro-tarfile sets, rptar will also always sort all paths being archived.How does repro-tarfile work?Tar archives are not normally reproducible even when containing files with identical content because of metadata. In particular, the usual culprits are:Last-modified timestamps of added filesFile-system permissions (mode) of added filesFile owner user and group of added filesIf using gzip compression, the uncompressed filename in the gzip headerIf using gzip compression, the last modified timestamp the gzip headerrepro_tarfile.ReproducibleTarFileis a subclass oftarfile.TarFilethat overrides theaddfilemethod (which is also used interally byadd) with a version that set the above file metadata to fixed values. It also overrides thegzopenmethod used for gzip compression to override the gzip header values. Note that repro-tarfile does not modify the original files—it simply overrides the metadata written to the archive.You can effectively reproduce what repro-tarfile does in a.tar.gzcase with something like this:fromgzipimportGzipFilefrompathlibimportPathimporttarfilewithPath("archive.tar.gz").open("wb")asfp:withGzipFile(filename="",fileobj=fp,mode="wb",mtime=315532800)asgz:withtarfile.open(fileobj=gz,mode="w")astar:# Use write to add a file to the archivetarinfo=tar.gettarinfo("examples/data.txt",arcname="data.txt")tarinfo.mtime=315532800tarinfo.mode=0o644tarinfo.uid=0tarinfo.gid=0tarinfo.uname=""tarinfo.gname=""withPath("examples/data.txt").open("rb")asfp2:tar.addfile(tarinfo,fp2)It's kind of a pain! We believe repro-tarfile is sufficiently more convenient to justify a small package.See the next two sections for more details about the replacement metadata values and how to customize them.Fixed metadata valuesHere's a quick reference table of the fixed metadata values. You can use the associated environment variable to override a value.Metadata fieldDefaultEnvironment variableLast modified timestamp315532800(1980-01-01 00:00:00 UTC)SOURCE_DATE_EPOCHFile mode644(rw-r--r--)REPRO_TARFILE_FILE_MODEDirectory mode755(rwxr-xr-x)REPRO_TARFILE_DIR_MODEOwner user ID0REPRO_TARFILE_UIDOwner group ID0REPRO_TARFILE_GIDOwner user nameempty stringREPRO_TARFILE_UNAMEOwner group nameempty stringREPRO_TARFILE_GNAMEGzip archive filenameempty stringGzip last modified timestamp315532800(1980-01-01 00:00:00 UTC)SOURCE_DATE_EPOCHFor deeper explanations, see below.Last-modified timestampsTar archives store the last-modified timestamps of added files and directories. The default fixed value used by repro-tarfile is 315532800, which corresponds to 1980-01-01 00:00:00 UTC.You can customize this value with theSOURCE_DATE_EPOCHenvironment variable. If set, it will be used as the fixed value instead. This should be an integer corresponding to theUnix epoch timeof the timestamp you want to set, e.g.,1704067230for 2024-01-01 00:00:00 UTC.SOURCE_DATE_EPOCHis astandardcreated by theReproducible Builds projectfor software distributions.File-system permissionsTar archives store the file-system permissions of files and directories. The default permissions set for new files or directories often can be different across different systems or users without any intentional choices being made. (These default permissions are controlled by something calledumask.) repro-tarfile will set these to fixed values. By default, the fixed values are0o644(rw-r--r--) for files and0o755(rwxr-xr-x) for directories, which matches the common defaultumaskof0o022for root users on Unix systems. (The0oprefixis how you can write an octal—i.e., base 8—integer literal in Python.)You can customize these values using the environment variablesREPRO_ZIPFILE_FILE_MODEandREPRO_ZIPFILE_DIR_MODE. They should be in three-digit octalUnix numeric notation, e.g.,644forrw-r--r--.File owner user and groupIn typical file systems, every file and directory has an owner. Tar archives record the user and group information of the owner. If different users or systems are generating identical files and then archiving them, the owner information will likely be different. By default, repro-tarfile uses user and group IDs values of0, and empty strings for the user and group names. These are the standard values recommended by theReproducible Builds project.You can customize the user and group IDs using the environment variablesREPRO_TARFILE_UIDandREPRO_TARFILE_GID. The values should be integers. You can customize the user and group names using the environment variablesREPRO_TARFILE_UNAMEandREPRO_TARFILE_GNAME.Gzip header valuesThe gzip compression file format includes a header that contains metadata about the compressed file—in this case, the tar archive. This header includes the archive filename and the last modified timestamp of the archive. By default, repro-tarfile sets the archive filename to an empty string, and the last modified timestamp to the same default value as the added files last modified timestamp, 315532800, which corresponds to 1980-01-01 00:00:00 UTC.The environment variableSOURCE_DATE_EPOCHused to customize the added file last modified timestamp will also be used to set the gzip header last modified timestamp. Currently, we don't support a way to customize the archive filename override.Why care about reproducible tar archives?Tar archives are often useful when dealing with a set of multiple files, especially if the files are large and can be compressed. Creating reproducible tar archives is often useful for:Building a software package.This is a development best practice to make it easier to verify distributed software packages. See theReproducible Builds projectfor more explanation.Working with data.Verify that your data pipeline produced the same outputs, and avoid further reprocessing of identical data.Packaging machine learning model artifacts.Manage model artifact packages more effectively by knowing when they contain identical models.Related Tools and Alternativeshttps://diffoscope.org/Can do a rich comparison of archive files and show what specifically differshttps://salsa.debian.org/reproducible-builds/strip-nondeterminismPerl library for removing nondeterministic metadata from file archives