def post(self):
    # Only blobstore/upload service/scotty requests should be
    # invoking this handler.
    if not handlers.IsBlobstore():
      logging.critical(
          'POST to uploadpkg not from Blobstore: %s', self.request.headers)
      self.redirect('/admin/packages')

    # TODO(user): do we check is admin?

    if not self.get_uploads('file'):
      logging.error('Upload package does not exist.')
      return

    blob_info = self.get_uploads('file')[0]
    blobstore_key = str(blob_info.key())

    # Obtain a lock on the PackageInfo entity for this package.
    lock = 'pkgsinfo_%s' % blob_info.filename
    if not gae_util.ObtainLock(lock, timeout=5.0):
      gae_util.SafeBlobDel(blobstore_key)
      self.redirect(
          '/admin/uploadpkg?mode=error&msg=PackageInfo is locked')
      return

    p = models.PackageInfo.get_by_key_name(blob_info.filename)
    if not p:
      gae_util.ReleaseLock(lock)
      gae_util.SafeBlobDel(blobstore_key)
      self.redirect(
          '/admin/uploadpkg?mode=error&msg=PackageInfo not found')
      return

    if not p.IsSafeToModify():
      gae_util.ReleaseLock(lock)
      gae_util.SafeBlobDel(blobstore_key)
      self.redirect(
          '/admin/uploadpkg?mode=error&msg=PackageInfo is not modifiable')
      return

    installer_item_size = p.plist['installer_item_size']
    size_difference = int(blob_info.size / 1024) - installer_item_size
    if abs(size_difference) > 1:
      gae_util.ReleaseLock(lock)
      gae_util.SafeBlobDel(blobstore_key)
      msg = 'Blob size (%s) does not match PackageInfo plist size (%s)' % (
          blob_info.size, installer_item_size)
      self.redirect('/admin/uploadpkg?mode=error&msg=%s' % msg)
      return

    old_blobstore_key = None
    if p.blob_info:
      # a previous blob exists.  delete it when the update has succeeded.
      old_blobstore_key = p.blob_info.key()

    p.blob_info = blob_info

    # update the PackageInfo model with the new plist string and blobstore key.
    try:
      p.put()
      error = None
    except db.Error, e:
      logging.exception('error on PackageInfo.put()')
      error = 'pkginfo.put() failed with: %s' % str(e)

    # if it failed, delete the blob that was just uploaded -- it's
    # an orphan.
    if error is not None:
      gae_util.SafeBlobDel(blobstore_key)
      gae_util.ReleaseLock(lock)
      self.redirect('/admin/uploadpkg?mode=error&msg=%s' % error)
      return

    # if an old blob was associated with this Package, delete it.
    # the new blob that was just uploaded has replaced it.
    if old_blobstore_key:
      gae_util.SafeBlobDel(old_blobstore_key)

    gae_util.ReleaseLock(lock)

    user = users.get_current_user().email()
    # Log admin upload to Datastore.
    admin_log = models.AdminPackageLog(
        user=user, action='uploadpkg', filename=blob_info.filename)
    admin_log.put()


    self.redirect(
        '/admin/uploadpkg?mode=success&filename=%s' % blob_info.filename)
POST Handler.
dataset/ETHPy150Open google/simian/src/simian/mac/admin/uploadpkg.py/UploadPackage.post