relpipe-data/examples-awk-debugging.xml
branchv_0
changeset 258 2868d772c27e
equal deleted inserted replaced
257:a39066264509 258:2868d772c27e
       
     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>Debugging AWK transformations</nadpis>
       
     6 	<perex>discovering variable mappings and transformation internals</perex>
       
     7 	<m:pořadí-příkladu>02200</m:pořadí-příkladu>
       
     8 
       
     9 	<text xmlns="http://www.w3.org/1999/xhtml">
       
    10 		
       
    11 		<p>In most cases, AWK transformations should be quite straightforward, but sometimes we need to look inside the box.</p>
       
    12 		
       
    13 		<h2>Mapping attributes to variables</h2>
       
    14 		
       
    15 		<p>
       
    16 			Relations have named attributes but in a language like AWK we work with named variables.
       
    17 			In most cases, the names will match 1:1. But not always.
       
    18 			The mapping is needed because not all valid attribute names are also valid variable names in particular language, thus sometimes some escaping or prefixing is necessary. 
       
    19 			So there is <code>--debug-variable-mapping</code> option for printing the mappings between attributes and variables.
       
    20 		</p>
       
    21 		
       
    22 		<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \
       
    23 	| relpipe-tr-awk \
       
    24 		--relation '.*' \
       
    25 			--for-each '1' \
       
    26 			--debug-variable-mapping \
       
    27 	| relpipe-out-tabular]]></m:pre>
       
    28 
       
    29 		<p>This option prepends additional relation with these metadata to the stream:</p>
       
    30 		
       
    31 		<pre><![CDATA[fstab.variableMapping:
       
    32  ╭────────────────────┬───────────────────╮
       
    33  │ attribute (string) │ variable (string) │
       
    34  ├────────────────────┼───────────────────┤
       
    35  │ device             │ device            │
       
    36  │ dump               │ dump              │
       
    37  │ mount_point        │ mount_point       │
       
    38  │ options            │ options           │
       
    39  │ pass               │ pass              │
       
    40  │ scheme             │ scheme            │
       
    41  │ type               │ type              │
       
    42  ╰────────────────────┴───────────────────╯
       
    43 Record count: 7
       
    44 fstab:
       
    45  ╭─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬───────────────────────────────────────┬────────────────┬────────────────╮
       
    46  │ scheme (string) │ device                      (string) │ mount_point (string) │ type (string) │ options                      (string) │ dump (integer) │ pass (integer) │
       
    47  ├─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼───────────────────────────────────────┼────────────────┼────────────────┤
       
    48  │ UUID            │ 29758270-fd25-4a6c-a7bb-9a18302816af │ /                    │ ext4          │ relatime,user_xattr,errors=remount-ro │              0 │              1 │
       
    49  │                 │ /dev/sr0                             │ /media/cdrom0        │ udf,iso9660   │ user,noauto                           │              0 │              0 │
       
    50  │                 │ /dev/sde                             │ /mnt/data            │ ext4          │ relatime,user_xattr,errors=remount-ro │              0 │              2 │
       
    51  │ UUID            │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home                │ btrfs         │ relatime                              │              0 │              2 │
       
    52  │                 │ /dev/mapper/sdf_crypt                │ /mnt/private         │ xfs           │ relatime                              │              0 │              2 │
       
    53  ╰─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴───────────────────────────────────────┴────────────────┴────────────────╯
       
    54 Record count: 5]]></pre>
       
    55 
       
    56 		<p>If we are interested only in the mappings, we should use it in combination with <code>--drop</code> option:</p>
       
    57 
       
    58 		<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \
       
    59 	| relpipe-tr-awk \
       
    60 		--relation '.*' \
       
    61 			--for-each '1' \
       
    62 			--debug-variable-mapping \
       
    63 			--drop \
       
    64 	| relpipe-out-tabular]]></m:pre>
       
    65 
       
    66 		<p>which skips the actual data:</p>
       
    67 		
       
    68 		<pre><![CDATA[fstab.variableMapping:
       
    69  ╭────────────────────┬───────────────────╮
       
    70  │ attribute (string) │ variable (string) │
       
    71  ├────────────────────┼───────────────────┤
       
    72  │ device             │ device            │
       
    73  │ dump               │ dump              │
       
    74  │ mount_point        │ mount_point       │
       
    75  │ options            │ options           │
       
    76  │ pass               │ pass              │
       
    77  │ scheme             │ scheme            │
       
    78  │ type               │ type              │
       
    79  ╰────────────────────┴───────────────────╯
       
    80 Record count: 7]]></pre>
       
    81 
       
    82 		<p>Because there were no collisions, variables have same names as attributes. But in this case:</p>
       
    83 				
       
    84 		<m:pre jazyk="bash"><![CDATA[relpipe-in-cli generate t 3 \
       
    85 		"if"      string \
       
    86 		"6pack"   string \
       
    87 		"spa ces" string \
       
    88 	| relpipe-tr-awk \
       
    89 		--relation t \
       
    90 			--debug-variable-mapping \
       
    91 			--drop \
       
    92 	| relpipe-out-tabular]]></m:pre>
       
    93 	
       
    94 		<p>mapping rules come in to the play:</p>
       
    95 		
       
    96 		<pre><![CDATA[t.variableMapping:
       
    97  ╭────────────────────┬───────────────────╮
       
    98  │ attribute (string) │ variable (string) │
       
    99  ├────────────────────┼───────────────────┤
       
   100  │ 6pack              │ _6pack            │
       
   101  │ if                 │ _if               │
       
   102  │ spa ces            │ spa_ces           │
       
   103  ╰────────────────────┴───────────────────╯
       
   104 Record count: 3]]></pre>
       
   105 
       
   106 		<p>in order to make variable names valid in AWK.</p>
       
   107 
       
   108 
       
   109 		<h2>Inspecting the internals of an AWK transformation</h2>
       
   110 		
       
   111 		<p>
       
   112 			The <code>relpipe-tr-awk</code> calls AWK as a child process and passes data of given relation to it for actual processing.
       
   113 			Because it executes <code>awk</code> program found on <code>$PATH</code>, we can easily switch the AWK implementations.
       
   114 			In the source code repository, there is <code>scripts/awk</code> – a wrapper script.
       
   115 			We can modify the <code>$PATH</code>, so this wrapper will be called by <code>relpipe-tr-awk</code>.
       
   116 			This script captures CLI arguments, STDIN, STDOUT, STDERR and the exit code and saves them to files in the temp directory.
       
   117 			Using GNU Screen and the <em>inotifywait</em> we can build a kind of IDE and watch what happens inside during the transformation:
       
   118 		</p>
       
   119 		
       
   120 		<m:img src="img/awk-wrapper-debug-1.png"/>
       
   121 		
       
   122 		<p>
       
   123 			So we can inspect the generated AWK code and the inputs and outputs of the AWK process.
       
   124 			Recommended usage is described in the <code>scripts/awk</code> script.
       
   125 		</p>
       
   126 
       
   127 	</text>
       
   128 	
       
   129 </stránka>