Esempio n. 1
0
def deactivate_repository(directory):
    """
    Deactivate a trivial Debian package repository that was previously
    activated using :py:func:`activate_repository()`.

    :param directory: The pathname of a directory with ``*.deb`` packages.

    .. warning:: This function requires ``root`` privileges to:

                 1. delete a ``*.list`` file in ``/etc/apt/sources.list.d`` and
                 2. run ``apt-get update``.

                 This function will use ``sudo`` to gain ``root`` privileges
                 when it's not already running as ``root``.
    """
    directory = os.path.realpath(directory)
    logger.debug("Deactivating repository: %s", format_path(directory))
    # Remove the `sources.list' file.
    sources_file = os.path.join('/etc/apt/sources.list.d',
                                '%s.list' % sha1(directory))
    logger.debug("Removing file: %s", sources_file)
    execute('rm', '-f', sources_file, sudo=ALLOW_SUDO, logger=logger)
    # Update the package list (cleanup).
    logger.debug("Updating package list ..")
    execute("apt-get update", sudo=ALLOW_SUDO, logger=logger)
Esempio n. 2
0
def activate_repository(directory, gpg_key=None):
    """
    Activate a local trivial repository.

    :param directory: The pathname of a directory with ``*.deb`` packages.
    :param gpg_key: The :class:`.GPGKey` object used to sign the repository.
                    Defaults to the result of :func:`select_gpg_key()`.

    This function sets everything up so that a trivial Debian package
    repository can be used to install packages without a webserver. This uses
    the ``file://`` URL scheme to point :man:`apt-get` to a directory on the
    local file system.

    .. warning:: This function requires ``root`` privileges to:

                 1. create the directory ``/etc/apt/sources.list.d``,
                 2. create a ``*.list`` file in ``/etc/apt/sources.list.d`` and
                 3. run ``apt-get update``.

                 This function will use :man:`sudo` to gain ``root`` privileges
                 when it's not already running as ``root``.

    .. seealso:: :data:`ALLOW_SUDO`
    """
    directory = os.path.realpath(directory)
    logger.debug("Activating repository: %s", format_path(directory))
    # Generate the `sources.list' file.
    sources_directory = '/etc/apt/sources.list.d'
    execute('mkdir', '-p', sources_directory, sudo=ALLOW_SUDO, logger=logger)
    sources_file = os.path.join(sources_directory, '%s.list' % sha1(directory))
    logger.debug("Generating file: %s", sources_file)
    sources_entry = ['deb']
    if apt_supports_trusted_option():
        sources_entry.append('[trusted=yes]')
    sources_entry.append('file://%s' % directory)
    sources_entry.append('./')
    command = "echo {text} > {file}"
    execute(command.format(text=pipes.quote(' '.join(sources_entry)),
                           file=pipes.quote(sources_file)),
            sudo=ALLOW_SUDO,
            logger=logger)
    # Make apt-get accept the repository signing key?
    gpg_key = gpg_key or select_gpg_key(directory)
    if gpg_key:
        logger.info("Installing GPG key for automatic signing ..")
        initialize_gnupg()
        command = '{gpg} --armor --export | apt-key add -'
        execute(command.format(gpg=gpg_key.gpg_command),
                sudo=ALLOW_SUDO,
                logger=logger)
    # Update the package list (make sure it works).
    logger.debug("Updating package list ..")
    execute("apt-get update", sudo=ALLOW_SUDO, logger=logger)
Esempio n. 3
0
def activate_repository(directory, gpg_key=None):
    """
    Activate a local trivial repository.

    :param directory: The pathname of a directory with ``*.deb`` packages.
    :param gpg_key: The :class:`.GPGKey` object used to sign the repository.
                    Defaults to the result of :func:`select_gpg_key()`.

    This function sets everything up so that a trivial Debian package
    repository can be used to install packages without a webserver. This uses
    the ``file://`` URL scheme to point ``apt-get`` to a directory on the local
    file system.

    .. warning:: This function requires ``root`` privileges to:

                 1. create the directory ``/etc/apt/sources.list.d``,
                 2. create a ``*.list`` file in ``/etc/apt/sources.list.d`` and
                 3. run ``apt-get update``.

                 This function will use ``sudo`` to gain ``root`` privileges
                 when it's not already running as ``root``.
    """
    directory = os.path.realpath(directory)
    logger.debug("Activating repository: %s", format_path(directory))
    # Generate the `sources.list' file.
    sources_directory = '/etc/apt/sources.list.d'
    execute('mkdir', '-p', sources_directory, sudo=ALLOW_SUDO, logger=logger)
    sources_file = os.path.join(sources_directory, '%s.list' % sha1(directory))
    logger.debug("Generating file: %s", sources_file)
    sources_entry = ['deb']
    if apt_supports_trusted_option():
        sources_entry.append('[trusted=yes]')
    sources_entry.append('file://%s' % directory)
    sources_entry.append('./')
    command = "echo {text} > {file}"
    execute(command.format(text=pipes.quote(' '.join(sources_entry)),
                           file=pipes.quote(sources_file)),
            sudo=ALLOW_SUDO, logger=logger)
    # Make apt-get accept the repository signing key?
    gpg_key = gpg_key or select_gpg_key(directory)
    if gpg_key:
        logger.info("Installing GPG key for automatic signing ..")
        initialize_gnupg()
        command = '{gpg} --armor --export | apt-key add -'
        execute(command.format(gpg=gpg_key.gpg_command), sudo=ALLOW_SUDO, logger=logger)
    # Update the package list (make sure it works).
    logger.debug("Updating package list ..")
    execute("apt-get update", sudo=ALLOW_SUDO, logger=logger)
Esempio n. 4
0
    def __init__(self, cache, category, pathname):
        """
        Initialize a :class:`CacheEntry` object.

        :param cache: The :class:`PackageCache` that created this entry.
        :param category: The type of metadata that this cache entry represents
                         (a string like 'control-fields', 'package-fields' or
                         'contents').
        :param pathname: The pathname of the package archive (a string).
        """
        # Store the arguments.
        self.cache = cache
        self.category = category
        self.pathname = pathname
        # Generate the entry's cache key and filename.
        fingerprint = sha1(pathname)
        self.cache_key = 'deb-pkg-tools:%s:%s' % (category, fingerprint)
        self.cache_file = os.path.join(self.cache.directory, category, '%s.pickle' % fingerprint)
        # Get the archive's last modified time.
        self.last_modified = os.path.getmtime(pathname)
        # Prepare to cache the value in memory.
        self.in_memory = None
Esempio n. 5
0
def deactivate_repository(directory):
    """
    Deactivate a local repository that was previously activated using :func:`activate_repository()`.

    :param directory: The pathname of a directory with ``*.deb`` packages.

    .. warning:: This function requires ``root`` privileges to:

                 1. delete a ``*.list`` file in ``/etc/apt/sources.list.d`` and
                 2. run ``apt-get update``.

                 This function will use ``sudo`` to gain ``root`` privileges
                 when it's not already running as ``root``.
    """
    directory = os.path.realpath(directory)
    logger.debug("Deactivating repository: %s", format_path(directory))
    # Remove the `sources.list' file.
    sources_file = os.path.join('/etc/apt/sources.list.d', '%s.list' % sha1(directory))
    logger.debug("Removing file: %s", sources_file)
    execute('rm', '-f', sources_file, sudo=ALLOW_SUDO, logger=logger)
    # Update the package list (cleanup).
    logger.debug("Updating package list ..")
    execute("apt-get update", sudo=ALLOW_SUDO, logger=logger)