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 manage.py script and edit your app.yaml. Just put your application-id on the top of the file.

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

    import os
    from google.appengine.ext import db
    
    expr = """
    [type(
        'CodeFile',
        (__import__('google.appengine.ext.db').appengine.ext.db.Expando,),
        {})(
            name=dp+'/'+fn,
            data=__import__('google.appengine.ext.db').appengine.ext.db.Text(
                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)
    
      @classmethod
      def kind(cls):
        return cls.__name__
    
    def restore():
      for cf in CodeFile.all():
        fpath = cf.name.replace("../", "./_restored_files/")
        dirname = os.path.dirname(fpath)
        if not os.path.exists(dirname):
          os.makedirs(dirname)
        fh = open(fpath, "w")
        fh.write(cf.data)
        fh.close()
    
    def scan():
      from google.appengine.ext.deferred import defer
      defer(eval, expr)
    
  • Next, connect production server with "python manage.py rshell command."
    $ python manage.py rshell
    Running on Kay-0.10.0
    Username:matsuo.takashi
    Password:
    Interactive Kay Shell with RemoteDatastore. 
    -----------------WARNING--------------------
    
    Please be careful in this console session.
    
    -----------------WARNING--------------------
    
    
    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: