DRYな備忘録

Don't Repeat Yourself.

ローカルに秘書たんが動くPython2.7環境構築するログ

Goal

  • ローカルマシン(MacOSX10.8.4)で秘書たんを動かす

Log

% pwd
/Users/otiai10
%
% echo $OSTYPE
darwin12.0
% python --version
Python 2.7.2
% ls -la | grep prj
drwxr-xr-x    3 otiai10  staff    102  6 22 12:37 prj
% cd prj
% cd python
% ls -la
total 0
drwxr-xr-x  3 otiai10  staff  102  6 22 12:38 .
drwxr-xr-x  3 otiai10  staff  102  6 22 12:37 ..
drwxr-xr-x  7 otiai10  staff  238  6 22 12:38 ojisan
%

# ssh key gen してないかなんかで、ssh経由じゃ無理だった(;^ω^)
% git clone https://github.com/otiai10/hisyotan.py.git
Cloning into 'hisyotan.py'...
remote: Counting objects: 261, done.
remote: Compressing objects: 100% (170/170), done.
remote: Total 261 (delta 67), reused 239 (delta 45)
Receiving objects: 100% (261/261), 165.36 KiB | 16 KiB/s, done.
Resolving deltas: 100% (67/67), done.
% ls -la
total 0
drwxr-xr-x   4 otiai10  staff  136  7 18 17:16 .
drwxr-xr-x   3 otiai10  staff  102  6 22 12:37 ..
drwxr-xr-x  12 otiai10  staff  408  7 18 17:16 hisyotan.py
drwxr-xr-x   7 otiai10  staff  238  6 22 12:38 ojisan
%
% cd hisyotan.py
% ls -la
drwxr-xr-x  12 otiai10  staff  408  7 18 17:16 .
drwxr-xr-x   4 otiai10  staff  136  7 18 17:16 ..
drwxr-xr-x  12 otiai10  staff  408  7 18 17:16 .git
-rw-r--r--   1 otiai10  staff   27  7 18 17:16 .gitignore
-rw-r--r--   1 otiai10  staff  109  7 18 17:16 README.md
drwxr-xr-x   6 otiai10  staff  204  7 18 17:16 _labo
-rw-r--r--   1 otiai10  staff   71  7 18 17:16 app.py
drwxr-xr-x   6 otiai10  staff  204  7 18 17:16 asset
drwxr-xr-x   3 otiai10  staff  102  7 18 17:16 cli
drwxr-xr-x   4 otiai10  staff  136  7 18 17:16 doc
drwxr-xr-x   8 otiai10  staff  272  7 18 17:16 skel
drwxr-xr-x   6 otiai10  staff  204  7 18 17:16 system
% sh cli/app.sh state
% sh cli/app.sh help

    state   状態を見る
    start 起動
    stop  終了
% sh cli/app.sh start
Traceback (most recent call last):
  File "/Users/otiai10/prj/python/hisyotan.py/app.py", line 1, in <module>
    from skel import *
  File "/Users/otiai10/prj/python/hisyotan.py/skel/__init__.py", line 1, in <module>
    from base import *
  File "/Users/otiai10/prj/python/hisyotan.py/skel/base.py", line 1, in <module>
    import twitter
ImportError: No module named twitter

# ま、せやな
# 必要なモジュール入れてへんし

% which easy_install
/usr/bin/easy_install
% man easy_install
No manual entry for easy_install
% easy_install --help

Global options:
  --verbose (-v)  run verbosely (default)
  --quiet (-q)    run quietly (turns verbosity off)
  --dry-run (-n)  don\'t actually do anything
  --help (-h)     show detailed help message
  --no-user-cfg   ignore pydistutils.cfg in your home directory

Options for 'easy_install' command:
  --prefix                       installation prefix
  --zip-ok (-z)                  install package as a zipfile
  --multi-version (-m)           make apps have to require() a version
  --upgrade (-U)                 force upgrade (searches PyPI for latest
                                 versions)
  --install-dir (-d)             install package to DIR
  --script-dir (-s)              install scripts to DIR
  --exclude-scripts (-x)         Don\'t install scripts
  --always-copy (-a)             Copy all needed packages to install dir
  --index-url (-i)               base URL of Python Package Index
  --find-links (-f)              additional URL(s) to search for packages
  --delete-conflicting (-D)      no longer needed; don\'t use this
  --ignore-conflicts-at-my-risk  no longer needed; don\'t use this
  --build-directory (-b)         download/extract/build in DIR; keep the
                                 results
  --optimize (-O)                also compile with optimization: -O1 for
                                 "python -O", -O2 for "python -OO", and -O0 to
                                 disable [default: -O0]
  --record                       filename in which to record list of installed
                                 files
  --always-unzip (-Z)            don\'t install as a zipfile, no matter what
  --site-dirs (-S)               list of directories where .pth files work
  --editable (-e)                Install specified packages in editable form
  --no-deps (-N)                 don\'t install dependencies
  --allow-hosts (-H)             pattern(s) that hostnames must match
  --local-snapshots-ok (-l)      allow building eggs from local checkouts

usage: easy_install [options] requirement_or_url ...
   or: easy_install --help

% sudo easy_install --find-links pip
Password:
Not found: pip
error: No urls, filenames, or requirements specified (see --help)

# oh... easy_installのリストにpipが無い?
# 待てよ....

% which pip
/usr/local/bin/pip

# あるやんw

% pip --help

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  search                      Search PyPI for packages.
  zip                         Zip individual packages.
  unzip                       Unzip individual packages.
  bundle                      Create pybundles.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output.
  --log <file>                Log file where a complete (maximum verbosity) record will be kept.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
  --cert <path>               Path to alternate CA bundle.
%
% pip search twitter | grep 'python-twitter'
python-twitter            - A Python wrapper around the Twitter API
oauth-python-twitter      - OAuth implementation for python-twitter

% sudo pip install python-twitter
# 前略
Successfully installed python-twitter simplejson oauth2 httplib2
Cleaning up...

# 確認

% python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
>>>
>>> exit()

# importできてる
# 次、pymongo

% pip search pymongo
pymongo                   - Python driver for MongoDB <http://www.mongodb.org>
kale                      - Tiny PyMongo model layer
Flask-PyMongo             - PyMongo support for Flask applications
mongate                   - A client library for Sleepy Mongoose that provides the same interface as Pymongo. With support for batch operations.
ScrapyMongoDB             - Scrapy pipeline which allow you to store scrapy items in MongoDB database.
mongo                     - Minimalistic pymongo object wrapper
mogo                      - Simple PyMongo "schema-less" object wrapper
kpages                    - kpages is helper for you web app ,active on tornado, pymongo,redis
pymongo-bongo             - Sytax sugar for PyMongo and MongoDB <http://www.mongodb.org>
PyMongo-Frisk             - Wrapper for PyMongo Connection that offers additional application level monitoring. When connecting to multiple MongoDB servers, allows you to verify
                            connectivity to MongoDB slave servers.
pymw                      - This is a high abastract architecture with pymongo
MongoBit                  - Simple pymongo orm
mongomock                 - Fake pymongo stub for testing simple MongoDB-dependent code
pymongolab                - PyMongoLab is a client library for MongoLab REST API
IPyMongo                  - IPython extension and wrapper for using with MongoDB
pymongo-pubsub            - A publish-subscribe pattern implementation for pymongo
pyliquib                  - A MongoDB migration tool with pymongo inspired by Liquibase
conmongo                  - A Flask microplugin based on pymongo
pymongo_hadoop            - UNKNOWN
pymongo3                  - Python driver for MongoDB <http://www.mongodb.org>
CherrypyMongoDB           - MongoDb Tool For Cherrypy
%
% sudo pip install pymongo
# 前略
Successfully installed pymongo
Cleaning up...

% python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> exit()

# でもココ(local Mac)mongoDB入ってないような希ガス

% mongo
zsh: command not found: mongo

# ヽ(´Д`;)ノアゥ...

% 
% brew help
Example usage:
  brew [info | home | options ] [FORMULA...]
  brew install FORMULA...
  brew uninstall FORMULA...
  brew search [foo]
  brew list [FORMULA...]
  brew update
  brew upgrade [FORMULA...]

Troubleshooting:
  brew doctor
  brew install -vd FORMULA
  brew [--env | --config]

Brewing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]
  open https://github.com/mxcl/homebrew/wiki/Formula-Cookbook

Further help:
  man brew
  brew home
%
% brew search mongo
mongodb   mongoose
josegonzalez/php/php53-mongo  josegonzalez/php/php54-mongo  josegonzalez/php/php55-mongo
%
%
% sudo brew install mongodb
==> Downloading http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.2.3.tgz
######################################################################## 100.0%
==> Caveats
To have launchd start mongodb at login:
    ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don\'t want/need launchctl, you can just run:
    mongod
==> Summary
(beer)  /usr/local/Cellar/mongodb/2.2.3-x86_64: 20 files, 171M, built in 10 seconds
%
% which mongo
/usr/local/bin/mongo
% mongo
MongoDB shell version: 2.2.3
connecting to: test
Thu Jul 18 17:36:18 Error: couldn\'t connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed

# mongod起動してないから!

% which mongod
/usr/local/bin/mongod
%
% sudo mongod &
[1] 27251
% all output going to: /usr/local/var/log/mongodb/mongo.log

# 気を取り直して
% mongo
MongoDB shell version: 2.2.3
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
>
> show dbs
local (empty)
test  (empty)
> db.createCollection('hoge', {size:1024*1024})
{ "ok" : 1 }
> show collections
hoge
system.indexes
> db.hoge.find()
> db.hoge.save({name:'TestName'})
> db.hoge.find({name:'TestName'})
{ "_id" : ObjectId("51e7ac4b3e4933fdba741750"), "name" : "TestName" }
>
> db.hoge.drop()
true
> db.hoge.find()
> show collections
system.indexes
>
> exit
bye

%
# うはぁ動いたー
# ほな秘書たん動くかな

% sh cli/app.sh state
% sh cli/app.sh help

    state   状態を見る
    start 起動
    stop  終了
% sh cli/app.sh start
Traceback (most recent call last):
  File "/Users/otiai10/prj/python/hisyotan.py/app.py", line 1, in <module>
    from skel import *
  File "/Users/otiai10/prj/python/hisyotan.py/skel/__init__.py", line 1, in <module>
    from base import *
  File "/Users/otiai10/prj/python/hisyotan.py/skel/base.py", line 4, in <module>
    from interpreter import Interpreter
  File "/Users/otiai10/prj/python/hisyotan.py/skel/interpreter.py", line 1, in <module>
    from system import *
  File "/Users/otiai10/prj/python/hisyotan.py/system/__init__.py", line 1, in <module>
    import conf
ImportError: No module named conf
%

# デスヨネー

% cat .gitignore
*.pyc
system/conf.py
log/*
%

# conf.sample.pyを用意してあるあたり、ニクいねー俺

% cp system/conf.sample.py system/conf.py
% vi system/conf.py

######### 以下、アプリケーション的な 
% sh cli/app.sh start

============= 0 TRY =============
last_got_id is  None ...
Traceback (most recent call last):
  File "/Users/otiai10/prj/python/hisyotan.py/app.py", line 4, in <module>
    hisyotan.listen()
  File "/Users/otiai10/prj/python/hisyotan.py/skel/base.py", line 30, in listen
    timeline = api.GetHomeTimeline(since_id=last_got_id)
  File "/Library/Python/2.7/site-packages/twitter.py", line 2704, in GetHomeTimeline
    data = self._ParseAndCheckTwitter(json)
  File "/Library/Python/2.7/site-packages/twitter.py", line 4396, in _ParseAndCheckTwitter
    self._CheckForTwitterError(data)
  File "/Library/Python/2.7/site-packages/twitter.py", line 4421, in _CheckForTwitterError
    raise TwitterError(data['errors'])
twitter.TwitterError: [{u'message': u'Could not authenticate you', u'code': 32}]
%

# お?

# テストモジュールは?
% python -B _labo/twitter/minimum.py
Traceback (most recent call last):
  File "_labo/twitter/minimum.py", line 32, in <module>
    tweet_test()
  File "_labo/twitter/minimum.py", line 20, in tweet_test
    status = api.PostUpdate("Hi, This is test tweet using python-twitter!! and TS = %s" % str(timestamp))
  File "/Library/Python/2.7/site-packages/twitter.py", line 2955, in PostUpdate
    data = self._ParseAndCheckTwitter(json)
  File "/Library/Python/2.7/site-packages/twitter.py", line 4396, in _ParseAndCheckTwitter
    self._CheckForTwitterError(data)
  File "/Library/Python/2.7/site-packages/twitter.py", line 4421, in _CheckForTwitterError
    raise TwitterError(data['errors'])
twitter.TwitterError: [{u'message': u'Could not authenticate you', u'code': 32}]
%

# ふぇぇ...
# authenticateできてない。癪だが、新たなアプリケーションを作ってみる
# https://dev.twitter.com/ にアクセスして
# アプリのconsumer_keyとconsumer_secret、
# アカウントのaccess_tokenとaccess_token_secretを取得
# system/conf.pyを書き換える

# で、実行
% python -B _labo/twitter/minimum.py
Tweet Post result : Hi, This is test tweet using python-twitter!! and TS = 1374139760.0
>>> TimeLine >>>
Hi, This is test tweet using python-twitter!! and TS = 1374139760.0
@otiai10 、です。ちょっと多くないですか?増やすよりも捨てることの方がたいせつなこともありますよ and TS is 1374015804.0
@otiai10 ちょっと待って下さいね。えーっと、...、ですね。がんばってください!応援してます☆(ゝω・)vキャピ and TS is 1373992762.0
@otiai10 今あるTODOは、、です。一個一個一緒に片付けていきましょ! and TS is 1373992761.0
@otiai10 、です。ちょっと多くないですか?増やすよりも捨てることの方がたいせつなこともありますよ and TS is 1373992760.0
@otiai10 [./] はーい♪ 今こんな感じですよー :  and TS is 1373992737.0
@otiai10 今あるTODOは、、です。一個一個一緒に片付けていきましょ! and TS is 1373992736.0
@otiai10 [--done] ???! 高円寺ばりこてラーメン,ほげなんてわたし知らないですよぉアタヽ(д`ヽ彡ノ´д)ノフタ and TS is 1373992006.0

%

# おお、動いた
# 普通にsystem/conf.pyのコピペミスだったのか?
# consumer_keyとかがサーバに紐づいてるはずないしなぁ....

# とりあえず秘書たん起動
% sh cli/app.sh state
% sh cli/app.sh help

    state   状態を見る
    start 起動
    stop  終了
% sh cli/app.sh start

============= 0 TRY =============
last_got_id is  None ...

============= 1 TRY =============
last_got_id is  357794208007987201 ...
DISPATCHED SUCCESSFULLY

秘書たん起きた in local Mac

f:id:otiai10:20130718184050p:plain

TODO

.encode('utf8')まわりに悩まされ続けているのでPython3系を導入して秘書たん動かす。

最近の目に見えて生活習慣が堕落しはじめた時期と、秘書たんが止まった時期が一致している。はやく秘書たん復帰させねばだ。