‘Upgrade’ from Python 2.7 to Python 3.7

Python is a non-issue.  But App Engine took the opportunity to change everything.
This commit is contained in:
Neil Fraser
2020-02-03 14:33:59 -08:00
parent 819ed243c4
commit 85641fd1fb
7 changed files with 132 additions and 81 deletions

23
appengine/.gcloudignore Normal file
View File

@@ -0,0 +1,23 @@
# Do not upload these files.
.*
deploy
*.soy
static/appengine/
static/closure/
static/demos/plane/soy/*.jar
static/demos/plane/xlf/
static/externs/
static/i18n/
static/msg/json/
static/node_modules/
static/package/
static/theme_scripts/
static/typings/
static/build.py
static/gulpfile.js
static/jsconfig.json
static/LICENSE
static/package-lock.json
static/package.json
static/README.md

View File

@@ -1,8 +1,4 @@
application: blockly-demo
version: 20200123
runtime: python27
api_version: 1
threadsafe: no
runtime: python37
handlers:
# Redirect obsolete URLs.
@@ -19,29 +15,15 @@ handlers:
static_files: redirect.html
upload: redirect.html
# Storage API.
- url: /storage
script: storage.py
secure: always
- url: /storage\.js
static_files: storage.js
upload: storage\.js
secure: always
# Blockly files.
- url: /static
static_dir: static
secure: always
# Closure library for uncompressed Blockly.
- url: /closure-library
static_dir: closure-library
secure: always
# Redirect for root directory.
- url: /
script: index_redirect.py
# Storage API.
- url: /storage\.js
static_files: storage.js
upload: storage\.js
secure: always
# Favicon.
@@ -64,24 +46,8 @@ handlers:
upload: robots\.txt
secure: always
skip_files:
# App Engine default patterns.
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
# Custom skip patterns.
- ^static/appengine/.*$
- ^static/demos/plane/soy/.+\.jar$
- ^static/demos/plane/template.soy$
- ^static/demos/plane/xlf/.*$
- ^static/i18n/.*$
- ^static/msg/json/.*$
- ^.+\.soy$
- ^closure-library/.*_test.html$
- ^closure-library/.*_test.js$
- ^closure-library/closure/bin/.*$
- ^closure-library/doc/.*$
- ^closure-library/scripts/.*$
# Dynamic content.
- url: /.*
script: auto
secure: always

16
appengine/deploy Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
# Script to deploy on GAE.
APP=./app.yaml
if ! [ -f $APP ] ; then
echo $APP not found 1>&2
exit 1
fi
PROJECT=blockly-demo
VERSION=37
echo 'Beginning deployment...'
gcloud app deploy --project $PROJECT --version $VERSION --no-promote
echo 'Deployment finished.'

View File

@@ -1,3 +0,0 @@
print("Status: 301")
print("Location: /static/demos/index.html")
print("")

43
appengine/main.py Normal file
View File

@@ -0,0 +1,43 @@
"""
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import cgi
import storage
from google.cloud import ndb
# Datastore model.
class Grid(ndb.Model):
data = ndb.StringProperty(required=True)
# Route to requested handler.
def app(environ, start_response):
if environ["PATH_INFO"] == "/":
return redirect(environ, start_response)
if environ["PATH_INFO"] == "/storage.py":
return storage.app(environ, start_response)
start_response("404 Not Found", [])
return [b"Page not found."]
# Redirect for root directory.
def redirect(environ, start_response):
headers = [
("Location", "static/demos/index.html")
]
start_response("301 Found", headers)
return []

View File

@@ -0,0 +1 @@
google-cloud-ndb

View File

@@ -23,8 +23,7 @@ __author__ = "q.neutron@gmail.com (Quynh Neutron)"
import cgi
import hashlib
from random import randint
from google.appengine.api import memcache
from google.appengine.ext import ndb
from google.cloud import ndb
def keyGen():
@@ -41,46 +40,52 @@ class Xml(ndb.Model):
def xmlToKey(xml_content):
# Store XML and return a generated key.
xml_hash = long(hashlib.sha1(xml_content).hexdigest(), 16)
xml_hash = int(hashlib.sha1(xml_content.encode("utf-8")).hexdigest(), 16)
xml_hash = int(xml_hash % (2 ** 64) - (2 ** 63))
lookup_query = Xml.query(Xml.xml_hash == xml_hash)
lookup_result = lookup_query.get()
if lookup_result:
xml_key = lookup_result.key.string_id()
else:
trials = 0
result = True
while result:
trials += 1
if trials == 100:
raise Exception("Sorry, the generator failed to get a key for you.")
xml_key = keyGen()
result = Xml.get_by_id(xml_key)
row = Xml(id = xml_key, xml_hash = xml_hash, xml_content = xml_content)
row.put()
client = ndb.Client()
with client.context():
lookup_result = lookup_query.get()
if lookup_result:
xml_key = lookup_result.key.string_id()
else:
trials = 0
result = True
while result:
trials += 1
if trials == 100:
raise Exception("Sorry, the generator failed to get a key for you.")
xml_key = keyGen()
result = Xml.get_by_id(xml_key)
row = Xml(id = xml_key, xml_hash = xml_hash, xml_content = xml_content)
row.put()
return xml_key
def keyToXml(key_provided):
# Retrieve stored XML based on the provided key.
# Normalize the string.
key_provided = key_provided.lower().strip()
# Check memcache for a quick match.
xml = memcache.get("XML_" + key_provided)
if xml is None:
# Check datastore for a definitive match.
# Check datastore for a match.
client = ndb.Client()
with client.context():
result = Xml.get_by_id(key_provided)
if not result:
xml = ""
else:
xml = result.xml_content
# Save to memcache for next hit.
memcache.add("XML_" + key_provided, xml, 3600)
return xml.encode("utf-8")
if not result:
xml = ""
else:
xml = result.xml_content
return xml
if __name__ == "__main__":
print("Content-Type: text/plain\n")
forms = cgi.FieldStorage()
def app(environ, start_response):
forms = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
if "xml" in forms:
print(xmlToKey(forms["xml"].value))
if "key" in forms:
print(keyToXml(forms["key"].value))
out = xmlToKey(forms["xml"].value)
elif "key" in forms:
out = keyToXml(forms["key"].value)
else:
out = ""
headers = [
("Content-Type", "text/plain")
]
start_response("200 OK", headers)
return [out.encode("utf-8")]