<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.tuxion.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">

    <title>Jon Bernard</title>
    
    <link href="http://tuxion.com/" />
    <updated>2012-05-16T02:16:01-04:00</updated>
    <id>http://tuxion.com/</id>
    <author>
        <name>Jon Bernard</name>
        
    </author>

    
        <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.tuxion.com/jbernard" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="jbernard" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
            <title>Get More Out of Vim's makeprg</title>
            <link href="http://tuxion.com/2011/09/30/vim-makeprg.html" />
            <updated>2011-09-30T00:00:00-04:00</updated>
            <id>http://tuxion.com/2011/09/30/vim-makeprg</id>
            <content type="html">&lt;p&gt;This started out as a small post on how to preview markdown documents in Vim
using &lt;code&gt;makeprg&lt;/code&gt;, but somehow I got carried away.  More on the markdown thing in
a bit.&lt;/p&gt;

&lt;p&gt;When you type &lt;code&gt;:make&lt;/code&gt; in Vim, the value of &lt;code&gt;makeprg&lt;/code&gt; is executed in
a subprocess.  Traditionally this value is set to &lt;em&gt;make&lt;/em&gt;, but you can set this
to anything you like. You can see the current value by typing &lt;code&gt;:set makeprg&lt;/code&gt;.
When you combine this with Vim&amp;rsquo;s support for filetype plugins, there&amp;rsquo;s
a decent amount of value that I think is sometimes overlooked.  So this
article is an attempt to explain some of the things you can do with &lt;em&gt;makeprg&lt;/em&gt;
and filetype detection.&lt;/p&gt;

&lt;h2&gt;Filetype Plugins&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;A filetype plugin is like a global plugin, except that it sets options and
defines mappings for the current buffer only.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Using a filetype plugin, we can set &lt;code&gt;makeprg&lt;/code&gt; to something useful depending on
the type of a file it happens to be.  Adding your own filetype plugin is easy,
just create a file of the form: &lt;code&gt;&amp;lt;filetype&amp;gt;.vim&lt;/code&gt; inside the
&lt;code&gt;~/.vim/after/ftplugin&lt;/code&gt; directory.  The &lt;em&gt;after&lt;/em&gt; subdirectory is technically
optional, but I prefer it as it keeps things isolated from other installed
plugins.  The &lt;em&gt;after&lt;/em&gt; subdirectory is sourced last as Vim initializes itself,
so putting your options there ensures they aren&amp;rsquo;t clobbered by other plugins.&lt;/p&gt;

&lt;p&gt;For a given Vim buffer, you can see the filetype by typing &lt;code&gt;:set ft&lt;/code&gt;. Some
people put this information in their statusline, which can be helpful if
you&amp;rsquo;re writing Vim plugins or just want to make sure things are being detected
correctly.&lt;/p&gt;

&lt;p&gt;For example, if I want to define custom settings for the &lt;em&gt;C&lt;/em&gt; filetype, I would
create &lt;code&gt;~/.vim/after/ftplugin/c.vim&lt;/code&gt; and put my settings there.  Any time
a file of type &lt;em&gt;C&lt;/em&gt; is loaded, my settings are applied. We can use this
mechanism to set a custom &lt;code&gt;makeprg&lt;/code&gt; for certain filetypes.&lt;/p&gt;

&lt;h2&gt;Customizing makeprg for Markdown&lt;/h2&gt;

&lt;p&gt;I take all of my notes in
&lt;a href="http://daringfireball.net/projects/markdown/"&gt;Markdown&lt;/a&gt;, which is nearly
plain text with a tiny amount of markup.  This allows me to quickly convert my
personal notes into decent looking html documentation.  It seems to be a regular
occurrence that my notes become the initial documentation for the project or
subproject that I&amp;rsquo;m working on.  Many wiki&amp;rsquo;s support Markdown, so it ends up
saving me work by just keeping everything in that format.&lt;/p&gt;

&lt;p&gt;Even though Markdown&amp;rsquo;s syntax is very lean and simple, I still end up
forgetting some of it from time to time.  I find it useful to generate and
view the HTML output for the buffer that I&amp;rsquo;m working on periodically to make
sure things look right.  This is really easy by setting a custom &lt;code&gt;makeprg&lt;/code&gt; for
the markdown filetype.&lt;/p&gt;

&lt;h3&gt;The Hammer Plugin&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/robgleeson"&gt;Robert Gleeson&lt;/a&gt; wrote a Vim plugin called
&lt;a href="https://github.com/robgleeson/hammer.vim"&gt;Hammer&lt;/a&gt; that basically does exactly
what I explain here and quite a bit more.  It might be perfect for you, but
for me it&amp;rsquo;s much more than I need.  I&amp;rsquo;ve had
&lt;a href="https://github.com/robgleeson/hammer.vim/issues/11"&gt;problems&lt;/a&gt; when the
version of Ruby on the system is not the same as what Vim was compiled with.
Check it out though, you might love it.&lt;/p&gt;

&lt;h3&gt;A Simpler Approach&lt;/h3&gt;

&lt;p&gt;All I want is to type &lt;code&gt;:make&lt;/code&gt; and have Vim generate an HTML version in &lt;code&gt;/tmp&lt;/code&gt;
that I can point my browser to. So I put the following in
&lt;code&gt;~/.vim/after/ftplugin/markdown.vim&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="vim"&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;makeprg&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;redcarpet\ %\ &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="sr"&gt;/tmp/&lt;/span&gt;%&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;.html
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s take a quick look at what&amp;rsquo;s going on here:&lt;/p&gt;

&lt;ul class="square"&gt;

    &lt;li&gt;The above setting is applied only when editing a markdown buffer.
    Personally, I use
    &lt;a href="https://github.com/tanoku/redcarpet"&gt;redcarpet&lt;/a&gt; to parse
    markdown, but there are many others that you can substitute here.&lt;/li&gt;

    &lt;li&gt;Spaces in option strings must be escaped so Vim knows when to stop
    parsing.&lt;/li&gt;

    &lt;li&gt;% expands to the name of the file currently being edited.&lt;/li&gt;

    &lt;li&gt;%&lt; expands to the name of the file without the trailing extension.
    This allows us to change the name of the file and add the html
    extension.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;So the above command calls &lt;code&gt;redcarpet&lt;/code&gt; on the filename and redirects the
output to &lt;code&gt;/tmp/&amp;lt;filename&amp;gt;.html&lt;/code&gt;.  That&amp;rsquo;s it, super simple.  Once this
completes, I point my browser to &lt;code&gt;file:/tmp&lt;/code&gt; and load the file I&amp;rsquo;m interested
in.&lt;/p&gt;

&lt;p&gt;This same approach can be taken with &lt;strong&gt;ReStructured Text&lt;/strong&gt; by putting the
following in &lt;code&gt;~/.vim/after/ftplugin/rst.vim&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="vim"&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;makeprg&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;rst2html.&lt;span class="k"&gt;py&lt;/span&gt;\ %\ &lt;span class="sr"&gt;/tmp/&lt;/span&gt;%&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;.html
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Extra Credit&lt;/h2&gt;

&lt;p&gt;For &lt;em&gt;C&lt;/em&gt;, we could set &lt;code&gt;makeprg&lt;/code&gt; to call gcc directly instead of &lt;code&gt;make&lt;/code&gt; if
a Makefile is not found in the current working directory.  I find this very
helpful when creating a scratch source file to test certain behaviour or
a quick theory.  Here&amp;rsquo;s what the contents of &lt;code&gt;~/.vim/after/ftplugin/c.vim&lt;/code&gt;
might look like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="vim"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;filereadable&lt;span class="p"&gt;(&lt;/span&gt;expand&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;ldquo;%:p:h&amp;rdquo;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;.&lt;span class="s2"&gt;&amp;ldquo;/Makefile&amp;rdquo;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;setlocal&lt;/span&gt; &lt;span class="nb"&gt;makeprg&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;gcc\ &lt;span class="p"&gt;&amp;ndash;&lt;/span&gt;Wall\ &lt;span class="p"&gt;&amp;ndash;&lt;/span&gt;Wextra\ &lt;span class="p"&gt;&amp;ndash;&lt;/span&gt;&lt;span class="k"&gt;o&lt;/span&gt;\ %&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;\ %
&lt;span class="k"&gt;endif&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now when you load a file of type &lt;em&gt;C&lt;/em&gt; and there is no Makefile, typing &lt;code&gt;:make&lt;/code&gt;
will compile your source file into an executable of the same name (minus the
extension).  I use this all the time.  You could do the same thing for C++ by
changing the filename to &lt;code&gt;cpp.vim&lt;/code&gt; and changing &lt;code&gt;gcc&lt;/code&gt; to &lt;code&gt;g++&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Future Work&lt;/h2&gt;

&lt;p&gt;It would be cool to copy the target filename to the clipboard for easy pasting
into the browser&amp;rsquo;s URL bar.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>New GPG Key</title>
            <link href="http://tuxion.com/2010/07/20/new-gpg-key.html" />
            <updated>2010-07-20T00:00:00-04:00</updated>
            <id>http://tuxion.com/2010/07/20/new-gpg-key</id>
            <content type="html">&lt;p&gt;I&amp;rsquo;ve recently set up a new GPG key, and will be transitioning away from my old
one. I have done this in order to migrate to a larger RSA key and stronger hash
functions, and NOT due to any known key compromise. The old key will continue
to be valid for some time, but future correspondence should use the new one
wherever possible.&lt;/p&gt;

&lt;p&gt;I have created a &lt;a href="http://people.debian.org/~jbernard/key-transition-2010-07-20.txt.asc"&gt;transition
document&lt;/a&gt;
and signed it with both keys.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Don't Clobber Your Registers</title>
            <link href="http://tuxion.com/2010/07/02/clobber-registers.html" />
            <updated>2010-07-02T00:00:00-04:00</updated>
            <id>http://tuxion.com/2010/07/02/clobber-registers</id>
            <content type="html">&lt;p&gt;I recently needed to call the &lt;code&gt;cpuid&lt;/code&gt; instruction from some C++ code. This is
&lt;a href="http://wikipedia.org/wiki/CPUID"&gt;well documented on wikipedia&lt;/a&gt; and everything
went fine until I hit a snag moving the code from x86_64 to i386.&lt;/p&gt;

&lt;p&gt;It seems that &lt;code&gt;%ebx&lt;/code&gt; is the PIC register for i386, and so you must take care to
not clobber it when calling &lt;code&gt;cpuid&lt;/code&gt;. This is accompolished by explictly saving
and restoring the register before and after the call to &lt;code&gt;cpuid&lt;/code&gt;. An additional
step is required to tell gcc that &lt;code&gt;%ebx&lt;/code&gt; will not be clobbered by the call to
&lt;code&gt;cpuid&lt;/code&gt;. The last lines of assembly define which registers were potentially
clobbered so gcc can generate appropriate code. By manually saving and
restoring &lt;code&gt;%ebx&lt;/code&gt;, it can be saftely omitted from that list. I did not find any
information documenting this specifically, so here it is!  The following code
returns the result of &lt;code&gt;cpuid&lt;/code&gt; and runs on both x86_64 and i386.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="c"&gt;&lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="nf"&gt;cpuid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;regs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cpuid_leaf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ebx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ecx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;edx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;asm&lt;/span&gt; &lt;span class="k"&gt;volatile&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="cp"&gt;#if defined(__i386__)&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;pushl %%ebx;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
&lt;span class="cp"&gt;#endif&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;movl %4, %%eax;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;cpuid;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;movl %%eax, %0;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;movl %%ebx, %1;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;movl %%ecx, %2;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;movl %%edx, %3;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
&lt;span class="cp"&gt;#if defined(__i386__)&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;popl %%ebx;&lt;/span&gt;&lt;span class="se"&gt;\n\t&lt;/span&gt;&lt;span class="s"&gt;&amp;rdquo;&lt;/span&gt;
&lt;span class="cp"&gt;#endif&lt;/span&gt;
             &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;ldquo;=m&amp;rdquo;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&gt;&amp;ldquo;=m&amp;rdquo;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ebx&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&gt;&amp;ldquo;=m&amp;rdquo;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ecx&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&gt;&amp;ldquo;=m&amp;rdquo;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;edx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
             &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;ldquo;r&amp;rdquo;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cpuid_leaf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
             &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;ldquo;%eax&amp;rdquo;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="cp"&gt;#if !defined(__i386__)&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;%ebx&amp;rdquo;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="cp"&gt;#endif&lt;/span&gt;
             &lt;span class="s"&gt;&amp;ldquo;%ecx&amp;rdquo;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;ldquo;%edx&amp;rdquo;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;regs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;regs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ebx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;regs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ecx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;regs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;edx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;For additional information, I found &lt;a href="http://sam.zoy.org/blog/2007-04-13-shlib-with-non-pic-code-have-inline-assembly-and-pic-mix-well"&gt;Sam Hocovar&amp;rsquo;s
post&lt;/a&gt;
to be quite informative.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Find command in $PATH</title>
            <link href="http://tuxion.com/2010/04/19/find-cmd-in-path.html" />
            <updated>2010-04-19T00:00:00-04:00</updated>
            <id>http://tuxion.com/2010/04/19/find-cmd-in-path</id>
            <content type="html">&lt;p&gt;I make calls to external commands from Python quite often. Sometimes it is
desirable to know if a command is available on the running system before making
a call to &lt;code&gt;subprocess.call()&lt;/code&gt;. Here&amp;rsquo;s the code I came up with for doing that:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_in_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;ldquo;PATH&amp;rdquo;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pathsep&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;full_cmd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_cmd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;access&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_cmd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;X_OK&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;full_cmd&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If there&amp;rsquo;s a better way to do this, or even a builtin python way, don&amp;rsquo;t
hesitate to mention it.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Periodic timers in PyGTK</title>
            <link href="http://tuxion.com/2010/04/16/periodic-timers-in-pygtk.html" />
            <updated>2010-04-16T00:00:00-04:00</updated>
            <id>http://tuxion.com/2010/04/16/periodic-timers-in-pygtk</id>
            <content type="html">&lt;p&gt;I&amp;rsquo;ve been writing a ton of &lt;a href="http://www.python.org"&gt;Python&lt;/a&gt; code lately and I&amp;rsquo;m
absolutely loving it. As a C programmer for the better part of the last 15
years, I find the syntax and ease of expression to be refreshing and fun.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve recently been tasked with writing a GUI interface to an existing product
we have at &lt;a href="http://www.miserware.com"&gt;MiserWare&lt;/a&gt; called
&lt;a href="http://grano.la"&gt;Granola&lt;/a&gt; &amp;mdash; a free (as in beer) version of our enterprise
power management solution.&lt;/p&gt;

&lt;p&gt;I found myself needing a periodic timer to update the window with new
statistics coming in from the daemon. You can do this in regular python, but
PyGTK, specifically &lt;code&gt;gobject&lt;/code&gt;, makes it very easy. Here&amp;rsquo;s an example that draws
a simple window with a label displaying the current counter.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="python"&gt;&lt;span class="c"&gt;#!/usr/bin/env python&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;gtk&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;gobject&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PerodicTimer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

        &lt;span class="c"&gt;# create a simple window with a label&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gtk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Window&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;destroy&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;wid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;gtk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main_quit&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;delete_event&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;gtk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main_quit&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;vbox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gtk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;VBox&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gtk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Periodic Timer&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;vbox&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pack_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;show_all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="c"&gt;# register a periodic timer&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;gobject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeout_add_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Counter: &amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;periodic_timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PerodicTimer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;gtk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The bulk of the code is just setting up a gtk window, the timer code is only 5
lines or so. As long as &lt;code&gt;callback()&lt;/code&gt; returns &lt;code&gt;True&lt;/code&gt;, the timer gets
re-registered for the original timeout value (1 second in this example). One
caveat I found, &lt;code&gt;gobject.timeout_add_seconds()&lt;/code&gt; seems to depend on the GTK main
loop, so you cannot use it from a regular non-GTK python application.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Byobu Rackspace Cost Plugin</title>
            <link href="http://tuxion.com/2010/01/23/byobu-rackspace-cost-plugin.html" />
            <updated>2010-01-23T00:00:00-05:00</updated>
            <id>http://tuxion.com/2010/01/23/byobu-rackspace-cost-plugin</id>
            <content type="html">&lt;p&gt;If you haven&amp;rsquo;t heard of &lt;a href="https://launchpad.net/byobu"&gt;Byobu&lt;/a&gt; you&amp;rsquo;re missing
out. It&amp;rsquo;s an enhancement to GNU screen that, among many cool features, allows
users to define their own plugins and have them displayed in the statusbar
along the bottom of the terminal.&lt;/p&gt;

&lt;p&gt;The default installation comes with a plugin that displays the approximate
Amazon EC2 cost of the running instance. I wanted a plugin that worked for
Rackspace servers, so I modified it and implemented the Rackspace Cloudserver
pricing model to create
&lt;a href="http://github.com/jbernard/byobu-rcs-cost"&gt;byobu-rcs-cost&lt;/a&gt;. Documentation is
in the README, it&amp;rsquo;s pretty straightforward to install and use.&lt;/p&gt;

&lt;p&gt;Now I can quickly see the estimated cost of each server I&amp;rsquo;m logged into, pretty
cool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; This plugin was merged into the official byobu release as of
version 2.73. See the &lt;a href="https://launchpad.net/byobu"&gt;byobu project page&lt;/a&gt; for
more details.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Ubuntu Resource Management</title>
            <link href="http://tuxion.com/2009/10/13/ubuntu-resource-managment-simple-example.html" />
            <updated>2009-10-13T00:00:00-04:00</updated>
            <id>http://tuxion.com/2009/10/13/ubuntu-resource-managment-simple-example</id>
            <content type="html">&lt;p&gt;I recently wanted to limit CPU usage on my Ubuntu Karmic workstation. Since
&lt;a href="https://launchpad.net/ubuntu/+source/libcgroup"&gt;libcgroup&lt;/a&gt; is included in the
upcoming Karmic release, setting up cgroups on an Ubuntu server has never been
easier.&lt;/p&gt;

&lt;h3&gt;The Goal&lt;/h3&gt;

&lt;p&gt;My needs were as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My machine is dedicated to running an Apache Web Server and I would like for
it to have priority on the CPU whenever it&amp;rsquo;s needed.&lt;/li&gt;
&lt;li&gt;I have an occasional user on my system that chews up CPU like it&amp;rsquo;s going out
of style and slows down my web server. In this example, we&amp;rsquo;ll call this user
&lt;em&gt;kirkland&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to only allow kirkland CPU cycles when no other process is
requesting them. Similar to a &lt;em&gt;sponge&lt;/em&gt;, kirkland will be throttled to 0 if my
apache daemon is really busy.&lt;/p&gt;

&lt;h3&gt;Getting There&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Install libcgroup:
&lt;pre class="terminal"&gt;&lt;code&gt;$ sudo aptitude install libcgroup cgroup-bin
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;Edit /etc/cgconfig.conf, define a sponge group:
&lt;pre&gt;&lt;code&gt;group sponge {
    perm {
        task {
            uid = kirkland;
            gid = kirkland;
        }
        admin {
            uid = root;
            gid = root;
        }
    }
    cpu {
        cpu.shares = 1;
    }
}
mount {
    cpu = /mnt/cgroups/cpu;
    cpuacct = /mnt/cgroups/cpuacct;
}&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;Edit /etc/cgrules.conf, assign kirkland to the sponge group:
&lt;pre&gt;&lt;code&gt;kirkland         cpu             sponge/&lt;br&gt;*                *               sysdefault/&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;Restart cgconfig and cgred services:
&lt;pre class="terminal"&gt;&lt;code&gt;$ sudo service cgconfig restart
$ sudo service cgred restart
&lt;/code&gt;&lt;/pre&gt;
Processes created by kirkland should now automatically get moved into the
sponge cgroup and will only recieve CPU cycles when no other processes are in
need.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For more information about libgroup, you can visit the &lt;a href="http://libcg.sourceforge.net/"&gt;libcgroup project
page&lt;/a&gt;. And for cgroups in general see the
&lt;a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=tree;f=Documentation/cgroups"&gt;current kernel
documentation&lt;/a&gt;.&lt;/p&gt;
</content>
        </entry>
    

</feed>

