PY3 fixes and added tests for gluon/admin.py

This commit is contained in:
ilvalle
2016-06-24 22:54:56 +02:00
parent 48350664f0
commit 8aecaf4514
19 changed files with 75 additions and 69 deletions
+3 -5
View File
@@ -14,7 +14,6 @@ import os
import sys
import traceback
import zipfile
import urllib
from shutil import rmtree
from gluon.utils import web2py_uuid
from gluon.fileutils import w2p_pack, w2p_unpack, w2p_pack_plugin, w2p_unpack_plugin
@@ -23,7 +22,7 @@ from gluon.fileutils import read_file, write_file, parse_version
from gluon.restricted import RestrictedError
from gluon.settings import global_settings
from gluon.cache import CacheOnDisk
from gluon._compat import urlopen, to_native
if not global_settings.web2py_runtime_gae:
import site
@@ -338,8 +337,7 @@ def check_new_version(myversion, version_url):
"""
try:
from urllib import urlopen
version = urlopen(version_url).read()
version = to_native(urlopen(version_url).read())
pversion = parse_version(version)
pmyversion = parse_version(myversion)
except IOError:
@@ -423,7 +421,7 @@ def upgrade(request, url='http://web2py.com'):
full_url = url + '/examples/static/web2py_%s.zip' % version_type
filename = abspath('web2py_%s_downloaded.zip' % version_type)
try:
write_file(filename, urllib.urlopen(full_url).read(), 'wb')
write_file(filename, urlopen(full_url).read(), 'wb')
except Exception as e:
return False, e
try:
+4 -1
View File
@@ -18,7 +18,7 @@ import fnmatch
import os
import copy
import random
from gluon._compat import builtin, PY2, unicodeT, to_native, to_bytes, iteritems, basestring
from gluon._compat import builtin, PY2, unicodeT, to_native, to_bytes, iteritems, basestring, reduce, xrange, long
from gluon.storage import Storage, List
from gluon.template import parse_template
from gluon.restricted import restricted, compile2
@@ -400,6 +400,9 @@ _base_environment_['PY2'] = PY2
_base_environment_['to_native'] = to_native
_base_environment_['to_bytes'] = to_bytes
_base_environment_['iteritems'] = iteritems
_base_environment_['reduce'] = reduce
_base_environment_['xrange'] = xrange
def build_environment(request, response, session, store_current=True):
"""
+1 -1
View File
@@ -6,7 +6,7 @@
from __future__ import print_function
import re
import urllib
from gluon._compat import maketrans, urllib_quote, unicodeT, _local_html_escape, to_bytes, to_native, _local_html_escape as escape
from gluon._compat import maketrans, urllib_quote, unicodeT, _local_html_escape, to_bytes, to_native, _local_html_escape as escape, xrange
from ast import parse as ast_parse
import ast
+4 -1
View File
@@ -115,7 +115,10 @@ def write_file(filename, value, mode='w'):
"""Writes <value> to filename, making sure to close the file
explicitly on exit.
"""
f = open(filename, mode)
if PY2:
f = open(filename, mode)
else:
f = open(filename, mode, encoding="utf8")
try:
return f.write(value)
finally:
+2 -1
View File
@@ -2826,7 +2826,8 @@ class MARKMIN(XmlComponent):
return to_bytes(html) if not self.kwargs else to_bytes(DIV(XML(html), **self.kwargs).xml())
def __str__(self):
return self.xml()
# In PY3 __str__ cannot return bytes (TypeError: __str__ returned non-string (type bytes))
return to_native(self.xml())
def ASSIGNJS(**kargs):
+3 -23
View File
@@ -11,7 +11,7 @@ import errno
import socket
import logging
import platform
from gluon._compat import iteritems, to_bytes
from gluon._compat import iteritems, to_bytes, StringIO, urllib_unquote
# Define Constants
VERSION = '1.2.6'
@@ -182,13 +182,6 @@ class Connection(object):
# Import System Modules
import socket
try:
from io import StringIO
except ImportError:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
# Import Package Modules
# package imports removed in monolithic build
@@ -1179,19 +1172,6 @@ from wsgiref.headers import Headers
from threading import Thread
from datetime import datetime
try:
from urllib import unquote
except ImportError:
from urllib.parse import unquote
try:
from io import StringIO
except ImportError:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
try:
from ssl import SSLError
except ImportError:
@@ -1436,7 +1416,7 @@ class Worker(Thread):
req[k] = ""
if k == 'path':
req['path'] = r'%2F'.join(
[unquote(x) for x in re_SLASH.split(v)])
[urllib_unquote(x) for x in re_SLASH.split(v)])
self.protocol = req['protocol']
return req
@@ -1471,7 +1451,7 @@ class Worker(Thread):
if '?' in path:
path, query_string = path.split('?', 1)
path = r'%2F'.join([unquote(x) for x in re_SLASH.split(path)])
path = r'%2F'.join([urllib_unquote(x) for x in re_SLASH.split(path)])
req.update(path=path,
query_string=query_string,
+26 -1
View File
@@ -10,8 +10,12 @@ fix_sys_path(__file__)
from gluon.compileapp import compile_application, remove_compiled_application
from gluon.fileutils import w2p_pack, w2p_unpack
import os
from gluon.globals import Request
from gluon.admin import app_compile, app_create, app_cleanup, check_new_version, app_uninstall
from gluon.main import global_settings
import os, shutil
WEB2PY_VERSION_URL = "http://web2py.com/examples/default/version"
class TestPack(unittest.TestCase):
""" Tests the compileapp.py module """
@@ -30,6 +34,27 @@ class TestPack(unittest.TestCase):
w2p_unpack(test_path, unpack_path)
return
def test_admin_compile(self):
#apps = ['welcome', 'admin', 'examples']
request = Request(env={})
request.application = 'a'
request.controller = 'c'
request.function = 'f'
request.folder = 'applications/admin'
apps = ['welcome']
for appname in apps:
appname_path = os.path.join(os.getcwd(), 'applications', appname)
self.assertEqual(app_compile(appname_path, request), None)
# remove any existing test_app
new_app = 'test_app_%s' % (appname)
if(os.path.exists('applications/%s' % (new_app))):
shutil.rmtree('applications/%s' % (new_app))
self.assertEqual(app_create(new_app, request), True)
self.assertEqual(os.path.exists('applications/test_app_%s/controllers/default.py' % (appname)), True)
self.assertEqual(app_cleanup(new_app, request), True)
self.assertEqual(app_uninstall(new_app, request), True)
self.assertNotEqual(check_new_version(global_settings.web2py_version, WEB2PY_VERSION_URL), -1)
return
if __name__ == '__main__':
unittest.main()
+3 -3
View File
@@ -13,7 +13,7 @@ from __future__ import print_function
import datetime
import sys
from gluon._compat import StringIO, thread
from gluon._compat import StringIO, thread, xrange
import time
import threading
import os
@@ -87,7 +87,7 @@ class IO(object):
def __init__(self):
""" """
self.buffer = cStringIO.StringIO()
self.buffer = StringIO()
def write(self, data):
""" """
@@ -968,7 +968,7 @@ def console():
run_system_tests(options)
if options.quiet:
capture = cStringIO.StringIO()
capture = StringIO()
sys.stdout = capture
logger.setLevel(logging.CRITICAL + 1)
else: