Lifecycle and Hooks#
Pythonの成果物として、プロジェクトはPythonプロジェクトライフサイクルのさまざまなフェーズを経て、PDMはそれらのフェーズで期待されるタスクを実行するためのコマンドを提供します。
また、これらのステップに取り付けられたフックにより、次のことが可能になります。
- 同じ名前のsignalsをリッスンするプラグイン。
- 開発者が同じ名前でカスタムスクリプトを定義する。
さらに、pre_invoke
シグナルはANYコマンドが呼び出される前に発行され、プラグインがプロジェクトやオプションを事前に変更できるようにします。
組み込みコマンドは、現在3つのグループに分かれています。
- the initialization phase
- the dependencies management.
- the publication phase.
ほとんどの場合、インストールフェーズとパブリッシングフェーズの間に、いくつかの繰り返しタスク(ハウスキーピング、リンティング、テストなど)を実行する必要があります。これが、PDMでuser scriptsを使用して独自のタスク/フェーズを定義できる理由です。
完全な柔軟性を提供するために、PDMでは、必要に応じて一部のフックとタスクをスキップできます。
Initialization#
初期化フェーズは、pdm init
コマンドを実行して既存のプロジェクトを初期化することにより(プロンプトでpyproject.toml
ファイルに入力します)、プロジェクトのライフタイム中に1回だけ発生します。
次のフックがトリガーされます。:
flowchart LR
subgraph pdm-init [pdm init]
direction LR
post-init{{Emit post_init}}
init --> post-init
end
Dependencies management#
依存関係の管理は、開発者が次の作業を実行できるようにするために必要です。:
lock
:pyproject.toml
要件からロックファイルを計算します。sync
:ロックファイルからPEP582パッケージを同期(追加/削除/更新)し、現在のプロジェクトを編集可能としてインストールします。add
:依存性を追加します。remove
:依存関係を削除します。
これらの手順はすべて、次のコマンドで直接実行できます。:
pdm lock
:lock
タスクを実行しますpdm sync
:sync
タスクを実行しますpdm install
:必要に応じてlock
の前にsync
タスクを実行しますpdm add
:依存関係要件を追加し、再ロックしてから同期します。pdm remove
:依存関係の要件を削除し、再ロックしてから同期します。pdm update
:最新バージョンからの依存関係を再ロックして同期します。
次のフックがトリガーされます。:
flowchart LR
subgraph pdm-install [pdm install]
direction LR
subgraph pdm-lock [pdm lock]
direction TB
pre-lock{{Emit pre_lock}}
post-lock{{Emit post_lock}}
pre-lock --> lock --> post-lock
end
subgraph pdm-sync [pdm sync]
direction TB
pre-install{{Emit pre_install}}
post-install{{Emit post_install}}
pre-install --> sync --> post-install
end
pdm-lock --> pdm-sync
end
Switching Python version#
これは依存関係管理の特殊なケースです。pdm use
を使用して現在のPythonバージョンを切り替えると、新しいPythonインタプリタでpost_use
シグナルが出力されます。
flowchart LR
subgraph pdm-use [pdm use]
direction LR
post-use{{Emit post_use}}
use --> post-use
end
Publication#
パッケージ/ライブラリを公開する準備ができたら、すぐに公開タスクが必要になります。
build
: build/compile assets requiring it and package everything into a Python package (sdist, wheel)upload
: upload/publish the package to a remote PyPI index
これらの手順はすべて、次のコマンドで実行できます。
build
:それを必要とするアセットをビルド/コンパイルし、すべてをPythonパッケージにパッケージします(sdist, wheel)upload
:パッケージをリモートPyPIインデックスにアップロード/公開します。
次のフックがトリガーされます。:
flowchart LR
subgraph pdm-publish [pdm publish]
direction LR
pre-publish{{Emit pre_publish}}
post-publish{{Emit post_publish}}
subgraph pdm-build [pdm build]
pre-build{{Emit pre_build}}
post-build{{Emit post_build}}
pre-build --> build --> post-build
end
%% subgraph pdm-upload [pdm upload]
%% pre-upload{{Emit pre_upload}}
%% post-upload{{Emit post_upload}}
%% pre-upload --> upload --> post-upload
%% end
pre-publish --> pdm-build --> upload --> post-publish
end
フックが含まれた最初の失敗で実行が停止します。
User scripts#
ユーザスクリプトについては、それぞれのセクションで詳しく説明しています。ただし、次の点に注意してください。:
- 各ユーザスクリプトは、複合スクリプトを含む
pre_*
およびpost_*
スクリプトを定義できます。 - 各
run
実行は、pre_run
フックとpost_run
フックをトリガーします。 - 各スクリプトの実行によって、
pre_script
フックとpost_script
フックがトリガーされます。
次のようなscripts
の定義があるとします。:
1 2 3 4 5 6 7 8 9 |
|
pdm run test
のライフサイクルは次のようになります。:
flowchart LR
subgraph pdm-run-test [pdm run test]
direction LR
pre-run{{Emit pre_run}}
post-run{{Emit post_run}}
subgraph run-test [test task]
direction TB
pre-script{{Emit pre_script}}
post-script{{Emit post_script}}
pre-test[Execute pre_test]
post-test[Execute post_test]
test[Execute test]
pre-script --> pre-test --> test --> post-test --> post-script
end
pre-run --> run-test --> post-run
end
一方、pdm run composite
は次のようになります。:
flowchart LR
subgraph pdm-run-composite [pdm run composite]
direction LR
pre-run{{Emit pre_run}}
post-run{{Emit post_run}}
subgraph run-composite [composite task]
direction TB
pre-script-composite{{Emit pre_script}}
post-script-composite{{Emit post_script}}
pre-composite[Execute pre_composite]
post-composite[Execute post_composite]
subgraph run-test [test task]
direction TB
pre-script-test{{Emit pre_script}}
post-script-test{{Emit post_script}}
pre-test[Execute pre_test]
post-test[Execute post_test]
pre-script-test --> pre-test --> test --> post-test --> post-script-test
end
pre-script-composite --> pre-composite --> run-test --> post-composite --> post-script-composite
end
pre-run --> run-composite --> post-run
end
Skipping#
--skip
オプションを使用すると、組み込みコマンドおよびカスタムユーザスクリプトに対して実行するタスクとフックを制御できます。
It accepts a comma-separated list of hooks/task names to skip as well as the predefined :all
, :pre
and :post
shortcuts respectively skipping all hooks, all pre_*
hooks and all post_*
hooks.
すべてのフック、すべてのpre_*
フック、およびすべてのpost_*
フックをそれぞれスキップする、事前定義された:all
、:pre
、および:post
ショートカットと同様に、スキップするフック/タスク名のコンマ区切りのリストを受け入れます。スキップリストはPDM_SKIP_HOOKS
環境変数に指定することもできますが、--skip
パラメータが指定されるとすぐに上書きされます。
前のスクリプトブロックでpdm run --skip=:pre,post_test composite
を実行すると、ライフサイクルが次のように短縮されます。
flowchart LR
subgraph pdm-run-composite [pdm run composite]
direction LR
post-run{{Emit post_run}}
subgraph run-composite [composite task]
direction TB
post-script-composite{{Emit post_script}}
post-composite[Execute post_composite]
subgraph run-test [test task]
direction TB
post-script-test{{Emit post_script}}
test --> post-script-test
end
run-test --> post-composite --> post-script-composite
end
run-composite --> post-run
end