Refactored package finding
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent fb1a673 · Browse files · 3 files·+46 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+46 added−33 removed
- tests:+4 added−4 removed
- other:+42 added−29 removed
Provenance
- Commit
b3aaf6d- Tree
609f83c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
fb1a673
3 files changed+46 added−33 removed
modified:flask/app.py+5 added-29 removed
22 unmodified lines23 from werkzeug.exceptions import HTTPException, InternalServerError, \24 MethodNotAllowed, BadRequest25 26 from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \27- locked_cached_property, _tojson_filter, _endpoint_from_view_func27+ locked_cached_property, _tojson_filter, _endpoint_from_view_func, \28+ find_package29 from .wrappers import Request, Response30 from .config import ConfigAttribute, Config31 from .ctx import RequestContext32 from .globals import _request_ctx_stack, request528 unmodified lines561 the package.562 563 .. versionadded:: 0.8564 """564- root_mod = sys.modules[self.import_name.split('.')[0]]565- # we're not using root_mod.__file__ here since the module could be566- # virtual. We're trusting the _PackageBoundObject to have calculated567- # the proper name.568- package_path = self.root_path569- if hasattr(root_mod, '__path__'):570- package_path = os.path.dirname(package_path)571-572- # leave the egg wrapper folder or the actual .egg on the filesystem573- if os.path.basename(package_path).endswith('.egg'):574- package_path = os.path.dirname(package_path)575-576- site_parent, site_folder = os.path.split(package_path)577- py_prefix = os.path.abspath(sys.prefix)578- if package_path.startswith(py_prefix):579- base_dir = py_prefix580- elif site_folder == 'site-packages':581- parent, folder = os.path.split(site_parent)582- # Windows like installations583- if folder.lower() == 'lib':584- base_dir = parent585- # UNIX like installations586- elif os.path.basename(parent).lower() == 'lib':587- base_dir = os.path.dirname(parent)588- else:589- base_dir = site_parent590- else:565+ prefix, package_path = find_package(self.import_name)566+ if prefix is None:567 return os.path.join(package_path, 'instance')592- return os.path.join(base_dir, 'share', self.name + '-instance')568+ return os.path.join(prefix, 'var', self.name + '-instance')569 570 def open_instance_resource(self, resource, mode='rb'):571 """Opens a resource from the application's instance folder572 (:attr:`instance_path`). Otherwise works like964 unmodified linesmodified:flask/helpers.py+37 added
472 unmodified lines473 except (KeyError, AttributeError):474 return os.getcwd()475 476 477+def find_package(import_name):478+ """Finds a package and returns the prefix (or None if the package is479+ not installed) as well as the folder that contains the package or480+ module as a tuple.481+ """482+ root_mod = sys.modules[import_name.split('.')[0]]483+ package_path = getattr(root_mod, '__file__', None)484+ if package_path is None:485+ package_path = os.getcwd()486+ else:487+ package_path = os.path.abspath(os.path.dirname(package_path))488+ if hasattr(root_mod, '__path__'):489+ package_path = os.path.dirname(package_path)490+491+ # leave the egg wrapper folder or the actual .egg on the filesystem492+ test_package_path = package_path493+ if os.path.basename(test_package_path).endswith('.egg'):494+ test_package_path = os.path.dirname(test_package_path)495+496+ site_parent, site_folder = os.path.split(test_package_path)497+ py_prefix = os.path.abspath(sys.prefix)498+ if test_package_path.startswith(py_prefix):499+ return py_prefix, package_path500+ elif site_folder.lower() == 'site-packages':501+ parent, folder = os.path.split(site_parent)502+ # Windows like installations503+ if folder.lower() == 'lib':504+ base_dir = parent505+ # UNIX like installations506+ elif os.path.basename(parent).lower() == 'lib':507+ base_dir = os.path.dirname(parent)508+ else:509+ base_dir = site_parent510+ return base_dir, package_path511+ return None, package_path512+513+514 class locked_cached_property(object):515 """A decorator that converts a function into a lazy property. The516 function wrapped is called the first time to retrieve the result517 and then that calculated result is used the next time you access112 unmodified linesmodified:tests/flask_tests.py+4 added-4 removed
1036 unmodified lines1037 sys.modules['myapp'] = mod1038 try:1039 mod.app = flask.Flask(mod.__name__)1040 self.assertEqual(mod.app.instance_path,1041- os.path.join(expected_prefix, 'share',1041+ os.path.join(expected_prefix, 'var',1042 'myapp-instance'))1043 finally:1044 sys.modules['myapp'] = None1045 8 unmodified lines1054 sys.modules['myapp'] = mod1055 try:1056 mod.app = flask.Flask(mod.__name__)1057 self.assertEqual(mod.app.instance_path,1058- os.path.join(expected_prefix, 'share',1058+ os.path.join(expected_prefix, 'var',1059 'myapp-instance'))1060 finally:1061 sys.modules['myapp'] = None1062 8 unmodified lines1071 sys.modules['myapp'] = mod1072 try:1073 mod.app = flask.Flask(mod.__name__)1074 self.assertEqual(mod.app.instance_path,1075- os.path.join(expected_prefix, 'share',1075+ os.path.join(expected_prefix, 'var',1076 'myapp-instance'))1077 finally:1078 sys.modules['myapp'] = None1079 8 unmodified lines1088 sys.modules['myapp'] = mod1089 try:1090 mod.app = flask.Flask(mod.__name__)1091 self.assertEqual(mod.app.instance_path,1092- os.path.join(expected_prefix, 'share',1092+ os.path.join(expected_prefix, 'var',1093 'myapp-instance'))1094 finally:1095 sys.modules['myapp'] = None1096 1103 unmodified lines