Introducing wk

posted

A while back I wrote about some handy aliases for virtualenv and serializing frequently and commonly chained commands that one uses to setup project environments. This is a first step at realizing the ideas I had when I wrote that post.

The need for WK

Like the previous post explained, I have these project aliases that setup my work environments,

alias wk.frankvalcarcel.com="workon pelican && cd /Users/frankv/Work/frankvalcarcel.com && ls -la"

These aliases activate the project's virtualenv, navigates to the project's directory, and for now... lists the contents of the directory. I wanted a little more.

I don't think my workflow differs much from other Python developers. Usually I need my env activated, my editor opened to the proper directory, a project specific command run, and usually a browser window opened. In all the chain of commands will usually look something like this:

     $ workon foo
(foo)$ cd ~/projects/foo
(foo)$ subl .
(foo)$ python manage.py fooUp
 ** Starting foo on 0.0.0.0:5000
 ** Reloader etc.

Setting up project automation is going to be a pretty small checklist. I knew right away that abstracting the great scripting tools in the terminal was not a good approach. I need only provide the mechanism upon which to chain these parts together, and I got my idea for this from Bower's project initialization...

$ bower init
? name: foo
? version: 0.0.1
? description: this is a foo project, demo bower init
? license: MIT
? homepage: www.foo.com
? set currently installed components as dependencies?: Yes
? add commonly ignored files to ignore list?: Yes

That's obviously a lighter version of the full init script, and for wk I certainly don't need that kind of information.

Getting Started

wk is going to be a work in progress for sometime. Mostly because I think I'll have to fork and modify the library I used to write it. Luckily that is the only dependency you wont already have in your native Python environment. wk is built on click

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary.

click is another great tool by the incredible Armin Ronacher(@mitsuhiko), the same guy that brought us flask and werkzeug; (yes I'm starting to feel like a fanboy, but hey... how could you not?!). For what I want click has a couple limitations I hope to work around.

wk is publicly available on GitHub and I welcome forks, feedback and pull-requests. Checkout the README to help you get started. The things you'll need are a global installation of click and to install wk in editable mode. So from your wk project directory, run

$ pip install click
$ pip install --editable .

The last step is to setup the wk project directory. I borrowed this idea from virtualenv wrapper and rvm. It works for them, so it should be fine in this case. Add this line to your .bashrc file, it will instruct wk to load and save from the specified directory:

export WK_HOME=$HOME/.wk

Now you'll be able to run wk anywhere on your terminal.

$ wk
Usage: wk [OPTIONS] COMMAND [ARGS]...

  Create a wk object and remember it as as the context object.  From this
  point onwards other commands can refer to it by using the @wk_ctx
  decorator.

Options:
  --name NAME         wk project name
  --config KEY VALUE  Overrides a config key/value pair.
  -v, --verbose       verbose ouput
  --version           Show the version and exit.
  --help              Show this message and exit.

Commands:
  load
  setup

Setting Up a Project

I'm trying to make this step as easy as possible. Ideally, from the directory your project is located you should be able to fly through setup by just slapping enter. I'll explain each step and their respective defaults:

Start by running wk setup

user@host:~/foo$ wk setup
wk setup project
enter project name, [foo]:
enter project directory, [/Users/frankv/Work/projects/foo]:

The project name will become the project command, it's default is the current directories name. The project directory will default to the current one as well. Next wk will gather information about any virtualenv, big shot out to Emily Morehouse(@emilyemorhouse) for the help with this part!

It works two ways and handles existing envs pretty well.

VirtualEnv? [Y/n]: Y
From existing? [y/N]: N

If you select From existing? and you are using virtualenv wrapper, wk will list the envs and let you select which one corresponds to your project.

From existing? [y/N]: Y
 0: amazon-scraper
 1: breakingbad
 2: brewfest
 3: cuttledocs
enter number:

Entering the number of the env will save that env's location with your wk project. If you select N then wk will make a new env named after the project.

From existing? [y/N]: N
virtualenv name, [foo]:
New python executable in foo/bin/python
Installing setuptools, pip...done.
Press any key to continue ...

If you do not use virtualenv wrapper all of this happens in the project directory that you specified earlier.

Once you complete the setup, wk serializes the project object into a YAML file under the WK_HOME directory that you defined in your .bashrc file. I chose YAML over JSON for a few reasons but will build in a swappable serialization system later. I purposefully avoided serialization libraries like pickle for reasons that will become much clearer in future releases.

The YAML file for this example looks like this;

!!python/object:wk.WK
config:
  directory: /Users/frankv/Work/projects/foo
  venv: true
  venv_directory: /Users/frankv/.venvs/foo
name: foo
verbose: false

Some of this I didn't explain yet, I will in future updates. For now the setup command is only about 60% complete. Once I'm closer to a beta release I'll publish full documentation on readthedocs. Currently I wouldn't even claim to be pre-alpha, I'm mostly putting this out there for some early stage feedback.

Loading a Project

The load command is really fucking broken right now. I was hoping to avoid commands like: wk load foo in lieu of wk.foo, but I know I can't do this! This is what I'll spend the next couple weeks working on, but jobs and other deadlines will probably guarantee slow turn around. Watch my twitter or this blog for updates, or fork your own version and join the fun.

Python, Productivity, Workflow

Latest Posts

The Martian.

This book was absolutely riveting. It kept me up two nights in a row and had me imagining amber Martian landscapes around the clock. The author, Andy Weir, was previously a software engineer,...

Pragmatic MVP, References

The Pragmatic MVP is a talk I gave at TalTech Expo 2015 on building effective early stage prototypes. Below is a list of websites, articles, and books I used in preparation...

Introducing wk

A while back I wrote about some handy aliases for virtualenv and serializing frequently and commonly chained commands that one uses to setup project environments. This is a first step at......