[Python] pasterコマンドを追加する方法

概要

Pylonsのプロジェクトでpasterコマンドを実行すると、

Usage: /usr/bin/paster COMMAND
usage: paster [paster_options] COMMAND [command_options]

options:
  --version         show program's version number and exit
  --plugin=PLUGINS  Add a plugin to the list of commands (plugins are Egg
                    specs; will also require() the Egg)
  -h, --help        Show this help message

Commands:
  create          Create the file layout for a Python distribution
  grep            Search project for symbol
  help            Display help
  make-config     Install a package and create a fresh config file/directory
  points          Show information about entry points
  serve           Serve the described application
  setup-app       Setup an application, given a config file

pylons:
  controller      Create a Controller and accompanying functional test
  restcontroller  Create a REST Controller and accompanying functional test
  shell           Open an interactive shell with the Pylons app loaded

のように、Pylons用のコマンドが使えるようになります。

この記事では、このようにpasterに新しいコマンドを追加する方法について説明します。

やりかた

ここでは、"Hello, paster!"と表示するhelloコマンドを作ることにします。

まず、pasterコマンドで、helloコマンドを実装するプロジェクト(名前は"Hello"とします)を作成します。

$ paster create Hello
Selected and implied templates:
  PasteScript#basic_package  A basic setuptools-enabled package

Variables:
  egg:      Hello
  package:  hello
  project:  Hello
Enter version (Version (like 0.1)) ['']: 
Enter description (One-line description of the package) ['']: 
Enter long_description (Multi-line description (in reST)) ['']: 
Enter keywords (Space-separated keywords/tags) ['']: 
Enter author (Author name) ['']:
Enter author_email (Author email) ['']:
Enter url (URL of homepage) ['']:
Enter license_name (License name) ['']:
Enter zip_safe (True/False: if the package can be distributed as a .zip file) [False]:
Creating template basic_package
Creating directory ./Hello
  Recursing into +package+
    Creating ./Hello/hello/
    Copying __init__.py to ./Hello/hello/__init__.py
  Copying setup.cfg to ./Hello/setup.cfg
  Copying setup.py_tmpl to ./Hello/setup.py
Running /usr/bin/python setup.py egg_info

helloコマンドを実装するHelloクラスを、hello/commands.pyに作成します。コードは以下の通りです。クラス変数のうち、parserとsummaryは必須です。

# -*- coding: utf-8 -*-

from paste.script.command import Command

class Hello(Command):
    u"""\"Hello, paster!\"を表示します。
    """

    group_name = "hello"
    parser = Command.standard_parser()
    summary = __doc__.splitlines()[0]

    def command(self):
        print "Hello, paster!"

# vim: tabstop=4 shiftwidth=4 expandtab

setup.pyに、以下を記述します。"コマンド名 = モジュール名:クラス名"とします。

      entry_points="""
      # -*- Entry points: -*-
[paste.paster_command]
hello = hello.commands:Hello
      """,

コードの変更箇所は以上です。できあがったら、インストールします。

$ sudo python setup.py install
running install
running bdist_egg
running egg_info
writing Hello.egg-info/PKG-INFO
writing top-level names to Hello.egg-info/top_level.txt
writing dependency_links to Hello.egg-info/dependency_links.txt
writing entry points to Hello.egg-info/entry_points.txt
reading manifest file 'Hello.egg-info/SOURCES.txt'
writing manifest file 'Hello.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-i686/egg
running install_lib
running build_py
creating build/bdist.linux-i686/egg
creating build/bdist.linux-i686/egg/hello
copying build/lib/hello/__init__.py -> build/bdist.linux-i686/egg/hello
copying build/lib/hello/commands.py -> build/bdist.linux-i686/egg/hello
byte-compiling build/bdist.linux-i686/egg/hello/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-i686/egg/hello/commands.py to commands.pyc
creating build/bdist.linux-i686/egg/EGG-INFO
copying Hello.egg-info/PKG-INFO -> build/bdist.linux-i686/egg/EGG-INFO
copying Hello.egg-info/SOURCES.txt -> build/bdist.linux-i686/egg/EGG-INFO
copying Hello.egg-info/dependency_links.txt -> build/bdist.linux-i686/egg/EGG-INFO
copying Hello.egg-info/entry_points.txt -> build/bdist.linux-i686/egg/EGG-INFO
copying Hello.egg-info/not-zip-safe -> build/bdist.linux-i686/egg/EGG-INFO
copying Hello.egg-info/paster_plugins.txt -> build/bdist.linux-i686/egg/EGG-INFO
copying Hello.egg-info/top_level.txt -> build/bdist.linux-i686/egg/EGG-INFO
creating 'dist/Hello-0.0dev-py2.4.egg' and adding 'build/bdist.linux-i686/egg' to it
removing 'build/bdist.linux-i686/egg' (and everything under it)
Processing Hello-0.0dev-py2.4.egg
removing '/usr/lib/python2.4/site-packages/Hello-0.0dev-py2.4.egg' (and everything under it)
creating /usr/lib/python2.4/site-packages/Hello-0.0dev-py2.4.egg
Extracting Hello-0.0dev-py2.4.egg to /usr/lib/python2.4/site-packages
Hello 0.0dev is already the active version in easy-install.pth

Installed /usr/lib/python2.4/site-packages/Hello-0.0dev-py2.4.egg
Processing dependencies for Hello==0.0dev
Finished processing dependencies for Hello==0.0dev

これを使うプロジェクトでは、*.egg-infoディレクトリのpaster_plugins.txtに、

Hello

を記述します。以上で、pasterコマンドを実行すると、

$ paster
Usage: /usr/bin/paster COMMAND
usage: paster [paster_options] COMMAND [command_options]

options:
  --version         show program's version number and exit
  --plugin=PLUGINS  Add a plugin to the list of commands (plugins are Egg
                    specs; will also require() the Egg)
  -h, --help        Show this help message

Commands:
  create       Create the file layout for a Python distribution
  grep         Search project for symbol
  help         Display help
  make-config  Install a package and create a fresh config file/directory
  points       Show information about entry points
  serve        Serve the described application
  setup-app    Setup an application, given a config file

hello:
  hello        "Hello, paster!"を表示します。

と表示され、paster helloコマンドを実行すると、

$ paster hello
Hello, paster!

となります。

このHelloプロジェクトは、http://nekomimists.ddo.jp/~tom/repository/Hello-0.0dev.tar.gzからダウンロードできます。