Ejemplo n.º 1
0
def test_iter_pth_paths(mock_exists):
    # Ensure path checking always returns True for dummy paths.
    mock_exists.return_value = True

    with temporary_dir() as tmpdir:
        in_tmp = lambda f: os.path.join(tmpdir, f)

        PTH_TEST_MAPPING = {
            # A mapping of .pth file content -> expected paths.
            '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python\n':
            [
                '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python'
            ],
            'relative_path\nrelative_path2\n\nrelative_path3': [
                in_tmp('relative_path'),
                in_tmp('relative_path2'),
                in_tmp('relative_path3')
            ],
            'duplicate_path\nduplicate_path': [in_tmp('duplicate_path')],
            'randompath\nimport nosuchmodule\n': [in_tmp('randompath')],
            'import sys\nfoo\n/bar/baz': [in_tmp('foo'), '/bar/baz'],
            'import nosuchmodule\nfoo': [],
            'import nosuchmodule\n': [],
            'import bad)syntax\n': [],
        }

        for i, pth_content in enumerate(PTH_TEST_MAPPING):
            pth_tmp_path = os.path.abspath(
                os.path.join(tmpdir, 'test%s.pth' % i))
            with open(pth_tmp_path, 'wb') as f:
                f.write(to_bytes(pth_content))
            assert sorted(PTH_TEST_MAPPING[pth_content]) == sorted(
                list(iter_pth_paths(pth_tmp_path)))
Ejemplo n.º 2
0
def test_iter_pth_paths(mock_exists):
    # type: (Any) -> None
    # Ensure path checking always returns True for dummy paths.
    mock_exists.return_value = True

    with temporary_dir() as tmpdir:
        in_tmp = lambda f: os.path.join(tmpdir, f)

        PTH_TEST_MAPPING = {
            # A mapping of .pth file content -> expected paths.
            "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python\n": [
                "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"
            ],
            "relative_path\nrelative_path2\n\nrelative_path3": [
                in_tmp("relative_path"),
                in_tmp("relative_path2"),
                in_tmp("relative_path3"),
            ],
            "duplicate_path\nduplicate_path": [in_tmp("duplicate_path")],
            "randompath\nimport nosuchmodule\n": [in_tmp("randompath")],
            "import sys\nfoo\n/bar/baz": [in_tmp("foo"), "/bar/baz"],
            "import nosuchmodule\nfoo": [],
            "import nosuchmodule\n": [],
            "import bad)syntax\n": [],
        }  # type: Dict[str, List[str]]

        for i, pth_content in enumerate(PTH_TEST_MAPPING):
            pth_tmp_path = os.path.abspath(os.path.join(tmpdir, "test%s.pth" % i))
            with open(pth_tmp_path, "wb") as f:
                f.write(to_bytes(pth_content))
            assert sorted(PTH_TEST_MAPPING[pth_content]) == sorted(
                list(iter_pth_paths(pth_tmp_path))
            )
Ejemplo n.º 3
0
    def _extras_paths(cls):
        # type: () -> Iterator[str]
        standard_lib = sysconfig.get_python_lib(standard_lib=True)

        try:
            makefile = sysconfig.parse_makefile(  # type: ignore[attr-defined]
                sysconfig.get_makefile_filename()
            )
        except (AttributeError, IOError):
            # This is not available by default in PyPy's distutils.sysconfig or it simply is
            # no longer available on the system (IOError ENOENT)
            makefile = {}

        extras_paths = filter(
            None, makefile.get("EXTRASPATH", "").split(":")
        )  # type: Iterable[str]
        for path in extras_paths:
            yield os.path.join(standard_lib, path)

        # Handle .pth injected paths as extras.
        sitedirs = cls._get_site_packages()
        for pth_path in cls._scan_pth_files(sitedirs):
            TRACER.log("Found .pth file: %s" % pth_path, V=3)
            for extras_path in iter_pth_paths(pth_path):
                yield extras_path
Ejemplo n.º 4
0
def test_iter_pth_paths(mock_exists):
  # Ensure path checking always returns True for dummy paths.
  mock_exists.return_value = True

  with temporary_dir() as tmpdir:
    in_tmp = lambda f: os.path.join(tmpdir, f)

    PTH_TEST_MAPPING = {
      # A mapping of .pth file content -> expected paths.
      '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python\n': [
        '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python'
      ],
      'relative_path\nrelative_path2\n\nrelative_path3': [
        in_tmp('relative_path'),
        in_tmp('relative_path2'),
        in_tmp('relative_path3')
      ],
      'duplicate_path\nduplicate_path': [in_tmp('duplicate_path')],
      'randompath\nimport nosuchmodule\n': [in_tmp('randompath')],
      'import nosuchmodule\nfoo': [],
      'import nosuchmodule\n': [],
      'import bad)syntax\n': [],
    }

    for i, pth_content in enumerate(PTH_TEST_MAPPING):
      pth_tmp_path = os.path.abspath(os.path.join(tmpdir, 'test%s.pth' % i))
      with open(pth_tmp_path, 'wb') as f:
        f.write(to_bytes(pth_content))
      assert sorted(PTH_TEST_MAPPING[pth_content]) == sorted(list(iter_pth_paths(pth_tmp_path)))
Ejemplo n.º 5
0
Archivo: pex.py Proyecto: jsirois/pex
  def _extras_paths(cls):
    standard_lib = sysconfig.get_python_lib(standard_lib=True)

    try:
      makefile = sysconfig.parse_makefile(sysconfig.get_makefile_filename())
    except (AttributeError, IOError):
      # This is not available by default in PyPy's distutils.sysconfig or it simply is
      # no longer available on the system (IOError ENOENT)
      makefile = {}

    extras_paths = filter(None, makefile.get('EXTRASPATH', '').split(':'))
    for path in extras_paths:
      yield os.path.join(standard_lib, path)

    # Handle .pth injected paths as extras.
    sitedirs = cls._get_site_packages()
    for pth_path in cls._scan_pth_files(sitedirs):
      TRACER.log('Found .pth file: %s' % pth_path, V=3)
      for extras_path in iter_pth_paths(pth_path):
        yield extras_path