もた日記

くだらないことを真面目にやる

Pythonメモ : cookiecutterでプロジェクトの雛形を作成する

cookiecutter


github.com

cookiecutterを使用するとPythonパッケージやDjangoなどのプロジェクトの雛形を作成することができるので試してみる。Pythonで書かれているツールなのでPython関連の雛形が多いが、他言語の雛形も作成できるようだ。

f:id:wonder-wall:20170907170207p:plain f:id:wonder-wall:20170907202737p:plain


インストール


pipでインストールできるので下記コマンドを実行。

$ pip install cookiecutter

ヘルプメッセージ

$ cookiecutter --help
Usage: cookiecutter [OPTIONS] TEMPLATE [EXTRA_CONTEXT]...

  Create a project from a Cookiecutter project template (TEMPLATE).

  Cookiecutter is free and open source software, developed and managed by
  volunteers. If you would like to help out or fund the project, please get
  in touch at https://github.com/audreyr/cookiecutter.

Options:
  -V, --version              Show the version and exit.
  --no-input                 Do not prompt for parameters and only use
                             cookiecutter.json file content
  -c, --checkout TEXT        branch, tag or commit to checkout after git clone
  -v, --verbose              Print debug information
  --replay                   Do not prompt for parameters and only use
                             information entered previously
  -f, --overwrite-if-exists  Overwrite the contents of the output directory if
                             it already exists
  -o, --output-dir PATH      Where to output the generated project dir into
  --config-file PATH         User configuration file
  --default-config           Do not load a config file. Use the defaults
                             instead
  --debug-file PATH          File to be used as a stream for DEBUG logging
  -h, --help                 Show this message and exit.


使い方


使い方はcookiecutter TEMPLATEのように雛形を指定する。雛形は公開されているものを利用するか自作する。 公開されている雛形リストはREADMEにまとめれており、人気がありそうなのは以下の雛形。以降で実際に試してみる。

cookiecutter-django

  • スター数:2,864
  • WebアプリケーションフレームワークDjangoの雛形

cookiecutter-flask

  • スター数:1,453
  • WebアプリケーションフレームワークFlaskの雛形

cookiecutter-pypackage

  • スター数:1,121
  • Pythonパッケージの雛形

cookiecutter-data-science

  • スター数:637
  • データサイエンス向けの雛形


cookiecutter-pypackage


Pythonパッケージの場合。雛形は以下のようにURLを指定すればよい(ローカルディレクトリを指定することもできる)。途中で雛形作成に必要なプロジェクト名などを聞かれるので入力していく(何も入力せずにリターンキーを押すと[]内のデフォルト値が適用される)。

$ cookiecutter https://github.com/audreyr/cookiecutter-pypackage
full_name [Audrey Roy Greenfeld]:
email [aroy@alum.mit.edu]:
github_username [audreyr]:
project_name [Python Boilerplate]:
project_slug [python_boilerplate]:
project_short_description [Python Boilerplate contains all the boilerplate you need to create a Python package.]:
pypi_username [audreyr]:
version [0.1.0]:
use_pytest [n]:
use_pypi_deployment_with_travis [y]:
Select command_line_interface:
1 - Click
2 - No command-line interface
Choose from 1, 2 [1]:
create_author_file [y]:
Select open_source_license:
1 - MIT license
2 - BSD license
3 - ISC license
4 - Apache Software License 2.0
5 - GNU General Public License v3
6 - Not open source
Choose from 1, 2, 3, 4, 5, 6 [1]:

全ての質問に回答すると下記ディレクトリ構成のプロジェクトが作成される(ファイル内の値は入力した値で置換されている)。

$ exa --tree python_boilerplate
python_boilerplate
├── AUTHORS.rst
├── CONTRIBUTING.rst
├── HISTORY.rst
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.rst
├── docs
│  ├── Makefile
│  ├── authors.rst
│  ├── conf.py
│  ├── contributing.rst
│  ├── history.rst
│  ├── index.rst
│  ├── installation.rst
│  ├── make.bat
│  ├── readme.rst
│  └── usage.rst
├── python_boilerplate
│  ├── __init__.py
│  ├── cli.py
│  └── python_boilerplate.py
├── requirements_dev.txt
├── setup.cfg
├── setup.py
├── tests
│  ├── __init__.py
│  └── test_python_boilerplate.py
├── tox.ini
└── travis_pypi_setup.py


cookiecutter-django


WebアプリケーションフレームワークDjangoの場合。use_*の質問でどの環境、ツールを使用するか指定できる。

cookiecutter https://github.com/pydanny/cookiecutter-django
project_name [Project Name]:
project_slug [project_name]:
author_name [Daniel Roy Greenfeld]:
email [you@example.com]:
description [A short description of the project.]:
domain_name [example.com]:
version [0.1.0]:
timezone [UTC]:
use_whitenoise [y]:
use_celery [n]:
use_mailhog [n]:
use_sentry_for_error_reporting [y]:
use_opbeat [n]:
use_pycharm [n]:
windows [n]:
use_docker [n]:
use_heroku [n]:
use_elasticbeanstalk_experimental [n]:
use_compressor [n]:
Select postgresql_version:
1 - 9.6
2 - 9.5
3 - 9.4
4 - 9.3
5 - 9.2
Choose from 1, 2, 3, 4, 5 [1]:
Select js_task_runner:
1 - Gulp
2 - Grunt
3 - None
Choose from 1, 2, 3 [1]:
custom_bootstrap_compilation [n]:
Select open_source_license:
1 - MIT
2 - BSD
3 - GPLv3
4 - Apache Software License 2.0
5 - Not open source
Choose from 1, 2, 3, 4, 5 [1]:


cookiecutter-data-science


データサイエンス向けのプロジェクトの場合。

$ cookiecutter https://github.com/drivendata/cookiecutter-data-science
project_name [project_name]:
repo_name [project_name]:
author_name [Your name (or your organization/company/team)]:
description [A short description of the project.]:
Select open_source_license:
1 - MIT
2 - BSD
3 - Not open source
Choose from 1, 2, 3 [1]:
s3_bucket [[OPTIONAL] your-bucket-for-syncing-data (do not include 's3://')]:
aws_profile [default]:
Select python_interpreter:
1 - python
2 - python3
Choose from 1, 2 [1]:

決まった形式はないのかもしれないが、ディレクトリ構成などは参考になるのかも。

$ exa --tree project_name
project_name
├── LICENSE
├── Makefile
├── README.md
├── data
│  ├── external
│  ├── interim
│  ├── processed
│  └── raw
├── docs
│  ├── Makefile
│  ├── commands.rst
│  ├── conf.py
│  ├── getting-started.rst
│  ├── index.rst
│  └── make.bat
├── models
├── notebooks
├── references
├── reports
│  └── figures
├── requirements.txt
├── src
│  ├── __init__.py
│  ├── data
│  │  └── make_dataset.py
│  ├── features
│  │  └── build_features.py
│  ├── models
│  │  ├── predict_model.py
│  │  └── train_model.py
│  └── visualization
│     └── visualize.py
├── test_environment.py
└── tox.ini


雛形の自作


雛形をゼロから自作するのは大変なので基本的には既存の雛形をForkして変更するのが簡単そう。雛形の作成方法はドキュメントを参照。 なお、cookiecutter用の雛形を作成するcookiecutte-templateという雛形もあった。

$ cookiecutter https://github.com/eviweb/cookiecutter-template
full_name [Your name]:
email [Your address email (eq. you@example.com)]:
github_username [Your github username]:
project_name [Name of the project]:
project_slug [cookiecutter-name-of-the-project]:
project_short_description [A short description of the project]:
release_date [2017-09-07]:
version [0.1.0]:
copy_hooks [no]:
$ exa --tree cookiecutter-name-of-the-project
cookiecutter-name-of-the-project
├── CHANGELOG.md
├── LICENSE
├── README.md
├── VERSION
├── cookiecutter.json
└── {{cookiecutter.project_slug}}