Ejemplo n.º 1
0
 def follow(self, context):
     """Follow this link to find more.
     """
     urls = []
     informations = []
     stream = open_uri(self.url)
     try:
         content_type = stream.headers.get('content-type', '').split(';')[0]
         if content_type not in ['text/html']:
             raise NetworkError('Not HTML', self.url)
         parser = LinkParser(self.url)
         parser.feed(stream.read())
     except HTMLParser.HTMLParseError:
         logger.warn("Discarding unreadable HTML page '%s'", self.url)
         return urls, informations
     finally:
         stream.close()
     for url, filename, name, rel in parser.links:
         if context.is_disabled_link(url, True):
             continue
         information = parse_filename(filename, url=url)
         if information:
             informations.append(information)
         else:
             urls.append(self.__class__(url, filename, name, rel))
     return urls, informations
Ejemplo n.º 2
0
    def render_template(self, source_path, output_path, factory):
        """Render a template using Template engine ``factory`` from
        ``source_path`` into ``output_path``.
        """
        __status__ = u"Rendering template for %s." % output_path
        from genshi.template import TemplateError

        logger.info('Creating file %s from template.' % output_path)
        if output_path not in self.status.installed_paths:
            self.status.test_override_rule(output_path)
        success = False
        source_file = open_uri(source_path)
        try:
            try:
                template = factory(source_file.read())
            except TemplateError, error:
                raise TemplateRenderingError(error)
            output_file = open(output_path, 'wb')
            try:
                output_file.write(
                    template.generate(
                        section=self.options,
                        configuration=self.options.configuration,
                        status=self.status
                        ).render())
                success = True
            except TemplateError, error:
                raise TemplateRenderingError(error)
Ejemplo n.º 3
0
 def read(cls, uri):
     """Read a configuration file located at the given uri.
     """
     abs_uri = absolute_uri(uri)
     input = open_uri(abs_uri)
     try:
         return cls.read_lines(input.readlines, abs_uri)
     finally:
         input.close()
Ejemplo n.º 4
0
def parse_manifest(manifest_name):
    """Read the given manifest file to give a dictionary of rules
    describing which files to include.
    """
    manifest = open_uri(manifest_name)
    regular_rules = {}
    recursive_rules = {}
    line_number = 0
    for text in manifest.readlines():
        line_number += 1
        # Comment
        if not text.strip() or text[0] in '#;':
            continue

        def manifest_current_location():
            manifest.close()
            return u'%s at line %d' % (manifest_name, line_number)

        parts = [t.strip() for t in text.split()]
        command = parts[0].lower()
        if command == 'include':
            if len(parts) < 2:
                raise ConfigurationError(
                    manifest_current_location(),
                    u"Malformed include directive")
            for part in parts[1:]:
                dirname = os.path.dirname(part)
                if not dirname:
                    dirname = './'
                else:
                    dirname = os.path.normpath(dirname) + os.path.sep
                rule = regular_rules.setdefault(dirname, list())
                rule.append(os.path.basename(part))
        elif command == 'recursive-include':
            if len(parts) < 3:
                raise ConfigurationError(
                    manifest_current_location(),
                    u"Malformed recursive-include directive")
            dirname = os.path.normpath(parts[1]) + os.path.sep
            rule = recursive_rules.setdefault(dirname, list())
            rule.extend(parts[2:])
        else:
            raise ConfigurationError(
                manifest_current_location(),
                u"Unknow manifest directive %s" % command)
    manifest.close()
    return (regular_rules, recursive_rules,)
Ejemplo n.º 5
0
    def __call__(self, distribution, path, interpretor, trust=-99):
        setup_py = os.path.join(path, 'setup.py')
        if os.path.isfile(setup_py):
            # You need to clean first the egg_info. install_requires
            # will trigger strange things only if it exists.
            egg_info_parent, egg_info = find_egg_info(distribution, path)
            if egg_info is not None and os.path.isdir(egg_info):
                # We will use the egg SOURCES.txt as input for a
                # MANIFEST. Most of packages miss one or have a
                # incomplete one and won't install everything without
                # one.
                if trust < 0:
                    source_file = os.path.join(egg_info, 'SOURCES.txt')
                    manifest_file = os.path.join(path, 'MANIFEST.in')
                    if os.path.isfile(source_file):
                        create_manifest_from_source(source_file, manifest_file)
                shutil.rmtree(egg_info)

            # Determine which version of setuptools to use
            version = None
            environ = self.environ.get(distribution.name, {})
            if distribution.name == 'setuptools':
                # To install setuptools, we need the same version.
                version = str(distribution.version)
            else:
                version = self.version

            def execute(*command, **options):
                kwargs = {'environ': environ, 'version': version}
                kwargs.update(options)
                return interpretor.execute_setuptools(
                    *command, **kwargs)

            # Apply patches
            if distribution.name in self.patches:
                for patch in self.patches[distribution.name]:
                    stream = open_uri(patch)
                    try:
                        output, errors, code = get_cmd_output(
                            'patch', '-p0', path=path, input=stream.read())
                    finally:
                        stream.close()
                    if code:
                        raise InstallationError(
                            u'Error while patching setuptools egg %s.' % (
                                distribution.name),
                            detail='\n'.join((output, errors)))

            # Get fresh egg_info
            output, errors, code = execute('egg_info', path=path)
            if not code:
                egg_info_parent, egg_info = find_egg_info(distribution, path)
                if egg_info is not None and os.path.isdir(egg_info):
                    return NativeSetuptoolsLoader(
                        path, egg_info, distribution,
                        source_path=egg_info_parent, execute=execute)
                else:
                    logger.debug(
                        u"Could not find egg-info in  %s, " % (path))
            elif self.errors:
                raise PackageError(
                    u"Setuptools retuned status code %s in  %s." % (
                        code, path),
                    detail='\n'.join((output, errors)))
            else:
                logger.info(
                    u"Setuptools retuned status code %s in  %s, " % (
                        code, path))
        return None