From 30a0ac6a1bc3c709bbad8d398b9cdc2ca3e4ab7b Mon Sep 17 00:00:00 2001 From: Dinis Date: Thu, 16 May 2019 18:51:26 +0100 Subject: [PATCH] Refactor methods from gluon/admin to gluon/fileutils --- gluon/admin.py | 60 +++------------------------------------------ gluon/compileapp.py | 4 +-- gluon/fileutils.py | 51 ++++++++++++++++++++++++++++++++++++++ gluon/main.py | 4 +-- gluon/shell.py | 5 ++++ 5 files changed, 63 insertions(+), 61 deletions(-) diff --git a/gluon/admin.py b/gluon/admin.py index 79c8e7ff..842d853d 100644 --- a/gluon/admin.py +++ b/gluon/admin.py @@ -16,12 +16,11 @@ import os import traceback from shutil import rmtree, copyfileobj import zipfile -import sys from gluon.fileutils import (w2p_pack, create_app, w2p_unpack, - w2p_pack_plugin, w2p_unpack_plugin, - up, fix_newlines, abspath, recursive_unlink, - read_file, write_file, parse_version, missing_app_folders) + w2p_pack_plugin, w2p_unpack_plugin, + up, fix_newlines, abspath, recursive_unlink, + write_file, parse_version) from gluon.restricted import RestrictedError from gluon.settings import global_settings from gluon.cache import CacheOnDisk @@ -29,7 +28,7 @@ from gluon._compat import urlopen, to_native # TODO: move into add_path_first if not global_settings.web2py_runtime_gae: - import site + pass REGEX_DEFINE_TABLE = r"""^\w+\.define_table\(\s*['"](?P\w+)['"]""" @@ -442,54 +441,3 @@ def upgrade(request, url='http://web2py.com'): return True, None except Exception as e: return False, e - - -# TODO: move to fileutils -def add_path_first(path): - sys.path = [path] + [p for p in sys.path if ( - not p == path and not p == (path + '/'))] - if not global_settings.web2py_runtime_gae: - if not path in sys.path: - site.addsitedir(path) - - -# TODO: move to fileutils -def try_mkdir(path): - if not os.path.exists(path): - try: - if os.path.islink(path): - # path is a broken link, try to mkdir the target of the link - # instead of the link itself. - os.mkdir(os.path.realpath(path)) - else: - os.mkdir(path) - except OSError as e: - if e.errno == 17: # "File exists" (race condition). - pass - else: - raise - - -# TODO: move to fileutils -def create_missing_folders(): - if not global_settings.web2py_runtime_gae: - for path in ('applications', 'deposit', 'site-packages', 'logs'): - try_mkdir(abspath(path, gluon=True)) - """ - OLD sys.path dance - paths = (global_settings.gluon_parent, abspath( - 'site-packages', gluon=True), abspath('gluon', gluon=True), '') - """ - for p in (global_settings.gluon_parent, - abspath('site-packages', gluon=True), - ''): - add_path_first(p) - - -# TODO: move to fileutils -def create_missing_app_folders(request): - if not global_settings.web2py_runtime_gae: - if request.folder not in global_settings.app_folders: - for amf in missing_app_folders(request.folder): - try_mkdir(amf) - global_settings.app_folders.add(request.folder) diff --git a/gluon/compileapp.py b/gluon/compileapp.py index 7d190e30..cde7ee3b 100644 --- a/gluon/compileapp.py +++ b/gluon/compileapp.py @@ -40,12 +40,10 @@ from gluon.validators import Validator from gluon.settings import global_settings from pydal.base import BaseAdapter from gluon.custom_import import custom_import_install -from gluon.fileutils import mktree, listdir, read_file, write_file, abspath +from gluon.fileutils import mktree, listdir, read_file, write_file, abspath, add_path_first from gluon.template import parse_template from gluon.cfs import getcfs from gluon.restricted import restricted, compile2 -from gluon.admin import add_path_first - CACHED_REGEXES = {} CACHED_REGEXES_MAX_SIZE = 1000 diff --git a/gluon/fileutils.py b/gluon/fileutils.py index 3c805ca4..f6af6fea 100644 --- a/gluon/fileutils.py +++ b/gluon/fileutils.py @@ -11,6 +11,7 @@ File operations from gluon import storage import os +import sys import re import tarfile import glob @@ -47,6 +48,9 @@ __all__ = ( 'w2p_pack_plugin', 'w2p_unpack_plugin', 'fix_newlines', + 'create_missing_folders', + 'create_missing_app_folders', + 'add_path_first', ) @@ -441,3 +445,50 @@ def abspath(*relpath, **kwargs): if kwargs.get('gluon', False): return os.path.join(global_settings.gluon_parent, path) return os.path.join(global_settings.applications_parent, path) + + +def try_mkdir(path): + if not os.path.exists(path): + try: + if os.path.islink(path): + # path is a broken link, try to mkdir the target of the link + # instead of the link itself. + os.mkdir(os.path.realpath(path)) + else: + os.mkdir(path) + except OSError as e: + if e.errno == 17: # "File exists" (race condition). + pass + else: + raise + + +def create_missing_folders(): + if not global_settings.web2py_runtime_gae: + for path in ('applications', 'deposit', 'site-packages', 'logs'): + try_mkdir(abspath(path, gluon=True)) + """ + OLD sys.path dance + paths = (global_settings.gluon_parent, abspath( + 'site-packages', gluon=True), abspath('gluon', gluon=True), '') + """ + for p in (global_settings.gluon_parent, + abspath('site-packages', gluon=True), + ''): + add_path_first(p) + + +def create_missing_app_folders(request): + if not global_settings.web2py_runtime_gae: + if request.folder not in global_settings.app_folders: + for amf in missing_app_folders(request.folder): + try_mkdir(amf) + global_settings.app_folders.add(request.folder) + + +def add_path_first(path): + sys.path = [path] + [p for p in sys.path if ( + not p == path and not p == (path + '/'))] + if not global_settings.web2py_runtime_gae: + if not path in sys.path: + site.addsitedir(path) diff --git a/gluon/main.py b/gluon/main.py index 08d04860..131d5ec5 100644 --- a/gluon/main.py +++ b/gluon/main.py @@ -27,10 +27,10 @@ import string from gluon._compat import Cookie, urllib_quote # from thread import allocate_lock -from gluon.fileutils import abspath, read_file, write_file +from gluon.fileutils import abspath, read_file, write_file, create_missing_folders, create_missing_app_folders, \ + add_path_first from gluon.settings import global_settings from gluon.utils import web2py_uuid, unlocalised_http_header_date -from gluon.admin import add_path_first, create_missing_folders, create_missing_app_folders from gluon.globals import current # Remarks: diff --git a/gluon/shell.py b/gluon/shell.py index fc64886d..a69a1bf1 100644 --- a/gluon/shell.py +++ b/gluon/shell.py @@ -28,6 +28,7 @@ from gluon.restricted import RestrictedError from gluon.globals import Request, Response, Session from gluon.storage import Storage, List from gluon.admin import w2p_unpack +from gluon.fileutils import create_missing_app_folders from pydal.base import BaseAdapter from gluon._compat import iteritems, ClassType, PY2 @@ -166,6 +167,9 @@ def env( path_info = '%s?%s' % (path_info, '&'.join(vars)) request.env.path_info = path_info + # Ensure necessary folders are created + create_missing_app_folders(request) + # Monkey patch so credentials checks pass. def check_credentials(request, other_application='admin'): @@ -257,6 +261,7 @@ def run( # underscore necessary because request.vars is a property extra_request['_vars'] = vars _env = env(a, c=c, f=f, import_models=import_models, extra_request=extra_request) + if c: pyfile = os.path.join('applications', a, 'controllers', c + '.py') pycfile = os.path.join('applications', a, 'compiled',