def compile_tex(self, s, d): if os.path.exists(d): # Remove the destination to clear out any existing junk shutil.rmtree(d) self.mkpath(d) tex_files = [os.path.join(s, f) for f in os.listdir(s) \ if os.path.splitext(f)[1] == '.tex'] for src in tex_files: dest = os.path.join( d, os.path.splitext(os.path.basename(src))[0] + '.pdf') log.debug('Compiling {} to {}'.format(src, dest)) p = subprocess.Popen(['rubber', '-d', '--into', d, src], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: raise errors.DistutilsFileError( 'Failed to compile {} to {}.\nStdout:\n{}\n---\n' 'Stderr:\n{}'.format(src, dest, stdout, stderr)) # Clear up the auxiliary files log.debug('Cleaning temporary files from {}'.format(d)) for f in [f for f in os.listdir(d) if os.path.splitext(f)[1] \ in ['.aux', '.log', '.out']]: os.remove(os.path.join(d, f))
def set_extra_files(extra_files): # Let's do a sanity check for filename in extra_files: if not os.path.exists(filename): raise errors.DistutilsFileError( '%s from the extra_files option in setup.cfg does not ' 'exist' % filename) global _extra_files _extra_files[:] = extra_files[:]
def compile_docs(self, s, d, comp_cmd, ext): if os.path.exists(d): # Remove the destination to clear out any existing junk shutil.rmtree(d) self.mkpath(d) for f in os.listdir(s): src = os.path.join(s, f) if not os.path.isfile(src): log.warn('Skipping non-file {}'.format(src)) continue dest = os.path.join(d, os.path.splitext(f)[0] + ext) log.debug('Compiling {} to {}'.format(src, dest)) cmd = [comp_cmd, src, dest] try: if sys.platform == 'win32': cmd.insert(0, 'python') p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: raise errors.DistutilsFileError( 'Failed to compile {} to {}.\nStdout:\n{}\n---\n' 'Stderr:\n{}'.format(src, dest, stdout, stderr)) except OSError as e: if e.errno == 2: # The non-.py version of the command was not found; try it # with the .py extension (for platforms like Gentoo) cmd[0] = cmd[0] + '.py' if sys.platform == 'win32': cmd.insert(0, 'python') p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: raise errors.DistutilsFileError( 'Failed to compile {} to {}.\nStdout:\n{}\n---\n' 'Stderr:\n{}'.format(src, dest, stdout, stderr)) else: raise
def run(self): self.run_command('build_msdeploy') if not os.path.exists(self.manifest_filename): raise errors.DistutilsFileError( 'No Web Deploy manifest found at {0}'.format( self.manifest_filename)) if self.install not in self.distribution.have_run: self.install.run() self.distribution.have_run[self.install] = 1 sdist.sdist.run(self)
def add_msdeploy(self, path, *args): cwd = os.getcwd() try: os.chdir(path) distribution = self.distribution if os.path.abspath(path) != os.path.abspath(cwd): distribution = core.run_setup( 'setup.py', stop_after='commandline') distribution.build = distribution.get_command_obj('build') distribution.build.ensure_finalized() distribution.has_msdeploy_manifest = ( 'build_msdeploy' in distribution.build.get_sub_commands()) if not distribution.has_msdeploy_manifest: raise errors.DistutilsFileError( 'No Web Deploy manifest found for {0}'.format(path)) distribution.msdeploy_file = options.get_egg_name( distribution) + '.msdeploy.zip' distribution.msdeploy_package = os.path.abspath( os.path.join('dist', distribution.msdeploy_file)) distribution.msdeploy_package_url = urlparse.urlunsplit(( 'file', '', urllib.pathname2url(distribution.msdeploy_package), '', '')) webpi_size = os.path.getsize(distribution.msdeploy_package) cmd = ['fciv', '-sha1', distribution.msdeploy_package] webpi_sha1 = '' try: webpi_sha1_output = subprocess.check_output(cmd) except OSError, error: if error.errno == errno.ENOENT: logger.exception('Error getting SHA1:\n{0}'.format( ' '.join(cmd))) else: raise else:
def cfg_to_args(path='setup.cfg', script_args=()): """Distutils2 to distutils1 compatibility util. This method uses an existing setup.cfg to generate a dictionary of keywords that can be used by distutils.core.setup(kwargs**). :param path: The setup.cfg path. :param script_args: List of commands setup.py was called with. :raises DistutilsFileError: When the setup.cfg file is not found. """ # The method source code really starts here. if sys.version_info >= (3, 2): parser = configparser.ConfigParser() else: parser = configparser.SafeConfigParser() if not os.path.exists(path): raise errors.DistutilsFileError("file '%s' does not exist" % os.path.abspath(path)) try: parser.read(path, encoding='utf-8') except TypeError: # Python 2 doesn't accept the encoding kwarg parser.read(path) config = {} for section in parser.sections(): config[section] = dict() for k, value in parser.items(section): config[section][k.replace('-', '_')] = value # Run setup_hooks, if configured setup_hooks = has_get_option(config, 'global', 'setup_hooks') package_dir = has_get_option(config, 'files', 'packages_root') # Add the source package directory to sys.path in case it contains # additional hooks, and to make sure it's on the path before any existing # installations of the package if package_dir: package_dir = os.path.abspath(package_dir) sys.path.insert(0, package_dir) try: if setup_hooks: setup_hooks = [ hook for hook in split_multiline(setup_hooks) if hook != 'pbr.hooks.setup_hook' ] for hook in setup_hooks: hook_fn = resolve_name(hook) try: hook_fn(config) except SystemExit: log.error('setup hook %s terminated the installation') except: e = sys.exc_info()[1] log.error('setup hook %s raised exception: %s\n' % (hook, e)) log.error(traceback.format_exc()) sys.exit(1) # Run the pbr hook pbr.hooks.setup_hook(config) kwargs = setup_cfg_to_setup_kwargs(config, script_args) # Set default config overrides kwargs['include_package_data'] = True kwargs['zip_safe'] = False register_custom_compilers(config) ext_modules = get_extension_modules(config) if ext_modules: kwargs['ext_modules'] = ext_modules entry_points = get_entry_points(config) if entry_points: kwargs['entry_points'] = entry_points # Handle the [files]/extra_files option files_extra_files = has_get_option(config, 'files', 'extra_files') if files_extra_files: extra_files.set_extra_files(split_multiline(files_extra_files)) finally: # Perform cleanup if any paths were added to sys.path if package_dir: sys.path.pop(0) return kwargs
def pbr(dist, attr, value): """Implements the actual pbr setup() keyword. When used, this should be the only keyword in your setup() aside from `setup_requires`. If given as a string, the value of pbr is assumed to be the relative path to the setup.cfg file to use. Otherwise, if it evaluates to true, it simply assumes that pbr should be used, and the default 'setup.cfg' is used. This works by reading the setup.cfg file, parsing out the supported metadata and command options, and using them to rebuild the `DistributionMetadata` object and set the newly added command options. The reason for doing things this way is that a custom `Distribution` class will not play nicely with setup_requires; however, this implementation may not work well with distributions that do use a `Distribution` subclass. """ try: _monkeypatch_distribution() if not value: return if isinstance(value, string_type): path = os.path.abspath(value) else: path = os.path.abspath('setup.cfg') if not os.path.exists(path): raise errors.DistutilsFileError( 'The setup.cfg file %s does not exist.' % path) # Converts the setup.cfg file to setup() arguments try: attrs = util.cfg_to_args(path, dist.script_args) except Exception: e = sys.exc_info()[1] # NB: This will output to the console if no explicit logging has # been setup - but thats fine, this is a fatal distutils error, so # being pretty isn't the #1 goal.. being diagnosable is. logging.exception('Error parsing') raise errors.DistutilsSetupError( 'Error parsing %s: %s: %s' % (path, e.__class__.__name__, e)) # Repeat some of the Distribution initialization code with the newly # provided attrs if attrs: # Skips 'options' and 'licence' support which are rarely used; may # add back in later if demanded for key, val in attrs.items(): if hasattr(dist.metadata, 'set_' + key): getattr(dist.metadata, 'set_' + key)(val) elif hasattr(dist.metadata, key): setattr(dist.metadata, key, val) elif hasattr(dist, key): setattr(dist, key, val) else: msg = 'Unknown distribution option: %s' % repr(key) warnings.warn(msg) # Re-finalize the underlying Distribution core.Distribution.finalize_options(dist) # This bit comes out of distribute/setuptools if isinstance(dist.metadata.version, integer_types + (float,)): # Some people apparently take "version number" too literally :) dist.metadata.version = str(dist.metadata.version) finally: _restore_distribution_monkeypatch()
def pbr(dist, attr, value): """Implements the actual pbr setup() keyword. When used, this should be the only keyword in your setup() aside from `setup_requires`. If given as a string, the value of pbr is assumed to be the relative path to the setup.cfg file to use. Otherwise, if it evaluates to true, it simply assumes that pbr should be used, and the default 'setup.cfg' is used. This works by reading the setup.cfg file, parsing out the supported metadata and command options, and using them to rebuild the `DistributionMetadata` object and set the newly added command options. The reason for doing things this way is that a custom `Distribution` class will not play nicely with setup_requires; however, this implementation may not work well with distributions that do use a `Distribution` subclass. """ if not value: return if isinstance(value, string_type): path = os.path.abspath(value) else: path = os.path.abspath('setup.cfg') if not os.path.exists(path): raise errors.DistutilsFileError( 'The setup.cfg file %s does not exist.' % path) # Converts the setup.cfg file to setup() arguments try: attrs = util.cfg_to_args(path) except Exception: e = sys.exc_info()[1] raise errors.DistutilsSetupError('Error parsing %s: %s: %s' % (path, e.__class__.__name__, e)) # Repeat some of the Distribution initialization code with the newly # provided attrs if attrs: # Skips 'options' and 'licence' support which are rarely used; may add # back in later if demanded for key, val in attrs.items(): if hasattr(dist.metadata, 'set_' + key): getattr(dist.metadata, 'set_' + key)(val) elif hasattr(dist.metadata, key): setattr(dist.metadata, key, val) elif hasattr(dist, key): setattr(dist, key, val) else: msg = 'Unknown distribution option: %s' % repr(key) warnings.warn(msg) # Re-finalize the underlying Distribution core.Distribution.finalize_options(dist) # This bit comes out of distribute/setuptools if isinstance(dist.metadata.version, integer_types + (float, )): # Some people apparently take "version number" too literally :) dist.metadata.version = str(dist.metadata.version) # This bit of hackery is necessary so that the Distribution will ignore # normally unsupport command options (namely pre-hooks and post-hooks). # dist.command_options is normally a dict mapping command names to dicts of # their options. Now it will be a defaultdict that returns IgnoreDicts for # the each command's options so we can pass through the unsupported options ignore = ['pre_hook.*', 'post_hook.*'] dist.command_options = util.DefaultGetDict(lambda: util.IgnoreDict(ignore))
def pbr(dist, attr, value): """Implements the actual pbr setup() keyword. When used, this should be the only keyword in your setup() aside from `setup_requires`. If given as a string, the value of pbr is assumed to be the relative path to the setup.cfg file to use. Otherwise, if it evaluates to true, it simply assumes that pbr should be used, and the default 'setup.cfg' is used. This works by reading the setup.cfg file, parsing out the supported metadata and command options, and using them to rebuild the `DistributionMetadata` object and set the newly added command options. The reason for doing things this way is that a custom `Distribution` class will not play nicely with setup_requires; however, this implementation may not work well with distributions that do use a `Distribution` subclass. """ if not value: return if isinstance(value, string_type): path = os.path.abspath(value) else: path = os.path.abspath('setup.cfg') if not os.path.exists(path): raise errors.DistutilsFileError( 'The setup.cfg file %s does not exist.' % path) # Converts the setup.cfg file to setup() arguments try: attrs = util.cfg_to_args(path, dist.script_args) except Exception: e = sys.exc_info()[1] # NB: This will output to the console if no explicit logging has # been setup - but thats fine, this is a fatal distutils error, so # being pretty isn't the #1 goal.. being diagnosable is. logging.exception('Error parsing') raise errors.DistutilsSetupError('Error parsing %s: %s: %s' % (path, e.__class__.__name__, e)) # There are some metadata fields that are only supported by # setuptools and not distutils, and hence are not in # dist.metadata. We are OK to write these in. For gory details # see # https://github.com/pypa/setuptools/pull/1343 _DISTUTILS_UNSUPPORTED_METADATA = ('long_description_content_type', 'project_urls', 'provides_extras') # Repeat some of the Distribution initialization code with the newly # provided attrs if attrs: # Skips 'options' and 'licence' support which are rarely used; may # add back in later if demanded for key, val in attrs.items(): if hasattr(dist.metadata, 'set_' + key): getattr(dist.metadata, 'set_' + key)(val) elif hasattr(dist.metadata, key): setattr(dist.metadata, key, val) elif hasattr(dist, key): setattr(dist, key, val) elif key in _DISTUTILS_UNSUPPORTED_METADATA: setattr(dist.metadata, key, val) else: msg = 'Unknown distribution option: %s' % repr(key) warnings.warn(msg) # Re-finalize the underlying Distribution try: super(dist.__class__, dist).finalize_options() except TypeError: # If dist is not declared as a new-style class (with object as # a subclass) then super() will not work on it. This is the case # for Python 2. In that case, fall back to doing this the ugly way dist.__class__.__bases__[-1].finalize_options(dist) # This bit comes out of distribute/setuptools if isinstance(dist.metadata.version, integer_types + (float, )): # Some people apparently take "version number" too literally :) dist.metadata.version = str(dist.metadata.version)
def finalize_options(self): if not self.root: raise errors.DistutilsArgError("``root`` option is required.") self.root = Path(self.root) if not self.root.exists(): raise errors.DistutilsFileError(f"``{self.root}`` does not exist.")
def write_manifest(self): # TODO Seems not to be working the first time for pyramid manifest_template = self.manifest_filename + '.in' if not os.path.exists(manifest_template): self.logger.warn( 'No Web Deploy manifest template found at {0}'.format( manifest_template)) # No manifest template, don't update real manifest return # Substitute environment variables so that hashes can be dynamice manifest_str = os.path.expandvars(open(manifest_template).read()) manifest = minidom.parseString(manifest_str) for runcommand in manifest.getElementsByTagName('runCommand'): # Collect the attributes that need to be passed as settings path = None settings_attrs = {} for name, value in runcommand.attributes.items(): if name == 'path': # provider value/key attribute path = value continue elif name.startswith('MSDeploy.'): # Leave these alone continue settings_attrs[name] = value # Remove them from the output XML runcommand.removeAttribute(name) if path is None: raise errors.DistutilsFileError( 'No `path` attribute in a <runCommand> element in {0}' .format(manifest_template)) # Assemble the msdeploy.exe source command line settings = ','.join('{0}={1}'.format(*item) for item in settings_attrs.items()) if settings: settings = ',' + settings source = '-source:runCommand="{0}"{1}'.format(path, settings) tmp = tempfile.mkdtemp() package = os.path.join(tmp, 'runCommand.zip') try: cmd = ( '"{msdeploy}" -verb:sync {source} -dest:package={package}' .format(msdeploy=self.msdeploy_exe, source=source, package=package)) self.logger.info( 'Generating runCommand manifest:\n{0}'.format(cmd)) if self.msdeploy_exe and os.path.exists(self.msdeploy_exe): subprocess.check_call(cmd, shell=True) else: self.logger.error( 'msdeploy.exe does not exist: {0}'.format( self.msdeploy_exe)) continue tmp_manifest = minidom.parseString( zipfile.ZipFile(package).read('archive.xml')) finally: shutil.rmtree(tmp) new_runcommands = tmp_manifest.getElementsByTagName('runCommand') if not new_runcommands: raise errors.DistutilsExecError( 'No <runCommand> elements found in {0}:archive.xml' .format(package)) elif len(new_runcommands) > 1: raise errors.DistutilsExecError( 'Multiple <runCommand> elements found in {0}:archive.xml' .format(package)) options = new_runcommands[0].getAttribute( 'MSDeploy.MSDeployProviderOptions') runcommand.setAttribute( 'MSDeploy.MSDeployProviderOptions', options) self.logger.info('Writing Web Deploy manifest to {0}'.format( self.manifest_filename)) manifest.writexml(open(self.manifest_filename, 'w'))