def addpackage(sitedir, pthfile, known_dirs=None): """ Wrapper for site.addpackage Try and work out which directories are added by the .pth and add them to the known_dirs set """ known_dirs = set(known_dirs or []) with open(join(sitedir, pthfile)) as f: for n, line in enumerate(f): if line.startswith("#"): continue line = line.rstrip() if line: if line.startswith(("import ", "import\t")): exec (line, globals(), locals()) continue else: p_rel = join(sitedir, line) p_abs = abspath(line) if isdir(p_rel): os.environ['PATH'] += env_t(os.pathsep + p_rel) sys.path.append(p_rel) added_dirs.add(p_rel) elif isdir(p_abs): os.environ['PATH'] += env_t(os.pathsep + p_abs) sys.path.append(p_abs) added_dirs.add(p_abs) if isfile(pthfile): site.addpackage(sitedir, pthfile, known_dirs) else: logging.debug("pth file '%s' not found")
def test_nested_namespace_import(self): pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' site.addpackage(resources.RESOURCE_PATH, pth, []) pkg_resources._namespace_packages['foogle'] = ['foogle.crank'] pkg_resources._namespace_packages['foogle.crank'] = [] try: self.manager.ast_from_module_name('foogle.crank') finally: del pkg_resources._namespace_packages['foogle'] sys.modules.pop('foogle')
def test_addpackage_import_bad_pth_file(self): # Issue 5258 pth_dir, pth_fn = self.make_pth("abc\x00def\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 1") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegexpMatches(err_out.getvalue(), "Traceback") self.assertRegexpMatches(err_out.getvalue(), "TypeError")
def test_addpackage_import_bad_exec(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 2") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegexpMatches(err_out.getvalue(), "Traceback") self.assertRegexpMatches(err_out.getvalue(), "ImportError")
def test_addpackage_import_bad_exec(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n") with captured_stderr() as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegex(err_out.getvalue(), "line 2") self.assertRegex(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegex(err_out.getvalue(), 'Traceback') self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
def test_addpackage_import_bad_pth_file(self): # Issue 5258 pth_dir, pth_fn = self.make_pth("abc\x00def\n") with captured_stderr() as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegex(err_out.getvalue(), "line 1") self.assertRegex(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: ditto previous XXX comment. self.assertRegex(err_out.getvalue(), 'Traceback') self.assertRegex(err_out.getvalue(), 'ValueError')
def test_namespace_and_file_mismatch(self): filepath = unittest.__file__ ast = self.manager.ast_from_file(filepath) self.assertEqual(ast.name, 'unittest') pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' site.addpackage(resources.RESOURCE_PATH, pth, []) pkg_resources._namespace_packages['foogle'] = [] try: with self.assertRaises(exceptions.AstroidImportError): self.manager.ast_from_module_name('unittest.foogle.fax') finally: del pkg_resources._namespace_packages['foogle'] sys.modules.pop('foogle')
def test_addpackage_import_bad_syntax(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("import bad)syntax\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 1") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: the previous two should be independent checks so that the # order doesn't matter. The next three could be a single check # but my regex foo isn't good enough to write it. self.assertRegexpMatches(err_out.getvalue(), "Traceback") self.assertRegexpMatches(err_out.getvalue(), r"import bad\)syntax") self.assertRegexpMatches(err_out.getvalue(), "SyntaxError")
def test_namespace_and_file_mismatch(self): filepath = unittest.__file__ ast = self.manager.ast_from_file(filepath) self.assertEqual(ast.name, 'unittest') pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' site.addpackage(resources.RESOURCE_PATH, pth, []) pkg_resources._namespace_packages['foogle'] = [] # pylint: disable=no-member try: with self.assertRaises(exceptions.AstroidImportError): self.manager.ast_from_module_name('unittest.foogle.fax') finally: del pkg_resources._namespace_packages['foogle'] # pylint: disable=no-member sys.modules.pop('foogle')
def test_namespace_package_pth_support(self) -> None: pth = "foogle_fax-0.12.5-py2.7-nspkg.pth" site.addpackage(resources.RESOURCE_PATH, pth, []) try: module = self.manager.ast_from_module_name("foogle.fax") submodule = next(module.igetattr("a")) value = next(submodule.igetattr("x")) self.assertIsInstance(value, astroid.Const) with self.assertRaises(AstroidImportError): self.manager.ast_from_module_name("foogle.moogle") finally: sys.modules.pop("foogle")
def test_namespace_and_file_mismatch(self): filepath = unittest.__file__ ast = self.manager.ast_from_file(filepath) self.assertEqual(ast.name, "unittest") pth = "foogle_fax-0.12.5-py2.7-nspkg.pth" site.addpackage(resources.RESOURCE_PATH, pth, []) pkg_resources._namespace_packages["foogle"] = [] try: with self.assertRaises(exceptions.AstroidImportError): self.manager.ast_from_module_name("unittest.foogle.fax") finally: del pkg_resources._namespace_packages["foogle"] sys.modules.pop("foogle")
def test_addpackage(self): # Make sure addpackage() imports if the line starts with 'import', # adds directories to sys.path for any line in the file that is not a # comment or import that is a valid directory name for where the .pth # file resides; invalid directories are not added pth_file = PthFile() pth_file.cleanup(prep=True) # to make sure that nothing is # pre-existing that shouldn't be try: pth_file.create() site.addpackage(pth_file.base_dir, pth_file.filename, set()) self.pth_file_tests(pth_file) finally: pth_file.cleanup()
def test_addpackage_import_bad_syntax(self): # Issue 10642 pth_dir, pth_fn = self.make_pth("import bad)syntax\n") with captured_output("stderr") as err_out: site.addpackage(pth_dir, pth_fn, set()) self.assertRegexpMatches(err_out.getvalue(), "line 1") self.assertRegexpMatches(err_out.getvalue(), re.escape(os.path.join(pth_dir, pth_fn))) # XXX: the previous two should be independent checks so that the # order doesn't matter. The next three could be a single check # but my regex foo isn't good enough to write it. self.assertRegexpMatches(err_out.getvalue(), 'Traceback') self.assertRegexpMatches(err_out.getvalue(), r'import bad\)syntax') self.assertRegexpMatches(err_out.getvalue(), 'SyntaxError')
def test_namespace_package_pth_support(self): pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' site.addpackage(resources.RESOURCE_PATH, pth, []) pkg_resources._namespace_packages['foogle'] = [] # pylint: disable=no-member try: module = self.manager.ast_from_module_name('foogle.fax') submodule = next(module.igetattr('a')) value = next(submodule.igetattr('x')) self.assertIsInstance(value, astroid.Const) with self.assertRaises(exceptions.AstroidImportError): self.manager.ast_from_module_name('foogle.moogle') finally: del pkg_resources._namespace_packages['foogle'] # pylint: disable=no-member sys.modules.pop('foogle')
def test_namespace_package_pth_support(self): pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' site.addpackage(resources.RESOURCE_PATH, pth, []) pkg_resources._namespace_packages['foogle'] = [] try: module = self.manager.ast_from_module_name('foogle.fax') submodule = next(module.igetattr('a')) value = next(submodule.igetattr('x')) self.assertIsInstance(value, astroid.Const) with self.assertRaises(exceptions.AstroidImportError): self.manager.ast_from_module_name('foogle.moogle') finally: del pkg_resources._namespace_packages['foogle'] sys.modules.pop('foogle')
def test_namespace_package_pth_support(self): directory = os.path.join(resources.DATA_DIR, "data") pth = "foogle_fax-0.12.5-py2.7-nspkg.pth" site.addpackage(directory, pth, []) # pylint: disable=no-member; can't infer _namespace_packages, created at runtime. pkg_resources._namespace_packages["foogle"] = [] try: module = self.manager.ast_from_module_name("foogle.fax") submodule = next(module.igetattr("a")) value = next(submodule.igetattr("x")) self.assertIsInstance(value, astroid.Const) with self.assertRaises(exceptions.AstroidImportError): self.manager.ast_from_module_name("foogle.moogle") finally: del pkg_resources._namespace_packages["foogle"] sys.modules.pop("foogle")
def test_namespace_package_pth_support(self): directory = os.path.join(resources.DATA_DIR, 'data') pth = 'foogle_fax-0.12.5-py2.7-nspkg.pth' site.addpackage(directory, pth, []) # pylint: disable=no-member; can't infer _namespace_packages, created at runtime. pkg_resources._namespace_packages['foogle'] = [] try: module = self.manager.ast_from_module_name('foogle.fax') submodule = next(module.igetattr('a')) value = next(submodule.igetattr('x')) self.assertIsInstance(value, astroid.Const) with self.assertRaises(exceptions.AstroidImportError): self.manager.ast_from_module_name('foogle.moogle') finally: del pkg_resources._namespace_packages['foogle'] sys.modules.pop('foogle')
def test_addpackage_import_bad_pth_file(self): # Issue 5258 pth_dir, pth_fn = self.make_pth("abc\x00def\n") with captured_stderr() as err_out: self.assertFalse(site.addpackage(pth_dir, pth_fn, set())) self.assertEqual(err_out.getvalue(), "") for path in sys.path: if isinstance(path, str): self.assertNotIn("abc\x00def", path)
def addpackage(sys_sitedir, pthfile, known_dirs): """ Wrapper for site.addpackage Try and work out which directories are added by the .pth and add them to the known_dirs set :param sys_sitedir: system site-packages directory :param pthfile: path file to add :param known_dirs: set of known directories """ with open(join(sys_sitedir, pthfile)) as f: for n, line in enumerate(f): if line.startswith("#"): continue line = line.rstrip() if line: if line.startswith(("import ", "import\t")): exec(line, globals(), locals()) continue else: p_rel = join(sys_sitedir, line) p_abs = abspath(line) if isdir(p_rel): os.environ['PATH'] += env_t(os.pathsep + p_rel) sys.path.append(p_rel) added_dirs.add(p_rel) elif isdir(p_abs): os.environ['PATH'] += env_t(os.pathsep + p_abs) sys.path.append(p_abs) added_dirs.add(p_abs) if isfile(pthfile): site.addpackage(sys_sitedir, pthfile, known_dirs) else: logging.debug("pth file '%s' not found")
For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ from .utils import rel import os, site # Build paths inside the project like this: os.path.join(BASE_DIR, ...) ROOT_DIR = rel('..') BASE_DIR = rel('') # site.addpackage is actually responsible for *.pth file processing site.addpackage(os.path.join(ROOT_DIR), 'modules.pth', known_paths=set()) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '2(71@@pf!j$a#u$_y$6vmf-=@z$x2z#q-=(+ono)dt!uu!niol' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = bool(os.environ.get('DEBUG', False)) TEMPLATE_DEBUG = DEBUG ALLOWED_HOSTS = [] #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Application definition #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from __future__ import absolute_import, division, print_function, unicode_literals import site import os def rel(*path): """ Converts path relative to the project root into an absolute path :rtype: str """ return os.path.abspath( os.path.join( os.path.dirname(__file__), *path ) ).replace("\\", "/") site.addpackage(rel(), "apps.pth", known_paths=set())
def test_addpackage_empty_lines(self): # Issue 33689 pth_dir, pth_fn = self.make_pth("\n\n \n\n") known_paths = site.addpackage(pth_dir, pth_fn, set()) self.assertEqual(known_paths, set())
def update_event(self, inp=-1): self.set_output_val( 0, site.addpackage(self.input(0), self.input(1), self.input(2)))