relpipe-data/examples-apt.xml
author František Kučera <franta-hg@frantovo.cz>
Mon, 27 Jul 2020 17:51:53 +0200
branchv_0
changeset 310 aeda3cb4528d
parent 257 a39066264509
child 326 ab7f333f1225
permissions -rw-r--r--
examples: Querying an RDF triplestore using SPARQL

<stránka
	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
	
	<nadpis>Reading apt (Debian package system) results</nadpis>
	<perex>using Recutils and/or Relational pipes</perex>
	<m:pořadí-příkladu>02000</m:pořadí-příkladu>

	<text xmlns="http://www.w3.org/1999/xhtml">
		
		<p>
			Debian, Ubuntu and other derived GNU/Linux distributions have their package system based on the <code>.deb</code> packages and tools like <code>apt</code> (or formerly <code>aptitude</code> and <code>apt-get</code>).
			This is a command line tool for installing packages and also for searching, showing metadata and other related tasks.
			We can show a metadata for a package this way:
		</p>
		
		<m:pre jazyk="text"><![CDATA[$ apt show emacs
Package: emacs
Version: 47.0
Priority: optional
Section: editors
Source: emacs-defaults
Installed-Size: 8 192 B
Supported: 5y
Download-Size: 1 748 B
Description: GNU Emacs editor (metapackage)
 GNU Emacs is the extensible self-documenting text editor.
 This is a metapackage that will always depend on the latest
 recommended Emacs release.
 …]]></m:pre>

		<p>
			This is some kind of ordinary human-readable listing.
			But, it seems familiar, like we have met somewhere before… Yes, it is almost the same format as recfile from <a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>!
			There are field names, colons and values. Each pair on a separate line.
			And there is also a convention for multi-line text – just with a little difference: indentation instead of <code>+</code>.
			But we can tune it with just a little <code>sed</code> transformation
			and then read it like a recfile:
		</p>
		
		<m:pre jazyk="bash"><![CDATA[apt show emacs25* \
	| sed 's/^ /+/g' \
	| relpipe-in-recfile \
	| relpipe-tr-cut '.*' 'Package|Version|Section|Installed-Size|Homepage|Supported' \
	| relpipe-out-tabular]]></m:pre>
	
		<p>And print results in a pretty table:</p>
		
		<pre><![CDATA[recfile:
 ╭─────────────────────────┬──────────────────┬────────────────────┬─────────────────────────┬────────────────────────────────────┬────────────────────╮
 │ Package        (string) │ Version (string) │ Section   (string) │ Installed-Size (string) │ Homepage                  (string) │ Supported (string) │
 ├─────────────────────────┼──────────────────┼────────────────────┼─────────────────────────┼────────────────────────────────────┼────────────────────┤
 │ emacs25                 │ 25.2+1-6         │ editors            │ 19,7 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
 │ emacs25-nox             │ 25.2+1-6         │ editors            │ 17,9 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
 │ emacs25-bin-common      │ 25.2+1-6         │ editors            │ 473 kB                  │ http://www.gnu.org/software/emacs/ │ 5y                 │
 │ emacs25-common-non-dfsg │ 25.2+1-1         │ multiverse/editors │ 4 685 kB                │                                    │                    │
 │ emacs25-common          │ 25.2+1-6         │ editors            │ 66,9 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
 │ emacs25-el              │ 25.2+1-6         │ editors            │ 16,3 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
 │ emacs25-dbg             │ 25.2+1-6         │ debug              │ 5 604 kB                │ http://www.gnu.org/software/emacs/ │ 5y                 │
 │ emacs25-nox-dbg         │ 25.2+1-6         │ debug              │ 3 996 kB                │ http://www.gnu.org/software/emacs/ │ 5y                 │
 ╰─────────────────────────┴──────────────────┴────────────────────┴─────────────────────────┴────────────────────────────────────┴────────────────────╯
Record count: 8]]></pre>

		<p>
			We can use also GNU Recutils, but we have to rename some fields like <code>Installed-Size</code> because the <code>-</code> hyphen does not seem to be edible.
			This can be fixed this way:
		</p>
		
		<m:pre jazyk="bash"><![CDATA[apt show emacs* \
	| sed -E \
		-e 's/^([^-]+)-([^-]+)-([^:]+)/\1_\2_\3/g' \
		-e 's/^([^-]+)-([^:]+)/\1_\2/g' \
		-e 's/^ /+/g' \
	| recsel -p Package,Version,Homepage]]></m:pre>

		<p>However, it is still quite dirty, because <code>apt</code> complains about:</p>
		
		<pre>WARNING: apt does not have a stable CLI interface. Use with caution in scripts.</pre>
		
		<p>
			So it would be better to introduce some machine-readable output in the <code>apt</code> tool.
			But until that: <em>Happy hacking!</em>
			For some ad-hoc operations it is usable and we can e.g. convert the results to a CSV or a LibreOffice Calc file (ODS).
		</p>
		
		<m:pre jazyk="bash"><![CDATA[apt show emacs25* \
	| sed 's/^ /+/g' \
	| relpipe-in-recfile \
	| relpipe-tr-cut '.*' 'Package|Version|Section|Installed-Size|Homepage|Supported' \
	| relpipe-out-ods \
	> apt-packages.fods]]></m:pre>
	
		<p>The machine-readable format should also carry the sizes in bytes instead of human-friendly units.</p>
		
		<p>Good news are that the <a href="https://www.gnu.org/software/guix/manual/en/html_node/Invoking-guix-package.html">Guix SD</a> package system already prints results in regular recfile format.</p>
		
	</text>

</stránka>