Skip to content
Myr

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

3 files+46 added−33 removed

modified:flask/app.py+5 added-29 removed
22 unmodified lines
23from werkzeug.exceptions import HTTPException, InternalServerError, \24 MethodNotAllowed, BadRequest2526from .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_package29from .wrappers import Request, Response30from .config import ConfigAttribute, Config31from .ctx import RequestContext32from .globals import _request_ctx_stack, request
528 unmodified lines
561 the package.562563 .. 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)571572 # 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)575576 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')569570 def open_instance_resource(self, resource, mode='rb'):571 """Opens a resource from the application's instance folder572 (:attr:`instance_path`). Otherwise works like
964 unmodified lines
modified:flask/helpers.py+37 added
472 unmodified lines
473 except (KeyError, AttributeError):474 return os.getcwd()475476477def 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)490491 # 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)495496 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_path512513514class 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 access
112 unmodified lines
modified:tests/flask_tests.py+4 added-4 removed
1036 unmodified lines
1037 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 lines
1054 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 lines
1071 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 lines
1088 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