2011年3月8日火曜日

GAEで対応クライアントをZIPファイルで配布する

GAEで作ったサーバーからクライアントソフトウェアをダウンロードして展開して実行するだけにしたい。

サーバーでAPIのURLやアプリキー等を書いた設定ファイルと実行ファイルをZIPファイルに纏めてダウンロードさせます。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# download.py 2011.3.7
import os
import logging
import urlparse
import random
import zipfile
import StringIO
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import login_required
from google.appengine.ext.webapp.util import run_wsgi_app

class download(webapp.RequestHandler):
    @login_required
    def get(self, filename):
        output = StringIO.StringIO()
        zip = zipfile.ZipFile(output, 'a')
        user = users.get_current_user()
        o = urlparse.urlparse(self.request.url)
        api_url = "http://" + o.netloc + "/api"
        appkey = "%08x" % random.randint(0x10000000, 0xffffffff)
        template_values = {'appkey': appkey, 'user_id':user.user_id(), 'api_url': api_url}
        path = os.path.join(os.path.dirname(__file__), "client", "client.ini")
        zip.writestr("client.ini", template.render(path, template_values))
        path = os.path.join(os.path.dirname(__file__), "client", "client.py")
        zip.write(path, "client.py")
        zip.close()
        self.response.headers['Content-Type'] = "application/zip"
        self.response.headers['Content-Disposition'] = 'attachment; filename="%s"' % filename
        self.response.out.write(output.getvalue())

application = webapp.WSGIApplication([
    ('/download/(client.zip)', download),
], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()

0 件のコメント: