Google App Engineのstatic_dirのバグ対応(Windows環境)

すぐ対策されると思うけど、Windows環境限定のバグのメモ。

現象:app.yamlにstatic_dirを設定するとエラーが出る

app.yaml を下記のような状態にして、

application: helloworld
version: 1
runtime: python
api_version: 1

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /.*
  script: helloworld.py

サーバを立ち上げる(コマンドラインから dev_appserver.py helloworld などを実行する)と、

ERROR 2008-04-11 00:15:26,483 dev_appserver_main.py] Application configuration file invalid:
regex invalid: unbalanced parenthesis

というエラーが出てサーバが立ち上がらなくなる。

ダメな対策:app.yamlを書き換える

app.yamlのstatic_dir設定部分を下記のように書き換えると、サーバは立ち上がるものの、static_dirの設定はエラーったままである。

- url: /stylesheets/
  static_dir: stylesheets

ページをリクエストしてサーバの動作ログを見るとERRORが2つ出てる。Error encountered reading file とか [Errno 2] No such file or directory とか。staticファイルのHTTPレスポンスコードでも404が返ってきてる。ダメじゃん。

これでなんとかなる対策:dev_appserver.pyを書き換える

\google\appengine\tools\dev_appserver.py の2369〜2370行を書き換える。

元のコードの2369〜2370行。

      regex = os.path.join(re.escape(regex), '(.*)')
      path = os.path.join(path, '\\1')

これ↑を↓に書き換える。

      regex = re.escape(regex) + '/(.*)'
      path = path + '/\\1'

Pythonなので、インデントに注意すること。

参考(参考っつーかもはや和訳じゃんね)。