examples: Reading apt (Debian package system) results v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 09 Apr 2019 22:53:40 +0200
branchv_0
changeset 257 a39066264509
parent 256 822ffd23d679
child 258 2868d772c27e
examples: Reading apt (Debian package system) results
relpipe-data/examples-apt.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/relpipe-data/examples-apt.xml	Tue Apr 09 22:53:40 2019 +0200
@@ -0,0 +1,99 @@
+<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>