Poetryでお手軽yamllint環境を手に入れる(環境を汚さずに)
概要
全てはこのツイートで(ry
昨日、書いたこの記事( https://t.co/kg1iCQqITC )まんまのプルリクを会社で出したら「pipとかrequirements.txtとかマジ古いからやめれwwww」とか言われちゃったので調べてるんだけど
— あとらす (@Lychee_jam) October 7, 2019
python周りはよくわからんな
~
「最低でもpipenv、今ならpoetryがおすすめ」とアドバイス頂いたので調べて再度作成してみました。
ゴールは以前と同じで、pre-commitを使ってコミットする際に自動でyamllintの実行です。
前回の記事はこちら。
目次
参考サイト様
- sdispater/poetry: Python dependency management and packaging made easy.
- Poetry: Python の依存関係管理とパッケージングを支援するツール | org-技術
- Poetryを使ったPythonパッケージ開発からPyPI公開まで - PYTHONIC BOOM BOOM HEAD
前提
- MacBook Pro 2017 Mojave
$ sw_vers ProductName: Mac OS X ProductVersion: 10.14.6 BuildVersion: 18G95 $ python -V Python 3.7.4 $ poetry --version Poetry 0.12.17
成果物
https://github.com/Lycheejam/laradock
前回、pipで制作したものからpoetryに変更した感じです。
導入手順
下準備
すでに該当のプロジェクトが存在する場合は適宜読み替えてください。
$ git clone git@github.com:Lycheejam/laradock.git # 先程、クローンしたディレクトリに移動 $ cd laradock
poetry init
poetry init
コマンドですぐ終わります。
案内に従ってやるだけ。簡単すぎて笑う。
$ poetry init This command will guide you through creating your pyproject.toml config. Package name [laravel-first-sample]: laradock Version [0.1.0]: Description []: Author [Lycheejam <lycheejamwow@gmail.com>, n to skip]: License []: Compatible Python versions [^3.7]: Would you like to define your dependencies (require) interactively? (yes/no) [yes] no Would you like to define your dev dependencies (require-dev) interactively (yes/no) [yes] Search for package: yamllint Found 3 packages matching yamllint Enter package # to add, or the complete package name if it is not listed: [0] yamllint-junit [1] yamllint [2] extended-yamllint > 1 Enter the version constraint to require (or leave blank to use the latest version): Using version ^1.17 for yamllint Search for a package: pre-commit Found 100 packages matching pre-commit Enter package # to add, or the complete package name if it is not listed: [ 0] pre-commit # ...省略 [99] bumpversionsimple > 0 Enter the version constraint to require (or leave blank to use the latest version): Using version ^1.18 for pre-commit Search for a package: Generated file [tool.poetry] name = "laradock" version = "0.1.0" description = "" authors = ["Lycheejam <lycheejamwow@gmail.com>"] [tool.poetry.dependencies] python = "^3.7" [tool.poetry.dev-dependencies] yamllint = "^1.17" pre-commit = "^1.18" [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api" Do you confirm generation? (yes/no) [yes]
必要なパッケージのインストール
以下を実行。
$ poetry install Creating virtualenv laradock-py3.7 in /Users/{user}/work/laradock/.venv Installing dependencies from lock file Package operations: 14 installs, 0 updates, 0 removals - Installing more-itertools (7.2.0) - Installing pyyaml (5.1.2) - Installing six (1.12.0) - Installing zipp (0.6.0) - Installing aspy.yaml (1.3.0) - Installing cfgv (2.0.1) - Installing identify (1.4.7) - Installing importlib-metadata (0.23) - Installing nodeenv (1.3.3) - Installing pathspec (0.6.0) - Installing toml (0.10.0) - Installing virtualenv (16.7.5) - Installing pre-commit (1.18.3) - Installing yamllint (1.17.0)
ファイル確認
できてますね。
$ ls -la total 80 drwxr-xr-x 13 z staff 416 10 9 00:18 ./ drwxr-xr-x 33 z staff 1056 10 9 00:17 ../ -rw-r--r-- 1 z staff 106 8 19 09:06 .env drwxr-xr-x 15 z staff 480 10 9 00:19 .git/ -rw-r--r-- 1 z staff 62 10 6 04:57 .gitignore -rw-r--r-- 1 z staff 114 10 6 05:11 .pre-commit-config.yaml -rw-r--r-- 1 z staff 75 10 6 05:08 .yamllint -rw-r--r-- 1 z staff 1066 8 6 00:04 LICENSE -rw-r--r-- 1 z staff 1154 10 6 06:02 README.md -rw-r--r-- 1 z staff 942 10 6 05:13 docker-compose.yml drwxr-xr-x 6 z staff 192 8 11 16:45 docker-configs/ -rw-r--r-- 1 z staff 6014 10 9 00:14 poetry.lock -rw-r--r-- 1 z staff 313 10 9 00:13 pyproject.toml
pre-commit configの作成
ここは前回と全く同じです。コピペです。
pre-commit用のconfigファイルをプロジェクト直下に作成し、
pre-commit hookに反応してyamllintが走るように設定します。
$ vi .pre-commit-config.yaml
--- repos: - repo: https://github.com/adrienverge/yamllint.git rev: v1.15.0 hooks: - id: yamllint
yamllint configの作成
ここも前回と全く同じです。コピペです。
yamllint用のconfigファイルをプロジェクト直下に作成し、
yamllintが実行される際に適用されるルールを設定します。
$ vi .yamllint
--- extends: default rules: line-length: max: 140 level: warning
仮想環境に切り替え
venvとコマンドが変わるだけで雰囲気は一緒です。
ちょっとターミナルが切り替わらないのはなんかな〜と言った感じですが。
$ poetry shell
pre-commitのインストール
pre-commitをインストールしてgit commitした際に実行されるようにします。
$ pre-commit install pre-commit installed at .git/hooks/pre-commit
以上で環境構築は完了です。
前回記事で紹介していた残りの部分は割愛。
再配布についてもリポジトリをクローンしてもらって、プロジェクトディレクトリでpoetry install
でOKです。
雑感
これで作り直したプルリクはマージされたぞおおおおお。
ついでに社内で布教した。