我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用setuptools.Command()。
def run(self): aliases = self.distribution.get_option_dict('aliases') if not self.args: print("Command Aliases") print("---------------") for alias in aliases: print("setup.py alias", format_alias(alias, aliases)) return elif len(self.args)==1: alias, = self.args if self.remove: command = None elif alias in aliases: print("setup.py alias", format_alias(alias, aliases)) return else: print("No alias definition found for %r" % alias) return else: alias = self.args[0] command = ' '.join(map(shquote,self.args[1:])) edit_config(self.filename, {'aliases': {alias:command}}, self.dry_run)
def combine_commands(*commands): """Return a Command that combines several commands.""" class CombinedCommand(Command): def initialize_options(self): self.commands = [] for C in commands: self.commands.append(C(self.distribution)) for c in self.commands: c.initialize_options() def finalize_options(self): for c in self.commands: c.finalize_options() def run(self): for c in self.commands: c.run() return CombinedCommand
def ensure_targets(targets): """Return a Command that checks that certain files exist. Raises a ValueError if any of the files are missing. Note: The check is skipped if the `--skip-npm` flag is used. """ class TargetsCheck(BaseCommand): def run(self): if skip_npm: log.info('Skipping target checks') return missing = [t for t in targets if not os.path.exists(t)] if missing: raise ValueError(('missing files: %s' % missing)) return TargetsCheck # `shutils.which` function copied verbatim from the Python-3.3 source.
def patch_all(): # we can't patch distutils.cmd, alas distutils.core.Command = setuptools.Command has_issue_12885 = sys.version_info <= (3, 5, 3) if has_issue_12885: # fix findall bug in distutils (http://bugs.python.org/issue12885) distutils.filelist.findall = setuptools.findall needs_warehouse = ( sys.version_info < (2, 7, 13) or (3, 0) < sys.version_info < (3, 3, 7) or (3, 4) < sys.version_info < (3, 4, 6) or (3, 5) < sys.version_info <= (3, 5, 3) ) if needs_warehouse: warehouse = 'https://upload.pypi.org/legacy/' distutils.config.PyPIRCCommand.DEFAULT_REPOSITORY = warehouse _patch_distribution_metadata_write_pkg_file() _patch_distribution_metadata_write_pkg_info() # Install Distribution throughout the distutils for module in distutils.dist, distutils.core, distutils.cmd: module.Distribution = setuptools.dist.Distribution # Install the patched Extension distutils.core.Extension = setuptools.extension.Extension distutils.extension.Extension = setuptools.extension.Extension if 'distutils.command.build_ext' in sys.modules: sys.modules['distutils.command.build_ext'].Extension = ( setuptools.extension.Extension ) patch_for_msvc_specialized_compiler()
def Extension(*args, **kwds): """ Simple wrapper about distutils.core.Extension that adds additional PyObjC specific flags. """ os_level = get_sdk_level() if os_level is None: os_level = get_os_level() cflags = ["-DPyObjC_BUILD_RELEASE=%02d%02d"%(tuple(map(int, os_level.split('.'))))] ldflags = [] if 'clang' in get_config_var('CC'): cflags.append('-Wno-deprecated-declarations') CFLAGS = get_config_var('CFLAGS') if '-isysroot' not in CFLAGS and os.path.exists('/usr/include/stdio.h'): # We're likely on a system with de Xcode Command Line Tools. # Explicitly use the most recent problems to avoid compile problems. data = os.popen('xcodebuild -version -sdk macosx Path').read() data = data.strip() if data: cflags.append('-isysroot') cflags.append(data) if os_level == '10.4': cflags.append('-DNO_OBJC2_RUNTIME') if 'extra_compile_args' in kwds: kwds['extra_compile_args'] = kwds['extra_compile_args'] + cflags else: kwds['extra_compile_args'] = cflags if 'extra_link_args' in kwds: kwds['extra_link_args'] = kwds['extra_link_args'] + ldflags else: kwds['extra_link_args'] = ldflags return _Extension(*args, **kwds)
def RunCustomCommand(self, command_list): print 'Running command: %s' % command_list p = subprocess.Popen( command_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Can use communicate(input='y\n'.encode()) if the command run requires # some confirmation. stdout_data, _ = p.communicate() print 'Command output: %s' % stdout_data if p.returncode != 0: raise RuntimeError( 'Command %s failed: exit code: %s' % (command_list, p.returncode))
def initialize_options(self): """ Initialize Clean Command """ self.cwd = None
def finalize_options(self): """ Set Current Working directory for Clean Command """ self.cwd = os.getcwd()
def run(self): """ Execute Clean Command """ assert os.getcwd() == self.cwd, 'Must be in package root: %s' % self.cwd os.system('rm -rf ./docs/_build ./build ./dist ./*.egg-info') os.system('rm -rf ./*.log *.xml .coverage *.html')
def finalize_options(self): if self.scrappie_model is None: raise ValueError('Command "{}" requires scrappie-model=<value> option.'.format(model_cmd_name)) else: if not os.path.exists(self.scrappie_model): raise ValueError('Model header {} does not exist.'.format(self.scrappie_model))
def call(self, command): env = os.environ.copy() env['PYTHONPATH'] = ''.join(':' + x for x in sys.path) self.announce('Run command: {}'.format(command), level=2) try: subprocess.check_call(command.split(), env=env) except subprocess.CalledProcessError as error: self._returncode = 1 message = 'Command failed with exit code {}' message = message.format(error.returncode) self.announce(message, level=2)
def RunCustomCommand(self, command_list): print 'Running command: %s' % command_list p = subprocess.Popen(command_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Can use communicate(input='y\n'.encode()) if the command run requires # some confirmation. stdout_data, _ = p.communicate() print 'Command output: %s' % stdout_data if p.returncode != 0: raise RuntimeError('Command %s failed: exit code: %s' % (command_list, p.returncode))