9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NTTコミュニケーションズAdvent Calendar 2017

Day 2

Python Watchdogのtricksを試す

Posted at

はじめに

WatchdogとはPython製のファイル監視ライブラリです。
開発中、ファイルの更新を監視してテストを自動で走らせたい時などに利用します。
今回はWatchdogのwatchmedoコマンドのtricksを試します。

インストール

$ pip install watchdog
$ watchmedo --version
watchmedo 0.8.3

使い方

まずはヘルプを眺めます。

$ watchmedo --help
usage: watchmedo [-h] [--version]
                 {tricks-from,tricks,tricks-generate-yaml,generate-tricks-yaml,log,shell-command,auto-restart}
                 ...

positional arguments:
  {tricks-from,tricks,tricks-generate-yaml,generate-tricks-yaml,log,shell-command,auto-restart}
    tricks-from (tricks)
                        Subcommand to execute tricks from a tricks
                        configuration file. :param args: Command line argument
                        options.
    tricks-generate-yaml (generate-tricks-yaml)
                        Subcommand to generate Yaml configuration for tricks
                        named on the command line. :param args: Command line
                        argument options.
    log                 Subcommand to log file system events to the console.
                        :param args: Command line argument options.
    shell-command       Subcommand to execute shell commands in response to
                        file system events. :param args: Command line argument
                        options.
    auto-restart        Subcommand to start a long-running subprocess and
                        restart it on matched events. :param args: Command
                        line argument options.

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>.
Copyright 2012 Google, Inc.

Licensed under the terms of the Apache license, version 2.0. Please see
LICENSE in the source code for more information.

tricks-generate-yaml

YAMLファイルを生成してくれそうなサブコマンドを打ちます。

$ watchmedo tricks-generate-yaml
python-path: [.]
tricks:

ちょっと物足りないですね。

tricksファイルの書き方

続いて書き方を確認します。
サンプルは以下の様に書かれています。tricks以下にクラスとパラメータを指定してあげれば良さそうです。

tricks.yaml
tricks:
- watchdog.tricks.LoggerTrick:
    patterns: ["*.py", "*.js"]
- watchmedo_webtricks.GoogleClosureTrick:
    patterns: ['*.js']
    hash_names: true
    mappings_format: json                  # json|yaml|python
    mappings_module: app/javascript_mappings
    suffix: .min.js
    compilation_level: advanced            # simple|advanced
    source_directory: app/static/js/
    destination_directory: app/public/js/
    files:
      index-page:
      - app/static/js/vendor/jquery.js
      - app/static/js/base.js
      - app/static/js/index-page.js
      about-page:
      - app/static/js/vendor/jquery.js
      - app/static/js/base.js
      - app/static/js/about-page.js

というわけで以下の方針でファイルを作成しました。

  • LoggerTrick でログ出力をする
    • ディレクトリの更新は無視
  • ShellCommandTrickで実行する
    • *.py を監視
    • 実行中は監視を無視
tricks.yaml
python-path: [.]
tricks:
- watchdog.tricks.LoggerTrick:
    patterns: ["*"]
    ignore_directories: true
- watchdog.tricks.ShellCommandTrick:
    patterns: ["*.py"]
    shell_command: python ${watch_src_path}
    drop_during_process: true

実行

作成したファイルをtricks-fromで指定します。

$ watchmedo tricks-from tricks.yaml

早速適当なエディタでファイルを作ってみます。

hello.py
#coding: utf-8

print("hello tricks!")

保存すると以下が出力されます。作成時のイベントを拾ってスクリプトを実行できているようです。
更新時はon_modified、削除時はon_deletedが出力されます。

on_created(self=<watchdog.tricks.LoggerTrick object at 0x6ffff92b510>, event=<FileCreatedEvent: src_path='./hello.py'>)
hello tricks!
9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?