def gather(self):
        # Config has lowest precedence
        http_proxy = self.http_proxy
        https_proxy = self.https_proxy

        # Gconf has higher precedence
        proxy = self.proxy
        if proxy.get("use_http_proxy", False):
            if proxy.get("use_authentication", False):
                http_proxy = "http://%s:%s@%s:%s" % (
                    proxy["authentication_user"],
                    proxy["authentication_password"],
                    proxy["host"],
                    proxy["port"],
                )
            elif "host" in proxy:
                http_proxy = "http://%s:%s" % (proxy["host"], proxy["port"])

            if proxy.get("use_same_proxy", False):
                https_proxy = http_proxy
            elif "secure_host" in proxy:
                https_proxy = "https://%s:%s" % (proxy["secure_host"], proxy["secure_port"])

        # Environment has highest precedence
        http_proxy = get_variable("http_proxy", http_proxy)
        https_proxy = get_variable("https_proxy", https_proxy)

        add_variable("http_proxy", http_proxy)
        add_variable("https_proxy", https_proxy)
Exemple #2
0
    def gather(self):
        # Config has lowest precedence
        http_proxy = self.http_proxy
        https_proxy = self.https_proxy

        # Gconf has higher precedence
        proxy = self.proxy
        if proxy.get("use_http_proxy", False):
            if proxy.get("use_authentication", False):
                http_proxy = "http://%s:%s@%s:%s" % (
                    proxy["authentication_user"],
                    proxy["authentication_password"], proxy["host"],
                    proxy["port"])
            elif "host" in proxy:
                http_proxy = "http://%s:%s" % (proxy["host"], proxy["port"])

            if proxy.get("use_same_proxy", False):
                https_proxy = http_proxy
            elif "secure_host" in proxy:
                https_proxy = "https://%s:%s" % (proxy["secure_host"],
                                                 proxy["secure_port"])

        # Environment has highest precedence
        http_proxy = get_variable("http_proxy", http_proxy)
        https_proxy = get_variable("https_proxy", https_proxy)

        add_variable("http_proxy", http_proxy)
        add_variable("https_proxy", https_proxy)
    def create_application(self, args=sys.argv):
        # Create data directory
        data_directory = get_variable("CHECKBOX_DATA", ".")
        safe_make_directory(data_directory)

        # Prepend environment options
        string_options = get_variable("CHECKBOX_OPTIONS", "")
        args[:0] = split(string_options)
        (options, args) = self.parse_options(args)

        # Replace shorthands
        for shorthand in "blacklist", "blacklist_file", "whitelist", "whitelist_file":
            key = ".*/jobs_info/%s" % shorthand
            value = getattr(options, shorthand)
            if value:
                options.config.append("=".join([key, value]))

        # Set logging early
        set_logging(options.log_level, options.log)

        # Config setup
        if len(args) != 2:
            sys.stderr.write(_("Missing configuration file as argument.\n"))
            sys.exit(1)

        config = Config()
        config_filename = posixpath.expanduser(args[1])
        config.read_filename(config_filename)
        config.read_configs(options.config)

        section_name = "checkbox/plugins/client_info"
        section = config.get_section(section_name)
        if not section:
            section = config.add_section(section_name)
        section.set("name", posixpath.basename(args[1]) \
            .replace(".ini", ""))
        section.set("version", config.get_defaults().version)

        # Check options
        if options.version:
            print(config.get_defaults().version)
            sys.exit(0)

        return self.application_factory(config)
Exemple #4
0
    def create_application(self, args=sys.argv):
        # Create data directory
        data_directory = get_variable("CHECKBOX_DATA", ".")
        safe_make_directory(data_directory)

        # Prepend environment options
        string_options = get_variable("CHECKBOX_OPTIONS", "")
        args[:0] = split(string_options)
        (options, args) = self.parse_options(args)

        # Replace shorthands
        for shorthand in "blacklist", "blacklist_file", "whitelist", "whitelist_file":
            key = ".*/jobs_info/%s" % shorthand
            value = getattr(options, shorthand)
            if value:
                options.config.append("=".join([key, value]))

        # Set logging early
        set_logging(options.log_level, options.log)

        # Config setup
        if len(args) != 2:
            sys.stderr.write(_("Missing configuration file as argument.\n"))
            sys.exit(1)

        config = Config()
        config_filename = posixpath.expanduser(args[1])
        config.read_filename(config_filename)
        config.read_configs(options.config)

        section_name = "checkbox/plugins/client_info"
        section = config.get_section(section_name)
        if not section:
            section = config.add_section(section_name)
        section.set("name", posixpath.basename(args[1]) \
            .replace(".ini", ""))
        section.set("version", config.get_defaults().version)

        # Check options
        if options.version:
            print(config.get_defaults().version)
            sys.exit(0)

        return self.application_factory(config)
    def show_url(self, url):
        """Open the given URL in a new browser window.

        Display an error dialog if everything fails."""

        # If we are called through sudo, determine the real user id and
        # run the browser with it to get the user's web browser settings.
        try:
            uid = int(get_variable("SUDO_UID"))
            gid = int(get_variable("SUDO_GID"))
            sudo_prefix = ["sudo", "-H", "-u", "#%s" % uid]
        except (TypeError):
            uid = os.getuid()
            gid = None
            sudo_prefix = []

        # if ksmserver is running, try kfmclient
        try:
            if (os.getenv("DISPLAY") and
                    subprocess.call(
                        ["pgrep", "-x", "-u", str(uid), "ksmserver"],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0):
                subprocess.Popen(sudo_prefix + ["kfmclient", "openURL", url])
                return
        except OSError:
            pass

        # if gnome-session is running, try gnome-open; special-case
        # firefox (and more generally, mozilla browsers) and epiphany
        # to open a new window with respectively -new-window and
        # --new-window; special-case chromium-browser to allow file://
        # URLs as needed by the checkbox report.
        try:
            if (os.getenv("DISPLAY") and
                subprocess.call(
                    ["pgrep", "-x", "-u", str(uid), "gnome-panel|gconfd-2"],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0):
                from gi.repository import Gio

                preferred_xml_app = Gio.app_info_get_default_for_type(
                    "application/xml", False)
                if preferred_xml_app:
                    preferred_browser = preferred_xml_app.get_executable()
                    browser = re.match(
                        "((firefox|seamonkey|flock)[^\s]*)",
                        preferred_browser)
                    if browser:
                        subprocess.Popen(
                            sudo_prefix +
                            [browser.group(0), "-new-window", url])
                        return

                    browser = re.match("(epiphany[^\s]*)", preferred_browser)
                    if browser:
                        subprocess.Popen(
                            sudo_prefix +
                            [browser.group(0), "--new-window", url])
                        return

                    browser = re.match(
                        "(chromium-browser[^\s]*)", preferred_browser)
                    if browser:
                        subprocess.Popen(
                            sudo_prefix +
                            [browser.group(0),
                                "--allow-file-access-from-files", url])
                        return

                    subprocess.Popen(
                        sudo_prefix +
                        [preferred_browser % url], shell=True)
                    return

                subprocess.Popen(sudo_prefix + ["gnome-open", url])
                return
        except OSError:
            pass

        # fall back to webbrowser
        if uid and gid:
            os.setgroups([gid])
            os.setgid(gid)
            os.setuid(uid)
            remove_variable("SUDO_USER")
            add_variable("HOME", pwd.getpwuid(uid).pw_dir)

        webbrowser.open(url, new=True, autoraise=True)
        return
    def show_url(self, url):
        """Open the given URL in a new browser window.

        Display an error dialog if everything fails."""

        (r, w) = os.pipe()
        if os.fork() > 0:
            os.close(w)
            (pid, status) = os.wait()
            if status:
                text = _("Unable to start web browser to open %s." % url)
                message = os.fdopen(r).readline()
                if message:
                    text += "\n" + message
                self.show_error(text)
            try:
                os.close(r)
            except OSError:
                pass
            return

        os.setsid()
        os.close(r)

        # If we are called through sudo, determine the real user id and run the
        # browser with it to get the user's web browser settings.
        try:
            uid = int(get_variable("SUDO_UID"))
            gid = int(get_variable("SUDO_GID"))
            sudo_prefix = ["sudo", "-H", "-u", "#%s" % uid]
        except (TypeError):
            uid = os.getuid()
            gid = None
            sudo_prefix = []

        # figure out appropriate web browser
        try:
            # if ksmserver is running, try kfmclient
            try:
                if os.getenv("DISPLAY") and \
                        subprocess.call(["pgrep", "-x", "-u", str(uid), "ksmserver"],
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0:
                    subprocess.call(sudo_prefix + ["kfmclient", "openURL", url])
                    sys.exit(0)
            except OSError:
                pass

            # if gnome-session is running, try gnome-open; special-case firefox
            # (and more generally, mozilla browsers) and epiphany to open a new window
            # with respectively -new-window and --new-window
            try:
                if os.getenv("DISPLAY") and \
                        subprocess.call(["pgrep", "-x", "-u", str(uid), "gnome-panel|gconfd-2"],
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0:
                    gct = subprocess.Popen(sudo_prefix + ["gconftool", "--get",
                        "/desktop/gnome/url-handlers/http/command"],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    if gct.wait() == 0:
                        preferred_browser = gct.communicate()[0]
                        browser = re.match("((firefox|seamonkey|flock)[^\s]*)", preferred_browser)
                        if browser:
                            subprocess.call(sudo_prefix + [browser.group(0), "-new-window", url])
                            sys.exit(0)

                        browser = re.match("(epiphany[^\s]*)", preferred_browser)
                        if browser:
                            subprocess.call(sudo_prefix + [browser.group(0), "--new-window", url])
                            sys.exit(0)

                        subprocess.call(sudo_prefix + [preferred_browser % url], shell=True)
                        sys.exit(0)

                    if subprocess.call(sudo_prefix + ["gnome-open", url]) == 0:
                        sys.exit(0)
            except OSError:
                pass

            # fall back to webbrowser
            if uid and gid:
                os.setgroups([gid])
                os.setgid(gid)
                os.setuid(uid)
                remove_variable("SUDO_USER")
                add_variable("HOME", pwd.getpwuid(uid).pw_dir)

            webbrowser.open(url, new=True, autoraise=True)
            sys.exit(0)

        except Exception, e:
            os.write(w, str(e))
            sys.exit(1)
Exemple #7
0
    def show_url(self, url):
        """Open the given URL in a new browser window.

        Display an error dialog if everything fails."""

        # If we are called through sudo, determine the real user id and
        # run the browser with it to get the user's web browser settings.
        try:
            uid = int(get_variable("SUDO_UID"))
            gid = int(get_variable("SUDO_GID"))
            sudo_prefix = ["sudo", "-H", "-u", "#%s" % uid]
        except (TypeError):
            uid = os.getuid()
            gid = None
            sudo_prefix = []

        # if ksmserver is running, try kfmclient
        try:
            if (os.getenv("DISPLAY") and
                    subprocess.call(
                        ["pgrep", "-x", "-u", str(uid), "ksmserver"],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0):
                subprocess.Popen(sudo_prefix + ["kfmclient", "openURL", url])
                return
        except OSError:
            pass

        # if gnome-session is running, try gnome-open; special-case
        # firefox (and more generally, mozilla browsers) and epiphany
        # to open a new window with respectively -new-window and
        # --new-window; special-case chromium-browser to allow file://
        # URLs as needed by the checkbox report.
        try:
            if (os.getenv("DISPLAY") and
                subprocess.call(
                    ["pgrep", "-x", "-u", str(uid), "gnome-panel|gconfd-2"],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0):
                from gi.repository import Gio

                preferred_xml_app = Gio.app_info_get_default_for_type(
                    "application/xml", False)
                if preferred_xml_app:
                    preferred_browser = preferred_xml_app.get_executable()
                    browser = re.match(
                        "((firefox|seamonkey|flock)[^\s]*)",
                        preferred_browser)
                    if browser:
                        subprocess.Popen(
                            sudo_prefix +
                            [browser.group(0), "-new-window", url])
                        return

                    browser = re.match("(epiphany[^\s]*)", preferred_browser)
                    if browser:
                        subprocess.Popen(
                            sudo_prefix +
                            [browser.group(0), "--new-window", url])
                        return

                    browser = re.match(
                        "(chromium-browser[^\s]*)", preferred_browser)
                    if browser:
                        subprocess.Popen(
                            sudo_prefix +
                            [browser.group(0),
                                "--allow-file-access-from-files", url])
                        return

                    subprocess.Popen(
                        sudo_prefix +
                        [preferred_browser % url], shell=True)
                    return

                subprocess.Popen(sudo_prefix + ["gnome-open", url])
                return
        except OSError:
            pass

        # fall back to webbrowser
        if uid and gid:
            os.setgroups([gid])
            os.setgid(gid)
            os.setuid(uid)
            remove_variable("SUDO_USER")
            add_variable("HOME", pwd.getpwuid(uid).pw_dir)

        webbrowser.open(url, new=True, autoraise=True)
        return
 def prompt_begin(self, interface):
     if get_variable("UPSTART_JOB") and not self.enable:
         self._manager.reactor.stop_all()
Exemple #9
0
class ApplicationManager:

    application_factory = Application

    default_log = os.path.join(get_variable("CHECKBOX_DATA", "."),
                               "checkbox.log")
    default_log_level = "info"

    def parse_options(self, args):
        usage = _("Usage: checkbox [OPTIONS]")
        parser = OptionParser(usage=usage)
        parser.add_option("--version",
                          action="store_true",
                          help=_("Print version information and exit."))
        parser.add_option("-l",
                          "--log",
                          metavar="FILE",
                          default=self.default_log,
                          help=_("The file to write the log to."))
        parser.add_option(
            "--log-level",
            default=self.default_log_level,
            help=_("One of debug, info, warning, error or critical."))
        parser.add_option("-c",
                          "--config",
                          action="append",
                          type="string",
                          default=[],
                          help=_("Configuration override parameters."))
        parser.add_option(
            "-b",
            "--blacklist",
            help=_("Shorthand for --config=.*/jobs_info/blacklist."))
        parser.add_option(
            "-B",
            "--blacklist-file",
            help=_("Shorthand for --config=.*/jobs_info/blacklist_file."))
        parser.add_option(
            "-w",
            "--whitelist",
            help=_("Shorthand for --config=.*/jobs_info/whitelist."))
        parser.add_option(
            "-W",
            "--whitelist-file",
            help=_("Shorthand for --config=.*/jobs_info/whitelist_file."))
        return parser.parse_args(args)

    def create_application(self, args=sys.argv):
        # Create data directory
        data_directory = get_variable("CHECKBOX_DATA", ".")
        safe_make_directory(data_directory)

        # Prepend environment options
        string_options = get_variable("CHECKBOX_OPTIONS", "")
        args[:0] = split(string_options)
        (options, args) = self.parse_options(args)

        # Replace shorthands
        for shorthand in "blacklist", "blacklist_file", "whitelist", "whitelist_file":
            key = ".*/jobs_info/%s" % shorthand
            value = getattr(options, shorthand)
            if value:
                options.config.append("=".join([key, value]))

        # Set logging early
        set_logging(options.log_level, options.log)

        # Config setup
        if len(args) != 2:
            sys.stderr.write(_("Missing configuration file as argument.\n"))
            sys.exit(1)

        config = Config()
        config_filename = posixpath.expanduser(args[1])
        config.read_filename(config_filename)
        config.read_configs(options.config)

        section_name = "checkbox/plugins/client_info"
        section = config.get_section(section_name)
        if not section:
            section = config.add_section(section_name)
        section.set("name", posixpath.basename(args[1]) \
            .replace(".ini", ""))
        section.set("version", config.get_defaults().version)

        # Check options
        if options.version:
            print(config.get_defaults().version)
            sys.exit(0)

        return self.application_factory(config)