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 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 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 get_setuptools_script_dir(): " 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 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 _convert_metadata(zf, destination_eggdir, dist_info, egg_info): def get_metadata(name): with zf.open(posixpath.join(dist_info, name)) as fp: value = fp.read().decode('utf-8') if PY3 else fp.read() return email.parser.Parser().parsestr(value) wheel_metadata = get_metadata('WHEEL') # Check wheel format version is supported. wheel_version = parse_version(wheel_metadata.get('Wheel-Version')) wheel_v1 = ( parse_version('1.0') <= wheel_version < parse_version('2.0dev0') ) if not wheel_v1: raise ValueError( 'unsupported wheel format version: %s' % wheel_version) # Extract to target directory. os.mkdir(destination_eggdir) zf.extractall(destination_eggdir) # Convert metadata. dist_info = os.path.join(destination_eggdir, dist_info) dist = Distribution.from_location( destination_eggdir, dist_info, metadata=PathMetadata(destination_eggdir, dist_info), ) # Note: Evaluate and strip markers now, # as it's difficult to convert back from the syntax: # foobar; "linux" in sys_platform and extra == 'test' def raw_req(req): req.marker = None return str(req) install_requires = list(sorted(map(raw_req, dist.requires()))) extras_require = { extra: sorted( req for req in map(raw_req, dist.requires((extra,))) if req not in install_requires ) for extra in dist.extras } os.rename(dist_info, egg_info) os.rename( os.path.join(egg_info, 'METADATA'), os.path.join(egg_info, 'PKG-INFO'), ) setup_dist = SetuptoolsDistribution( attrs=dict( install_requires=install_requires, extras_require=extras_require, ), ) write_requirements( setup_dist.get_command_obj('egg_info'), None, os.path.join(egg_info, 'requires.txt'), )
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 _convert_metadata(zf, destination_eggdir, dist_info, egg_info): def get_metadata(name): with zf.open(posixpath.join(dist_info, name)) as fp: value = fp.read().decode('utf-8') if PY3 else fp.read() return email.parser.Parser().parsestr(value) wheel_metadata = get_metadata('WHEEL') # Check wheel format version is supported. wheel_version = parse_version(wheel_metadata.get('Wheel-Version')) wheel_v1 = (parse_version('1.0') <= wheel_version < parse_version('2.0dev0')) if not wheel_v1: raise ValueError('unsupported wheel format version: %s' % wheel_version) # Extract to target directory. os.mkdir(destination_eggdir) zf.extractall(destination_eggdir) # Convert metadata. dist_info = os.path.join(destination_eggdir, dist_info) dist = Distribution.from_location( destination_eggdir, dist_info, metadata=PathMetadata(destination_eggdir, dist_info), ) # Note: Evaluate and strip markers now, # as it's difficult to convert back from the syntax: # foobar; "linux" in sys_platform and extra == 'test' def raw_req(req): req.marker = None return str(req) install_requires = list(sorted(map(raw_req, dist.requires()))) extras_require = { extra: sorted(req for req in map(raw_req, dist.requires((extra, ))) if req not in install_requires) for extra in dist.extras } os.rename(dist_info, egg_info) os.rename( os.path.join(egg_info, 'METADATA'), os.path.join(egg_info, 'PKG-INFO'), ) setup_dist = SetuptoolsDistribution(attrs=dict( install_requires=install_requires, extras_require=extras_require, ), ) write_requirements( setup_dist.get_command_obj('egg_info'), None, os.path.join(egg_info, 'requires.txt'), )
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 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
def executable(request) -> pathlib.PosixPath: """Let setuptools compute the path to the built executable""" with chdir(request.config.rootdir) as root_dir: command = "build" distribution = Distribution({ "script_name": __file__, "script_args": [command], }) distribution.parse_config_files() distribution.parse_command_line() command = distribution.get_command_obj(command) command.ensure_finalized() return (pathlib.PosixPath(root_dir).absolute() / command.build_platlib / "hades-dhcp-script")
def getprefix(self): '''Retrieve setup tool calculated prefix :returns: prefix :rtype: string ''' dist = Distribution({'cmdclass': {'install': OnlyGetScriptPath}}) dist.dry_run = True # not sure if necessary, but to be safe dist.parse_config_files() try: dist.parse_command_line() except (distutils.errors.DistutilsArgError, AttributeError): pass command = dist.get_command_obj('install') command.ensure_finalized() command.run() prefix = dist.install_scripts.replace('/bin', '') return prefix
def extra_link_args(): """ Add arguments for Data Execution Prevention and ASLR. This is only relevant for older Python versions. :return: `list` of arguments """ dist = Distribution() dist.parse_config_files() try: dist.parse_command_line() except (TypeError, DistutilsArgError): # Happens with setup.py --help pass build = dist.get_command_obj('build') build.ensure_finalized() if new_compiler(compiler=build.compiler).compiler_type == 'msvc': return ["/NXCOMPAT", "/DYNAMICBASE"] return []
def run(self): """ Print desired path (according to subclass) to stdout. Unfortunately, setuptools automatically prints "running scriptdir" on stdout. So, for example, the output will look like this (for example): running scriptdir /usr/local/bin The shell command `tail` can be used to get only the relevant line: `python setup.py scriptdir | tail --lines=1` """ # pylint: disable=no-self-use # Create fake install to get a setuptools script directory path. dist = Distribution({"cmdclass": {"install": _ScriptDirSpy}}) dist.dry_run = True dist.parse_config_files() command = dist.get_command_obj("install") command.ensure_finalized() command.run() print(self.get_dir_from_distribution(dist))
def build() -> None: # if running in RTD, skip compilation if os.environ.get("READTHEDOCS") == "True": return extensions = [ Extension( "pyobs_sbig.sbigudrv", [ "pyobs_sbig/sbigudrv.pyx", "src/csbigcam.cpp", "src/csbigimg.cpp" ], libraries=["sbigudrv", "cfitsio"], include_dirs=[numpy.get_include(), "/usr/include/cfitsio"], extra_compile_args=["-fPIC"], ) ] ext_modules = cythonize( extensions, compiler_directives={ "binding": True, "language_level": "3" }, ) distribution = Distribution({ "name": "extended", "ext_modules": ext_modules, "cmdclass": { "build_ext": cython_build_ext, }, }) # distribution.package_dir = "extended" distribution.run_command("build_ext") build_ext_cmd = distribution.get_command_obj("build_ext") build_ext_cmd.copy_extensions_to_source()
def install_as_egg(self, destination_eggdir): '''Install wheel as an egg directory.''' with zipfile.ZipFile(self.filename) as zf: dist_basename = '%s-%s' % (self.project_name, self.version) dist_info = '%s.dist-info' % dist_basename dist_data = '%s.data' % dist_basename def get_metadata(name): with zf.open('%s/%s' % (dist_info, name)) as fp: value = fp.read().decode('utf-8') if PY3 else fp.read() return email.parser.Parser().parsestr(value) wheel_metadata = get_metadata('WHEEL') dist_metadata = get_metadata('METADATA') # Check wheel format version is supported. wheel_version = parse_version(wheel_metadata.get('Wheel-Version')) if not parse_version('1.0') <= wheel_version < parse_version( '2.0dev0'): raise ValueError('unsupported wheel format version: %s' % wheel_version) # Extract to target directory. os.mkdir(destination_eggdir) zf.extractall(destination_eggdir) # Convert metadata. dist_info = os.path.join(destination_eggdir, dist_info) dist = Distribution.from_location(destination_eggdir, dist_info, metadata=PathMetadata( destination_eggdir, dist_info)) # Note: we need to evaluate and strip markers now, # as we can't easily convert back from the syntax: # foobar; "linux" in sys_platform and extra == 'test' def raw_req(req): req.marker = None return str(req) install_requires = list(sorted(map(raw_req, dist.requires()))) extras_require = { extra: list( sorted(req for req in map(raw_req, dist.requires((extra, ))) if req not in install_requires)) for extra in dist.extras } egg_info = os.path.join(destination_eggdir, 'EGG-INFO') os.rename(dist_info, egg_info) os.rename(os.path.join(egg_info, 'METADATA'), os.path.join(egg_info, 'PKG-INFO')) setup_dist = SetuptoolsDistribution(attrs=dict( install_requires=install_requires, extras_require=extras_require, )) write_requirements(setup_dist.get_command_obj('egg_info'), None, os.path.join(egg_info, 'requires.txt')) # Move data entries to their correct location. dist_data = os.path.join(destination_eggdir, dist_data) dist_data_scripts = os.path.join(dist_data, 'scripts') if os.path.exists(dist_data_scripts): egg_info_scripts = os.path.join(destination_eggdir, 'EGG-INFO', 'scripts') os.mkdir(egg_info_scripts) for entry in os.listdir(dist_data_scripts): # Remove bytecode, as it's not properly handled # during easy_install scripts install phase. if entry.endswith('.pyc'): os.unlink(os.path.join(dist_data_scripts, entry)) else: os.rename(os.path.join(dist_data_scripts, entry), os.path.join(egg_info_scripts, entry)) os.rmdir(dist_data_scripts) for subdir in filter( os.path.exists, (os.path.join(dist_data, d) for d in ('data', 'headers', 'purelib', 'platlib'))): unpack(subdir, destination_eggdir) if os.path.exists(dist_data): os.rmdir(dist_data) # Fix namespace packages. namespace_packages = os.path.join(egg_info, 'namespace_packages.txt') if os.path.exists(namespace_packages): with open(namespace_packages) as fp: namespace_packages = fp.read().split() for mod in namespace_packages: mod_dir = os.path.join(destination_eggdir, *mod.split('.')) mod_init = os.path.join(mod_dir, '__init__.py') if os.path.exists( mod_dir) and not os.path.exists(mod_init): with open(mod_init, 'w') as fp: fp.write(NAMESPACE_PACKAGE_INIT)
def install_as_egg(self, destination_eggdir): '''Install wheel as an egg directory.''' with zipfile.ZipFile(self.filename) as zf: dist_basename = '%s-%s' % (self.project_name, self.version) dist_info = '%s.dist-info' % dist_basename dist_data = '%s.data' % dist_basename def get_metadata(name): with zf.open('%s/%s' % (dist_info, name)) as fp: value = fp.read().decode('utf-8') if PY3 else fp.read() return email.parser.Parser().parsestr(value) wheel_metadata = get_metadata('WHEEL') dist_metadata = get_metadata('METADATA') # Check wheel format version is supported. wheel_version = parse_version(wheel_metadata.get('Wheel-Version')) if not parse_version('1.0') <= wheel_version < parse_version('2.0dev0'): raise ValueError('unsupported wheel format version: %s' % wheel_version) # Extract to target directory. os.mkdir(destination_eggdir) zf.extractall(destination_eggdir) # Convert metadata. dist_info = os.path.join(destination_eggdir, dist_info) dist = Distribution.from_location( destination_eggdir, dist_info, metadata=PathMetadata(destination_eggdir, dist_info) ) # Note: we need to evaluate and strip markers now, # as we can't easily convert back from the syntax: # foobar; "linux" in sys_platform and extra == 'test' def raw_req(req): req.marker = None return str(req) install_requires = list(sorted(map(raw_req, dist.requires()))) extras_require = { extra: list(sorted( req for req in map(raw_req, dist.requires((extra,))) if req not in install_requires )) for extra in dist.extras } egg_info = os.path.join(destination_eggdir, 'EGG-INFO') os.rename(dist_info, egg_info) os.rename(os.path.join(egg_info, 'METADATA'), os.path.join(egg_info, 'PKG-INFO')) setup_dist = SetuptoolsDistribution(attrs=dict( install_requires=install_requires, extras_require=extras_require, )) write_requirements(setup_dist.get_command_obj('egg_info'), None, os.path.join(egg_info, 'requires.txt')) # Move data entries to their correct location. dist_data = os.path.join(destination_eggdir, dist_data) dist_data_scripts = os.path.join(dist_data, 'scripts') if os.path.exists(dist_data_scripts): egg_info_scripts = os.path.join(destination_eggdir, 'EGG-INFO', 'scripts') os.mkdir(egg_info_scripts) for entry in os.listdir(dist_data_scripts): # Remove bytecode, as it's not properly handled # during easy_install scripts install phase. if entry.endswith('.pyc'): os.unlink(os.path.join(dist_data_scripts, entry)) else: os.rename(os.path.join(dist_data_scripts, entry), os.path.join(egg_info_scripts, entry)) os.rmdir(dist_data_scripts) for subdir in filter(os.path.exists, ( os.path.join(dist_data, d) for d in ('data', 'headers', 'purelib', 'platlib') )): unpack(subdir, destination_eggdir) if os.path.exists(dist_data): os.rmdir(dist_data) # Fix namespace packages. namespace_packages = os.path.join(egg_info, 'namespace_packages.txt') if os.path.exists(namespace_packages): with open(namespace_packages) as fp: namespace_packages = fp.read().split() for mod in namespace_packages: mod_dir = os.path.join(destination_eggdir, *mod.split('.')) mod_init = os.path.join(mod_dir, '__init__.py') if os.path.exists(mod_dir) and not os.path.exists(mod_init): with open(mod_init, 'w') as fp: fp.write(NAMESPACE_PACKAGE_INIT)