relpipe-data/examples-apt.xml
branchv_0
changeset 257 a39066264509
child 326 ab7f333f1225
equal deleted inserted replaced
256:822ffd23d679 257:a39066264509
       
     1 <stránka
       
     2 	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
       
     3 	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
       
     4 	
       
     5 	<nadpis>Reading apt (Debian package system) results</nadpis>
       
     6 	<perex>using Recutils and/or Relational pipes</perex>
       
     7 	<m:pořadí-příkladu>02000</m:pořadí-příkladu>
       
     8 
       
     9 	<text xmlns="http://www.w3.org/1999/xhtml">
       
    10 		
       
    11 		<p>
       
    12 			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>).
       
    13 			This is a command line tool for installing packages and also for searching, showing metadata and other related tasks.
       
    14 			We can show a metadata for a package this way:
       
    15 		</p>
       
    16 		
       
    17 		<m:pre jazyk="text"><![CDATA[$ apt show emacs
       
    18 Package: emacs
       
    19 Version: 47.0
       
    20 Priority: optional
       
    21 Section: editors
       
    22 Source: emacs-defaults
       
    23 Installed-Size: 8 192 B
       
    24 Supported: 5y
       
    25 Download-Size: 1 748 B
       
    26 Description: GNU Emacs editor (metapackage)
       
    27  GNU Emacs is the extensible self-documenting text editor.
       
    28  This is a metapackage that will always depend on the latest
       
    29  recommended Emacs release.
       
    30  …]]></m:pre>
       
    31 
       
    32 		<p>
       
    33 			This is some kind of ordinary human-readable listing.
       
    34 			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>!
       
    35 			There are field names, colons and values. Each pair on a separate line.
       
    36 			And there is also a convention for multi-line text – just with a little difference: indentation instead of <code>+</code>.
       
    37 			But we can tune it with just a little <code>sed</code> transformation
       
    38 			and then read it like a recfile:
       
    39 		</p>
       
    40 		
       
    41 		<m:pre jazyk="bash"><![CDATA[apt show emacs25* \
       
    42 	| sed 's/^ /+/g' \
       
    43 	| relpipe-in-recfile \
       
    44 	| relpipe-tr-cut '.*' 'Package|Version|Section|Installed-Size|Homepage|Supported' \
       
    45 	| relpipe-out-tabular]]></m:pre>
       
    46 	
       
    47 		<p>And print results in a pretty table:</p>
       
    48 		
       
    49 		<pre><![CDATA[recfile:
       
    50  ╭─────────────────────────┬──────────────────┬────────────────────┬─────────────────────────┬────────────────────────────────────┬────────────────────╮
       
    51  │ Package        (string) │ Version (string) │ Section   (string) │ Installed-Size (string) │ Homepage                  (string) │ Supported (string) │
       
    52  ├─────────────────────────┼──────────────────┼────────────────────┼─────────────────────────┼────────────────────────────────────┼────────────────────┤
       
    53  │ emacs25                 │ 25.2+1-6         │ editors            │ 19,7 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
       
    54  │ emacs25-nox             │ 25.2+1-6         │ editors            │ 17,9 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
       
    55  │ emacs25-bin-common      │ 25.2+1-6         │ editors            │ 473 kB                  │ http://www.gnu.org/software/emacs/ │ 5y                 │
       
    56  │ emacs25-common-non-dfsg │ 25.2+1-1         │ multiverse/editors │ 4 685 kB                │                                    │                    │
       
    57  │ emacs25-common          │ 25.2+1-6         │ editors            │ 66,9 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
       
    58  │ emacs25-el              │ 25.2+1-6         │ editors            │ 16,3 MB                 │ http://www.gnu.org/software/emacs/ │ 5y                 │
       
    59  │ emacs25-dbg             │ 25.2+1-6         │ debug              │ 5 604 kB                │ http://www.gnu.org/software/emacs/ │ 5y                 │
       
    60  │ emacs25-nox-dbg         │ 25.2+1-6         │ debug              │ 3 996 kB                │ http://www.gnu.org/software/emacs/ │ 5y                 │
       
    61  ╰─────────────────────────┴──────────────────┴────────────────────┴─────────────────────────┴────────────────────────────────────┴────────────────────╯
       
    62 Record count: 8]]></pre>
       
    63 
       
    64 		<p>
       
    65 			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.
       
    66 			This can be fixed this way:
       
    67 		</p>
       
    68 		
       
    69 		<m:pre jazyk="bash"><![CDATA[apt show emacs* \
       
    70 	| sed -E \
       
    71 		-e 's/^([^-]+)-([^-]+)-([^:]+)/\1_\2_\3/g' \
       
    72 		-e 's/^([^-]+)-([^:]+)/\1_\2/g' \
       
    73 		-e 's/^ /+/g' \
       
    74 	| recsel -p Package,Version,Homepage]]></m:pre>
       
    75 
       
    76 		<p>However, it is still quite dirty, because <code>apt</code> complains about:</p>
       
    77 		
       
    78 		<pre>WARNING: apt does not have a stable CLI interface. Use with caution in scripts.</pre>
       
    79 		
       
    80 		<p>
       
    81 			So it would be better to introduce some machine-readable output in the <code>apt</code> tool.
       
    82 			But until that: <em>Happy hacking!</em>
       
    83 			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).
       
    84 		</p>
       
    85 		
       
    86 		<m:pre jazyk="bash"><![CDATA[apt show emacs25* \
       
    87 	| sed 's/^ /+/g' \
       
    88 	| relpipe-in-recfile \
       
    89 	| relpipe-tr-cut '.*' 'Package|Version|Section|Installed-Size|Homepage|Supported' \
       
    90 	| relpipe-out-ods \
       
    91 	> apt-packages.fods]]></m:pre>
       
    92 	
       
    93 		<p>The machine-readable format should also carry the sizes in bytes instead of human-friendly units.</p>
       
    94 		
       
    95 		<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>
       
    96 		
       
    97 	</text>
       
    98 
       
    99 </stránka>