Esempio n. 1
0
    def _apply():
        debug('Write ini settings to %s' % filename)
        debug(config)

        cfg = ConfigParser()
        cfg.read(filename)
        for section, values in config.items():
            if not cfg.has_section(section):
                if strict_sections:
                    raise Exception('No section %s in ini file %s' % (section, filename))
                else:
                    cfg.add_section(section)

            for key, val in values.items():
                cfg.set(section, key, val)

        with open(filename, 'w') as configfile:
            cfg.write(configfile)

        event(on_apply)
Esempio n. 2
0
class RepoFile(object):
    """
    Represents a .repo file, including operations to manipulate its repositories and
    CRUD operations on the file itself.
    """

    # Be careful when changing the spacing below, the parser takes issue when the comment
    # indicator isn't in the first column. The blank line at the end is fine though.
    FILE_HEADER = """#
# Pulp Repositories
# Managed by Pulp client
#

"""

    def __init__(self, filename):
        """
        @param filename: absolute path to the repo file; the repo file does not need to
                         exist at the time of instantiation, the save method will write it
                         out if it doesn't
        @type  filename: string; may not be None

        @raise ValueError: if filename is missing
        """
        if filename is None:
            raise ValueError("Filename must be specified when creating a RepoFile")

        self.filename = filename
        self.parser = ConfigParser()

    # -- file manipulation ------------------------------------------------------------

    def delete(self):
        """
        If the repo file exists, it will be deleted. If not, this method does nothing.

        @raise Exception: if there is an error during the delete
        """
        if os.path.exists(self.filename):
            os.unlink(self.filename)

    def load(self, allow_missing=True):
        """
        Loads the repo file.

        @param allow_missing: if True, this call will not throw an error if the file cannot
                              be found; defaults to True
        @type  allow_missing: bool

        @raise Exception: if there is an error during the read
        """
        if allow_missing and not os.path.exists(self.filename):
            return

        r = Reader(self.filename)
        self.parser.readfp(r)

    def save(self):
        """
        Saves the current repositories to the repo file.

        @raise Exception: if there is an error during the write
        """
        # If the file doesn't exist, initialize with Pulp header
        first_write = not os.path.exists(self.filename)

        f = open(self.filename, "w")

        if first_write:
            f.write(RepoFile.FILE_HEADER)

        # Write the contents of the parser
        self.parser.write(f)

        f.close()

    # -- contents manipulation ------------------------------------------------------------

    def add_repo(self, repo):
        """
        Adds a new repo to this object, however the file is not saved.

        This is not saved as an object reference, so future changes to the passed in
        repo will not be captured in this RepoFile instance. If changes are made to
        the original Repo object, it must be passed into the RepoFile instance through
        update_repo in order for the changes to be captured.

        @param repo: repo to add; may not be None
        @type  repo: L{Repo}
        """
        self.parser.add_section(repo.id)
        self._repo_to_parser(repo)

    def remove_repo_by_name(self, repo_name):
        """
        Removes the repo with the given name. If the repo does not exist, this
        method does nothing.

        @param repo: identifies the repo to remove
        @type  repo: string
        """
        return self.parser.remove_section(repo_name)

    def update_repo(self, repo):
        """
        Updates the underlying store with the latest contents of a repo. The repo
        passed to this method must have been created prior to this call.

        The repo is not saved as an object reference. Instead, the values are captured
        to the underlying store at the point in time this is called.

        @param repo: repo instance containing updated values to store; cannot be None
        @type  repo: L{Repo}
        """
        self._repo_to_parser(repo)

    def get_repo(self, repo_id):
        """
        Loads a repo by repo ID. If the repo does not exist, returns None.

        @param repo_id: id of the repo to retrieve
        @type  repo_id: string

        @return: repo instance if one exists; None otherwise
        @rtype:  L{Repo}
        """
        if self.parser.has_section(repo_id):
            repo = self._parser_to_repo(repo_id)
            return repo
        else:
            return None

    def all_repos(self):
        """
        Returns a list of all repos in the store.

        @return: list of repo instances; empty list if there are none
        @rtype:  list of L{Repo}
        """
        repos = []
        for repo_name in self.parser.sections():
            repo = self._parser_to_repo(repo_name)
            repos.append(repo)

        return repos

    def _repo_to_parser(self, repo):
        """
        Adds the contents of the repo to the underlying store. This call assumes
        the parser section has already been created.

        @param repo: repo instance to update in the parser; cannot be None
        @type  repo: L{Repo}
        """
        for k, v in repo.items():
            if v:
                self.parser.set(repo.id, k, v)
            else:
                self.parser.remove_option(repo.id, k)

    def _parser_to_repo(self, repo_name):
        """
        Utility for converting the config parser section into a repo object.

        @param repo_name: name of the repo being retrieved from the store
        @type  repo_name: string

        @return: repo instance populated from the section data
        @rtype:  L{Repo}
        """
        repo = Repo(repo_name)
        for key, value in self.parser.items(repo_name):
            repo[key] = value

        return repo
Esempio n. 3
0
class RepoFile(object):
    '''
    Represents a .repo file, including operations to manipulate its repositories and
    CRUD operations on the file itself.
    '''

    # Be careful when changing the spacing below, the parser takes issue when the comment
    # indicator isn't in the first column. The blank line at the end is fine though.
    FILE_HEADER = '''#
# Pulp Repositories
# Managed by Pulp client
#

'''

    def __init__(self, filename):
        '''
        @param filename: absolute path to the repo file; the repo file does not need to
                         exist at the time of instantiation, the save method will write it
                         out if it doesn't
        @type  filename: string; may not be None

        @raise ValueError: if filename is missing
        '''
        if filename is None:
            raise ValueError(
                'Filename must be specified when creating a RepoFile')

        self.filename = filename
        self.parser = ConfigParser()

    # -- file manipulation ------------------------------------------------------------

    def delete(self):
        '''
        If the repo file exists, it will be deleted. If not, this method does nothing.

        @raise Exception: if there is an error during the delete
        '''
        if os.path.exists(self.filename):
            os.unlink(self.filename)

    def load(self, allow_missing=True):
        '''
        Loads the repo file.

        @param allow_missing: if True, this call will not throw an error if the file cannot
                              be found; defaults to True
        @type  allow_missing: bool

        @raise Exception: if there is an error during the read
        '''
        if allow_missing and not os.path.exists(self.filename):
            return

        r = Reader(self.filename)
        self.parser.readfp(r)

    def save(self):
        '''
        Saves the current repositories to the repo file.

        @raise Exception: if there is an error during the write
        '''
        # If the file doesn't exist, initialize with Pulp header
        first_write = not os.path.exists(self.filename)

        f = open(self.filename, 'w')

        if first_write:
            f.write(RepoFile.FILE_HEADER)

        # Write the contents of the parser
        self.parser.write(f)

        f.close()

    # -- contents manipulation ------------------------------------------------------------

    def add_repo(self, repo):
        '''
        Adds a new repo to this object, however the file is not saved.

        This is not saved as an object reference, so future changes to the passed in
        repo will not be captured in this RepoFile instance. If changes are made to
        the original Repo object, it must be passed into the RepoFile instance through
        update_repo in order for the changes to be captured.

        @param repo: repo to add; may not be None
        @type  repo: L{Repo}
        '''
        self.parser.add_section(repo.id)
        self._repo_to_parser(repo)

    def remove_repo_by_name(self, repo_name):
        '''
        Removes the repo with the given name. If the repo does not exist, this
        method does nothing.

        @param repo: identifies the repo to remove
        @type  repo: string
        '''
        return self.parser.remove_section(repo_name)

    def update_repo(self, repo):
        '''
        Updates the underlying store with the latest contents of a repo. The repo
        passed to this method must have been created prior to this call.

        The repo is not saved as an object reference. Instead, the values are captured
        to the underlying store at the point in time this is called.

        @param repo: repo instance containing updated values to store; cannot be None
        @type  repo: L{Repo}
        '''
        self._repo_to_parser(repo)

    def get_repo(self, repo_id):
        '''
        Loads a repo by repo ID. If the repo does not exist, returns None.

        @param repo_id: id of the repo to retrieve
        @type  repo_id: string

        @return: repo instance if one exists; None otherwise
        @rtype:  L{Repo}
        '''
        if self.parser.has_section(repo_id):
            repo = self._parser_to_repo(repo_id)
            return repo
        else:
            return None

    def all_repos(self):
        '''
        Returns a list of all repos in the store.

        @return: list of repo instances; empty list if there are none
        @rtype:  list of L{Repo}
        '''
        repos = []
        for repo_name in self.parser.sections():
            repo = self._parser_to_repo(repo_name)
            repos.append(repo)

        return repos

    def _repo_to_parser(self, repo):
        '''
        Adds the contents of the repo to the underlying store. This call assumes
        the parser section has already been created.

        @param repo: repo instance to update in the parser; cannot be None
        @type  repo: L{Repo}
        '''
        for k, v in repo.items():
            if v:
                self.parser.set(repo.id, k, v)
            else:
                self.parser.remove_option(repo.id, k)

    def _parser_to_repo(self, repo_name):
        '''
        Utility for converting the config parser section into a repo object.

        @param repo_name: name of the repo being retrieved from the store
        @type  repo_name: string

        @return: repo instance populated from the section data
        @rtype:  L{Repo}
        '''
        repo = Repo(repo_name)
        for key, value in self.parser.items(repo_name):
            repo[key] = value

        return repo