Esempio n. 1
0
    def __init__(self, generic_package_list):
        """Initialize and download/retrieve from cache distro specific package names."""
        self.generic_package_list = generic_package_list
        self.specific_packages = []

        for package in generic_package_list:
            cache_dir = path.join(path.expanduser("~"), ".unixpackage")
            if not path.exists(cache_dir):
                os.makedirs(cache_dir)

            cache_filename = path.join(cache_dir, "{0}.json".format(package))
            if path.exists(cache_filename):
                package_equivalents = utils.get_json_from_file(cache_filename)
            else:
                utils.log((
                    "Downloading and caching correct package names for: {0}...\n"
                ).format(package))

                url = "https://unixpackage.github.io/{0}.json".format(package)
                contents = utils.get_request(url)

                if contents is None:
                    raise exceptions.PackageNotFound(package, url)
                else:
                    package_equivalents = json.loads(contents)
                    utils.save_json_to_file(cache_filename,
                                            package_equivalents)

            specific_package_equivalent = self.get_specific_package(
                package_equivalents)

            if specific_package_equivalent is not None:
                if type(specific_package_equivalent) is list:
                    self.specific_packages.extend(specific_package_equivalent)
                elif utils.is_string(specific_package_equivalent):
                    self.specific_packages.append(specific_package_equivalent)
                elif specific_package_equivalent is None:
                    pass
                else:
                    raise PackageDescriptionNotUnderstood(package)
    def __init__(self, generic_package_list):
        """Initialize and download/retrieve from cache distro specific package names."""
        self.generic_package_list = generic_package_list
        self.specific_packages = []

        for package in generic_package_list:
            cache_dir = path.join(path.expanduser("~"), ".unixpackage")
            if not path.exists(cache_dir):
                os.makedirs(cache_dir)

            cache_filename = path.join(cache_dir, "{0}.json".format(package))
            if path.exists(cache_filename):
                package_equivalents = utils.get_json_from_file(cache_filename)
            else:
                utils.log((
                    "Downloading and caching correct package names for: {0}...\n"
                ).format(package))

                url = "https://unixpackage.github.io/{0}.json".format(package)
                contents = utils.get_request(url)

                if contents is None:
                    raise exceptions.PackageNotFound(package, url)
                else:
                    package_equivalents = json.loads(contents)
                    utils.save_json_to_file(cache_filename, package_equivalents)

            specific_package_equivalent = self.get_specific_package(package_equivalents)

            if specific_package_equivalent is not None:
                if type(specific_package_equivalent) is list:
                    self.specific_packages.extend(specific_package_equivalent)
                elif utils.is_string(specific_package_equivalent):
                    self.specific_packages.append(specific_package_equivalent)
                elif specific_package_equivalent is None:
                    pass
                else:
                    raise PackageDescriptionNotUnderstood(package)
Esempio n. 3
0
def install(generic_packages, polite=False):
    """Attempt installation of specified packages (if not already installed)."""
    if len(generic_packages) == 0:
        raise exceptions.NoPackagesSpecified()
    if len(generic_packages) > 10:
        install(generic_packages[10:])
        generic_packages = generic_packages[:10]

    package_group = package_group_for_my_distro()(generic_packages)

    if not package_group.check():
        not_preinstalled = package_group.not_installed()
        install_cmd = not_preinstalled.install_cmd()
        if package_group.need_sudo and polite:
            previous_signal = signal.getsignal(signal.SIGINT)
            signal.signal(signal.SIGINT, signal.SIG_IGN)
            log((
                "The following command must be run to continue. I am attempting to run it now:"
                "\n\n\n       {0}\n\n\n"
                "You can also run this command in another window "
                "and then hit Ctrl-C to continue.\n\n"
            ).format(install_cmd))
        try:
            check_call(install_cmd, shell=True)
        except exceptions.CalledProcessError:
            warn("\nWARNING : Command '{0}' returned error code\n\n".format(install_cmd))
        if package_group.need_sudo and polite:
            signal.signal(signal.SIGINT, previous_signal)

        # Double check that the packages were correctly installed
        not_installed_after_install_cmd = not_preinstalled.not_installed()

        if not not_installed_after_install_cmd.empty():
            # Throw meaningful error with the command to re-run to install packages
            raise exceptions.PackageInstallationFailed(
                not_installed_after_install_cmd.generic_package_list,
                not_installed_after_install_cmd.install_cmd()
            )
        else:
            log("Post-install package check for {0} successful!\n".format(
                ", ".join(not_preinstalled.generic_package_list)
            ))
    else:
        log("Already installed: {0}\n".format(', '.join(generic_packages)))
Esempio n. 4
0
def install(generic_packages, polite=False):
    """Attempt installation of specified packages (if not already installed)."""
    if len(generic_packages) == 0:
        raise exceptions.NoPackagesSpecified()
    if len(generic_packages) > 10:
        install(generic_packages[10:], polite=polite)
        generic_packages = generic_packages[:10]

    package_group = package_group_for_my_distro()(generic_packages)

    if not package_group.check():
        not_preinstalled = package_group.not_installed()
        install_cmd = not_preinstalled.install_cmd()
        if package_group.need_sudo and polite:
            previous_signal = signal.getsignal(signal.SIGINT)
            signal.signal(signal.SIGINT, signal.SIG_IGN)
            log((
                "The following command must be run to continue. I am attempting to run it now:"
                "\n\n\n       {0}\n\n\n"
                "You can also run this command in another window "
                "and then hit Ctrl-C to continue.\n\n").format(install_cmd))
        try:
            check_call(install_cmd, shell=True)
        except exceptions.CalledProcessError:
            warn("\nWARNING : Command '{0}' returned error code\n\n".format(
                install_cmd))
        if package_group.need_sudo and polite:
            signal.signal(signal.SIGINT, previous_signal)

        # Double check that the packages were correctly installed
        not_installed_after_install_cmd = not_preinstalled.not_installed()

        if not not_installed_after_install_cmd.empty():
            # Throw meaningful error with the command to re-run to install packages
            raise exceptions.PackageInstallationFailed(
                not_installed_after_install_cmd.generic_package_list,
                not_installed_after_install_cmd.install_cmd())
        else:
            log("Post-install package check for {0} successful!\n".format(
                ", ".join(not_preinstalled.generic_package_list)))
    else:
        log("Already installed: {0}\n".format(', '.join(generic_packages)))