The Bengfort Toolkit

Programming life has finally caused me to give into something that I’ve resisted for a while: the creation of a Bengfort Toolkit and specifically a benlib. This post is mostly a reminder that this toolkit now exists and that I spent valuable time creating it against my better judgement. And as a result, I should probably use it and update it.

I’ve already written (whoops, I almost said “you’ve already read” but I know no one reads this) posts about tools that I use frequently including clock.py and requires. These things have been simply Python scripts that I’ve put in ~/bin, which is part of my $PATH. These are too small or simple to require full blown repositories and PyPI listings on their own merit. Plus, I honestly believe that I’m the only one that uses them.

Dependencies are the problem though. For example, clock.py requires python-dateutil. This has resulted in my pretty much installing the dependent packages in every single one of my repositories. You may have noticed and removed extra dependencies on a pull request. However, I could live with that until …

I needed gitpython for another quick script to modify the ID line of my codebase. Finally, I couldn’t just stick the dependency in all of my projects, I needed management. Also this third script put me over the top on managing all the stuff that I have going on. The toolkit was created.

I’m excited because now I can pip install bengfort-toolkit anywhere I go, including on remote machines. But I’m still a little bit weary of having to maintain this toolkit. Particularly when stuff like this happens:

import sys
import fileinput

def comment_file(path):
    """
    Comments out a Python file so that it can't be imported.
    """
    for line in fileinput.input(path, inplace=True):
        if not line.startswith("#"):
            line = "# " + line
        sys.stdout.write(line)

Do you know what this does (I mean other than the obvious of printing out a python code that is commented out)? It literally modifies the file in place by moving the original file to a “.bak” extension, reading it, then hooking stdout up to the original file descriptor for writing. The module will then remove the “.bak” if it successfully completes.

Mind. Blown. Unix programmers, am I right?

Also, seriously?

Previously:

import os
import shutil
import tempfile

def comment_file(path):
    """
    Writes commented file to a temporary file.
    Then moves the temp file to original location.
    """
    with open(path, 'r') as f:
        o, tmp = tempfile.mkstemp()
        for line in f.readlines():
            if not line.startswith('#'):
                line = "# " + line
            o.write(line)

    shutil.copy(tmp, path)
    os.remove(tmp)

And the conflict is that I’m not sure what I prefer. When I open source stuff and expect other folks to use it, I feel confident that stuff like this can be resolved by general consensus. If others who use open source tools decide that one method is less secure than the other, they will update it. Bengfort Toolkit has none of these safe guards. But hopefully the toolkit will help me manage my own development workflow.