def test_install_requires_extra(deps, extras, installed): it = InstallTests() try: it.setUp() ins = Installer(samples_dir / 'extras' / 'pyproject.toml', python='mock_python', user=False, deps=deps, extras=extras) cmd = MockCommand('mock_python') get_reqs = ( "#!{python}\n" "import sys\n" "with open({recording_file!r}, 'wb') as w, open(sys.argv[-1], 'rb') as r:\n" " w.write(r.read())" ).format(python=sys.executable, recording_file=cmd.recording_file) cmd.content = get_reqs with cmd as mock_py: ins.install_requirements() with open(mock_py.recording_file) as f: str_deps = f.read() deps = str_deps.split('\n') if str_deps else [] assert set(deps) == installed finally: it.tearDown()
def test_symlink_other_python(self): if os.name == 'nt': raise SkipTest('symlink') (self.tmpdir / 'site-packages2').mkdir() (self.tmpdir / 'scripts2').mkdir() # Called by Installer._auto_user() : script1 = ("#!{python}\n" "import sysconfig\n" "print(True)\n" # site.ENABLE_USER_SITE "print({purelib!r})" # sysconfig.get_path('purelib') ).format(python=sys.executable, purelib=str(self.tmpdir / 'site-packages2')) # Called by Installer._get_dirs() : script2 = ("#!{python}\n" "import json, sys\n" "json.dump({{'purelib': {purelib!r}, 'scripts': {scripts!r} }}, " "sys.stdout)" ).format(python=sys.executable, purelib=str(self.tmpdir / 'site-packages2'), scripts=str(self.tmpdir / 'scripts2')) with MockCommand('mock_python', content=script1): ins = Installer(samples_dir / 'package1' / 'flit.ini', python='mock_python', symlink=True) with MockCommand('mock_python', content=script2): ins.install() assert_islink(self.tmpdir / 'site-packages2' / 'package1', to=samples_dir / 'package1' / 'package1') assert_isfile(self.tmpdir / 'scripts2' / 'pkg_script') with (self.tmpdir / 'scripts2' / 'pkg_script').open() as f: assert f.readline().strip() == "#!mock_python"
def test_pth_package(self): Installer.from_ini_path(samples_dir / 'package1' / 'flit.ini', pth=True).install() assert_isfile(self.tmpdir / 'site-packages' / 'package1.pth') with open(str(self.tmpdir / 'site-packages' / 'package1.pth')) as f: assert f.read() == str(samples_dir / 'package1') assert_isfile(self.tmpdir / 'scripts' / 'pkg_script')
def test_install_package(self): Installer.from_ini_path(samples_dir / 'package1' / 'flit.ini').install_directly() assert_isdir(self.tmpdir / 'site-packages' / 'package1') assert_isdir(self.tmpdir / 'site-packages' / 'package1-0.1.dist-info') assert_isfile(self.tmpdir / 'scripts' / 'pkg_script') with (self.tmpdir / 'scripts' / 'pkg_script').open() as f: assert f.readline().strip() == "#!" + sys.executable
def test_install_module(self): Installer.from_ini_path(samples_dir / 'module1_toml' / 'pyproject.toml').install_directly() assert_isfile(self.tmpdir / 'site-packages' / 'module1.py') assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.1.dist-info') self._assert_direct_url(samples_dir / 'module1_toml', 'module1', '0.1', expected_editable=False)
def test_install_requires(self): ins = Installer(samples_dir / 'requires-requests.toml', user=False, python='mock_python') with MockCommand('mock_python') as mockpy: ins.install_requirements() calls = mockpy.get_calls() assert len(calls) == 1 assert calls[0]['argv'][1:5] == ['-m', 'pip', 'install', '-r']
def test_install_module_pep621(self): Installer.from_ini_path( core_samples_dir / 'pep621_nodynamic' / 'pyproject.toml', ).install_directly() assert_isfile(self.tmpdir / 'site-packages' / 'module1.py') assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.3.dist-info') self._assert_direct_url(core_samples_dir / 'pep621_nodynamic', 'module1', '0.3', expected_editable=False)
def test_symlink_package(self): if os.name == 'nt': raise SkipTest("symlink") Installer.from_ini_path(samples_dir / 'package1' / 'flit.ini', symlink=True).install() assert_islink(self.tmpdir / 'site-packages' / 'package1', to=samples_dir / 'package1' / 'package1') assert_isfile(self.tmpdir / 'scripts' / 'pkg_script') with (self.tmpdir / 'scripts' / 'pkg_script').open() as f: assert f.readline().strip() == "#!" + sys.executable
def test_pth_package(self): Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', pth=True).install() assert_isfile(self.tmpdir / 'site-packages' / 'package1.pth') with open(str(self.tmpdir / 'site-packages' / 'package1.pth')) as f: assert f.read() == str(samples_dir / 'package1') assert_isfile(self.tmpdir / 'scripts' / 'pkg_script') self._assert_direct_url(samples_dir / 'package1', 'package1', '0.1', expected_editable=True)
def test_pip_install(self): ins = Installer(samples_dir / 'package1' / 'flit.ini', python='mock_python', user=False) with MockCommand('mock_python') as mock_py: ins.install() calls = mock_py.get_calls() assert len(calls) == 1 cmd = calls[0]['argv'] assert cmd[1:4] == ['-m', 'pip', 'install'] assert cmd[4].endswith('package1-0.1-py2.py3-none-any.whl')
def test_symlink_module_pep621(self): if os.name == 'nt': raise SkipTest("symlink") Installer.from_ini_path(core_samples_dir / 'pep621_nodynamic' / 'pyproject.toml', symlink=True).install_directly() assert_islink(self.tmpdir / 'site-packages' / 'module1.py', to=core_samples_dir / 'pep621_nodynamic' / 'module1.py') assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.3.dist-info') self._assert_direct_url(core_samples_dir / 'pep621_nodynamic', 'module1', '0.3', expected_editable=True)
def test_symlink_package(self): if os.name == 'nt': raise SkipTest("symlink") Installer(samples_dir / 'package1-pkg.ini', symlink=True).install() assert_islink(self.tmpdir / 'site-packages' / 'package1', to=str(samples_dir / 'package1')) assert_isfile(self.tmpdir / 'scripts' / 'pkg_script')
def test_install_package(self): oldcwd = os.getcwd() os.chdir(str(samples_dir / 'package1')) try: Installer.from_ini_path( pathlib.Path('pyproject.toml')).install_directly() finally: os.chdir(oldcwd) assert_isdir(self.tmpdir / 'site-packages' / 'package1') assert_isdir(self.tmpdir / 'site-packages' / 'package1-0.1.dist-info') assert_isfile(self.tmpdir / 'scripts' / 'pkg_script') with (self.tmpdir / 'scripts' / 'pkg_script').open() as f: assert f.readline().strip() == "#!" + sys.executable self._assert_direct_url(samples_dir / 'package1', 'package1', '0.1', expected_editable=False)
def test_install_reqs_my_python_if_needed_pep621(self): ins = Installer.from_ini_path( core_samples_dir / 'pep621_nodynamic' / 'pyproject.toml', deps='none', ) # This shouldn't try to get version & docstring from the module ins.install_reqs_my_python_if_needed()
def handle(self) -> int: from flit.install import Installer # type: ignore[import] from nr.util.fs import atomic_swap from slam.util.pygments import toml_highlight if not venv_check(self, 'refusing to link'): return 1 # logging.basicConfig(level=logging.INFO, format='%(message)s') num_projects = 0 num_skipped = 0 for project in self.app.repository.projects(): if not project.is_python_project: continue packages = project.packages() if not packages: continue num_projects += 1 if len(packages) > 1: self.line_error( 'warning: multiple packages can not currently be installed with <opt>slam link</opt>' ) num_skipped += 1 continue config = project.pyproject_toml.value() dist_name = project.dist_name() or project.directory.resolve().name if not self._setup_flit_config(packages[0].name, dist_name, config): return 1 if self.option('dump-pyproject'): self.line( f'<fg=dark_gray># {project.pyproject_toml.path}</fg>') self.line(toml_highlight(config)) continue with atomic_swap(project.pyproject_toml.path, 'w', always_revert=True) as fp: fp.close() project.pyproject_toml.value(config) project.pyproject_toml.save() installer = Installer.from_ini_path( project.pyproject_toml.path, python=shutil.which(self.option("python")), symlink=True) self.line(f'symlinking <info>{dist_name}</info>') installer.install() return 1 if num_skipped > 0 and num_projects == 1 else 0
def test_install_requires_with_extra_index_url(self): ins = Installer.from_ini_path(samples_dir / 'requires-requests.toml', user=False, python='mock_python', extra_index_url='https://fake.index') with MockCommand('mock_python') as mockpy: ins.install_requirements() calls = mockpy.get_calls() assert len(calls) == 1 assert calls[0]['argv'][1:7] == [ '-m', 'pip', 'install', '--extra-index-url', 'https://fake.index', '-r' ]
def test_pip_install(self): ins = Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', python='mock_python', user=False) with MockCommand('mock_python') as mock_py: ins.install() calls = mock_py.get_calls() assert len(calls) == 1 cmd = calls[0]['argv'] assert cmd[1:4] == ['-m', 'pip', 'install'] assert cmd[4].endswith('package1')
def install_plotchecker(symlink): from pathlib import Path from flit.install import Installer from flit.log import enable_colourful_output import mock # Hack to make docs build on RTD MOCK_MODULES = ['numpy', 'matplotlib', 'matplotlib.pyplot', 'matplotlib.colors', 'matplotlib.markers'] for mod_name in MOCK_MODULES: sys.modules[mod_name] = mock.Mock() enable_colourful_output() p = Path('flit.ini') Installer(p, symlink=symlink, deps='none').install()
def test_pip_install_with_extra_index_url(self): ins = Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', python='mock_python', user=False, extra_index_url='https://fake.index') with MockCommand('mock_python') as mock_py: ins.install() calls = mock_py.get_calls() assert len(calls) == 1 cmd = calls[0]['argv'] assert cmd[1:4] == ['-m', 'pip', 'install'] assert cmd[4].endswith('package1') assert cmd[5:7] == ['--extra-index-url', 'https://fake.index'] assert len(cmd) == 7
def test_symlink_package(self): Installer(samples_dir / 'package1-pkg.ini', symlink=True).install() assert_islink(self.tmpdir / 'site-packages' / 'package1', to=str(samples_dir / 'package1')) assert_isfile(self.tmpdir / 'scripts' / 'pkg_script')
def test_dist_name(self): Installer.from_ini_path(samples_dir / 'altdistname' / 'pyproject.toml').install_directly() assert_isdir(self.tmpdir / 'site-packages' / 'package1') assert_isdir(self.tmpdir / 'site-packages' / 'package_dist1-0.1.dist-info')
def test_entry_points(self): Installer.from_ini_path(samples_dir / 'entrypoints_valid' / 'pyproject.toml').install_directly() assert_isfile(self.tmpdir / 'site-packages' / 'package1-0.1.dist-info' / 'entry_points.txt')
def test_entry_points(self): Installer(samples_dir / 'entrypoints_valid' / 'flit.ini').install_directly() assert_isfile(self.tmpdir / 'site-packages' / 'package1-0.1.dist-info' / 'entry_points.txt')
def test_dist_name(self): Installer(samples_dir / 'altdistname' / 'flit.ini').install_directly() assert_isdir(self.tmpdir / 'site-packages' / 'package1') assert_isdir(self.tmpdir / 'site-packages' / 'package_dist1-0.1.dist-info')
def test_install_module(self): Installer(samples_dir / 'module1' / 'flit.ini').install_directly() assert_isfile(self.tmpdir / 'site-packages' / 'module1.py') assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.1.dist-info')
def test_extras_error(self): with pytest.raises(DependencyError): Installer(samples_dir / 'requires-requests.toml', user=False, deps='none', extras='dev')
"""Use this script to get set up for working on flit itself. python bootstrap_dev.py This symlinks flit into site-packages, and installs the flit command. """ from pathlib import Path from flit.install import Installer from flit.log import enable_colourful_output enable_colourful_output() p = Path('flit.ini') Installer(p, symlink=True).install()
from flit_core.inifile import LoadedConfig from flit.install import Installer ap = argparse.ArgumentParser() ap.add_argument('--user') args = ap.parse_args() logging.basicConfig(level=logging.INFO) # Construct config for flit_core core_config = LoadedConfig() core_config.module = 'flit_core' core_config.metadata = build_thyself.metadata_dict core_config.reqs_by_extra['.none'] = build_thyself.metadata.requires_dist install_kwargs = {'symlink': True} if os.name == 'nt': # Use .pth files instead of symlinking on Windows install_kwargs = {'symlink': False, 'pth': True} # Install flit_core Installer(my_dir / 'flit_core', core_config, user=args.user, **install_kwargs).install() print("Linked flit_core into site-packages.") # Install flit Installer.from_ini_path(my_dir / 'pyproject.toml', user=args.user, **install_kwargs).install() print("Linked flit into site-packages.")
def test_install_package(self): Installer(samples_dir / 'package1-pkg.ini').install_directly() assert_isdir(self.tmpdir / 'site-packages' / 'package1') assert_isdir(self.tmpdir / 'site-packages' / 'package1-0.1.dist-info') assert_isfile(self.tmpdir / 'scripts' / 'pkg_script')