def test_provides_extras_deterministic_order(): extras = collections.OrderedDict() extras['a'] = ['foo'] extras['b'] = ['bar'] attrs = dict(extras_require=extras) dist = Distribution(attrs) assert dist.metadata.provides_extras == ['a', 'b'] attrs['extras_require'] = collections.OrderedDict( reversed(list(attrs['extras_require'].items()))) dist = Distribution(attrs) assert dist.metadata.provides_extras == ['b', 'a']
def build() -> None: # if running in RTD, skip compilation if os.environ.get("READTHEDOCS") == "True": return # compile FLI library os.system("cd lib && make && cd ..") extensions = [ Extension( "pyobs_fli.flidriver", ["pyobs_fli/flidriver.pyx"], library_dirs=["lib/"], libraries=["fli", "cfitsio"], include_dirs=[numpy.get_include()], extra_compile_args=["-fPIC"], ) ] ext_modules = cythonize(extensions) distribution = Distribution( { "name": "extended", "ext_modules": ext_modules, "cmdclass": { "build_ext": cython_build_ext, }, } ) distribution.run_command("build_ext") build_ext_cmd = distribution.get_command_obj("build_ext") build_ext_cmd.copy_extensions_to_source()
def test_maintainer_author(name, attrs): tested_keys = { 'author': 'Author', 'author_email': 'Author-email', 'maintainer': 'Maintainer', 'maintainer_email': 'Maintainer-email', } # Generate a PKG-INFO file dist = Distribution(attrs) PKG_INFO = StringIO() dist.metadata.write_pkg_file(PKG_INFO) PKG_INFO.seek(0) pkg_lines = PKG_INFO.readlines() pkg_lines = [_ for _ in pkg_lines if _] # Drop blank lines pkg_lines_set = set(pkg_lines) # Duplicate lines should not be generated assert len(pkg_lines) == len(pkg_lines_set) for fkey, dkey in tested_keys.items(): val = attrs.get(dkey, None) if val is None: for line in pkg_lines: assert not line.startswith(fkey + ':') else: line = '%s: %s' % (fkey, val) assert line in pkg_lines_set
def test_maintainer_author(name, attrs, tmpdir): tested_keys = { 'author': 'Author', 'author_email': 'Author-email', 'maintainer': 'Maintainer', 'maintainer_email': 'Maintainer-email', } # Generate a PKG-INFO file dist = Distribution(attrs) fn = tmpdir.mkdir('pkg_info') fn_s = str(fn) dist.metadata.write_pkg_info(fn_s) with io.open(str(fn.join('PKG-INFO')), 'r', encoding='utf-8') as f: raw_pkg_lines = f.readlines() # Drop blank lines pkg_lines = list(filter(None, raw_pkg_lines)) pkg_lines_set = set(pkg_lines) # Duplicate lines should not be generated assert len(pkg_lines) == len(pkg_lines_set) for fkey, dkey in tested_keys.items(): val = attrs.get(dkey, None) if val is None: for line in pkg_lines: assert not line.startswith(fkey + ':') else: line = '%s: %s' % (fkey, val) assert line in pkg_lines_set
def fetch_build_egg(self, req): """ Specialized version of Distribution.fetch_build_egg that respects respects allow_hosts and index_url. """ from setuptools.command.easy_install import easy_install dist = Distribution({'script_args': ['easy_install']}) dist.parse_config_files() opts = dist.get_option_dict('easy_install') keep = ( 'find_links', 'site_dirs', 'index_url', 'optimize', 'site_dirs', 'allow_hosts' ) for key in list(opts): if key not in keep: del opts[key] # don't use any other settings if self.dependency_links: links = self.dependency_links[:] if 'find_links' in opts: links = opts['find_links'][1].split() + links opts['find_links'] = ('setup', links) if self.allow_hosts: opts['allow_hosts'] = ('test', self.allow_hosts) if self.index_url: opts['index_url'] = ('test', self.index_url) install_dir_func = getattr(self, 'get_egg_cache_dir', _os.getcwd) install_dir = install_dir_func() cmd = easy_install( dist, args=["x"], install_dir=install_dir, exclude_scripts=True, always_copy=False, build_directory=None, editable=False, upgrade=False, multi_version=True, no_report=True, user=False ) cmd.ensure_finalized() return cmd.easy_install(req)
def test_run_invalid_format(run_mock: mock.Mock): """Simulate error return from black on invalid format.""" dist = Distribution() run_mock.side_effect = subprocess.CalledProcessError(returncode=1, cmd="black") with pytest.raises(distutils.errors.DistutilsError): FormatCommand(dist).run()
def build_sample_plugin(): plugin_src_dir = os.path.join(os.path.dirname(__file__), '../../samples/scrapy-splitvariants-plugin') with cd(plugin_src_dir): if os.path.exists('dist'): shutil.rmtree('dist') d = Distribution( dict( version='1.0', name='scrapy_splitvariants_plugin', description='scrapy_splitvariants_plugin', packages=find_packages(exclude=['tests', 'tests.*']), entry_points={ 'scrapydd.spliderplugin': [ 'splitvariants = scrapy_splitvariants_plugin.plugin:Plugin', ], }, install_requires=['scrapy', 'scrapy-splitvariants'], zip_safe=True, )) d.script_name = 'setup.py' d.script_args = ['--quiet', 'clean', 'bdist_egg'] d.parse_command_line() d.run_commands() egg_name = os.listdir('dist')[0] return os.path.abspath(os.path.join('dist', egg_name))
def test_dist_fetch_build_egg(tmpdir): """ Check multiple calls to `Distribution.fetch_build_egg` work as expected. """ index = tmpdir.mkdir('index') index_url = urljoin('file://', pathname2url(str(index))) def sdist_with_index(distname, version): dist_dir = index.mkdir(distname) dist_sdist = '%s-%s.tar.gz' % (distname, version) make_nspkg_sdist(str(dist_dir.join(dist_sdist)), distname, version) with dist_dir.join('index.html').open('w') as fp: fp.write( DALS(''' <!DOCTYPE html><html><body> <a href="{dist_sdist}" rel="internal">{dist_sdist}</a><br/> </body></html> ''').format(dist_sdist=dist_sdist)) sdist_with_index('barbazquux', '3.2.0') sdist_with_index('barbazquux-runner', '2.11.1') with tmpdir.join('setup.cfg').open('w') as fp: fp.write( DALS(''' [easy_install] index_url = {index_url} ''').format(index_url=index_url)) reqs = ''' barbazquux-runner barbazquux '''.split() with tmpdir.as_cwd(): dist = Distribution() resolved_dists = [dist.fetch_build_egg(r) for r in reqs] assert [dist.key for dist in resolved_dists if dist] == reqs
def test_build_ext(): dist = Distribution() build_ext = ParallelBuildExt(dist) build_ext.initialize_options() build_ext.parallel = 1 build_ext.finalize_options()
def test_read_metadata(name, attrs): dist = Distribution(attrs) metadata_out = dist.metadata dist_class = metadata_out.__class__ # Write to PKG_INFO and then load into a new metadata object PKG_INFO = io.StringIO() metadata_out.write_pkg_file(PKG_INFO) PKG_INFO.seek(0) metadata_in = dist_class() metadata_in.read_pkg_file(PKG_INFO) tested_attrs = [ ('name', dist_class.get_name), ('version', dist_class.get_version), ('author', dist_class.get_contact), ('author_email', dist_class.get_contact_email), ('metadata_version', dist_class.get_metadata_version), ('provides', dist_class.get_provides), ('description', dist_class.get_description), ('long_description', dist_class.get_long_description), ('download_url', dist_class.get_download_url), ('keywords', dist_class.get_keywords), ('platforms', dist_class.get_platforms), ('obsoletes', dist_class.get_obsoletes), ('requires', dist_class.get_requires), ('classifiers', dist_class.get_classifiers), ('project_urls', lambda s: getattr(s, 'project_urls', {})), ('provides_extras', lambda s: getattr(s, 'provides_extras', set())), ] for attr, getter in tested_attrs: assert getter(metadata_in) == getter(metadata_out)
def setUp(self): # suppress(N802) """Create a temporary directory and put some files in it.""" super(TestPolysquareLintCommand, self).setUp() os.environ["JOBSTAMPS_DISABLED"] = "1" self._previous_directory = os.getcwd() project_directory = mkdtemp( prefix=os.path.join(os.getcwd(), "test_project_dir")) os.chdir(project_directory) def cleanup_func(): """Change into the previous dir and remove the project dir.""" os.chdir(self._previous_directory) shutil.rmtree(project_directory) self.addCleanup(cleanup_func) self.patch(polysquare_setuptools_lint, "sys_exit", Mock()) with self._open_test_file(): pass with self._open_module_file(): pass with self._open_setup_file() as f: # Write a very basic /setup.py file so that pyroma doesn't trip # and throw an exception. f.write("from setuptools import setup\n" "setup()\n") self._distribution = Distribution( dict(name="my-package", version="0.0.1", packages=fp(exclude=["test"])))
def _build_distribution(extensions: List[Extension]) -> Distribution: # Build a Distribution using cythonize() # Determine the build output directory if DEBUG_MODE: # For subsequent debugging, the C source needs to be in # the same tree as the Cython code (not in a separate build directory). build_dir = None elif ANNOTATION_MODE: build_dir = "build/annotated" else: build_dir = "build/optimized" distribution = Distribution( dict( name="nautilus_trader", ext_modules=cythonize( module_list=extensions, compiler_directives=CYTHON_COMPILER_DIRECTIVES, nthreads=os.cpu_count(), build_dir=build_dir, gdb_debug=DEBUG_MODE, ), zip_safe=False, ) ) distribution.package_dir = "nautilus_trader" return distribution
def self_upgrade(): """Upgrade ourselves with pip.""" # Run pip using the current python executable to accommodate for virtualenvs command = ( [sys.executable] + ["-m", "pip"] + ["install", "MozPhab"] + ["--upgrade"] + ["--no-cache-dir"] + ["--disable-pip-version-check"] ) if config.get_pre_releases: command += ["--pre"] # sys.path[0] is the directory containing the script that was used to # start python. This will be something like: # "<python environment>/bin" or "<python environment>\Scripts" (Windows) script_dir = Path(sys.path[0]) # If moz-phab was installed with --user, we need to pass it to pip # Create "install" setuptools command with --user to find the scripts_path d = Distribution() d.parse_config_files() i = d.get_command_obj("install", create=True) # Forcing the environment detected by Distribution to the --user one i.user = True i.prefix = i.exec_prefix = i.home = i.install_base = i.install_platbase = None i.finalize_options() # Checking if the moz-phab script is installed in user's scripts directory user_dir = Path(i.install_scripts).resolve() if script_dir == user_dir: command.append("--user") if environment.IS_WINDOWS: # Windows does not allow to remove the exe file of the running process. # Renaming the `moz-phab.exe` file to allow pip to install a new version. temp_exe = script_dir / "moz-phab-temp.exe" try: temp_exe.unlink() except FileNotFoundError: pass exe = script_dir / "moz-phab.exe" exe.rename(temp_exe) try: check_call(command) except Exception: temp_exe.rename(exe) raise if not exe.is_file(): # moz-phab.exe is not created - install wasn't needed. temp_exe.rename(exe) else: check_call(command)
def get_setuptools_install_scripts_dir(): dist = Distribution({"cmdclass": {"install": OnlyGetScriptPath}}) dist.dry_run = True # not sure if necessary, but to be safe dist.parse_config_files() command = dist.get_command_obj("install") command.ensure_finalized() command.run() return dist.install_scripts
def get_script_dir(): dist = Distribution({'cmdclass': {'install': OnlyGetScriptPath}}) dist.dry_run = True # not sure if necessary, but to be safe dist.parse_config_files() command = dist.get_command_obj('install') command.ensure_finalized() command.run() return dist.install_scripts
def _prepare_build_ext(kwargs): cmdclass = kwargs.setdefault("cmdclass", {}) get = Distribution({"cmdclass": cmdclass}).get_command_class cmdclass["build_ext"] = type("build_ext_with_extensions", (_build_ext_mixin, get("build_ext")), {}) if _build_ext_mixin._ext_gens: # Don't tag wheels as dist-specific if no extension. kwargs.setdefault("ext_modules", [Extension("", [])])
def _test_process_docstring(lines, obj_in_package): t = build_sphinx.build_sphinx(Distribution()) originals = lines[:] with patch.object(t, "_obj_in_package", return_value=obj_in_package): t._process_docstring("foo", lines) return originals
def create_cmd(self, **kwds): dist = Distribution(kwds) cmd = py2app_cmd(dist) cmd.dist_dir = "dist" cmd.fixup_distribution() cmd.finalize_options() return cmd
def get_python_bin_path(): " Get the directory setuptools installs scripts to for current python " dist = Distribution({'cmdclass': {'install': OnlyGetScriptPath}}) dist.dry_run = True # not sure if necessary dist.parse_config_files() command = dist.get_command_obj('install') command.ensure_finalized() command.run() return dist.install_scripts
def test_process_docstring__accepts_doctests_the_same_package(): t = build_sphinx.build_sphinx(Distribution()) lines = [">>> print('hello')"] expected_lines = lines[:] with patch.object(t, "_obj_in_package", return_value=True): t._process_docstring("foo", lines) assert expected_lines
def get_install_lib(args): # This helper uses the distutils/setuptools machinery to determine # where a command will install files based on the arguments passed # to setup.py dist = Distribution({'script_args': args}) dist.parse_command_line() install_cmd = dist.get_command_obj('install') install_cmd.ensure_finalized() return install_cmd.install_lib
def test_distribution_files_additional_files(): """ Test setup.py and tests discovery. """ dist = Distribution() with change_dir(os.path.join(TEST_PATH, "..")): dist.packages = find_packages() files = FormatCommand(dist).distribution_files() # Ensure data is appended to fake_package assert set(files) == {"setup.py", "setuptools_black", "tests"}
def test_run_eggs(pkg_resources_mock: mock.Mock, run_mock: mock.Mock): """Test .eggs are added to PYTHONPATH when calling black.""" # Mock setup behavior for installed egg pkg_resources_mock.working_set.entries = [ os.path.join(os.getcwd(), ".eggs", "fake_black.egg") ] dist = Distribution() FormatCommand(dist).run() # Ensure .eggs paths are added to PYTHONPATH path = run_mock.call_args[1]["env"]["PYTHONPATH"] assert os.path.join(".eggs", "fake_black.egg") in path
def test_distribution_files_package_dir(): """ Test packages collection when pacakge_dir is used to get packages from sub-directory. """ dist = Distribution() with change_dir(TEST_PATH): dist.packages = find_packages("data") dist.package_dir = {"": "data"} files = FormatCommand(dist).distribution_files() # Ensure data is appended to fake_package assert list(files) == [os.path.join("data", "fake_package")]
def get_setuptools_script_dir(): """ Find where setuptools will have installed the `microservice` entrypoint executable to. :return: Path to the setuptools script directory. """ dist = Distribution({'cmdclass': {'install': OnlyGetScriptPath}}) dist.dry_run = True # not sure if necessary, but to be safe dist.parse_config_files() command = dist.get_command_obj('install') command.ensure_finalized() command.run() return dist.install_scripts
def read_setuptools_cfg(): """ Reads the `setup.cfg` file and extracts the various requirements lists """ # see https://stackoverflow.com/a/30679041/7262247 from setuptools import Distribution dist = Distribution() dist.parse_config_files() return SetupCfg(setup_requires=dist.setup_requires, install_requires=dist.install_requires, tests_requires=dist.tests_require, extras_require=dist.extras_require)
def __init__(self): # TODO: is this really how we get the build paths? build_cmd = Distribution().get_command_obj('build') build_cmd.finalize_options() self.build_platlib = build_cmd.build_platlib self.build_temp = build_cmd.build_temp if platform.system() == "Windows": # Windows uses separate temp build folders for debug and release if build_cmd.debug: self.build_temp = os.path.join(self.build_temp, "Debug") else: self.build_temp = os.path.join(self.build_temp, "Release") build_ext_cmd = Distribution().get_command_obj('build_ext') build_ext_cmd.initialize_options() build_ext_cmd.setup_shlib_compiler() self.build_ext_cmd = build_ext_cmd self.root = "klayout"
def get_setuptools_script_dir(): """Summary. Returns: TYPE: Description """ dist = Distribution({'cmdclass': {'install': OnlyGetScriptPath}}) dist.dry_run = True # not sure if necessary dist.parse_config_files() command = dist.get_command_obj('install') command.ensure_finalized() command.run() return dist.install_scripts
def test_run(format_mock: mock.Mock, build_mock: mock.Mock): """Ensure both format and normal build are called in sequence.""" dist = Distribution() # Simulate setup.py registration dist.cmdclass = { "format": setuptools_black.setuptools_command.FormatCommand, } build_command = BuildCommand(dist) build_command.run() # Format run once, in check-only mode format_mock.return_value.run.assert_called_once_with() assert format_mock.return_value.check is True # Build run once, in check-only mode build_mock.run.assert_called_once_with(build_command)
def get_setuptools_script_dir(): # Run the above class just to get paths dist = Distribution({'cmdclass': {'install': GetPaths}}) dist.dry_run = True dist.parse_config_files() command = dist.get_command_obj('install') command.ensure_finalized() command.run() src_dir = glob(os.path.join(dist.install_libbase, 'pomoxis-*', 'exes'))[0] for exe in (os.path.join(src_dir, x) for x in os.listdir(src_dir)): print("Copying", os.path.basename(exe), '->', dist.install_scripts) shutil.copy(exe, dist.install_scripts) return dist.install_libbase, dist.install_scripts