git と GAE の練習 (2)

_git log_だの_git log -p_だのを確認。_git log -p --pretty=short_というのもあるのか。
機械的に hwlloworld.py をコピーして commit して、というのを繰り返してるだけなんですが、ぱっと見ベースで python のコードって可読性が高い。

webapp

メソドに渡される self という引数で request やら response やらをナニしてる模様。例えば

  • self.response.headers でヘッダを設定したり
  • self.response.out.write で出力
  • self.redirect でリダイレクト
  • self.request.uri はリクエストな uri 持ってこれるのか
  • self.request.get でフォームのパーツの名前指定でデータ取得可能

これまでのサンプル (Using the Datastroe) ではこれくらいまでしか出てきてない模様。あと、Using the Datastore ではクエリの色々なパターンが出てきてます。

Using Django Templates

ここではテンプレートの index.html が追加になっている。_git status_見てみたら以下。

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   helloworld.py
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       index.html
no changes added to commit (use "git add" and/or "git commit -a")
$

これ、index.html が git なインデックスに未登録、という事のはず。インデックスに登録するには_git add index.html_ってやれば良いのでしょうが、まるっと commit するのであれば

  • _git add -u_して_git commit_
  • _git commit -a_

という方法もあるみたい。今参照している Chapter.4 で commit を取り消す方法が紹介されてるので、それを試してみつつ上記のナニが正しいかどうかを確認してみます。

変更の取り消しと commit の確認

ええと、まずは index.html を add して commit してみます。

$ git add index.html 
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   index.html
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   helloworld.py
#
$

この状態で_git diff_だと helloworld.py の diff しか出力されませんが、_git diff HEAD_だと、index.html の追加も出力されます。
これで commit してみたんですが、helloworld.py の変更が commit されてない模様。

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   helloworld.py
#
no changes added to commit (use "git add" and/or "git commit -a")
$

うーん。これって_git commit -a_すべきだったのか。阿呆だな。< わし
丁度良いので直前の commit を取り消してみる。いっちゃん簡単なのは_git reset HEAD^_ですな。とりあえずヤッてみます。

$ git reset HEAD^
helloworld.py: locally modified
$

_git log_を見る限りは直前の commit は取り消されてます。_git status_は以下な出力。

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   helloworld.py
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       index.html
no changes added to commit (use "git add" and/or "git commit -a")
$

これで_git commit -a_すれば両方の修正がリポジトリに反映されるはず。で、_git status_見たら以下。

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   helloworld.py
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       index.html
no changes added to commit (use "git add" and/or "git commit -a")
$

とほほ。何してるんでしょ。このケイスだと_git add -A_してから_git commit -a_なのかなぁ。もっかい取り消してリトライ。

$ git reset HEAD^
$ git add -A
$ git commit -a
$ git status
# On branch master
nothing to commit (working directory clean)
$

ヨシ。_git log -p --pretty=short_でも直前 commit で二つのナニが盛り込まれてるのが確認できます。しかもリトライは全部歴史には残されてないですね。これはヒドいコマンドかも (何
あと、_git add -p_とか試してみます。とりあえず先の commit を再度ステ。

$ git reset HEAD^
helloworld.py: locally modified
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   helloworld.py
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       index.html
no changes added to commit (use "git add" and/or "git commit -a")
$

で、元に戻ったので、_git add -p_を試してみたんですが、index.html に関する情報が出てこないですね。結局の所、ファイルが追加されるケイスでは

  • 追加したファイルを明示的に_git add _する
  • _git add -A_する

の後に_git commit_するのか。ちなみに今回のように追加と修正を一つの commit に盛り込む場合には_git commit -a_する必要がある、と。しかしこのリポジトリの歴史を完全に書き換えできる、というのはなかなか凄いですな。間違えたらやり直しができる、というのは非常にポイント高い。
# って cvs とか svn とかそこまでがっつり使ってないですが
で、とりあえずこの時点のソレを github に反映。

$ git push origin master
Enter passphrase for key '/Users/tyamane/.ssh/id_rsa': 
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 2.29 KiB, done.
Total 10 (delta 4), reused 0 (delta 0)
To git@github.com:yamanetoshi/GettingStartedPython-GAE-.git
   495b2ba..0b57aa2  master -> master
$

stylesheet を追加している模様。app.yaml の修正と stylesheets ディレクトリの追加と stylesheets/main.css の追加と index.html の修正。
app.yaml を以下にせい、との事なんですが

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

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /.*
  script: helloworld.py

これは html 上のパスと実体の map をナニしてる、という理解で良いのかな。あと、拡張子で MIME type が云々って記述がある。とりあえず修正を盛り込み。
_git status_によれば以下。

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   app.yaml
#       modified:   index.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       stylesheets/
no changes added to commit (use "git add" and/or "git commit -a")
$

直前の経験によれば_git add -A_して_git commit -a_すれば OK なはず。

$ git add -A
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   app.yaml
#       modified:   index.html
#       new file:   stylesheets/main.css
#
$

大丈夫みたいです。

$ git commit -a
$

_git log -p --pretty=short_を見てみたらこの commit で上記修正が盛り込まれている模様。

app-engine に登録、なんですがどうなるのやら。とりあえず、_Application Identifier_を_yamanetoshi-sample_にして app.yaml も以下に。

application: yamanetoshi-sample
version: 1
runtime: python
api_version: 1

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /.*
  script: helloworld.py

で、Term of Service に agree して save したら_Successfully Created an Application_って言われたので tutorial に従って

$ /usr/local/bin/appcfg.py update helloworld/

すれば良いのか。

$ /usr/local/bin/appcfg.py update helloworld/
Application: yamanetoshi-sample; version: 1.
Server: appengine.google.com.
Scanning files on local disk.
Initiating update.
Email: yamanetoshi@gmail.com
Password for yamanetoshi@gmail.com: 
Cloning 1 static file.
Cloning 2 application files.
Deploying new version.
Checking if new version is ready to serve.
Will check again in 1 seconds.
Checking if new version is ready to serve.
Will check again in 2 seconds.
Checking if new version is ready to serve.
Closing update: new version is ready to start serving.
Uploading index definitions.
$

で、確認してみたら確かにありますな。とりあえず

$ git commit -a
$ git push origin master
Enter passphrase for key '/Users/tyamane/.ssh/id_rsa': 
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1.03 KiB, done.
Total 9 (delta 3), reused 0 (delta 0)
To git@github.com:yamanetoshi/GettingStartedPython-GAE-.git
   0b57aa2..fb1aa9a  master -> master
$

して tutorial は終わり。Git もですが GAE はもう少し色々使い方を検討する必要あり。