Exemplo n.º 1
0
    def __init__(self,
                 dist = None,
                 base_dir = "/usr/share/python-apt/templates"):
        self.metarelease_uri = ''
        self.templates = []
        self.arch = apt_pkg.config.find("APT::Architecture")

        location = None
        match_loc = re.compile(r"^#LOC:(.+)$")
        match_mirror_line = re.compile(
            r"^(#LOC:.+)|(((http)|(ftp)|(rsync)|(file)|(mirror)|(https))://"
            r"[A-Za-z0-9/\.:\-_@]+)$")
        #match_mirror_line = re.compile(r".+")

        if not dist:
            try:
                dist = Popen(["lsb_release", "-i", "-s"],
                             stdout=PIPE).communicate()[0].strip()
            except OSError as exc:
                if exc.errno != errno.ENOENT:
                    logging.warn('lsb_release failed, using defaults:' % exc)
                dist = "Debian"

        self.dist = dist

        map_mirror_sets = {}

        dist_fname = "%s/%s.info" % (base_dir, dist)
        with open(dist_fname) as dist_file:
            template = None
            component = None
            for line in dist_file:
                tokens = line.split(':', 1)
                if len(tokens) < 2:
                    continue
                field = tokens[0].strip()
                value = tokens[1].strip()
                if field == 'ChangelogURI':
                    self.changelogs_uri = _(value)
                elif field == 'MetaReleaseURI':
                    self.metarelease_uri = value
                elif field == 'Suite':
                    self.finish_template(template, component)
                    component=None
                    template = Template()
                    template.name = value
                    template.distribution = dist
                    template.match_name = "^%s$" % value
                elif field == 'MatchName':
                    template.match_name = value
                elif field == 'ParentSuite':
                    template.child = True
                    for nanny in self.templates:
                        # look for parent and add back ref to it
                        if nanny.name == value:
                            template.parents.append(nanny)
                            nanny.children.append(template)
                elif field == 'Available':
                    template.available = apt_pkg.string_to_bool(value)
                elif field == 'Official':
                    template.official = apt_pkg.string_to_bool(value)
                elif field == 'RepositoryType':
                    template.type = value
                elif field == 'BaseURI' and not template.base_uri:
                    template.base_uri = value
                elif field == 'BaseURI-%s' % self.arch:
                    template.base_uri = value
                elif field == 'MatchURI' and not template.match_uri:
                    template.match_uri = value
                elif field == 'MatchURI-%s' % self.arch:
                    template.match_uri = value
                elif (field == 'MirrorsFile' or
                      field == 'MirrorsFile-%s' % self.arch):
                    # Make the path absolute.
                    value = os.path.isabs(value) and value or \
                            os.path.abspath(os.path.join(base_dir, value))
                    if value not in map_mirror_sets:
                        mirror_set = {}
                        try:
                            with open(value) as value_f:
                                mirror_data = filter(match_mirror_line.match,
                                                     [x.strip() for x in
                                                      value_f])
                        except Exception:
                            print "WARNING: Failed to read mirror file"
                            mirror_data = []
                        for line in mirror_data:
                            if line.startswith("#LOC:"):
                                location = match_loc.sub(r"\1", line)
                                continue
                            (proto, hostname, dir) = split_url(line)
                            if hostname in mirror_set:
                                mirror_set[hostname].add_repository(proto, dir)
                            else:
                                mirror_set[hostname] = Mirror(
                                    proto, hostname, dir, location)
                        map_mirror_sets[value] = mirror_set
                    template.mirror_set = map_mirror_sets[value]
                elif field == 'Description':
                    template.description = _(value)
                elif field == 'Component':
                    if (component and not
                        template.has_component(component.name)):
                        template.components.append(component)
                    component = Component(value)
                elif field == 'CompDescription':
                    component.set_description(_(value))
                elif field == 'CompDescriptionLong':
                    component.set_description_long(_(value))
                elif field == 'ParentComponent':
                    component.set_parent_component(value)
            self.finish_template(template, component)
            template=None
            component=None
Exemplo n.º 2
0
    def parse(self, line):
        """ parse a given sources.list (textual) line and break it up
            into the field we have """
        self.line = line
        line = line.strip()
        # check if the source is enabled/disabled
        if line == "" or line == "#":  # empty line
            self.invalid = True
            return
        if line[0] == "#":
            self.disabled = True
            pieces = line[1:].strip().split()
            # if it looks not like a disabled deb line return
            if not pieces[0] in ("rpm", "rpm-src", "deb", "deb-src"):
                self.invalid = True
                return
            else:
                line = line[1:]
        # check for another "#" in the line (this is treated as a comment)
        i = line.find("#")
        if i > 0:
            self.comment = line[i + 1:]
            line = line[:i]
        # source is ok, split it and see what we have
        pieces = self.mysplit(line)
        # Sanity check
        if len(pieces) < 3:
            self.invalid = True
            return
        # Type, deb or deb-src
        self.type = pieces[0].strip()
        # Sanity check
        if self.type not in ("deb", "deb-src", "rpm", "rpm-src"):
            self.invalid = True
            return

        if pieces[1].strip()[0] == "[":
            options = pieces.pop(1).strip("[]").split()
            for option in options:
                try:
                    key, value = option.split("=", 1)
                except Exception:
                    self.invalid = True
                else:
                    if key == "arch":
                        self.architectures = value.split(",")
                    elif key == "trusted":
                        self.trusted = apt_pkg.string_to_bool(value)
                    else:
                        self.invalid = True

        # URI
        self.uri = pieces[1].strip()
        if len(self.uri) < 1:
            self.invalid = True
        # distro and components (optional)
        # Directory or distro
        self.dist = pieces[2].strip()
        if len(pieces) > 3:
            # List of components
            self.comps = pieces[3:]
        else:
            self.comps = []
Exemplo n.º 3
0
    def __init__(self, dist=None, base_dir="/usr/share/python-apt/templates"):
        self.metarelease_uri = ''
        self.templates = []
        self.arch = apt_pkg.config.find("APT::Architecture")

        location = None
        match_loc = re.compile(r"^#LOC:(.+)$")
        match_mirror_line = re.compile(
            r"^(#LOC:.+)|(((http)|(ftp)|(rsync)|(file)|(mirror)|(https))://"
            r"[A-Za-z0-9/\.:\-_@]+)$")
        #match_mirror_line = re.compile(r".+")

        if not dist:
            try:
                dist = Popen(["lsb_release", "-i", "-s"],
                             stdout=PIPE).communicate()[0].strip()
            except OSError as exc:
                if exc.errno != errno.ENOENT:
                    logging.warning('lsb_release failed, using defaults:' %
                                    exc)
                dist = "Debian"

        self.dist = dist

        map_mirror_sets = {}

        dist_fname = "%s/%s.info" % (base_dir, dist)
        with open(dist_fname) as dist_file:
            template = None
            component = None
            for line in dist_file:
                tokens = line.split(':', 1)
                if len(tokens) < 2:
                    continue
                field = tokens[0].strip()
                value = tokens[1].strip()
                if field == 'ChangelogURI':
                    self.changelogs_uri = _(value)
                elif field == 'MetaReleaseURI':
                    self.metarelease_uri = value
                elif field == 'Suite':
                    self.finish_template(template, component)
                    component = None
                    template = Template()
                    template.name = value
                    template.distribution = dist
                    template.match_name = "^%s$" % value
                elif field == 'MatchName':
                    template.match_name = value
                elif field == 'ParentSuite':
                    template.child = True
                    for nanny in self.templates:
                        # look for parent and add back ref to it
                        if nanny.name == value:
                            template.parents.append(nanny)
                            nanny.children.append(template)
                elif field == 'Available':
                    template.available = apt_pkg.string_to_bool(value)
                elif field == 'Official':
                    template.official = apt_pkg.string_to_bool(value)
                elif field == 'RepositoryType':
                    template.type = value
                elif field == 'BaseURI' and not template.base_uri:
                    template.base_uri = value
                elif field == 'BaseURI-%s' % self.arch:
                    template.base_uri = value
                elif field == 'MatchURI' and not template.match_uri:
                    template.match_uri = value
                elif field == 'MatchURI-%s' % self.arch:
                    template.match_uri = value
                elif (field == 'MirrorsFile'
                      or field == 'MirrorsFile-%s' % self.arch):
                    # Make the path absolute.
                    value = os.path.isabs(value) and value or \
                            os.path.abspath(os.path.join(base_dir, value))
                    if value not in map_mirror_sets:
                        mirror_set = {}
                        try:
                            with open(value) as value_f:
                                mirror_data = list(
                                    filter(match_mirror_line.match,
                                           [x.strip() for x in value_f]))
                        except Exception:
                            print("WARNING: Failed to read mirror file")
                            mirror_data = []
                        for line in mirror_data:
                            if line.startswith("#LOC:"):
                                location = match_loc.sub(r"\1", line)
                                continue
                            (proto, hostname, dir) = split_url(line)
                            if hostname in mirror_set:
                                mirror_set[hostname].add_repository(proto, dir)
                            else:
                                mirror_set[hostname] = Mirror(
                                    proto, hostname, dir, location)
                        map_mirror_sets[value] = mirror_set
                    template.mirror_set = map_mirror_sets[value]
                elif field == 'Description':
                    template.description = _(value)
                elif field == 'Component':
                    if (component
                            and not template.has_component(component.name)):
                        template.components.append(component)
                    component = Component(value)
                elif field == 'CompDescription':
                    component.set_description(_(value))
                elif field == 'CompDescriptionLong':
                    component.set_description_long(_(value))
                elif field == 'ParentComponent':
                    component.set_parent_component(value)
            self.finish_template(template, component)
            template = None
            component = None
Exemplo n.º 4
0
    def parse(self, line):
        """ parse a given sources.list (textual) line and break it up
            into the field we have """
        line = self.line.strip()
        #print line
        # check if the source is enabled/disabled
        if line == "" or line == "#":  # empty line
            self.invalid = True
            return
        if line[0] == "#":
            self.disabled = True
            pieces = line[1:].strip().split()
            # if it looks not like a disabled deb line return
            if not pieces[0] in ("rpm", "rpm-src", "deb", "deb-src"):
                self.invalid = True
                return
            else:
                line = line[1:]
        # check for another "#" in the line (this is treated as a comment)
        i = line.find("#")
        if i > 0:
            self.comment = line[i + 1:]
            line = line[:i]
        # source is ok, split it and see what we have
        pieces = self.mysplit(line)
        # Sanity check
        if len(pieces) < 3:
            self.invalid = True
            return
        # Type, deb or deb-src
        self.type = pieces[0].strip()
        # Sanity check
        if self.type not in ("deb", "deb-src", "rpm", "rpm-src"):
            self.invalid = True
            return

        if pieces[1].strip()[0] == "[":
            options = pieces.pop(1).strip("[]").split()
            for option in options:
                try:
                    key, value = option.split("=", 1)
                except Exception:
                    self.invalid = True
                else:
                    if key == "arch":
                        self.architectures = value.split(",")
                    elif key == "trusted":
                        self.trusted = apt_pkg.string_to_bool(value)
                    else:
                        self.invalid = True

        # URI
        self.uri = pieces[1].strip()
        if len(self.uri) < 1:
            self.invalid = True
        # distro and components (optional)
        # Directory or distro
        self.dist = pieces[2].strip()
        if len(pieces) > 3:
            # List of components
            self.comps = pieces[3:]
        else:
            self.comps = []