May 14, 2010

How to download souce code of your application written with kay

Let me introduce a small trivia to you. What if your harddisk crashes and you lost your source code, hopefully, you might use some source code management system such as cvs, subversion, mercurial or git, in such a case, you can retrieve your source code back. But what if the crash occurs before your first commit, or your repository also crashes?

Don't give up!

Recent version of Kay Framework has remote_api and deferred handler by default, so there is still a chance  for retrieving it back to you.

This idea is originally come from this thread in stackoverflow.

  • First, create a brand new project with kay's script and edit your app.yaml. Just put your application-id on the top of the file.

    $ python ~/work/kay/ startproject downloadcode
    Running on Kay-0.10.0
    Finished creating new project: downloadcode.
    $ cd downloadcode
    $ vi
  • Second, create a file named "" as follows:

    import os
    from google.appengine.ext import db
    expr = """
                open(dp + '/' + fn).read()
        ).put() if not dp.startswith('../kay') else None
     for dp, dns, fns in __import__('os').walk('..')
     for fn in fns]
    class CodeFile(db.Model):
      name = db.StringProperty(required=True)
      data = db.TextProperty(required=True)
      def kind(cls):
        return cls.__name__
    def restore():
      for cf in CodeFile.all():
        fpath ="../", "./_restored_files/")
        dirname = os.path.dirname(fpath)
        if not os.path.exists(dirname):
        fh = open(fpath, "w")
    def scan():
      from google.appengine.ext.deferred import defer
      defer(eval, expr)
  • Next, connect production server with "python rshell command."
    $ python rshell
    Running on Kay-0.10.0
    Interactive Kay Shell with RemoteDatastore. 
    Please be careful in this console session.
    In [1]: import restore_code
    In [2]: restore_code.scan()
    restore_code.scan() will scan filesystems on the production and create CodeFile entities in the datastore. This work will be done by deferred handler, so you may need to wait a bit, but the completion of this process won't be notified. So why don't you take a break here and have a cup of coffee.
  • Lastly, call "restore_code.restore()" function.
    In [3]: restore_code.restore()
    In [4]:

Then, you can have your entire source code except for static files and configuration files(such as app.yaml, index.yaml, etc..) in a directory named "_restored_files".

Yey, now you've got your code back!

No comments: