#------------------------------------------------------------------------------ #$Author: saulius $ #$Date: 2019-04-08 07:29:13 +0000 (Mon, 08 Apr 2019) $ #$Revision: 272 $ #$URL: svn+ssh://saulius.grazulis.lt/home/saulius/svn-repositories/idejos/well-behaving-programs/programs-with-binary-outputs.txt $ #$Id: programs-with-binary-outputs.txt 272 2019-04-08 07:29:13Z saulius $ #------------------------------------------------------------------------------ Designing *x and Linux CLI programs that produce binary outputs =============================================================== 1. Keywords MUST, MAY, SHOULD, SHOULD NOT in this text are interpreted as described in the RFC 2119 [1]. 2. This design pattern is meant for programs that always or occasionally produce binary files (such as PNG or JPEG images, archived GZ/BZ2/ZIP/etc. files and so on). PDF files are considered binary for the purpose of this memo. Programs MAY produce multiple files when called; a program SHOULD provide efficient means for separating multiple files, even when these files are output as a data stream to STDOUT. Rationale: data division into files may convey important (meta)information about the data being processed. The same pattern should be used when a program sometimes produces binary and sometimes text result (for example CIF downloader might download .cif (text) or .cif.gz (binary) files). 3. The program SHOULD NOT output a binary stream to STDOUT by default. Rationale: arbitrary binary streams clobber terminal settings and is most often useless to the user anyway. 4. If no output option is provided, the program SHOULD output a binary file into a file in the current working directory using a default name. The default name SHOULD be constructed using data ID, if such ID is available, or using the program name and a file format extension. If STDERR is a TTY, the program SHOULD output a standard 'NOTE' level message to STDERR stating the downloaded file name. Rationale: follow the "principle of a least surprise"; user should be able to locate downloaded files quickly an unambiguously. 5. Strictly speaking, The program does *not* need to implement option '--output-file' to specify the name of a single output file. Such behaviour is can be emulated by a redirection: prog --stdout id1234 > id1234.fmt Moreover, the '--output-file' in a shortened '--output' form will clash with the '--output-directory' option suggested below. The '--output-file' option MUST be implemented, however, if there is no '--stdout' option that would allow redirection of the output to STDOUT (and in that case, '--output-file -' MUST be the means to achieve such redirection). The '--output-file' MAY be implemented even when '--stdout' is available if the program would benefit from the information about the output file name (STDOUT redirections leave programs unaware about the name of the redirected file). 6. The program must implement option '--outdir' ('--output-directory') to specify output directory for multiple files. The directory specified in the '--outdir' option must exist before the program is run; if it does not, the program MUST output an error message about the missing output directory. Rationale: Making a mistake in the directory name would otherwise redirect data to an unexpected place; it seems that it is safe to assume such situation as an error. 7. The program MAY implement option '--prefix' for the prefix of the output files. The prefix MAY contain '/' in which case it denotes files in subdirectories. The prefix may contain %s-like formatting signs for specifying sharding. The intermediate directories MUST be created of they do not exist. Rationale: when creating directories for sharding we do not know the directory names in advance, and thus can not create them before running the program. 8. The program MAY implement option -s, --suffix to specify suffix (e.g. an extension) for downloaded files. 9. The program MAY implement option -d, --digits to specify number of digits (together with leading zeros) to be used for multiple file numbering. The '--digits' option is only necessary if the program numbers its output files (i.e. when it dos not use IDs for file names). Rationale (paragraphs 7-9): we need to have some control on the output names of the files so that they can be matched with the further pipeline; renaming files afterwards is awkward and error-prone. 10. The program MAY implement the option '--template' that specifies prefix and extension combined, with special wildcards indicating places where record IDs and other variable file name elements should be inserted. Examples are: the default behaviour: -t, --template "%{id}.%{ext}" the currently (2019-04-08) implemented COD sharding: -t, --template "cod/cif/%{c1,1}/%{c2,2}/%{c4,2}/%{id}.%{ext}" 11. If --template option is implemented, the program MAY skip implementation of --prefix, --digits and --suffix options. If the program implements the --prefix, --digits and --suffix options, it MAY skip the --template option. Rationale: the '--template' option and the {'--prefix', '--digits', '--suffix'} option set provide essentially the same functionality and thus having them all is redundant, either '--template' of the prefix option set will do. The '--template' is more flexible, and uses only a single option to achieve the same functionality, but it may be more tricky to implement and may be slower than a '--prefix/--digits/--suffix' set. If the '--prefix' option does not accept expansion wildcards, sharding will have to be implemented using external means (e.g. all files that go into a single shard will have to be downloaded using a separate command with a distinct --prefix value). 12. The --template option MUST implement the following expansion wildcards in the supplied values; the --suffix values MAY accept the following expansion wildcards: The following expansion expressions are recognised: %{id} -- the ID of the downloaded database record %{ext} -- the file name extension %{n} -- the number of the downloaded file %{04d} -- the file number printed out using '%04d' C format %{c4,2} -- 2 characters of the ID, starting with position 4 (1 based); useful for sharding %{F} -- Full year-month-day date of the download moment (as in date +%F). %{T} -- Full time of the download (as in date +%T). %{Y} -- Full year of the download (as in date +%Y). %{m} -- Month of the download (as in date +%m). %{d} -- Day of the download (as in date +%d). 13. If the program produces multiple binary files, it SHOULD place them into a current working directory by default, or in the output directory specified in the '--outdir' option. Rationale: the current working directory is the place where files are created by default. Any additional renaming is ad-hoc and may be confusing; it is better to control it explicitly with an option (--outdir destination-dir/, for example) 14. When downloading multiple files, the program SHOULD use data IDs if they are available as file name bases, with format extensions added. If no IDs are available, the program SHOULD use it's own name with a numbering suffix added as file basename, and a format specifying extension. Rationale: files must be easy to find and identify. All files should have unique names. Example: when gzip-packed PDB files from a PDB archive are downloaded from the PDB archive by a program 'pdb-download', the tree layout should be: pdb-download.d/ ├── 1knv.pdb.gz ├── 1xyz.pdb.gz └── 2ixs.pdb.gz 15. If a very large number (>1000) is to be produced, the files may be sharded in subdirectories, e.g.: cod-download.d/ ├── 2 │   └── 01 │   └── 00 │   ├── 2010000.cif.bz2 │   ├── 2010010.cif.bz2 │   └── 2010050.cif.bz2 └── 7 └── 00 ├── 04 │   ├── 7000400.cif.bz2 │   └── 7000410.cif.bz2 └── 05 ├── 7000500.cif.bz2 ├── 7000510.cif.bz2 └── 7000520.cif.bz2 Rationale: directories with very many files are awkward to process and may be not supported on some file systems. 16. The program SHOULD provide means to output all produced data to STDOUT using a specified option. Possible options are: a) --stdout b) --output - where '-' is the name of the standard output channel (STDOUT in this case). 17. When multiple files are requested to STDOUT, the program SHOULD resort to one of the following options: -- if files can be concatenated in such a way that the individual files can later be recovered from the single STDOUT data stream, that program MAY simply output all concatenated files to STDOUT; such method works, for example, for CIF files (text based format) or for gzip compressed texts (binary format); -- if there is no means to detect individual file limits (e.g. for variable file raw binary image rasters), then a tar archive with all files MAY be output instead of the concatenated individual files (GNU Tar format is preferred). Inside the archive, the files SHOULD have names constructed the methods indicated above, honouring --template, --prefix, --digits, --suffix and other relevant options. Rationale: we wan to be able to faithfully recover data and metadata from the output stream; we want to do this however in a simplest possible way. The tar format output is very powerful but may be tricky to implement, thus in many cases simpler file concatenation might be a viable option. References ========== 1. Bradner, S. (1997). Key words for use in RFCs to Indicate Requirement Levels, 1-3, http://www.ietf.org/rfc/rfc2119.txt.