Distutilsの拡張
***************

Distutilsは様々な方法で拡張できます。ほとんどの拡張は存在するコマンド
を新しいコマンドで置換する形でおこなわれます。新しいコマンドはたとえば
存在するコマンドを置換して、そのコマンドでパッケージをどう処理するかの
細部を変更することでプラットフォーム特有のパッケージ形式をサポートする
ために書かれているかもしれません。

ほとんどのdistutilsの拡張は存在するコマンドを変更したい "setup.py" ス
クリプト中で行われます。ほとんどはパッケージにコピーされるファイル拡張
子を ".py" の他に、いくつか追加するものです。

ほとんどのdistutilsのコマンド実装は "distutils.cmd" の
"distutils.cmd.Command" クラスのサブクラスとして実装されています。新し
いコマンドは "Command" を直接継承し、置換するコマンドでは置換対象のコ
マンドのサブクラスにすることで "Command" を間接的に継承します。コマン
ドは "Command" から派生したものである必要があります。


新しいコマンドの統合
====================

新しいコマンド実装を統合するにはいくつかの方法があります。一番難しいも
のは新機能をdistutils本体に取り込み、それのサポートを提供するPythonの
バージョンが出ることを待つ(そして使う)ことです。これは様々な理由で本当
に難しいことです。

もっとも一般的な、そしておそらくほとんどの場合にもっとも妥当な方法は、
新しい実装をあなたの "setup.py" スクリプトに取り込み、
"distutils.core.setup()" 関数でそれらを使うようにすることです:

   from distutils.command.build_py import build_py as _build_py
   from distutils.core import setup

   class build_py(_build_py):
       """Specialized Python source builder."""

       # implement whatever needs to be different...

   setup(cmdclass={'build_py': build_py},
         ...)

このアプローチは新実装をある特定のパッケージで利用したい時、そのパッケ
ージに興味をもつ人全員がコマンドの新実装を必要とする時にもっとも価値が
あります。

Beginning with Python 2.4, a third option is available, intended to
allow new commands to be added which can support existing "setup.py"
scripts without requiring modifications to the Python installation.
This is expected to allow third-party extensions to provide support
for additional packaging systems, but the commands can be used for
anything distutils commands can be used for.  A new configuration
option, "command_packages" (command-line option "--command-packages"),
can be used to specify additional packages to be searched for modules
implementing commands.  Like all distutils options, this can be
specified on the command line or in a configuration file.  This option
can only be set in the "[global]" section of a configuration file, or
before any commands on the command line.  If set in a configuration
file, it can be overridden from the command line; setting it to an
empty string on the command line causes the default to be used.  This
should never be set in a configuration file provided with a package.

この新オプションによってコマンド実装を探すためのパッケージをいくつでも
追加することができます。複数のパッケージ名はコンマで区切って指定します
。指定がなければ、検索は "distutils.command" パッケージのみで行われま
す。ただし "setup.py" がオプション "--command-packages
distcmds,buildcmds" で実行されている場合には、パッケージは
"distutils.command" 、 "distcmds" 、そして "buildcmds" を、この順番で
検索します。新コマンドはコマンドと同じ名前のモジュールに、コマンドと同
じ名前のクラスで実装されていると想定しています。上のコマドラインオプシ
ョンの例では、コマンド **bdist_openpkg** は、
"distcmds.bdist_openpkg.bdist_openpkg" か、
"buildcmds.bdist_openpkg.bdist_openpkg" で実装されるかもしれません。


配布物の種類を追加する
======================

配布物 ("dist/" ディレクトリの中のファイル) を作成するコマンドは、
**upload** がその配布物をPyPIにアップロードできるように、 "(command,
filename)" のペアを "self.distribution.dist_files" に追加する必要があ
ります。ペア中の *filename* はパスに関する情報を持たず、単にファイル名
だけを持ちます。 dry-run モードでも、何が作成されたかを示すために、同
じペアが必要になります。
