git と GAE の練習

入門 Git を片手に GAE の tutorial をナニ。
とりあえず MacBook 方面に git-core を導入。

$ sudo port install git-core

バージョンは 1.6.4.2 との事。色々依存してるパケジがあるんだなぁ、と思いつつ github にログイン。で、少々時間がかかったんですが、導入が完了した模様なので、初期設定を入門 Git に従って以下。

$ git config --global user.name "Toshiaki Yamane"
$ git config --global user.email "yamanetoshi@gmail.com"
$ git config --global color.ui auto
$ cat $HOME/.gitconfig
[user]
        name = Toshiaki.Yamane
        email = yamanetoshi@gmail.com
[color]
        ui = auto

で、GAE の Getting Started: Python に沿って進めます。Chapter 4 です。GAE 方面は端折って Hello, World! から。
ディレクトリを掘って

$ mkdir helloworld

情報投入。まず helloworld.py からで以下をナニ。

print 'Content-Type: text/plain'
print ''
print 'Hello, world!'

もう一つ。app.yaml が以下。

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

handlers:
- url: /.*
  script: helloworld.py

これで helloworld ディレクトリ配下が以下な状態。

$ cd helloworld
$ ls
app.yaml         helloworld.py
$

この状態で git init するのか。

$ git init
Initialized empty Git repository in /Users/tyamane/debian/gae/helloworld/.git/
$

あと、commit する前に確認せよ、との記述あり。

$ git var GIT_COMMITTER_IDENT
Toshiaki.Yamane <yamanetoshi@gmail.com> 1256992138 +0900
$ git var GIT_AUTHOR_IDENT
Toshiaki.Yamane <yamanetoshi@gmail.com> 1256992150 +0900
$

一応正しい模様。それでは一発目の commit を。最初の状態を全部リポジトリに登録するには _git add ._ で「現在のディレクトリ以下すべてのファイルの状態が記録したい」 とリクスト、との事。

$ git add .
$ git commit -m "Getting Started: Python"
[master (root-commit) df4a74a] Getting Started: Python
 2 files changed, 11 insertions(+), 0 deletions(-)
 create mode 100644 app.yaml
 create mode 100644 helloworld.py
$

あら? これだと github 使ってる意味無いじゃん。って思ったらまだ大丈夫な模様。github にてプロジェクトを作成して以下?

$ git remote add origin git@github.com:yamanetoshi/GettingStartedPython-GAE-.git
$ git push origin master

やってたんですが、push で失敗。key 登録してませんでした。しかも key 作ってさえいない事が判明。作ります。

$ ssh-keygen -t rsa -C "yamanetoshi@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/tyamane/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/tyamane/.ssh/id_rsa.
Your public key has been saved in /Users/tyamane/.ssh/id_rsa.pub.
The key fingerprint is:
89:a6:ed:c7:6c:cd:bd:fc:b3:92:4a:3e:5d:71:e2:27 yamanetoshi@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|       . .    o .|
|      o S    . + |
|     +        E .|
|    . .o o.o o o |
|     .  =o+.= .  |
|      .o  ooo+oo |
+-----------------+
$

で、公開カギを github 方面に登録するのか。で、リトライ。

$ git push origin master
Enter passphrase for key '/Users/tyamane/.ssh/id_rsa': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 412 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@github.com:yamanetoshi/GettingStartedPython-GAE-.git
 * [new branch]      master -> master
$

ヨシ。てーか、github って公開鍵が登録されてりゃ push できるのか。# イマサラ

最初の変更

とりあえず動作確認してから。そういえば、わしの MacBook ってたまに (?) /usr/local/bin が $PATH に入ってないんですがどーゆー事なのか。とりあえず dev_appserver を起動

$ /usr/local/bin/dev_appserver.py ../helloworld
INFO     2009-10-31 13:12:42,816 appengine_rpc.py:157] Server: appengine.google.com
INFO     2009-10-31 13:12:42,858 appcfg.py:348] Checking for updates to the SDK.
INFO     2009-10-31 13:12:43,967 appcfg.py:362] The SDK is up to date.
WARNING  2009-10-31 13:12:43,968 datastore_file_stub.py:443] Could not read datastore data from /var/folders/E8/E88aGtIrH2Oa8W6-udANCk+++TI/-Tmp-/dev_appserver.datastore
WARNING  2009-10-31 13:12:44,057 dev_appserver.py:3370] Could not initialize images API; you are likely missing the Python "PIL" module. ImportError: No module named _imaging
INFO     2009-10-31 13:12:44,115 dev_appserver_main.py:478] Running application helloworld on port 8080: http://localhost:8080

動作確認は OK な模様。当たり前ですが。では次の Using the webapp Framework を盛り込みを。コンテンツに出てるソースをコピペして動作確認。
dev_appserver 動かしっぱで変更が反映されました。このあたり、スクリプトは楽だな。で、テキスト見ると git diff せい、とある。

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Hello, webapp World!')

application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
$

色が付いてたりします。そりゃ良いのですが ls したら以下。

$ ls
app.yaml        helloworld.py   helloworld.py~  index.yaml
$

index.yaml って何だ、と以下。

$ cat index.yaml 
indexes:

# AUTOGENERATED

# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run.  If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying "# AUTOGENERATED").  If you want to manage some indexes
# manually, move them above the marker line.  The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
$

一応練習ってコトで .gitignore に以下を追加。

$ cat >.gitignore
index.yaml
*~
$

ここまでしなくてもテキスト的には変更かけたファイルだけを add してます。ここでもそれに沿ってナニ。

$ git add helloworld.py
$ git diff
$

ここで _git diff HEAD_ したら commit で盛り込まれるナニが出力される旨、テキストに記述されてます。あと、_git add -u_ が紹介されてます。これ、差分が発生しているファイルはインデクスに追加ね、という意味だと思ってて良いのかな。
# とりあえずスルーします
で、commit してみます。

$ git commit

アタマにメセジを追加した方が良いよな、という事でメセジは略しますが出力が以下。

[master dcf411d] Hello, World -> Using the Webapp Framework
 1 files changed, 17 insertions(+), 3 deletions(-)
 rewrite helloworld.py (100%)

で、次の節が_git statusコマンドと.gitignore_となってて、_git status_試したら以下。

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
nothing added to commit but untracked files present (use "git add" to track)
$

あ、_.gitignore_も commit しないと駄目ですか。

$ git add .gitignore
$ git commit

出力が以下。

[master 4277a14] add .gitignore
 1 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore

これで_git status_見てみたらどうなるか。

$ git status
# On branch master
nothing to commit (working directory clean)
$ ls
app.yaml        helloworld.py   helloworld.py~  index.yaml
$

ここまでで一応版数を上げてくナニは一段落とゆーコトで_git commit -a_というコマンドが紹介されてます。これって危険だなぁ、と思ったんですが git は結構楽に巻き戻しができるみたいなので (もちろんリポジトリの操作は十分な注意が必要ですが)、こうしたオペレーションも (理解できてれば) ありなんだろな、と。

とりあえず

進められる限り進めてく方向なんですが基本的に

  • ソースに手を入れる
  • 動作確認
  • _git status_とか_git diff_とか_git diff HEAD_とかで確認
  • OK なら_git commit -a_

という流れで良いのか。この流れで進めてみま。

次 (Using the Users Service)

Using the Users Service から盛り込み。で、試験してみたらログイン画面出ず。
おかしいな、と思いつつこのあたりはスデに一旦試験してた事に気づく。(とほほ
とりあえずステイタス確認してみます。

$ 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 diff
diff --git a/helloworld.py b/helloworld.py
index 704ef3a..f5149c2 100644
--- a/helloworld.py
+++ b/helloworld.py
@@ -1,10 +1,16 @@
+from google.appengine.api import users
 from google.appengine.ext import webapp
 from google.appengine.ext.webapp.util import run_wsgi_app
 
 class MainPage(webapp.RequestHandler):
   def get(self):
-    self.response.headers['Content-Type'] = 'text/plain'
-    self.response.out.write('Hello, webapp World!')
+    user = users.get_current_user()
+
+    if user:
+      self.response.headers['Content-Type'] = 'text/plain'
+      self.response.out.write('Hello, ' + user.nickname())
+    else:
+      self.redirect(users.create_login_url(self.request.uri))
 
 application = webapp.WSGIApplication(
                                      [('/', MainPage)],
$

_git commit -a_すれば helloworld.py が add されるはず。で、commit な出力が以下。

[master 495b2ba] Using the Webapp Framework -> Using the Users Service
 1 files changed, 8 insertions(+), 2 deletions(-)

とりあえず解説あんま読んでないんですが大丈夫かな。あ、ここで github に push してみるか。

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

続きは明日で。一通りさらったら変更履歴を色々確認してみたりとかな予定。