Example #1
0
 def test_get_paths(self):
     scheme = get_paths()
     default_scheme = _get_default_scheme()
     wanted = _expand_vars(default_scheme, None)
     wanted = sorted(wanted.items())
     scheme = sorted(scheme.items())
     self.assertEqual(scheme, wanted)
 def test_get_paths(self):
     scheme = get_paths()
     default_scheme = _get_default_scheme()
     wanted = _expand_vars(default_scheme, None)
     wanted = sorted(wanted.items())
     scheme = sorted(scheme.items())
     self.assertEqual(scheme, wanted)
    def setUp(self):
        super(InstallDataTestCase, self).setUp()
        scheme = _get_default_scheme()
        old_items = sysconfig._SCHEMES.items(scheme)

        def restore():
            sysconfig._SCHEMES.remove_section(scheme)
            sysconfig._SCHEMES.add_section(scheme)
            for option, value in old_items:
                sysconfig._SCHEMES.set(scheme, option, value)

        self.addCleanup(restore)
    def test_resources(self):
        install_dir = self.mkdtemp()
        scripts_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(
            name='Spamlib', version='0.1',
            data_files={'spamd': '{scripts}/spamd'})

        os.chdir(project_dir)
        self.write_file('spamd', '# Python script')
        sysconfig._SCHEMES.set(_get_default_scheme(), 'scripts', scripts_dir)
        sys.path.insert(0, install_dir)
        packaging.database.disable_cache()
        self.addCleanup(sys.path.remove, install_dir)
        self.addCleanup(packaging.database.enable_cache)

        cmd = install_dist(dist)
        cmd.outputs = ['spamd']
        cmd.install_lib = install_dir
        dist.command_obj['install_dist'] = cmd

        cmd = install_data(dist)
        cmd.install_dir = install_dir
        cmd.ensure_finalized()
        dist.command_obj['install_data'] = cmd
        cmd.run()

        cmd = install_distinfo(dist)
        cmd.ensure_finalized()
        dist.command_obj['install_distinfo'] = cmd
        cmd.run()

        # first a few sanity checks
        self.assertEqual(os.listdir(scripts_dir), ['spamd'])
        self.assertEqual(os.listdir(install_dir), ['Spamlib-0.1.dist-info'])

        # now the real test
        fn = os.path.join(install_dir, 'Spamlib-0.1.dist-info', 'RESOURCES')
        with open(fn, encoding='utf-8') as fp:
            content = fp.read().strip()

        expected = 'spamd,%s' % os.path.join(scripts_dir, 'spamd')
        self.assertEqual(content, expected)

        # just to be sure, we also test that get_file works here, even though
        # packaging.database has its own test file
        with packaging.database.get_file('Spamlib', 'spamd') as fp:
            content = fp.read()

        self.assertEqual('# Python script', content)
Example #5
0
 def get_versions(self):
     versions = defaultdict()
     bin_ = sysconfig._INSTALL_SCHEMES[
         sysconfig._get_default_scheme()]["scripts"]
     for p in self.root.glob("versions/*"):
         if p.parent.name == "envs" or p.name == "envs":
             continue
         bin_dir = Path(bin_.format(base=p.as_posix()))
         version_path = None
         if bin_dir.exists():
             version_path = PathEntry.create(
                 path=bin_dir.absolute().as_posix(),
                 only_python=False,
                 name=p.name,
                 is_root=True,
             )
         version = None
         try:
             version = PythonVersion.parse(p.name)
         except ValueError:
             entry = next(iter(version_path.find_all_python_versions()),
                          None)
             if not entry:
                 if self.ignore_unsupported:
                     continue
                 raise
             else:
                 version = entry.py_version.as_dict()
         except Exception:
             if not self.ignore_unsupported:
                 raise
             logger.warning("Unsupported Python version %r, ignoring...",
                            p.name,
                            exc_info=True)
             continue
         if not version:
             continue
         version_tuple = (
             version.get("major"),
             version.get("minor"),
             version.get("patch"),
             version.get("is_prerelease"),
             version.get("is_devrelease"),
             version.get("is_debug"),
         )
         self.roots[p] = version_path
         versions[version_tuple] = version_path
         self.paths.append(version_path)
     return versions
Example #6
0
def main():
    # Sysconfig checks
    print("Platform: {}".format(sysconfig.get_platform()))
    print("Python version: {}".format(sysconfig.get_python_version()))
    print(
        "Current installation scheme: {}".format(
            sysconfig._get_default_scheme()
        )
    )
    print("Paths")
    pprint(sysconfig.get_paths())
    print("Variables")
    pprint(sysconfig.get_config_vars())

    print("Environment")
    environ_dict = {k: v for k, v in environ.items()}
    pprint(environ_dict)
Example #7
0
 def env_paths(self):
     import sysconfig
     location = self.virtualenv_location if self.virtualenv_location else sys.prefix
     prefix = vistir.compat.Path(location).as_posix()
     scheme = sysconfig._get_default_scheme()
     config = {
         "base": prefix,
         "installed_base": prefix,
         "platbase": prefix,
         "installed_platbase": prefix
     }
     config.update(self._pyversion)
     paths = {
         k: v.format(**config)
         for k, v in sysconfig._INSTALL_SCHEMES[scheme].items()
     }
     if "prefix" not in paths:
         paths["prefix"] = prefix
     return paths
Example #8
0
    def create(cls, path, only_python=True, pythons=None, name=None):
        """Accepts a path to a base python version directory.

        Generates the pyenv version listings for it"""
        path = ensure_path(path)
        path_entries = defaultdict(PathEntry)
        bin_ = sysconfig._INSTALL_SCHEMES[
            sysconfig._get_default_scheme()]["scripts"]
        if path.as_posix().endswith(Path(bin_).name):
            path = path.parent
        bin_dir = ensure_path(bin_.format(base=path.as_posix()))
        if not name:
            name = path.name
        current_entry = PathEntry.create(bin_dir,
                                         is_root=True,
                                         only_python=True,
                                         pythons=pythons,
                                         name=name)
        path_entries[bin_dir.as_posix()] = current_entry
        return cls(name=name, base=bin_dir, paths=path_entries)
Example #9
0
 def update_event(self, inp=-1):
     self.set_output_val(0, sysconfig._get_default_scheme())
    def test_simple_run(self):
        pkg_dir, dist = self.create_dist()
        cmd = install_data(dist)
        cmd.install_dir = inst = os.path.join(pkg_dir, 'inst')
        scheme = _get_default_scheme()

        sysconfig._SCHEMES.set(scheme, 'inst',
                               os.path.join(pkg_dir, 'inst'))
        sysconfig._SCHEMES.set(scheme, 'inst2',
                               os.path.join(pkg_dir, 'inst2'))

        one = os.path.join(pkg_dir, 'one')
        self.write_file(one, 'xxx')
        inst2 = os.path.join(pkg_dir, 'inst2')
        two = os.path.join(pkg_dir, 'two')
        self.write_file(two, 'xxx')

        # FIXME this creates a literal \{inst2\} directory!
        cmd.data_files = {one: '{inst}/one', two: '{inst2}/two'}
        self.assertCountEqual(cmd.get_inputs(), [one, two])

        # let's run the command
        cmd.ensure_finalized()
        cmd.run()

        # let's check the result
        self.assertEqual(len(cmd.get_outputs()), 2)
        rtwo = os.path.split(two)[-1]
        self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
        rone = os.path.split(one)[-1]
        self.assertTrue(os.path.exists(os.path.join(inst, rone)))
        cmd.outfiles = []

        # let's try with warn_dir one
        cmd.warn_dir = True
        cmd.finalized = False
        cmd.ensure_finalized()
        cmd.run()

        # let's check the result
        self.assertEqual(len(cmd.get_outputs()), 2)
        self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
        self.assertTrue(os.path.exists(os.path.join(inst, rone)))
        cmd.outfiles = []

        # now using root and empty dir
        cmd.root = os.path.join(pkg_dir, 'root')
        three = os.path.join(cmd.install_dir, 'three')
        self.write_file(three, 'xx')

        sysconfig._SCHEMES.set(scheme, 'inst3', cmd.install_dir)

        cmd.data_files = {one: '{inst}/one', two: '{inst2}/two',
                          three: '{inst3}/three'}
        cmd.finalized = False
        cmd.ensure_finalized()
        cmd.run()

        # let's check the result
        self.assertEqual(len(cmd.get_outputs()), 3)
        self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
        self.assertTrue(os.path.exists(os.path.join(inst, rone)))
Example #11
0
import tempfile
import unittest
import subprocess
import zipapp
from pathlib import Path

from mesonbuild.mesonlib import windows_proof_rmtree, python_command, is_windows
from mesonbuild.coredata import version as meson_version

# Handle the scheme that Debian patches in the as default
import sysconfig
# This function was renamed and made public in Python 3.10
if hasattr(sysconfig, 'get_default_scheme'):
    scheme = sysconfig.get_default_scheme()
else:
    scheme = sysconfig._get_default_scheme()
if scheme == 'posix_local':
    scheme = 'posix_prefix'


def get_pypath():
    pypath = sysconfig.get_path('purelib', scheme=scheme, vars={'base': ''})
    # Ensure that / is the path separator and not \, then strip /
    return Path(pypath).as_posix().strip('/')


def get_pybindir():
    # 'Scripts' on Windows and 'bin' on other platforms including MSYS
    return sysconfig.get_path('scripts', scheme=scheme, vars={
        'base': ''
    }).strip('\\/')