<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Valentin's Lab &#187; comp</title>
	<atom:link href="https://vaab.blog.kal.fr/category/sci/comp/feed/" rel="self" type="application/rss+xml" />
	<link>https://vaab.blog.kal.fr</link>
	<description>Ratiocination of an opensource techie</description>
	<lastBuildDate>Thu, 15 Nov 2018 08:04:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F&amp;language=en_US&amp;category=text&amp;title=Valentin%27s+Lab&amp;description=Ratiocination+of+an+opensource+techie&amp;tags=blog" type="text/html" />
	<item>
		<title>Fixing Windows Python 2.7 unicode issue with subprocess&#8217;s Popen.</title>
		<link>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/</link>
		<comments>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/#comments</comments>
		<pubDate>Thu, 16 Mar 2017 10:22:49 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=591</guid>
		<description><![CDATA[TL;DR Fixing Windows Python 2.7 unicode issue with subprocess.Popen being unable to properly send command lines to the system. Here is the python code fixing unicode issue Details What Python 2.7 is plagued by bugs that won't be fixed. One &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<div class="section" id="tl-dr">
<h3>TL;DR</h3>
<p>Fixing <a class="reference external" href="http://bugs.python.org/issue19264">Windows Python 2.7 unicode issue</a> with <tt class="docutils literal">subprocess.Popen</tt> being unable to properly
send command lines to the system.</p>
<p>Here is <a class="reference external" href="https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9">the python code fixing unicode issue</a></p>
</div>
<div class="section" id="details">
<h3>Details</h3>
<div class="section" id="what">
<h4>What</h4>
<p>Python 2.7 is plagued by bugs that won't be fixed.</p>
<p>One of them is that <strong>you can't use unicode chars on
sub process call command line</strong> via <tt class="docutils literal"><span class="pre">subprocess.Popen(..)</span></tt>
in windows platform.</p>
<p>This code fixes <tt class="docutils literal"><span class="pre">Popen(..)</span></tt> in python 2.7 under windows. This allows your
code simply work on python 2.7 with little changes.</p>
</div>
<div class="section" id="how">
<h4>How</h4>
<p>The following code leverage <tt class="docutils literal">ctypes</tt> to call the <tt class="docutils literal"><span class="pre">CreateProcessW(..)</span></tt>
function of the windows C API. This function should have been used
in cPython 2.7 but wasn't. This is the core reason why Python 2.7
on windows does not support unicode command line.</p>
<p>This is how Python 3.0+ works.</p>
</div>
<div class="section" id="the-code">
<h4>The code</h4>
<p><strong>This code was not thoroughly tested</strong> and I'm posting it on a gist so I
can update it and people can comment if they run into any issues.</p>
<p><strong>So here is</strong> <a class="reference external" href="https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9">the python code fixing unicode issue</a>.</p>
</div>
<div class="section" id="a-test">
<h4>A test ?</h4>
<p>As a test, we'll create a small <tt class="docutils literal">test.py</tt> python file
that you can call with the new <tt class="docutils literal"><span class="pre">subprocess.Popen(..)</span></tt> provided
in the Gist.</p>
<p>To be complete, we'll need another fix of python 2.7 unicode support: <a class="reference external" href="http://code.activestate.com/recipes/572200/">recipe to read the arguments in unicode</a>.</p>
<p>Here's the full code for the testing:</p>
<ul class="simple">
<li>with <tt class="docutils literal">subprocess_fix</tt> module being the code in the Gist. Correctly
sending the full unicode command line to the system. This is thus used on the calling side.</li>
<li>And <tt class="docutils literal">commandline_fix</tt> module being the recipe to
decode the current programs unicode command line with a final
added <tt class="docutils literal">sys.argv = win32_unicode_argv()</tt> statement. This is thus used on the called side.</li>
</ul>
<p>The caller side (named <tt class="docutils literal">test.py</tt>):</p>
<pre class="literal-block">
# -*- coding: utf-8 -*-

from subprocess import PIPE
from subprocess_fix import Popen

def indent(text, chars=&quot; &quot;, first=None):
    if first:
        first_line = text.split(&quot;\n&quot;)[0]
        rest = '\n'.join(text.split(&quot;\n&quot;)[1:])
        return '\n'.join([(first + first_line).rstrip(),
                          indent(rest, chars=chars)])
    return '\n'.join([(chars + line).rstrip()
                      for line in text.split('\n')])

p = Popen(u&quot;python reading.py ć&quot;, shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()

if p.returncode != 0:
    print(&quot;errlvl: %s&quot; % p.returncode)

if err:
    print(&quot;stderr:\n%s&quot; % indent(err, &quot; | &quot;))
if out:
    print(&quot;stdout:\n%s&quot; % indent(out, &quot; | &quot;))
</pre>
<p>The called side (named <tt class="docutils literal">reading.py</tt>):</p>
<pre class="literal-block">
# -*- coding: utf-8 -*-

import sys

import commandline_fix

print &quot;command line received (repr): %r&quot; % (sys.argv, )
print &quot;command line received (str): %s&quot; % (u&quot; &quot;.join(sys.argv), )
</pre>
<p><tt class="docutils literal">test.py</tt> uses the fixed <tt class="docutils literal"><span class="pre">Popen(..)</span></tt> to call <tt class="docutils literal">reading.py</tt> in a subprocess
and it specifies a unicode character in the command line arguments. <tt class="docutils literal">reading.py</tt> will
simply read the unicode command line and print it to the console.</p>
<p>To run the test:</p>
<pre class="literal-block">
python test.py
</pre>
<p>Notice that depending on your active charset (you can check with <tt class="docutils literal">chcp</tt>),
the console might fail to display the character correctly. You might need
then to do a <tt class="docutils literal">chcp 65001</tt>.</p>
</div>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=591&amp;md5=77d3033e88bd60e262aba556bd682af8" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2017%2F03%2F16%2Ffixing-windows-python-2-7-unicode-issue-with-subprocesss-popen%2F&amp;language=en_GB&amp;category=text&amp;title=Fixing+Windows+Python+2.7+unicode+issue+with+subprocess%26%238217%3Bs+Popen.&amp;description=TL%3BDR+Fixing+Windows+Python+2.7+unicode+issue+with+subprocess.Popen+being+unable+to+properly+send+command+lines+to+the+system.+Here+is+the+python+code+fixing+unicode+issue+Details+What+Python...&amp;tags=ctypes%2Cpython%2Cwindows%2Cblog" type="text/html" />
	</item>
		<item>
		<title>trapping SIGINT, SIGQUIT in asynchronous tasks</title>
		<link>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/</link>
		<comments>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/#comments</comments>
		<pubDate>Sat, 30 Jul 2016 10:36:31 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[bash signals]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=562</guid>
		<description><![CDATA[Scattered in bash documentation lies the truth about SIGINT and SIGQUIT signals being mysteriously ignored and non-trappable in background processes. This has puzzled many, and will probably continue to do so. Traps Here's my try to unveil the terrible truth. &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Scattered in bash documentation lies the truth about <tt class="docutils literal">SIGINT</tt>
and <tt class="docutils literal">SIGQUIT</tt> signals being mysteriously ignored and
non-trappable in background processes.</p>
<p>This has puzzled many, and will probably continue to do so.</p>
<div class="section" id="traps">
<h3>Traps</h3>
<p>Here's my try to unveil the terrible truth.</p>
<p>Let this script separate all cases and enlighten us:</p>
<pre class="literal-block">
cat &lt;&lt;'EOF' &gt; /tmp/test.sh
#!/bin/bash
traps_show_and_test() {
    local label=&quot;$1&quot;
    echo; echo &quot;TRAPS from $label:&quot;; trap
    trap &quot;echo modified by $label&quot; SIGINT  ## &lt;-- MODIFICATION
    echo &quot;TRAPS from $label (after modification attempt):&quot;; trap
}
export -f traps_show_and_test

traps_show_and_test main
{ traps_show_and_test subshell; }
( traps_show_and_test subparen )
bash -c 'traps_show_and_test bash_subprocess'

traps_show_and_test job &amp; wait
{ bash -c 'traps_show_and_test subshell_job'; } &amp; wait
( bash -c 'traps_show_and_test subparen_job' ) &amp; wait
bash -c 'traps_show_and_test bash_job' &amp; wait

echo
echo FINAL main traps:
trap

EOF
chmod +x /tmp/test.sh
/tmp/test.sh
</pre>
<p>Running the previous script in bash (version is
<tt class="docutils literal"><span class="pre">4.3.46(1)-release</span></tt>) gives the following:</p>
<pre class="literal-block">
TRAPS from main:
TRAPS from main (after modification attempt):
trap -- 'echo modified by main' SIGINT

TRAPS from subshell:
trap -- 'echo modified by main' SIGINT
TRAPS from subshell (after modification attempt):
trap -- 'echo modified by subshell' SIGINT

TRAPS from subparen:
trap -- 'echo modified by subshell' SIGINT
TRAPS from subparen (after modification attempt):
trap -- 'echo modified by subparen' SIGINT

TRAPS from bash_subprocess:
TRAPS from bash_subprocess (after modification attempt):
trap -- 'echo modified by bash_subprocess' SIGINT

TRAPS from job:
trap -- 'echo modified by subshell' SIGINT
TRAPS from job (after modification attempt):
trap -- 'echo modified by job' SIGINT

TRAPS from subshell_job:
TRAPS from subshell_job (after modification attempt):
trap -- 'echo modified by subshell_job' SIGINT

TRAPS from subparen_job:
TRAPS from subparen_job (after modification attempt):
trap -- 'echo modified by subparen_job' SIGINT

TRAPS from bash_job:
trap -- '' SIGINT
trap -- '' SIGQUIT
TRAPS from bash_job (after modification attempt):
trap -- '' SIGINT
trap -- '' SIGQUIT

FINAL main traps:
trap -- 'echo modified by subshell' SIGINT
</pre>
<p>Let's highlight important points:</p>
<ul class="simple">
<li>Foreground Jobs<ul>
<li>subshells from <tt class="docutils literal"><span class="pre">{..}</span></tt> and <tt class="docutils literal"><span class="pre">(..)</span></tt> inherit their traps from the main scope.</li>
<li>only <tt class="docutils literal"><span class="pre">{..}</span></tt> can modify the main scope.</li>
<li>subprocesses do not inherit its trap from its parent process</li>
<li>they all can modify at least locally their traps</li>
</ul>
</li>
<li>Background Jobs<ul>
<li>bash function inherits its traps from main scope but can't modify it (like <tt class="docutils literal"><span class="pre">(..)</span></tt>)</li>
<li>processes in <tt class="docutils literal"><span class="pre">{..}</span> &amp;</tt> and <tt class="docutils literal"><span class="pre">(..)</span> &amp;</tt> do not inherit traps</li>
<li>direct background subprocess have default signed traps that are umodifiable</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="bash-documentation">
<h3>Bash documentation</h3>
<p>As a reference, here are the documentation from bash related to these behaviors:</p>
<p>Process group id effect on background process (in <em>Job Control</em> section of doc):</p>
<blockquote>
[...] processes whose process group ID is equal to the current terminal
process group ID [..] receive keyboard-generated signals such as
SIGINT.  These processes are said  to  be  in the  foreground.
<strong>Background processes</strong> are those whose process group ID  differs from
the terminal's; such processes <strong>are immune to keyboard-generated
signals</strong>.</blockquote>
<p>Default handler for <tt class="docutils literal">SIGINT</tt> and <tt class="docutils literal">SIGQUIT</tt> (in <em>Signals</em> section of doc):</p>
<blockquote>
Non-builtin commands run by bash have signal handlers set to
the values inherited by the shell from its parent.  When job
control is not in effect, <strong>asynchronous commands ignore SIGINT
and SIGQUIT</strong> in addition to these inherited handlers.</blockquote>
<p>and about modification of traps (in <tt class="docutils literal">trap</tt> builtin doc):</p>
<blockquote>
<strong>Signals  ignored upon entry to the shell cannot be trapped or reset</strong>.</blockquote>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=562&amp;md5=880d404889c09219cc780ffed2083da5" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2016%2F07%2F30%2Ftrapping-sigint-sigquit-in-asynchronous-tasks%2F&amp;language=en_GB&amp;category=text&amp;title=trapping+SIGINT%2C+SIGQUIT+in+asynchronous+tasks&amp;description=Scattered+in+bash+documentation+lies+the+truth+about+SIGINT+and+SIGQUIT+signals+being+mysteriously+ignored+and+non-trappable+in+background+processes.+This+has+puzzled+many%2C+and+will+probably+continue+to+do...&amp;tags=bash+signals%2Cblog" type="text/html" />
	</item>
		<item>
		<title>docker update, or how to incrementally build images without Dockerfile.</title>
		<link>https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/</link>
		<comments>https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/#comments</comments>
		<pubDate>Wed, 28 Jan 2015 02:20:56 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[admin]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[incremental]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=546</guid>
		<description><![CDATA[This handy little script covers an important missing feature in current docker's tool set. I needed to build incrementally my images: From a given image, I want full script control (un-tamed shell scripts) and access to hosts files to do &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>This handy little script covers an important missing feature in
current docker's tool set.</p>
<p>I needed to build incrementally my images:</p>
<p>From a given image, I want full script control (un-tamed shell
scripts) and access to hosts files to do whatever I want to
do. At the end, I need to commit the filesystem (and only the filesystem).</p>
<p><tt class="docutils literal"><span class="pre">docker-update</span></tt> manage all this:</p>
<pre class="literal-block">
docker-update my-docker-image -v /srv/files:/mnt/files &lt;&lt;EOF

## Full fledged shell script, will run in docker container
## have access to /mnt/files

cp /mnt/files/data /opt/myapp/data

...

EOF
</pre>
<p>A last thing about the <tt class="docutils literal"><span class="pre">docker-update</span></tt> script, is that it
tries to be clever (I know that's often the beginning of hell):
if you apply the same code to the same image ID, it won't execute
it, but will use the previous result instead.</p>
<p>You can disable the cache by inserting <tt class="docutils literal"># docker: ALWAYS</tt> in
your bash code.</p>
<p>You can look at the <a class="reference external" href="https://gist.github.com/vaab/89d710f452b1d4fc7912">source code of docker-update</a>, or directly
<a class="reference external" href="https://gist.githubusercontent.com/vaab/89d710f452b1d4fc7912/raw/7385874aba683250e873d56e4912b940f9c41be4/docker-update">download docker-update</a>.</p>
<div class="section" id="how-does-it-work">
<h3>How does it work</h3>
<p>It use <tt class="docutils literal">docker run <span class="pre">--entrypoint</span> /bin/bash IMAGE</tt> on the bash
script, then <tt class="docutils literal">docker commit</tt>.</p>
<p>Well that's the general
outline. In the detail, <tt class="docutils literal">docker</tt> <a class="reference external" href="https://github.com/docker/docker/issues/4362">current shortcomings</a> are making this idea much more
complex than it should.</p>
<p>The first <tt class="docutils literal">docker run</tt> will modify ENTRYPOINT and CMD of image,
so when you commit, you break your previous values. So
<tt class="docutils literal"><span class="pre">docker-update</span></tt> needs to save them, and push another commit to
set them back.</p>
</div>
<div class="section" id="docker-update-shortcomings">
<h3>docker-update shortcomings</h3>
<p>As a quick and dirty solution, this script has some shortcomings
and sharp corners.  Some of these could easily repaired, and
additions are welcome.</p>
<div class="section" id="nul-characters-in-your-code">
<h4>NUL characters in your code</h4>
<p>Any <tt class="docutils literal">NUL</tt> character from your bash script will be removed prior to be run.
That's a direct consequence of running <tt class="docutils literal">bash <span class="pre">-c</span> &quot;$code&quot;</tt>. There are probably
better ways to do this.</p>
</div>
<div class="section" id="complex-cmd-and-entrypoint">
<h4>Complex CMD and ENTRYPOINT</h4>
<p>I rely on the fact that the output format of <tt class="docutils literal">{{json .Config.Cmd}}</tt> templating system
is directly evaluable in the Dockerfile as arguments to CMD and ENTRYPOINT.</p>
<p>I'm pretty sure that this is bad. But it works in simple cases.</p>
</div>
<div class="section" id="caching">
<h4>Caching</h4>
<p>Remember that by default, your code will be checked wether it was already
run on the same image (identified by it's ID). If you don't want that to
happen, include <tt class="docutils literal"># docker: ALWAYS</tt>.</p>
<p>For instance:</p>
<pre class="literal-block">
docker-update MYIMAGE &lt;&lt;EOF
# docker: always
apt-get update &amp;&amp; apt-get upgrade -y
EOF
</pre>
</div>
<div class="section" id="docker-update-options">
<h4>docker-update options</h4>
<p>They are sent directly to <tt class="docutils literal">docker run</tt> command, and you can easily break
the command. It's primarily meant to be used with the <tt class="docutils literal"><span class="pre">-v</span></tt> option.</p>
</div>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=546&amp;md5=c2d6b5482df2e9d72fed4fb6d079f837" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2015%2F01%2F28%2Fdocker-update-or-how-to-incrementally-build-images-without-dockerfile%2F&amp;language=en_GB&amp;category=text&amp;title=docker+update%2C+or+how+to+incrementally+build+images+without+Dockerfile.&amp;description=This+handy+little+script+covers+an+important+missing+feature+in+current+docker%27s+tool+set.+I+needed+to+build+incrementally+my+images%3A+From+a+given+image%2C+I+want+full+script+control...&amp;tags=docker%2Cimage%2Cincremental%2Cblog" type="text/html" />
	</item>
		<item>
		<title>bash lore: how to properly parse NUL separated fields</title>
		<link>https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/</link>
		<comments>https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/#comments</comments>
		<pubDate>Sat, 03 Jan 2015 09:26:28 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[nul]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=513</guid>
		<description><![CDATA[As a lot of other part of bash, this is black magic. Lets suppose a friendly command that spits out NUL separated fields (as find -print0, shyaml get-values-0, ...). Which - may I insist - is the recommended way to &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>As a lot of other part of bash, this is black magic.</p>
<p>Lets suppose a friendly command that spits out <tt class="docutils literal">NUL</tt> separated fields (as <tt class="docutils literal">find <span class="pre">-print0</span></tt>, <tt class="docutils literal">shyaml <span class="pre">get-values-0</span></tt>, ...). Which - may I insist - is the recommended way to communicate wild binary data in a solid way in bash.</p>
<p>How would you parse adequately each individual records by paquets ?</p>
<p>For the purpose of demonstration, lets use the fixed content of a simple <tt class="docutils literal">data.bin</tt> file
as our <tt class="docutils literal">NUL</tt>-separated input:</p>
<pre class="literal-block">
cat &lt;&lt;EOF | tr : &quot;\000&quot; &gt; /tmp/data.bin
a:1:b:2 3:c:4
  5:d:6\n7:e::f:9
EOF
</pre>
<p>Let's verify that we have our <tt class="docutils literal">NUL</tt> bytes:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | hexdump -v -e '/1 &quot;%02X &quot;'
61 00 31 00 62 00 32 20 33 00 63 00 34 0A 20 20 35 00 64 00 36 5C 6E 37 00 65 00 00 66 00 39 0A
</pre>
<p>You have noticed that we have some values containing:</p>
<blockquote>
<ul class="simple">
<li>spaces (hex: <tt class="docutils literal">20</tt>),</li>
<li>line breaks (hex: <tt class="docutils literal">0A</tt>),</li>
<li>a <tt class="docutils literal">\</tt> followed by a <tt class="docutils literal">n</tt>.</li>
<li>a 0 sized value</li>
<li>a final value <tt class="docutils literal">9</tt> ending with a <tt class="docutils literal">0a</tt> and no final <tt class="docutils literal">00</tt>.</li>
</ul>
</blockquote>
<p>If using NUL separated fields is recommended, it's to support this kind of data.</p>
<p>I want the implementation of a function <tt class="docutils literal"><span class="pre">read-0</span></tt> that would allow this type of interaction:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | while read-0 f1 f2; do
    echo &quot;f1: '$f1', f2: '$f2'&quot;
  done
f1: 'a', f2: '1'
f1: 'b', f2: '2 3'
f1: 'c', f2: '4
  5'
f1: 'd', f2: '6\n7'
f1: 'e', f2: ''
f1: 'f', f2: '9
'
</pre>
<div class="section" id="first-try">
<h3>First try</h3>
<p>Let's be naive, and we'll use <tt class="docutils literal">read f1 f2</tt>:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | while read f1 f2; do echo &quot;f1: '$f1', f2: '$f2'&quot;; done
f1: 'a1b2', f2: '3c4'
f1: '5d6n7ef9', f2: ''
</pre>
<p>You can notice that:</p>
<blockquote>
<ul class="simple">
<li><tt class="docutils literal">NUL</tt> char where ignored for field separation</li>
<li>fields where separated upon <strong>consecutive</strong> space or return, it uses value stored in <tt class="docutils literal">IFS</tt> environment variable.</li>
<li>their are only 2 lines because the <tt class="docutils literal">\n</tt> was used to separate each record. We should use <tt class="docutils literal"><span class="pre">-d</span></tt> to specify the line delimiter.</li>
<li>Note that the <tt class="docutils literal">NUL</tt> chars are also extracted out of the data as variables don't support the NUL char.</li>
<li>The <tt class="docutils literal">\</tt> was eaten, because <tt class="docutils literal">read</tt> builtin parse and give it special meaning. We should use <tt class="docutils literal"><span class="pre">-r</span></tt> to avoid that.</li>
</ul>
</blockquote>
<p>But how should we provide the <tt class="docutils literal">NUL</tt> delimiter to the read builtin ? knowing that you can't put <tt class="docutils literal">NUL</tt> chars on the command line ? Hopefully I stumbled onto this blog post: <a class="reference external" href="http://transnum.blogspot.sg/2008/11/bashs-read-built-in-supports-0-as.html">http://transnum.blogspot.sg/2008/11/bashs-read-built-in-supports-0-as.html</a></p>
<p>Conclusion is that <tt class="docutils literal"><span class="pre">-d</span> ''</tt> should be understood magically by bash <tt class="docutils literal">read</tt> builtin to delimit lines with <tt class="docutils literal">NUL</tt>
characters.</p>
</div>
<div class="section" id="better-try">
<h3>Better try</h3>
<p>Let's apply our new acquired knowledge by trying <tt class="docutils literal"><span class="pre">IFS=$'\0'</span> read <span class="pre">-d</span> '' <span class="pre">-r</span> f1 f2</tt>:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | while IFS=$'\0' read -d '' -r f1 f2; do echo &quot;f1: '$f1', f2: '$f2'&quot;; done
f1: 'a', f2: ''
f1: '1', f2: ''
f1: 'b', f2: ''
f1: '2 3', f2: ''
f1: 'c', f2: ''
f1: '4
  5', f2: ''
f1: 'd', f2: ''
f1: '6\n7', f2: ''
f1: 'e', f2: ''
f1: '', f2: ''
f1: 'f', f2: ''
</pre>
<p>That's much better. But notice that:</p>
<blockquote>
<ul class="simple">
<li>we didn't get anything in <tt class="docutils literal">$f2</tt>, that's normal: by specifying <tt class="docutils literal">NUL</tt> as line delimiter (with <tt class="docutils literal"><span class="pre">-d</span> ''</tt>) and having NUL as field delimiter (<tt class="docutils literal">IFS</tt>) we will be doomed to have one field per record. We will need to manage the repacking in a <tt class="docutils literal">while</tt> loop. This doesn't sound too difficult.</li>
<li>where's the final field <tt class="docutils literal">0A</tt> ? Hum, as there is no <tt class="docutils literal">NUL</tt> final character in the data, <tt class="docutils literal">read</tt> returned errlvl 1 on this last field but filled correctly the variable. A simple <tt class="docutils literal">echo $f1</tt> prints <tt class="docutils literal">9</tt> (if you use this form: <tt class="docutils literal">while <span class="pre">IFS=''</span> read <span class="pre">-d</span> '' <span class="pre">-r</span> f1 f2; do echo &quot;f1: '$f1', f2: <span class="pre">'$f2'&quot;;</span> done &lt; /tmp/data.txt</tt> to access variables of the <tt class="docutils literal">while</tt>).</li>
</ul>
</blockquote>
</div>
<div class="section" id="final-implementation">
<h3>Final Implementation ?</h3>
<p>So knowing this, here is the final implemetation of <tt class="docutils literal"><span class="pre">read-0</span></tt>:</p>
<pre class="literal-block">
read-0() {
    local eof
    eof=
    while [ &quot;$1&quot; -a -z &quot;$eof&quot; ]; do
        IFS='' read -r -d '' &quot;$1&quot; || eof=true
        shift
    done
    test &quot;$eof&quot; != true -o -z &quot;$1&quot;
}
</pre>
<p>Final ? It surely properly works for our specification test. But what happens if <tt class="docutils literal">EOF</tt> happens before
we have fed all the variables ?:</p>
<pre class="literal-block">
$ echo -n &quot;a&quot; | while read-0 f1 f2; do echo &quot;f1: '$f1', f2: '$f2'&quot;; done
$
</pre>
<p>Nothing is spit out, despite the fact that we have sent a character.</p>
<p>This is now a specification issue: Do we want <tt class="docutils literal"><span class="pre">read-0</span></tt> to return errorlevel 0 when it hits <tt class="docutils literal">EOF</tt>
while filing the variables ? Okay, but we said 0-sized string was a possible value... <tt class="docutils literal"><span class="pre">read-0</span></tt> in the
current specification knows it hit <tt class="docutils literal">EOF</tt> while filling variables as your first variable can be the
0-sized string. We could make a special case, but I want to be able to distinguish a last empty element from an element.</p>
<p>That <tt class="docutils literal"><span class="pre">read-0</span></tt>, in the actual specification, can't do it. But we can offer a slight change in the way you build your while loop to allow that parsing.</p>
</div>
<div class="section" id="correct-implementation">
<h3>Correct Implementation</h3>
<p>To fill partial records, will need another specification change as current implementation will fail whenever it encounters EOF. This is an incompatible specification issue. Aside from this, we need also to take care to actually set the value of the remaining fields to the empty string. This will require to use another version of <tt class="docutils literal"><span class="pre">read-0</span></tt>:</p>
<pre class="literal-block">
read-0() {
    local eof
    eof=
    while [ &quot;$1&quot; ]; do
        IFS='' read -r -d '' -- &quot;$1&quot; || eof=true
        shift
    done
    test &quot;$eof&quot; != true
}
</pre>
<p>So this would work with <tt class="docutils literal"><span class="pre">read-0</span></tt>:</p>
<pre class="literal-block">
$ echo -n a | tr :  '\000' | {  eof= ; while [ -z $eof ]; do read-0 f1 f2 || eof=true; echo &quot;f1: '$f1', f2: '$f2'&quot;; done  }
f1: 'a', f2: ''

$ echo -n a: | tr :  '\000' | {  eof= ; while [ -z $eof ]; do read-0 f1 f2 || eof=true; echo &quot;f1: '$f1', f2: '$f2'&quot;; done  }
f1: 'a', f2: ''
</pre>
<p>Basically, this construct allows a last round in the loop after detecting EOF... and achieve the starting spec:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | {  eof= ; while [ -z $eof ]; do read-0 f1 f2 || eof=true; echo &quot;f1: '$f1', f2: '$f2'&quot;; done  }
f1: 'a', f2: '1'
f1: 'b', f2: '2 3'
f1: 'c', f2: '4
  5'
f1: 'd', f2: '6\n7'
f1: 'e', f2: ''
f1: 'f', f2: '9
'
</pre>
<p>Trivial ?</p>
<p>Happy hacking.</p>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=513&amp;md5=57730888592c27e128ffadf2e707ab79" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2015%2F01%2F03%2Fbash-lore-how-to-properly-parse-nul-separated-fields%2F&amp;language=en_GB&amp;category=text&amp;title=bash+lore%3A+how+to+properly+parse+NUL+separated+fields&amp;description=As+a+lot+of+other+part+of+bash%2C+this+is+black+magic.+Lets+suppose+a+friendly+command+that+spits+out+NUL+separated+fields+%28as+find+-print0%2C+shyaml+get-values-0%2C+...%29.+Which...&amp;tags=bash%2Cnul%2Cblog" type="text/html" />
	</item>
		<item>
		<title>bash lore: NUL character and variables</title>
		<link>https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/</link>
		<comments>https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/#comments</comments>
		<pubDate>Sat, 03 May 2014 10:29:27 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=489</guid>
		<description><![CDATA[Bash variables are often thought as being able to store any binary content. Please bear in mind that it can't store NUL character, and only this one. Here you are: $ ascii_table() { echo -en &#34;$(echo '\'0{0..3}{0..7}{0..7} &#124; tr -d &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Bash variables are often thought as being able to store any binary content.</p>
<p>Please bear in mind that it can't store <tt class="docutils literal">NUL</tt> character, and only this one.</p>
<p>Here you are:</p>
<pre class="literal-block">
$ ascii_table() { echo -en &quot;$(echo '\'0{0..3}{0..7}{0..7} | tr -d &quot; &quot;)&quot;; }
$ ascii_table | hd
00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
00000010  10 11 12 13 14 15 16 17  18 19 1a 1b 1c 1d 1e 1f  |................|
00000020  20 21 22 23 24 25 26 27  28 29 2a 2b 2c 2d 2e 2f  | !&quot;#$%&amp;'()*+,-./|
00000030  30 31 32 33 34 35 36 37  38 39 3a 3b 3c 3d 3e 3f  |0123456789:;&lt;=&gt;?|
00000040  40 41 42 43 44 45 46 47  48 49 4a 4b 4c 4d 4e 4f  |&#64;ABCDEFGHIJKLMNO|
00000050  50 51 52 53 54 55 56 57  58 59 5a 5b 5c 5d 5e 5f  |PQRSTUVWXYZ[\]^_|
00000060  60 61 62 63 64 65 66 67  68 69 6a 6b 6c 6d 6e 6f  |`abcdefghijklmno|
00000070  70 71 72 73 74 75 76 77  78 79 7a 7b 7c 7d 7e 7f  |pqrstuvwxyz{|}~.|
00000080  80 81 82 83 84 85 86 87  88 89 8a 8b 8c 8d 8e 8f  |................|
00000090  90 91 92 93 94 95 96 97  98 99 9a 9b 9c 9d 9e 9f  |................|
000000a0  a0 a1 a2 a3 a4 a5 a6 a7  a8 a9 aa ab ac ad ae af  |................|
000000b0  b0 b1 b2 b3 b4 b5 b6 b7  b8 b9 ba bb bc bd be bf  |................|
000000c0  c0 c1 c2 c3 c4 c5 c6 c7  c8 c9 ca cb cc cd ce cf  |................|
000000d0  d0 d1 d2 d3 d4 d5 d6 d7  d8 d9 da db dc dd de df  |................|
000000e0  e0 e1 e2 e3 e4 e5 e6 e7  e8 e9 ea eb ec ed ee ef  |................|
000000f0  f0 f1 f2 f3 f4 f5 f6 f7  f8 f9 fa fb fc fd fe ff  |................|
00000100
</pre>
<p>But:</p>
<pre class="literal-block">
$ echo -n &quot;$(ascii_table)&quot; | hd
00000000  01 02 03 04 05 06 07 08  09 0a 0b 0c 0d 0e 0f 10  |................|
00000010  11 12 13 14 15 16 17 18  19 1a 1b 1c 1d 1e 1f 20  |............... |
00000020  21 22 23 24 25 26 27 28  29 2a 2b 2c 2d 2e 2f 30  |!&quot;#$%&amp;'()*+,-./0|
00000030  31 32 33 34 35 36 37 38  39 3a 3b 3c 3d 3e 3f 40  |123456789:;&lt;=&gt;?&#64;|
00000040  41 42 43 44 45 46 47 48  49 4a 4b 4c 4d 4e 4f 50  |ABCDEFGHIJKLMNOP|
00000050  51 52 53 54 55 56 57 58  59 5a 5b 5c 5d 5e 5f 60  |QRSTUVWXYZ[\]^_`|
00000060  61 62 63 64 65 66 67 68  69 6a 6b 6c 6d 6e 6f 70  |abcdefghijklmnop|
00000070  71 72 73 74 75 76 77 78  79 7a 7b 7c 7d 7e 7f 80  |qrstuvwxyz{|}~..|
00000080  81 82 83 84 85 86 87 88  89 8a 8b 8c 8d 8e 8f 90  |................|
00000090  91 92 93 94 95 96 97 98  99 9a 9b 9c 9d 9e 9f a0  |................|
000000a0  a1 a2 a3 a4 a5 a6 a7 a8  a9 aa ab ac ad ae af b0  |................|
000000b0  b1 b2 b3 b4 b5 b6 b7 b8  b9 ba bb bc bd be bf c0  |................|
000000c0  c1 c2 c3 c4 c5 c6 c7 c8  c9 ca cb cc cd ce cf d0  |................|
000000d0  d1 d2 d3 d4 d5 d6 d7 d8  d9 da db dc dd de df e0  |................|
000000e0  e1 e2 e3 e4 e5 e6 e7 e8  e9 ea eb ec ed ee ef f0  |................|
000000f0  f1 f2 f3 f4 f5 f6 f7 f8  f9 fa fb fc fd fe ff     |...............|
000000ff
</pre>
<p>So bash variables (and <tt class="docutils literal"><span class="pre">$(...)</span></tt> idiom) are reasonably strong: if you know your content hasn't any <tt class="docutils literal">NUL</tt> characters you can safely use them. All this is probably
linked to the fact that command line arguments cannot hold <tt class="docutils literal">NUL</tt> characters neither, a common string termination in C programs.</p>
<p>If you really need to store full binary content in a bash variable, you should think about encoding it (base64, <tt class="docutils literal">xxd</tt>, or any format of yours).</p>
<p>Please understand that <tt class="docutils literal">bash</tt> might also be able to do what you want without using variables but only pipes (stdin and stdout). They won't suffer from this limitation of course.</p>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=489&amp;md5=1083440b003f7df80df9ef35d8b40717" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2014%2F05%2F03%2Fbash-lore-nul-character-and-variables%2F&amp;language=en_GB&amp;category=text&amp;title=bash+lore%3A+NUL+character+and+variables&amp;description=Bash+variables+are+often+thought+as+being+able+to+store+any+binary+content.+Please+bear+in+mind+that+it+can%27t+store+NUL+character%2C+and+only+this+one.+Here+you+are%3A...&amp;tags=bash%2Clinux%2Cblog" type="text/html" />
	</item>
		<item>
		<title>comparison of minimal packaging between distribute and distutils2</title>
		<link>https://vaab.blog.kal.fr/2013/02/22/comparison-of-minimal-packaging-between-distribute-and-distutils2/</link>
		<comments>https://vaab.blog.kal.fr/2013/02/22/comparison-of-minimal-packaging-between-distribute-and-distutils2/#comments</comments>
		<pubDate>Fri, 22 Feb 2013 15:27:14 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[distribute]]></category>
		<category><![CDATA[distutils2]]></category>
		<category><![CDATA[packaging]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=382</guid>
		<description><![CDATA[In a pythonic mind, we shoud keep things simple as possible. But when having to deal with distribution of your code, especially if it's ONE small python file, you'll might cringe when facing the boilerplate that is required. Even with &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2013/02/22/comparison-of-minimal-packaging-between-distribute-and-distutils2/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>In a pythonic mind, we shoud keep things simple as possible.</p>
<p>But when having to deal with distribution of your code,
especially if it's ONE small python file, you'll might cringe
when facing the boilerplate that is required. Even with
<a class="reference external" href="http://pythonhosted.org/distribute/setuptools.html">distribute</a> in place of setuptools.</p>
<p>And speaking of packaging system, python has a very complex
history of them (between distutils, setuptools, distribute,
distutils2), adding a considerable burden to get a clear
idea of what system you should use, and get a complete
documentation of one system. Especially when whole part of each
system share code, ideas, compatibility but aims to be better in
some obscure part for a newcomer.</p>
<p>Notice how this
<a class="reference external" href="http://stackoverflow.com/questions/6344076/differences-between-distribute-distutils-setuptools-and-distutils2">stackoverflow question about python packaging systems</a> gets
answered brilliantly and how concrete differences between all
these packaging solutions weren't mentioned.</p>
<p>However, I wanted a dead simple way to distribute my single file package. And I
found 2 ways:</p>
<blockquote>
<ul class="simple">
<li>one with <tt class="docutils literal">distribute</tt> (the actually recommended packaging
system),</li>
<li>one with <tt class="docutils literal">distutils2</tt> (the upcoming-but-not-finished replacement
of <tt class="docutils literal">distribute</tt>).</li>
</ul>
</blockquote>
<p>I realized that I could illustrate some simple differences for newcomers
when facing the need to package a simple python file.</p>
<div class="section" id="the-goal">
<h3>The goal</h3>
<p>You have a file <tt class="docutils literal">foo.py</tt> holding whatever python code you
want. <tt class="docutils literal">foo.py</tt> is alone in a directory.</p>
<div class="section" id="install-ability">
<h4>Install-ability</h4>
<p>You need to be able to type <tt class="docutils literal">python setup.py install</tt> (or something similar)
in the source directory and all the magic required will fire out to set things
how they are supposed to be in your python libraries, allowing you to invoke
<tt class="docutils literal">import foo</tt> in any of your python file without any unfriendly exception
being cast.</p>
</div>
<div class="section" id="distributability">
<h4>distributability</h4>
<p>Next, you should be able to type <tt class="docutils literal">python setup.py sdist register upload</tt> (or
something similar) to build a source distribution, register the package to the PyPI,
and send it. So that, finally, from any computer having access to internet, you
could cast a <tt class="docutils literal">pip install foo</tt> (or something similar) to get your package
downloaded from the PyPI, and installed.</p>
</div>
</div>
<div class="section" id="distribute-way">
<h3>Distribute way</h3>
<p>This is the simplest way I found to distribute
a ONE small python file using <tt class="docutils literal">distribute</tt>, which
seems to be the current best solution waiting
for <tt class="docutils literal">distutils2</tt>.</p>
<div class="section" id="requirements">
<h4>Requirements</h4>
<p>Make sure you have the last <tt class="docutils literal">distribute</tt> installed.
You can install it with this command:</p>
<pre class="literal-block">
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
</pre>
</div>
<div class="section" id="setup-py-file">
<h4>setup.py file</h4>
<p>Then create a <tt class="docutils literal">setup.py</tt> file in the same directory than <tt class="docutils literal">foo.py</tt>, containing:</p>
<pre class="literal-block">
from setuptools import setup
setup(
    ## Required for ``python setup.py install``
    name=&quot;foo_utils&quot;,
    version=&quot;0.1&quot;,
    py_modules=[&quot;foo&quot;]

    ## Required for ``python setup.py sdist upload install``
)
</pre>
<p>The <tt class="docutils literal">name</tt> will be your package name and <tt class="docutils literal">version</tt> will be its version, these
are used for naming the <tt class="docutils literal">.egg</tt> and PyPI references.</p>
<p>The following is a less documented option and makes the solution I have found so small:
<tt class="docutils literal">py_modules</tt> is the python module you want to distribute. Typing <tt class="docutils literal">foo</tt> targets
the <tt class="docutils literal">foo.py</tt> file.</p>
<p>Actually, complete reference of this trick can be found in <a class="reference external" href="http://docs.python.org/2/distutils/examples.html">distutils
documentation</a>, I couldn't find any reference to <tt class="docutils literal">py_modules</tt>
anywhere in the <a class="reference external" href="http://pythonhosted.org/distribute/setuptools.html">distribute documentation</a>.</p>
</div>
<div class="section" id="tada">
<h4>Tada !</h4>
<p>The small <tt class="docutils literal">setup.py</tt> you've written should allow you to type:</p>
<pre class="literal-block">
python setup.py install
</pre>
<!-- to reach the "install-ability" goal. -->
<p>To send it to PyPI, you'll need some other metadata as the author, summary,
licence and maybe a small description. These are to be added in your <tt class="docutils literal">setup.py</tt>
file, and then:</p>
<pre class="literal-block">
python setup.py sdist register upload
</pre>
<p>... will send it to PyPI.</p>
<p>At last you'll then be able to install it from anywhere with:</p>
<pre class="literal-block">
pip install foo_utils
</pre>
</div>
</div>
<div class="section" id="distutils2-way">
<h3>Distutils2 way</h3>
<p>The major modification that you'll notice here is the fact that the
configuration is not executable code anymore: <tt class="docutils literal">setup.py</tt> is replaced
by <tt class="docutils literal">setup.cfg</tt>.</p>
<div class="section" id="id1">
<h4>Requirements</h4>
<p>You need to install <tt class="docutils literal">distutils2</tt>:</p>
<pre class="literal-block">
pip install distutils2
</pre>
<p>If you have any trouble installing <tt class="docutils literal">distutils2</tt>, you might be interested in
the ending section about <tt class="docutils literal">the truth about distutils2</tt>. You could also try
something like that (adapt the version of course !):</p>
<pre class="literal-block">
cd /tmp
wget https://pypi.python.org/packages/source/D/Distutils2/Distutils2-1.0a4.tar.gz
tar xvzf Distutils2-1.0a4.tar.gz
cd Distutils2-1.0a4
python setup.py install
</pre>
</div>
<div class="section" id="setup-cfg">
<h4>setup.cfg</h4>
<p>In the same directory than <tt class="docutils literal">foo.py</tt>, you can create <tt class="docutils literal">setup.cfg</tt>
containing:</p>
<pre class="literal-block">
[metadata]
name = foo_utils
version = 0.1

[files]
modules = foo
</pre>
<p>Notice that <tt class="docutils literal">py_modules</tt> is in a <tt class="docutils literal">files</tt> section and has been renamed
to <tt class="docutils literal">modules</tt>.</p>
<p>This first version of <tt class="docutils literal">setup.cfg</tt> will allow you to achieve first goal of
&quot;install-ability&quot;. But you'll need more to get your code sent to PyPI:</p>
<pre class="literal-block">
[metadata]
name = foo_utils
version = 0.1.0
summary = A Simple example Foo program
description-file = README.rst CHANGELOG.rst

## sdist info
author = John Doe
author_email = john.doe&#64;example.com
classifier =
    Development Status :: 3 - Alpha
    License :: OSI Approved :: GNU General Public License (GPL)

[files]
modules = foo_utils
extra_files =
    README.rst
    CHANGELOG.rst
</pre>
<p>Well, it's not that small, but I'm not sure what is actually really required, and
I think a small summary or description won't hurt. Notice that we have to repeat our
selves for the inclusion of the <tt class="docutils literal">README.rst</tt> and <tt class="docutils literal">CHANGELOG.rst</tt> files that are used in
the description.</p>
<p>Of course, all details given hear are subject to changes in the upcoming releases of
<tt class="docutils literal">distutils2</tt>.</p>
</div>
<div class="section" id="id2">
<h4>Tada !</h4>
<p>The small <tt class="docutils literal">setup.cfg</tt> you've written should allow you to type:</p>
<pre class="literal-block">
pysetup install
</pre>
<p>To reach the &quot;install-ability&quot; goal. And:</p>
<pre class="literal-block">
pysetup run sdist register upload
</pre>
<p>To send it to PyPI. Then you should be able to install it from anywhere with:</p>
<pre class="literal-block">
pysetup install foo_utils
</pre>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">packages made with <tt class="docutils literal">distutils2</tt> won't be compatible with <tt class="docutils literal">pip install PACKAGE</tt> commands as
they do not provide any <tt class="docutils literal">setup.py</tt>. You'll receive <tt class="docutils literal">IOError: [Errno 2] No such file or directory</tt>.</p>
</div>
<p>If any of these commands don't work, jump ahead to the next section !.</p>
</div>
<div class="section" id="the-truth-about-distutils2">
<h4>The truth about distutils2</h4>
<p>Before getting something actually working, I ran in multiple issues, and these are some of
the trick I did to make it work.</p>
<p>First, get it installed:</p>
<pre class="literal-block">
cd /tmp
wget https://pypi.python.org/packages/source/D/Distutils2/Distutils2-1.0a4.tar.gz
tar xvzf Distutils2-1.0a4.tar.gz
cd Distutils2-1.0a4
python setup.py install
</pre>
<p>Then, I proceeded towards the distribute installation:</p>
<pre class="literal-block">
$ pysetup foo_utils
No handlers could be found for logger &quot;distutils2&quot;
</pre>
<p>Hum, after hacking into the <tt class="docutils literal">distutils2</tt> code, I've found that distutils hadn't
any default logger, once one set, it was only to realize that it wanted to display
a simple error to tell me that I messed with my arguments. The correct command is:</p>
<pre class="literal-block">
root&#64;myhost$ pysetup install foo_utils
Checking the installation location...
Unable to write in &quot;/usr/lib/python2.7/site-packages&quot;. Do you have the permissions ?
root&#64;myhost$
</pre>
<p>What are you talking about ?! I'm root ! After looking around, I found there were no such
directory <tt class="docutils literal"><span class="pre">site-packages</span></tt> in my testing VM, which should mimick an Ubuntu 12.04 standard
install. I pursued by:</p>
<pre class="literal-block">
root&#64;myhost$ mkdir /usr/lib/python2.7/site-packages
root&#64;myhost$ pysetup install foo_utils
Checking the installation location...
Getting information about 'foo_utils'...
u'python-debian': u'0.1.21ubuntu1' is not a valid version (field 'Version')
u'python-apt': u'0.8.3ubuntu7' is not a valid version (field 'Version')
u'ufw': u'0.31.1-1' is not a valid version (field 'Version')
u'Landscape Client': u'12.05' is not a valid version (field 'Version')
Installing u'foo_utils' 0.1.0...
running install_dist
running build
running build_py
running install_lib
byte-compiling /usr/lib/python2.7/site-packages/foo.py to foo.pyc
running install_distinfo
creating /usr/lib/python2.7/site-packages/foo-utils-0.1.0.dist-info
creating /usr/lib/python2.7/site-packages/foo-utils-0.1.0.dist-info/METADATA
creating /usr/lib/python2.7/site-packages/foo-utils-0.1.0.dist-info/INSTALLER
creating /usr/lib/python2.7/site-packages/foo-utils-0.1.0.dist-info/REQUESTED
creating /usr/lib/python2.7/site-packages/foo-utils-0.1.0.dist-info/RECORD
root&#64;myhost$
</pre>
<p>Success ! I then did the test to see if I could &quot;import&quot; my lib:</p>
<pre class="literal-block">
root&#64;myhost$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import foo
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
ImportError: No module named foo
&gt;&gt;&gt;
</pre>
<p>Failure ! A quick check to <tt class="docutils literal">sys.path</tt> will confirm that if the &quot;site-package&quot; was not
existent, it wasn't included in the <tt class="docutils literal">sys.path</tt>... which makes sense. I'm not aware of
all these details, but why <tt class="docutils literal">distutils2</tt> then tries to install packages in this directory ?</p>
<p>Anyway, if I want to make sure that <tt class="docutils literal"><span class="pre">site-packages</span></tt> gets in <tt class="docutils literal">sys.path</tt>, I can follow <a class="reference external" href="http://superuser.com/questions/247620/how-to-globally-modify-the-default-pythonpath-sys-path">superuser advice</a>:</p>
<pre class="literal-block">
echo ../site-packages &gt; /usr/lib/python2.7/dist-packages/site-packages.pth
</pre>
<p>This will easily add <tt class="docutils literal"><span class="pre">site-packages</span></tt> directory system wide.</p>
</div>
</div>
<div class="section" id="conclusion">
<h3>Conclusion</h3>
<p>Is the packaging system of python dead-simple for one file ? Well, yes, once a straightforward recipe was discovered painfully.</p>
<p>Is it easy to find clear and unambiguous documentation ? Not really.</p>
<p>Is <tt class="docutils literal">distutils2</tt> finished ? Nope.
Did you try with <a class="reference external" href="http://hg.python.org/distutils2/">last available code from hg.python.org</a> ? Yes.</p>
<p>Do you still need to use <tt class="docutils literal">distribute</tt> ? Yes.</p>
<p>Any comments ?</p>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=382&amp;md5=b1ad1bba5169dd4715c5a089b8fe888b" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2013/02/22/comparison-of-minimal-packaging-between-distribute-and-distutils2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2013%2F02%2F22%2Fcomparison-of-minimal-packaging-between-distribute-and-distutils2%2F&amp;language=en_GB&amp;category=text&amp;title=comparison+of+minimal+packaging+between+distribute+and+distutils2&amp;description=In+a+pythonic+mind%2C+we+shoud+keep+things+simple+as+possible.+But+when+having+to+deal+with+distribution+of+your+code%2C+especially+if+it%27s+ONE+small+python+file%2C+you%27ll+might...&amp;tags=distribute%2Cdistutils2%2Cpackaging%2Cpython%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Emacs and flymake for python, javascript, php, rst</title>
		<link>https://vaab.blog.kal.fr/2012/09/20/emacs-and-flymake-for-python-javascript-php-rst/</link>
		<comments>https://vaab.blog.kal.fr/2012/09/20/emacs-and-flymake-for-python-javascript-php-rst/#comments</comments>
		<pubDate>Thu, 20 Sep 2012 10:28:16 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[comp]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[information]]></category>
		<category><![CDATA[sci]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[flymake]]></category>
		<category><![CDATA[jhint]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[phplint]]></category>
		<category><![CDATA[rst]]></category>
		<category><![CDATA[rst2html]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=335</guid>
		<description><![CDATA[Emacs is a wonderfull editor, every sane people know this. Here's how to extend your flymake to some trendy languages. You'll find here a general overview by answering question as What's flymake ? for emacs, and How does it work &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2012/09/20/emacs-and-flymake-for-python-javascript-php-rst/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p><a class="reference external" href="http://www.gnu.org/software/emacs/">Emacs</a> is a wonderfull editor, every <a class="reference external" href="http://www.dina.kvl.dk/~abraham/religion/">sane people</a> know this. Here's
how to extend your <a class="reference external" href="http://www.emacswiki.org/emacs/FlyMake">flymake</a> to some trendy languages.</p>
<p>You'll find here a general overview by answering question as <a class="reference internal" href="#what-s-flymake">What's flymake ?</a> for emacs,
and <a class="reference internal" href="#how-does-it-work">How does it work ?</a>.</p>
<p>Following are the snippet of code to have nice <a class="reference external" href="http://www.emacswiki.org/emacs/FlyMake">flymake</a> integration for:</p>
<blockquote>
<ol class="arabic simple">
<li><a class="reference internal" href="#python">Python</a></li>
<li><a class="reference internal" href="#php">PHP</a></li>
<li><a class="reference internal" href="#javascript">Javascript</a></li>
<li><a class="reference internal" href="#restructured-text">ReSTructured Text</a></li>
</ol>
</blockquote>
<p>Happy coding !</p>
<div class="section" id="what-s-flymake">
<h3>What's flymake ?</h3>
<p><tt class="docutils literal"><span class="pre">flymake-mode</span></tt> in emacs is meant to &quot;compile&quot; your code while
you are typing (no need to save) so as to give you interesting
highlights on particular bits of code you've written.</p>
<p>If these can spot <em>syntax errors</em>, code analyzers can go much beyond and
show you <em>logical errors</em> as unused variables, unreachable code, an so
on. And there's more: you can be hinted when <em>breaking code style</em>
conventions that you've chosen, or even <em>code smell</em> detectors (too much
method in an object, too much local variable, ...)</p>
</div>
<div class="section" id="how-does-it-work">
<h3>How does it work ?</h3>
<div class="section" id="the-big-picture">
<h4>The big picture</h4>
<p><tt class="docutils literal"><span class="pre">flymake-mode</span></tt> is very simple, as you type it'll create a copy of your current
emacs buffer in a file usualy called <tt class="docutils literal"><span class="pre">&lt;filename&gt;-flymake.&lt;ext&gt;</span></tt>.</p>
<p>Then, it'll launch an executable on this file and will collect it's standard output.</p>
<p>To work out of the box, the format should be something like this:</p>
<pre class="literal-block">
myfile.myext:&lt;line no&gt;: &lt;ERROR|WARNING&gt;: error message
</pre>
<p>This is a typical output of my <tt class="docutils literal">pycheckers</tt> script which combines <a class="reference external" href="http://pypi.python.org/pypi/pep8">pep8</a>
code style checker and <a class="reference external" href="http://pypi.python.org/pypi/pylint">pylint</a> (syntax, logical, code style, code smell
checking):</p>
<pre class="literal-block">
converter.py:38: ERROR: E501 (pep8) line too long (141 characters)
converter.py:130: ERROR: E303 (pep8) too many blank lines (2)
converter.py:156: WARNING: W0212 (pylint) Access to a protected member _fields_id of a client class
converter.py:185: WARNING: R0911 (pylint) Too many return statements (9/6)
</pre>
<p><tt class="docutils literal">emacs</tt> will then use this output to highlight WARNING and ERROR message in
two different colors.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">Besides helping you in getting better code every day, having the same
format, one by line, with clear tagging of the error type and the analyzer
can be extremely usefull in continuous integration to get a valuable metric
on code quality.</p>
</div>
</div>
<div class="section" id="what-do-i-need">
<h4>What do I need ?</h4>
<p>You'll need to enable flymake once for all by adding the following line to
your <tt class="docutils literal"><span class="pre">~/.emacs</span></tt>, which will launch flymake whenever you'll open a file:</p>
<pre class="literal-block">
(add-hook 'find-file-hook 'flymake-find-file-hook)
</pre>
<p>Then to set up <tt class="docutils literal"><span class="pre">flymake-mode</span></tt> for a langage you'll need to:</p>
<blockquote>
<p>1. add a specific snippet of code in <tt class="docutils literal"><span class="pre">~/.emacs</span></tt> to configure the analyzer (ie:
to set the actual name of the executable analyzer and its command line
arguments if any).</p>
<p>2. create the executable code as it often require some tweaking to get the
correct output. This stage is optional if your analyzer output is already
in the correct format.</p>
</blockquote>
</div>
</div>
<div class="section" id="ok-where-s-the-code">
<h3>Ok, where's the code ?</h3>
<div class="section" id="python">
<h4>Python</h4>
<div class="section" id="analyzer">
<h5>Analyzer</h5>
<p>My <tt class="docutils literal">pycheckers</tt> code analyzer is actually written in python and will call
<a class="reference external" href="http://pypi.python.org/pypi/pep8">pep8</a> and <a class="reference external" href="http://pypi.python.org/pypi/pylint">pylint</a> and ensure that the output format is correct. These two
analyzers are available on <a class="reference external" href="http://pypi.python.org/pypi">PyPI</a> and thus are installable thanks to:</p>
<pre class="literal-block">
pip install pylint
pip install pep8
</pre>
<p>Please check the analyzer code at: <a class="reference external" href="https://gist.github.com/3754270">https://gist.github.com/3754270</a></p>
<p>I typically put the analyzer in <tt class="docutils literal">~/bin/pycheckers</tt> and ensure that it is
executable.</p>
</div>
<div class="section" id="id1">
<h5>Emacs</h5>
<p>Here's the emacs snippet:</p>
<pre class="literal-block">
;; Python flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-pycheckers-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                    'flymake-create-temp-inplace))
        (local-file (file-relative-name
                     temp-file
                     (file-name-directory buffer-file-name))))
      (list &quot;~/bin/pycheckers.py&quot;  (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
            '(&quot;\\.py\\'&quot; flymake-pycheckers-init)))
</pre>
</div>
<div class="section" id="for-the-lazier">
<h5>For the lazier</h5>
<p>This snippet will do the complete job for you:</p>
<pre class="literal-block">
mkdir -p ~/bin &amp;&amp;
cd ~/bin &amp;&amp;
wget https://raw.github.com/gist/3754270/a4fecffaff6ab702f5c8d37332afc90f6461e40b/pycheckers.py &amp;&amp;
chmod +x pycheckers.py &amp;&amp;
cat &lt;&lt;EOF &gt;&gt; ~/.emacs

;; Python flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-pycheckers-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                    'flymake-create-temp-inplace))
        (local-file (file-relative-name
                     temp-file
                     (file-name-directory buffer-file-name))))
      (list &quot;~/bin/pycheckers.py&quot;  (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
            '(&quot;\\.py\\'&quot; flymake-pycheckers-init)))

EOF
</pre>
<p>You'll need to restart <tt class="docutils literal">emacs</tt> for this to work. (Or highlight the added snippet
in the end of your <tt class="docutils literal">.emacs</tt> and call <tt class="docutils literal"><span class="pre">M-x</span> <span class="pre">eval-region</span></tt>)</p>
</div>
</div>
<div class="section" id="php">
<h4>Php</h4>
<div class="section" id="id2">
<h5>Analyzer</h5>
<p>I've also a <tt class="docutils literal">phpcheckers</tt>, even if it is very small, I feel better to know
that I could grep out some specific warnings or add another sub-analyzer quite
easily, and feel free to add your own include path as you wish in the
'packages-path' arguments:</p>
<pre class="literal-block">
#!/bin/bash

phplint --modules-path ~/var/lib/phplint/modules \
        --packages-path /usr/share/php \
        --no-print-notices &quot;$&#64;&quot;
</pre>
<p>So i'm using only <a class="reference external" href="http://www.icosaedro.it/phplint">phplint</a> here.</p>
</div>
<div class="section" id="id3">
<h5>Emacs</h5>
<p>Here's the emacs snippet:</p>
<pre class="literal-block">
;; PHP flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-phplint-init()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
         (local-file (file-relative-name
                      temp-file
                      (file-name-directory buffer-file-name))))
      (list &quot;phpcheckers&quot; (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
             '(&quot;\\.php[345]?$&quot; flymake-phplint-init))
  (add-to-list 'flymake-allowed-file-name-masks
               '(&quot;\\.inc$&quot; flymake-phplint-init)))
</pre>
</div>
<div class="section" id="id4">
<h5>For the lazier</h5>
<p>This snippet will do the complete job for you:</p>
<pre class="literal-block">
mkdir -p ~/bin &amp;&amp;
cd ~/bin &amp;&amp;
cat &lt;&lt;EOF &gt; ~/bin/phpcheckers &amp;&amp;
#!/bin/bash

phplint --modules-path ~/var/lib/phplint/modules \
        --packages-path /usr/share/php \
        --no-print-notices &quot;\$&#64;&quot;
EOF
chmod +x phpcheckers &amp;&amp;
cat &lt;&lt;EOF &gt;&gt; ~/.emacs

;; PHP flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-phplint-init()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                    'flymake-create-temp-inplace))
        (local-file (file-relative-name
                     temp-file
                     (file-name-directory buffer-file-name))))
      (list &quot;~/bin/phpcheckers&quot; (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
            '(&quot;\\.php[345]?$&quot; flymake-phplint-init))
  (add-to-list 'flymake-allowed-file-name-masks
               '(&quot;\\.inc$&quot; flymake-phplint-init)))

EOF
</pre>
<p>You'll need to restart <tt class="docutils literal">emacs</tt> for this to work. (Or highlight the added snippet
in the end of your <tt class="docutils literal">.emacs</tt> and call <tt class="docutils literal"><span class="pre">M-x</span> <span class="pre">eval-region</span></tt>)</p>
</div>
</div>
<div class="section" id="javascript">
<h4>Javascript</h4>
<div class="section" id="id5">
<h5>Analyzer</h5>
<p>I use a combination of <a class="reference external" href="http://www.jslint.com/lint.html">jslint</a> and <a class="reference external" href="http://www.jshint.com/about/">jshint</a>.</p>
<p>This is more work as <a class="reference external" href="http://www.jslint.com/lint.html">jslint</a> is widely criticized for behing too spotty on
some conventions only shared by its author, I find it useful when tamed down
to normal behavior.</p>
<p>Second, it's not so easy to get the correct format out of <a class="reference external" href="http://www.jslint.com/lint.html">jslint</a>. So I
created a <tt class="docutils literal"><span class="pre">jslint-emacs</span></tt> executable that lies in
<tt class="docutils literal"><span class="pre">~/bin/jslint-emacs</span></tt>. Here's the code:</p>
<pre class="literal-block">
#!/bin/bash

filename=&quot;$1&quot;

jslint &quot;$filename&quot; --nomen --maxerr 999999 |

## reformat from:
##
##  #45 Expected exactly one space between 'function' and '('.
##      init: function(parent, dataset, csv_import_record){ // Line 127, Pos 23
##
## to:
##
##  filename.js:94: WARNING: (jslint) Expected exactly one space between 'function' and '('.

         sed -r &quot;N;s/^\s*#[0-9]+ (.*)\n\s+.*\/\/ Line ([0-9]+),.*/$filename:\2: WARNING: W000 (jslint) \1/&quot; |

## Remove garbages lines that were not match by previous regex
## as their is a small header not matching regexp.

         grep -E &quot;^$filename:[0-9]+:&quot; |

         grep -v &quot;at column.*not column.*.&quot; |
         grep -v &quot;Expected '{' and instead saw 'return'&quot; |
         grep -v &quot;Expected exactly one space between ')' and 'return'&quot; |
         grep -v &quot;Combine this with the previous 'var' statement&quot; |
         grep -v &quot;Unexpected 'in'. Compare with undefined, or use .*&quot;

## force errorlevel 0 so as to avoid emacs ``flymake-mode`` to complain
exit 0
</pre>
<p>I've found that <tt class="docutils literal">jshint</tt> can use a specific reporter written in javascript, which
seems perfect to get the correct ouput from it.</p>
<p>This is my <tt class="docutils literal"><span class="pre">~/bin/jshint-emacs</span></tt>:</p>
<pre class="literal-block">
#!/bin/bash

jshint &quot;$1&quot; --reporter ~/.emacs.d/jslint-emacs-reporter.js
</pre>
<p>And of course, my <tt class="docutils literal"><span class="pre">~/.emacs.d/jslint-emacs-reporter.js</span></tt> is available as a
gist here: <a class="reference external" href="https://gist.github.com/3754379">https://gist.github.com/3754379</a></p>
<p>It seems that this javascript reporter could be used also with <tt class="docutils literal">jslint</tt> but I didn't
figure out how to do so. (This was maybe because I've already written the sed/grep thing
before).</p>
<p>Of course, this is my <tt class="docutils literal">~/bin/jscheckers</tt>:</p>
<pre class="literal-block">
#!/bin/bash

jslint-emacs &quot;$1&quot;
jshint-emacs &quot;$1&quot;
</pre>
</div>
<div class="section" id="id6">
<h5>Emacs</h5>
<p>Here's the emacs snippet:</p>
<pre class="literal-block">
;; Javascript flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-jslint-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
      (list &quot;jscheckers&quot; (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
               '(&quot;\\.js\\'&quot; flymake-jslint-init)))
</pre>
</div>
<div class="section" id="id7">
<h5>For the lazier</h5>
<p>This snippet will do the complete job for you:</p>
<pre class="literal-block">
mkdir -p ~/bin &amp;&amp;
cd ~/bin &amp;&amp;
cat &lt;&lt;EOF &gt; ~/bin/jslint-emacs &amp;&amp;
#!/bin/bash

filename=&quot;\$1&quot;

jslint &quot;\$filename&quot; --nomen --maxerr 999999 |

## reformat from:
##
##  #45 Expected exactly one space between 'function' and '('.
##      init: function(parent, dataset, csv_import_record){ // Line 127, Pos 23
##
## to:
##
##  filename.js:94: WARNING: (jslint) Expected exactly one space between 'function' and '('.

         sed -r &quot;N;s/^\s*#[0-9]+ (.*)\n\s+.*\/\/ Line ([0-9]+),.*/\$filename:\2: WARNING: W000 (jslint) \1/&quot; |

## Remove garbages lines that were not match by previous regex
## as their is a small header not matching regexp.

         grep -E &quot;^\$filename:[0-9]+:&quot; |

         grep -v &quot;at column.*not column.*.&quot; |
         grep -v &quot;Expected '{' and instead saw 'return'&quot; |
         grep -v &quot;Expected exactly one space between ')' and 'return'&quot; |
         grep -v &quot;Combine this with the previous 'var' statement&quot; |
      grep -v &quot;Unexpected 'in'. Compare with undefined, or use .*&quot;

## force errorlevel 0 so as to avoid emacs ``flymake-mode`` to complain
exit 0
EOF
chmod +x jslint-emacs &amp;&amp;
cat &lt;&lt;EOF &gt; ~/bin/jscheckers &amp;&amp;
#!/bin/bash

~/bin/jslint-emacs &quot;\$1&quot;
~/bin/jshint-emacs &quot;\$1&quot;

EOF
chmod +x jscheckers &amp;&amp;
mkdir -p ~/.emacs.d &amp;&amp;
cd ~/.emacs.d &amp;&amp;
wget https://raw.github.com/gist/3754379/3c43c50b4d85f50b8314af8e498b1b4f2cee58ea/jslint-emacs-reporter.js &amp;&amp;
cat &lt;&lt;EOF &gt;&gt; ~/.emacs

;; Javascript flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-jslint-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                    'flymake-create-temp-inplace))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
      (list &quot;jscheckers&quot; (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
               '(&quot;\\.js\\'&quot; flymake-jslint-init)))

EOF
</pre>
<p>You'll need to restart <tt class="docutils literal">emacs</tt> for this to work. (Or highlight the added snippet
in the end of your <tt class="docutils literal">.emacs</tt> and call <tt class="docutils literal"><span class="pre">M-x</span> <span class="pre">eval-region</span></tt>)</p>
</div>
</div>
<div class="section" id="restructured-text">
<h4>ReSTructured Text</h4>
<div class="section" id="id8">
<h5>Analyzer</h5>
<p>I was surprised to see that <a class="reference external" href="http://docutils.sourceforge.net/docs/user/tools.html">rst2html</a> will make very decent rst analyzer.</p>
<p><a class="reference external" href="http://docutils.sourceforge.net/docs/user/tools.html">rst2html</a> is provided by python <a class="reference external" href="http://pypi.python.org/pypi/docutils">docutils package</a>.</p>
<p>My <tt class="docutils literal">~/bin/rstcheckers</tt>:</p>
<pre class="literal-block">
#!/bin/bash

rst2html &quot;$1&quot; &gt; /dev/null
</pre>
<p>Surprising isn't it ? As it isn't a code analyzer but the compiler itself, it
does it's job really well, and in combination with flymake, it gives you a very
convenient on-the-fly syntax checking.</p>
<p>Note that <tt class="docutils literal">&gt;/dev/null</tt> is required as <tt class="docutils literal">rst2html</tt> jobs is to output HTML from your
<tt class="docutils literal">rst</tt> (which we don't want to see), and it casts errors on the standard error pipe.</p>
</div>
<div class="section" id="id9">
<h5>Emacs</h5>
<p>Here's the emacs snippet:</p>
<pre class="literal-block">
;; ReSTructured Text Flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-rst-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
      (list &quot;rstcheckers&quot; (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
               '(&quot;\\.rst\\'&quot; flymake-rst-init)))
</pre>
</div>
<div class="section" id="id10">
<h5>For the lazier</h5>
<p>This snippet will do the complete job for you:</p>
<pre class="literal-block">
mkdir -p ~/bin &amp;&amp;
cd ~/bin &amp;&amp;
cat &lt;&lt;EOF &gt; ~/bin/rstcheckers &amp;&amp;
#!/bin/bash

rst2html &quot;\$1&quot; &gt; /dev/null
EOF
chmod +x rstcheckers &amp;&amp;
cat &lt;&lt;EOF &gt;&gt; ~/.emacs

;; ReSTructured Text Flymake configuration

(when (load &quot;flymake&quot; t)
  (defun flymake-rst-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                    'flymake-create-temp-inplace))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
      (list &quot;rstcheckers&quot; (list local-file))))

  (add-to-list 'flymake-allowed-file-name-masks
               '(&quot;\\.rst\\'&quot; flymake-rst-init)))

EOF
</pre>
<p>You'll need to restart <tt class="docutils literal">emacs</tt> for this to work. (Or highlight the added snippet
in the end of your <tt class="docutils literal">.emacs</tt> and call <tt class="docutils literal"><span class="pre">M-x</span> <span class="pre">eval-region</span></tt>)</p>
</div>
</div>
</div>
<div class="section" id="conclusion">
<h3>Conclusion</h3>
<p>I hope this will let you add some more language to your emacs
<tt class="docutils literal"><span class="pre">flymake-mode</span></tt>. If you spot any errors, please let me know.</p>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=335&amp;md5=9ac35db7c92e40c75f8ffba62903aee9" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2012/09/20/emacs-and-flymake-for-python-javascript-php-rst/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2012%2F09%2F20%2Femacs-and-flymake-for-python-javascript-php-rst%2F&amp;language=en_GB&amp;category=text&amp;title=Emacs+and+flymake+for+python%2C+javascript%2C+php%2C+rst&amp;description=Emacs+is+a+wonderfull+editor%2C+every+sane+people+know+this.+Here%27s+how+to+extend+your+flymake+to+some+trendy+languages.+You%27ll+find+here+a+general+overview+by+answering+question+as...&amp;tags=emacs%2Cflymake%2Cjhint%2Cjslint%2Cphplint%2Crst%2Crst2html%2Cblog" type="text/html" />
	</item>
		<item>
		<title>cleaned and released kal-shlib-* code and dd_rhelp</title>
		<link>https://vaab.blog.kal.fr/2012/04/30/cleaned-and-released-kal-shlib-code-and-dd_rhelp/</link>
		<comments>https://vaab.blog.kal.fr/2012/04/30/cleaned-and-released-kal-shlib-code-and-dd_rhelp/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 16:44:47 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=327</guid>
		<description><![CDATA[This is a long time since I wrote the first line of this shell script library which has followed me since then. These library were managed on subversion, so this was the opportunity to move them toward GIT. kal-shlib-core -- &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2012/04/30/cleaned-and-released-kal-shlib-code-and-dd_rhelp/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>This is a long time since I wrote the first line of this shell script library which has followed me since then.</p>
<p>These library were managed on subversion, so this was the opportunity to move them toward GIT.</p>
<ul class="simple">
<li><a class="reference external" href="http://github.com/vaab/kal-shlib-core">kal-shlib-core</a> -- the core library loading mecanism</li>
<li><a class="reference external" href="http://github.com/vaab/kal-shlib-common">kal-shlib-common</a> -- very common and basic functions</li>
<li><a class="reference external" href="http://github.com/vaab/kal-shlib-pretty">kal-shlib-pretty</a> -- ANSI colored wrapper to get humble but pretty outputs</li>
<li><a class="reference external" href="http://github.com/vaab/kal-shlib-shunit">kal-shlib-shunit</a> -- Shell Unit test framework</li>
</ul>
<p>Same for <a class="reference external" href="https://github.com/vaab/dd_rhelp">dd_rhelp</a> which received a small but constant flow of thanks, remarks, and patches from all over the world. And I wasn't always very reactive.</p>
<p>Hope this will help people mess around with the code as they wish.</p>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=327&amp;md5=b5d57e50f62811b4d066b55b487a9990" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2012/04/30/cleaned-and-released-kal-shlib-code-and-dd_rhelp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2012%2F04%2F30%2Fcleaned-and-released-kal-shlib-code-and-dd_rhelp%2F&amp;language=en_GB&amp;category=text&amp;title=cleaned+and+released+kal-shlib-%2A+code+and+dd_rhelp&amp;description=This+is+a+long+time+since+I+wrote+the+first+line+of+this+shell+script+library+which+has+followed+me+since+then.+These+library+were+managed+on+subversion%2C+so+this...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>SpamAssassin, Amavis and ubuntu</title>
		<link>https://vaab.blog.kal.fr/2012/04/02/spamassassin-amavis-and-ubuntu/</link>
		<comments>https://vaab.blog.kal.fr/2012/04/02/spamassassin-amavis-and-ubuntu/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 19:59:01 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[admin]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=291</guid>
		<description><![CDATA[If running spamassassin (SA) with amavis without usage of spamd on ubuntu 10.04 and you feel that sa-learn isn't working right, here are a few tips. I had mails that were false positive and I had to dig a little &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2012/04/02/spamassassin-amavis-and-ubuntu/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>If running <tt class="docutils literal">spamassassin</tt> (SA) with <tt class="docutils literal">amavis</tt> <em>without</em> usage of <tt class="docutils literal">spamd</tt> on ubuntu 10.04 and you feel that <tt class="docutils literal"><span class="pre">sa-learn</span></tt> isn't working right, here are a few tips.</p>
<p>I had mails that were false positive and I had to dig a little more than usual to get it working.</p>
<dl class="docutils">
<dt>learning</dt>
<dd><p class="first">Think that SA is executed by <tt class="docutils literal">amavis</tt> user, so:</p>
<pre class="literal-block">
sudo -u amavis -H sa-learn --showdots {--ham|--spam} YOUR_MAIL_OR_MBOX_OR_MAILDIR
</pre>
<p class="last">The trick is the <em>sudo</em> part, <tt class="docutils literal"><span class="pre">sa-learn</span></tt> docs can be found everywhere on the net.</p>
</dd>
<dt>checking</dt>
<dd><p class="first">This is how to check how spamassassin will rate your mail:</p>
<pre class="literal-block">
cat badmail | sudo -u amavis -H spamassassin -t
</pre>
<p class="last">Which is very usefull when used with the same mail, before and after learning.</p>
</dd>
<dt>statistics</dt>
<dd><p class="first">Looking at statistics can be usefull to check that the number of spam and ham are consistent with your learning sessions:</p>
<pre class="last literal-block">
sudo -u amavis -H sa-learn --dump magic | grep am
</pre>
</dd>
</dl>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">Do not forget to restart <tt class="docutils literal">amavis</tt> with <tt class="docutils literal">/etc/init.d/amavis restart</tt>.</p>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=291&amp;md5=d24d5215efba1f8072bece0a58c57fac" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2012/04/02/spamassassin-amavis-and-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2012%2F04%2F02%2Fspamassassin-amavis-and-ubuntu%2F&amp;language=en_GB&amp;category=text&amp;title=SpamAssassin%2C+Amavis+and+ubuntu&amp;description=If+running+spamassassin+%28SA%29+with+amavis+without+usage+of+spamd+on+ubuntu+10.04+and+you+feel+that+sa-learn+isn%27t+working+right%2C+here+are+a+few+tips.+I+had+mails+that...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>extracting a subpart of a git repository with history</title>
		<link>https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/</link>
		<comments>https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 08:10:24 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[comp]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[sci]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=233</guid>
		<description><![CDATA[I recently had to extract a subdirectory of an existing git repository towards its own new repository. And I wanted to keep all the commit information and history that was related to this directory. EDIT: Even if the solution I &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>I recently had to extract a subdirectory of an existing git repository towards its own new repository. And I wanted to keep all the commit information and history that was related to this directory.</p>
<p><strong>EDIT</strong>: Even if the solution I used was simpler than I would have expected, I was far from knowing that GIT had a 'filter-branch' command which does the job perfectly well (and it is  <a class="reference external" href="http://progit.org/book/ch6-4.html#making_a_subdirectory_the_new_root">documented in ProGIT</a>). Thanks Jon for the tip.</p>
<div class="section" id="sketching-the-tools-used">
<h3>sketching the tools used</h3>
<p>First step, is to retrieve only the commit Hashes related to this directory:</p>
<pre class="literal-block">
git log --format=%H -- MY_DIRECTORY
</pre>
<p>to filter out only the commit Hashes related to this directory. Then:</p>
<pre class="literal-block">
git format-patch HASH -1
</pre>
<p>will be used to output patch files with the commit information (subject, author...)</p>
<p>Then, with some <tt class="docutils literal">sed</tt> you'll have to change the directories used in all the outputed patch:</p>
<pre class="literal-block">
sed -i s%MYOLDDIRECTORY%MYNEWDIRECTORY%g *.patch
</pre>
<p>The new directory in my case was &quot;&quot; (nothing) as it was in the root folder.</p>
<p>Finally, you'll have to apply your patches to your other repository with:</p>
<pre class="literal-block">
git am *.patch
</pre>
</div>
<div class="section" id="complete-commands-used">
<h3>Complete commands used</h3>
<p>The complete commands that I've used:</p>
<pre class="literal-block">
cd PARENT_OF_MYDIRECTORY
git log --format=%H -- MYDIRECTORY | tac | \
    (i=0
     while read h; do
         i=$[$i+1]
         git format-patch $h -1 --start-number $i
     done)
</pre>
<p>This produces <tt class="docutils literal"><span class="pre">NNNN-*.patch</span></tt> files, one for each commit:</p>
<pre class="literal-block">
sed -i s%MYDIRECTORY%%g *.patch
</pre>
<p>This will update the paths in all patches:</p>
<pre class="literal-block">
mv *.patch MY_NEW_REPO
cd MY_NEW_REPO
git am *.patch
</pre>
<p>A quick check with <tt class="docutils literal">gitk</tt> ensures me that message subject and commit were transfered ok.</p>
<p>Et voilà !</p>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=233&amp;md5=c192eb8bf3b9556f88fb1adac13f4fef" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2011%2F06%2F29%2Fextracting-a-subpart-of-a-git-repository-with-history%2F&amp;language=en_GB&amp;category=text&amp;title=extracting+a+subpart+of+a+git+repository+with+history&amp;description=I+recently+had+to+extract+a+subdirectory+of+an+existing+git+repository+towards+its+own+new+repository.+And+I+wanted+to+keep+all+the+commit+information+and+history+that+was...&amp;tags=git%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
