Example #1
0
def format_http_put(requst, ledmlen, xmldata, content_type="text/xml; charset=utf-8"):
    host = 'localhost'
    return  utils.cat(
"""PUT $requst HTTP/1.1\r
Host: $host\r
User-Agent: hplip/3.0\r
Content-Type: $content_type\r
Content-Length: $ledmlen\r
\r
$xmldata""")    
def format_http_put(requst,
                    ledmlen,
                    xmldata,
                    content_type="text/xml; charset=utf-8"):
    host = 'localhost'
    return utils.cat("""PUT $requst HTTP/1.1\r
Host: $host\r
User-Agent: hplip/3.0\r
Content-Type: $content_type\r
Content-Length: $ledmlen\r
\r
$xmldata""")
Example #3
0
    def format_http(self, soap, content_type="text/xml; charset=utf-8"):
        host = self.http_host
        soap_len = len(soap)

        return (utils.cat("""POST / HTTP/1.1\r
Host: $host\r
User-Agent: hplip/2.0\r
Content-Type: $content_type\r
Content-Length: $soap_len\r
Connection: close\r
SOAPAction: ""\r
\r\n""")).encode('utf-8') + soap
Example #4
0
    def format_http(self, soap, content_type="text/xml; charset=utf-8"):
        host = self.http_host
        soap_len = len(soap)

        return (utils.cat(
"""POST / HTTP/1.1\r
Host: $host\r
User-Agent: hplip/2.0\r
Content-Type: $content_type\r
Content-Length: $soap_len\r
Connection: close\r
SOAPAction: ""\r
\r\n""")).encode('utf-8') + soap
Example #5
0
    def format_http_post(self, requst, ledmlen, xmldata, content_type="text/xml; charset=utf-8"):
        host = self.http_host      
        
        return utils.cat(
"""POST $requst HTTP/1.1\r
Host: $host\r
User-Agent: hplip/2.0\r
Content-Type: $content_type\r
Content-Length: $ledmlen\r
Connection: Keep-alive\r
SOAPAction: ""\r
\r
$xmldata""")
Example #6
0
    def format_http_post(self,
                         requst,
                         ledmlen,
                         xmldata,
                         content_type="text/xml; charset=utf-8"):
        host = self.http_host

        return utils.cat("""POST $requst HTTP/1.1\r
Host: $host\r
User-Agent: hplip/2.0\r
Content-Type: $content_type\r
Content-Length: $ledmlen\r
Connection: Keep-alive\r
SOAPAction: ""\r
\r
$xmldata""")
Example #7
0
    def __getPluginFilesList(self, src_dir):
        if not os.path.exists(src_dir + "/plugin.spec"):
            log.warn("%s/plugin.spec file doesn't exists." % src_dir)
            return []

        cwd = os.getcwd()
        os.chdir(src_dir)

        plugin_spec = ConfigBase("plugin.spec")
        products = plugin_spec.keys("products")

        BITNESS = utils.getBitness()
        ENDIAN = utils.getEndian()
        PPDDIR = sys_conf.get('dirs', 'ppd')
        DRVDIR = sys_conf.get('dirs', 'drv')
        HOMEDIR = sys_conf.get('dirs', 'home')
        DOCDIR = sys_conf.get('dirs', 'doc')
        CUPSBACKENDDIR = sys_conf.get('dirs', 'cupsbackend')
        CUPSFILTERDIR = sys_conf.get('dirs', 'cupsfilter')
        RULESDIR = '/etc/udev/rules.d'
        BIN = sys_conf.get('dirs', 'bin')

        # Copying plugin.spec file to home dir.
        if src_dir != HOMEDIR:
            shutil.copyfile(src_dir + "/plugin.spec", HOMEDIR + "/plugin.spec")
            os.chmod(HOMEDIR + "/plugin.spec", 0o644)

        processor = utils.getProcessor()
        if processor == 'power_machintosh':
            ARCH = 'ppc'
        elif (processor == 'armv6l' or processor == 'armv7l'
              or processor == 'aarch64' or processor == 'aarch32'):
            ARCH = 'arm%d' % BITNESS
        else:
            ARCH = 'x86_%d' % BITNESS

        if BITNESS == 64:
            SANELIBDIR = '/usr/lib64/sane'
            LIBDIR = '/usr/lib64'
        else:
            SANELIBDIR = '/usr/lib/sane'
            LIBDIR = '/usr/lib'

        copies = []

        for PRODUCT in products:
            MODEL = PRODUCT.replace('hp-', '').replace('hp_', '')
            for s in plugin_spec.get("products", PRODUCT).split(','):

                if not plugin_spec.has_section(s):
                    log.error("Missing section [%s]" % s)
                    os.chdir(cwd)
                    return []

                src = plugin_spec.get(s, 'src', '')
                trg = plugin_spec.get(s, 'trg', '')
                link = plugin_spec.get(s, 'link', '')

                if not src:
                    log.error("Missing 'src=' value in section [%s]" % s)
                    os.chdir(cwd)
                    return []

                if not trg:
                    log.error("Missing 'trg=' value in section [%s]" % s)
                    os.chdir(cwd)
                    return []

                src = os.path.basename(utils.cat(src))
                trg = utils.cat(trg)

                if link:
                    link = utils.cat(link)

                copies.append((src, trg, link))

        copies = utils.uniqueList(copies)
        copies.sort()
        os.chdir(cwd)
        return copies
Example #8
0
def copyPluginFiles(src_dir):
    os.chdir(src_dir)

    plugin_spec = ConfigBase("plugin.spec")
    products = plugin_spec.keys("products")

    BITNESS = utils.getBitness()
    ENDIAN = utils.getEndian()
    PPDDIR = sys_conf.get('dirs', 'ppd')
    DRVDIR = sys_conf.get('dirs', 'drv')
    HOMEDIR = sys_conf.get('dirs', 'home')
    DOCDIR = sys_conf.get('dirs', 'doc')
    CUPSBACKENDDIR = sys_conf.get('dirs', 'cupsbackend')
    CUPSFILTERDIR = sys_conf.get('dirs', 'cupsfilter')
    RULESDIR = '/etc/udev/rules.d'

    processor = utils.getProcessor()
    if processor == 'power_machintosh':
        ARCH = 'ppc'
    else:
        ARCH = 'x86_%d' % BITNESS

    if BITNESS == 64:
        SANELIBDIR = '/usr/lib64/sane'
        LIBDIR = '/usr/lib64'
    else:
        SANELIBDIR = '/usr/lib/sane'
        LIBDIR = '/usr/lib'

    copies = []

    for PRODUCT in products:
        MODEL = PRODUCT.replace('hp-', '').replace('hp_', '')
        UDEV_SYSFS_RULES=sys_conf.get('configure','udev_sysfs_rules','no')
        for s in plugin_spec.get("products", PRODUCT).split(','):

            if not plugin_spec.has_section(s):
                log.error("Missing section [%s]" % s)
                return False

            src = plugin_spec.get(s, 'src', '')
            trg = plugin_spec.get(s, 'trg', '')
            link = plugin_spec.get(s, 'link', '')

           # In Cent os 5.x distro's SYSFS attribute will be used. and Other distro's uses ATTR/ATTRS attribute in rules. 
           # Following condition to check this...
            if UDEV_SYSFS_RULES == 'no' and 'sysfs' in src:
                continue
            if UDEV_SYSFS_RULES == 'yes' and 'sysfs' not in src:
                continue

            if not src:
                log.error("Missing 'src=' value in section [%s]" % s)
                return False

            if not trg:
                log.error("Missing 'trg=' value in section [%s]" % s)
                return False

            src = os.path.basename(utils.cat(src))
            trg = utils.cat(trg)

            if link:
                link = utils.cat(link)

            copies.append((src, trg, link))

    copies = utils.uniqueList(copies)
    copies.sort()

    os.umask(0)

    for src, trg, link in copies:

        if not os.path.exists(src):
            log.debug("Source file %s does not exist. Skipping." % src)
            continue

        if os.path.exists(trg):
            log.debug("Target file %s already exists. Replacing." % trg)
            os.remove(trg)

        trg_dir = os.path.dirname(trg)

        if not os.path.exists(trg_dir):
            log.debug("Target directory %s does not exist. Creating." % trg_dir)
            os.makedirs(trg_dir, 0755)

        if not os.path.isdir(trg_dir):
            log.error("Target directory %s exists but is not a directory. Skipping." % trg_dir)
            continue

        try:
            shutil.copyfile(src, trg)
        except (IOError, OSError), e:
            log.error("File copy failed: %s" % e.strerror)
            continue

        else:
            if not os.path.exists(trg):
                log.error("Target file %s does not exist. File copy failed." % trg)
                continue
            else:
                os.chmod(trg, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH)

            if link:
                if os.path.exists(link):
                    log.debug("Symlink already exists. Replacing.")
                    os.remove(link)

                log.debug("Creating symlink %s (link) to file %s (target)..." %
                    (link, trg))

                try:
                    os.symlink(trg, link)
                except (OSError, IOError), e:
                    log.debug("Unable to create symlink: %s" % e.strerror)
                    pass
Example #9
0
                                   vert_dpi, page_size, resolution, encoding))

                        job_id = self.job_id
                        delay = 0
                        faxnum = recipient['fax'].encode('ascii')
                        speeddial = 0

                        if resolution == RESOLUTION_STD:
                            res = "STANDARD"
                        elif resolution == RESOLUTION_FINE:
                            res = "FINE"
                        elif resolution == RESOLUTION_300DPI:
                            res = "SUPERFINE"

                        soap = utils.cat(
                            """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><Fax:BeginJob xmlns:Fax="urn:Fax"><ticket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Fax:Ticket"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$job_id</jobId><resolution xsi:type="Fax:Resolution">$res</resolution><delay xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$delay</delay><phoneNumber xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$faxnum</phoneNumber><speedDial xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$speeddial</speedDial></ticket></Fax:BeginJob></SOAP-ENV:Body></SOAP-ENV:Envelope>"""
                        )

                        data = self.format_http(soap)
                        log.log_data(data)

                        if log.is_debug():
                            file('beginjob.log', 'w').write(data)

                        self.dev.openSoapFax()
                        self.dev.writeSoapFax(data)
                        ret = cStringIO.StringIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass
Example #10
0
def start(language,
          auto=True,
          test_depends=False,
          test_unknown=False,
          assume_network=False,
          max_retries=3,
          enable=None,
          disable=None):
    try:
        core = CoreInstall(MODE_INSTALLER, INTERACTIVE_MODE)
        current_version = prop.installed_version_int
        log.debug("Currently installed version: 0x%06x" % current_version)
        core.enable = enable
        core.disable = disable

        if services.running_as_root():
            log.error(
                "You are running the installer as root. It is highly recommended that you run the installer as"
            )
            log.error(
                "a regular (non-root) user. Do you still wish to continue?")

            ok, ans = tui.enter_yes_no(log.bold("Continue with installation"),
                                       'n')
            if not ans or not ok:
                sys.exit(1)

        if auto:
            log.note(
                "Running in automatic mode. The most common options will be selected."
            )

        log.info("")
        log.note(
            "Defaults for each question are maked with a '*'. Press <enter> to accept the default."
        )
        core.init()
        vrs = core.get_distro_data('versions_list')
        Is_Manual_Distro = False
        distro_alternate_version = None
        if core.distro_version not in vrs and len(vrs):
            distro_alternate_version = vrs[len(vrs) - 1]
            if core.is_auto_installer_support(distro_alternate_version):
                log.error("%s-%s version is not supported, so all dependencies may not be installed. However trying to install using %s-%s version packages." \
                                                   %(core.distro_name, core.distro_version, core.distro_name, distro_alternate_version))
                ok, choice = tui.enter_choice(
                    "\nPress 'y' to continue auto installation. Press 'n' to quit auto instalation(y=yes, n=no*): ",
                    ['y', 'n'], 'n')
                if not ok or choice == 'n':
                    log.info("Installation exit")
                    sys.exit()
            else:
                # Even previous distro is not supported
                Is_Manual_Distro = True
        elif not core.is_auto_installer_support():
            # This distro is not supported
            Is_Manual_Distro = True

        if Is_Manual_Distro:
            log.error(
                "Auto installation is not supported for '%s' distro so all dependencies may not be installed. \nPlease install manually as mentioned in 'http://hplipopensource.com/hplip-web/install/manual/index.html' web-site"
                % core.distro_name)
            ok, choice = tui.enter_choice(
                "\nPress 'y' to continue auto installation. Press 'n' to quit auto instalation(y=yes, n=no*): ",
                ['y', 'n'], 'n')
            if not ok or choice == 'n':
                log.info("Installation exit")
                sys.exit()

        if not auto:
            tui.title("INSTALLATION MODE")
            log.info(
                "Automatic mode will install the full HPLIP solution with the most common options."
            )
            log.info(
                "Custom mode allows you to choose installation options to fit specific requirements."
            )

            #if os.getenv('DISPLAY') and utils.find_browser() is not None:
            if 0:
                ok, choice = tui.enter_choice(
                    "\nPlease choose the installation mode (a=automatic*, c=custom, w=web installer, q=quit) : ",
                    ['a', 'c', 'w'], 'a')
            else:
                ok, choice = tui.enter_choice(
                    "\nPlease choose the installation mode (a=automatic*, c=custom, q=quit) : ",
                    ['a', 'c'], 'a')

            if not ok: sys.exit(0)

            if choice == 'a':
                auto = True

            elif choice == 'w':
                import web_install
                log.debug("Starting web browser installer...")
                web_install.start(language)
                return

        log.info("\nInitializing. Please wait...")
        prev_hplip_version = sys_conf.get("hplip", "version", "0.0.0")
        pluginObj = pluginhandler.PluginHandle()
        prev_hplip_plugin_status = pluginObj.getStatus()

        if test_unknown:
            core.distro_name = 'unknown'
            core.distro = 0
            core.distro_version = 0

        #
        # HPLIP INSTALLATION
        #
        core.selected_component = 'hplip'

        #
        # INTRODUCTION
        #

        tui.title("INTRODUCTION")
        if core.selected_component == 'hplip':
            log.info(
                "This installer will install HPLIP version %s on your computer."
                % core.version_public)

        log.info(
            "Please close any running package management systems now (YaST, Adept, Synaptic, Up2date, etc)."
        )

        # For testing, mark all dependencies missing
        if test_depends:
            for d in core.have_dependencies:
                core.have_dependencies[d] = False

        num_req_missing = core.count_num_required_missing_dependencies()
        num_opt_missing = core.count_num_optional_missing_dependencies()

        #
        # CONFIRM AND SELECT DISTRO NAME AND VERSION
        #

        tui.title("DISTRO/OS CONFIRMATION")

        if core.distro_known():
            log.info("Distro appears to be %s %s.\n" % (core.get_distro_data(
                'display_name', '(unknown)'), core.distro_version))

        log.debug("Distro = %s Distro Name = %s Display Name= %s Version = %s Supported = %s" % \
            (core.distro, core.distro_name, core.distros[core.distro_name]['display_name'], \
            core.distro_version, core.distro_version_supported))

        distro_ok, ok = False, True
        if core.distro_known():
            ok, distro_ok = tui.enter_yes_no(
                'Is "%s %s" your correct distro/OS and version' %
                (core.get_distro_data('display_name',
                                      '(unknown)'), core.distro_version))

        if not ok:
            sys.exit(0)

        if distro_alternate_version:
            core.distro_version = distro_alternate_version

        core.distro_changed()
        if not distro_ok:
            tui.title("DISTRO/OS SELECTION")
            core.distro, core.distro_version = DISTRO_UNKNOWN, DISTRO_VER_UNKNOWN

            log.info(
                log.bold(
                    "\nChoose the name of the distro/OS that most closely matches your system:\n"
                ))

            max_name = 0
            for d in core.distros_index:
                dd = core.distros[core.distros_index[d]]
                if dd['display']:
                    max_name = max(max_name, len(dd['display_name']))

            formatter = utils.TextFormatter((
                {
                    'width': 4
                },
                {
                    'width': max_name,
                    'margin': 2
                },
            ))

            log.info(formatter.compose(("Num.", "Distro/OS Name")))
            log.info(formatter.compose(('-' * 4, '-' * (max_name))))

            d_temp = {}
            x = 0
            for d in core.distros_index:
                dd = core.distros[core.distros_index[d]]

                if dd['display']:
                    d_temp[x] = d
                    log.info(formatter.compose((str(x), dd['display_name'])))
                    x += 1

            ok, y = tui.enter_range(
                "\nEnter number 0...%d (q=quit) ?" % (x - 1), 0, x - 1)
            if not ok: sys.exit(0)

            core.distro = d_temp[y]
            core.distro_name = core.distros_index[core.distro]
            distro_display_name = core.distros[
                core.distro_name]['display_name']
            log.debug("Distro = %s Distro Name = %s Display Name= %s" %
                      (core.distro, core.distro_name, distro_display_name))

            if core.distro != DISTRO_UNKNOWN:
                versions = core.distros[core.distro_name]['versions'].keys()
                versions.sort(lambda x, y: utils.compare(x, y))

                log.info(
                    log.bold(
                        '\nChoose the version of "%s" that most closely matches your system:\n'
                        % distro_display_name))
                formatter = utils.TextFormatter((
                    {
                        'width': 4
                    },
                    {
                        'width': 40,
                        'margin': 2
                    },
                ))

                log.info(formatter.compose(("Num.", "Distro/OS Version")))
                log.info(formatter.compose(('-' * 4, '-' * 40)))

                log.info(formatter.compose(("0", "Unknown or not listed")))

                x = 1
                for ver in versions:
                    ver_info = core.distros[core.distro_name]['versions'][ver]

                    if ver_info['code_name'] and ver_info['release_date']:
                        text = ver + ' ("' + ver_info[
                            'code_name'] + '", Released ' + ver_info[
                                'release_date'] + ')'

                    elif ver_info['code_name']:
                        text = ver + ' ("' + ver_info['code_name'] + '")'

                    elif ver_info['release_date']:
                        text = ver + ' (Released ' + ver_info[
                            'release_date'] + ')'

                    else:
                        text = ver

                    if not ver_info['supported']:
                        text += " [Unsupported]"

                    log.info(formatter.compose((str(x), text)))
                    x += 1

                ok, core.distro_version_int = tui.enter_range(
                    "\nEnter number 0...%d (q=quit) ?" % (x - 1), 0, x - 1)
                if not ok: sys.exit(0)

                if core.distro_version_int == 0:
                    core.distro_version = DISTRO_VER_UNKNOWN
                    core.distro_version_supported = False
                else:
                    core.distro_version = versions[core.distro_version_int - 1]
                    core.distro_version_supported = core.get_ver_data(
                        'supported', False)

                log.debug("Distro = %s Distro Name = %s Display Name= %s Version = %s Supported = %s" % \
                    (core.distro, core.distro_name, core.distros[core.distro_name]['display_name'], \
                    core.distro_version, core.distro_version_supported))

                core.distro_changed()

                log.info("\nDistro set to: %s %s" % (core.get_distro_data(
                    'display_name', '(unknown)'), core.distro_version))

#            if core.distro == DISTRO_UNKNOWN or not core.distro_version_supported:
            if core.distro == DISTRO_UNKNOWN:
                log.error(
                    "The distribution/OS that you are running is not supported. This installer\ncannot install an unsupported distribution. Please check your distribution/OS\nand re-run this installer or perform a manual installation."
                )
                if num_req_missing:
                    log.error(
                        "The following REQUIRED dependencies are missing and need to be installed:"
                    )

                    for d, desc, opt in core.missing_required_dependencies():
                        log.error("Missing REQUIRED dependency: %s (%s)" %
                                  (d, desc))

                for d, desc, req, opt in core.missing_optional_dependencies():
                    if req:
                        log.warning(
                            "Missing OPTIONAL dependency: %s (%s) [Required for option '%s']"
                            % (d, desc, opt))
                    else:
                        log.warning(
                            "Missing OPTIONAL dependency: %s (%s) [Optional for option '%s']"
                            % (d, desc, opt))

                sys.exit(1)

        #
        # SELECT OPTIONS TO INSTALL
        #

        if not auto:
            tui.title("SELECT HPLIP OPTIONS")
            log.info(
                "You can select which HPLIP options to enable. Some options require extra dependencies."
            )
            log.info("")
            num_opt_missing = core.select_options(option_question_callback)

        else:
            enable_par = False
            core.selected_options['parallel'] = False

        #
        # COLLECT SUPERUSER PASSWORD
        #
        if not services.running_as_root():
            if core.passwordObj.getAuthType() == "sudo":
                tui.title("ENTER USER PASSWORD")
            else:
                tui.title("ENTER ROOT/SUPERUSER PASSWORD")

            ok = core.check_password()
            if not ok:
                log.error(
                    "3 incorrect attempts. (or) Insufficient permissions(i.e. try with sudo user).\nExiting."
                )
                sys.exit(1)

        # INSTALLATION NOTES
        #

        if core.is_auto_installer_support(distro_alternate_version):
            distro_notes = core.get_distro_data('notes', '').strip()
            ver_notes = core.get_ver_data('notes', '',
                                          distro_alternate_version).strip()

            if distro_notes or ver_notes:
                tui.title("INSTALLATION NOTES")

                if distro_notes:
                    log.info(distro_notes)

                if ver_notes:
                    log.info(ver_notes)

                log.info("")

                if not tui.continue_prompt(
                        "Please read the installation notes."):
                    sys.exit(0)

        #
        # PRE-INSTALL COMMANDS
        #
        tui.title("RUNNING PRE-INSTALL COMMANDS")
        if core.run_pre_install(
                progress_callback,
                distro_alternate_version):  # some cmds were run...
            num_req_missing = core.count_num_required_missing_dependencies()
            num_opt_missing = core.count_num_optional_missing_dependencies()
        log.info("OK")

        #
        # REQUIRED DEPENDENCIES INSTALL
        #
        package_mgr_cmd = core.get_distro_data('package_mgr_cmd')
        depends_to_install = []
        if num_req_missing:
            tui.title("INSTALL MISSING REQUIRED DEPENDENCIES")

            log.warn("There are %d missing REQUIRED dependencies." %
                     num_req_missing)
            log.notice(
                "Installation of dependencies requires an active internet connection."
            )

            for depend, desc, option in core.missing_required_dependencies():
                log.warning("Missing REQUIRED dependency: %s (%s)" %
                            (depend, desc))

                ok = False
                packages, commands = core.get_dependency_data(
                    depend, distro_alternate_version)
                log.debug("Packages: %s" % ','.join(packages))
                log.debug("Commands: %s" % ','.join(commands))

                #                if core.distro_version_supported and (packages or commands):
                if package_mgr_cmd and (packages or commands):
                    if auto:
                        answer = True
                    else:
                        ok, answer = tui.enter_yes_no(
                            "\nWould you like to have this installer install the missing dependency"
                        )
                        if not ok: sys.exit(0)

                    if answer:
                        ok = True
                        log.debug(
                            "Adding '%s' to list of dependencies to install." %
                            depend)
                        depends_to_install.append(depend)

                else:
                    log.warn(
                        "This installer cannot install '%s' for your distro/OS and/or version."
                        % depend)

                if not ok:
                    log.error(
                        "Installation cannot continue without this dependency. Please manually install this dependency and re-run this installer."
                    )
                    sys.exit(0)

        #
        # OPTIONAL dependencies
        #
        if num_opt_missing:
            tui.title("INSTALL MISSING OPTIONAL DEPENDENCIES")
            log.warn("There are %d missing OPTIONAL dependencies." %
                     num_opt_missing)

            log.notice(
                "Installation of dependencies requires an active internet connection."
            )

            for depend, desc, required_for_opt, opt in core.missing_optional_dependencies(
            ):

                if required_for_opt:
                    log.warning(
                        "Missing REQUIRED dependency for option '%s': %s (%s)"
                        % (opt, depend, desc))

                else:
                    log.warning(
                        "Missing OPTIONAL dependency for option '%s': %s (%s)"
                        % (opt, depend, desc))

                installed = False
                packages, commands = core.get_dependency_data(
                    depend, distro_alternate_version)
                log.debug("Packages: %s" % ','.join(packages))
                log.debug("Commands: %s" % ','.join(commands))

                #                if core.distro_version_supported and (packages or commands):
                if package_mgr_cmd and (packages or commands):
                    if auto:
                        answer = True
                    else:
                        ok, answer = tui.enter_yes_no(
                            "\nWould you like to have this installer install the missing dependency"
                        )
                        if not ok: sys.exit(0)

                    if answer:
                        log.debug(
                            "Adding '%s' to list of dependencies to install." %
                            depend)
                        depends_to_install.append(depend)

                    else:
                        log.warning(
                            "Missing dependencies may effect the proper functioning of HPLIP. Please manually install this dependency after you exit this installer."
                        )
                        log.warning(
                            "Note: Options that have REQUIRED dependencies that are missing will be turned off."
                        )

                        if required_for_opt:
                            log.warn("Option '%s' has been turned off." % opt)
                            core.selected_options[opt] = False
                else:
                    log.warn(
                        "This installer cannot install '%s' for your distro/OS and/or version."
                        % depend)

                    if required_for_opt:
                        log.warn("Option '%s' has been turned off." % opt)
                        core.selected_options[opt] = False

        log.debug("Dependencies to install: %s  hplip_present:%s" %
                  (depends_to_install, core.hplip_present))

        #        if core.distro_version_supported and \
        #            (depends_to_install or core.hplip_present) and \
        #            core.selected_component == 'hplip':

        if package_mgr_cmd and \
            (depends_to_install or core.hplip_present) and \
            core.selected_component == 'hplip':
            #
            # CHECK FOR RUNNING PACKAGE MANAGER
            #
            User_exit, Is_pkg_mgr_running = core.close_package_managers()
            if User_exit:
                sys.exit(0)
#            if Is_pkg_mgr_running:
#                log.debug("Some Package manager are still running. ")

#
# CHECK FOR ACTIVE NETWORK CONNECTION
#
            if not assume_network:
                tui.title("CHECKING FOR NETWORK CONNECTION")

                if not utils.check_network_connection():
                    log.error(
                        "The network appears to be unreachable. Installation may not resolve all dependencies without access to distribution repositories."
                    )
                    ok, choice = tui.enter_choice(
                        "Do you want to continue installation without network?. Press 'y' for YES. Press 'n' for NO (y=yes*, n=no) : ",
                        ['y', 'n'], 'y')
                    if not ok or choice == 'n':
                        log.info("Please connect network and try again")
                        sys.exit(1)
                    else:
                        log.debug("Continuing installation without network")
                else:
                    log.info("Network connection present.")

            #
            # PRE-DEPEND
            #

            tui.title("RUNNING PRE-PACKAGE COMMANDS")
            core.run_pre_depend(progress_callback, distro_alternate_version)
            log.info("OK")

            #
            # INSTALL PACKAGES AND RUN COMMANDS
            #

            tui.title("DEPENDENCY AND CONFLICT RESOLUTION")

            packages = []
            commands_to_run = []
            package_mgr_cmd = core.get_distro_data('package_mgr_cmd')

            # HACK!
            individual_pkgs = True
            if package_mgr_cmd.startswith('xterm'):
                individual_pkgs = False

            if package_mgr_cmd:
                log.debug("Preparing to install packages and run commands...")

                for d in depends_to_install:
                    log.debug("*** Processing dependency: %s" % d)
                    pkgs, commands = core.get_dependency_data(
                        d, distro_alternate_version)

                    if pkgs:
                        log.debug(
                            "Package(s) '%s' will be installed to satisfy dependency '%s'."
                            % (','.join(pkgs), d))

                        packages.extend(pkgs)

                    if commands:
                        log.debug(
                            "Command(s) '%s' will be run to satisfy dependency '%s'."
                            % (','.join(commands), d))

                        commands_to_run.extend(commands)

            else:
                log.error("Invalid package manager")

            log.debug("Packages: %s" % packages)
            log.debug("Commands: %s" % commands_to_run)
            log.debug("Install individual packages: %s" % individual_pkgs)

            if package_mgr_cmd and packages:
                if individual_pkgs:
                    for packages_to_install in packages:
                        retries = 0
                        while True:
                            cmd = utils.cat(package_mgr_cmd)
                            log.debug("Package manager command: %s" % cmd)

                            log.info(
                                "Running '%s'\nPlease wait, this may take several minutes..."
                                % cmd)
                            status, output = utils.run(cmd, core.passwordObj)

                            if status != 0:
                                retries += 1
                                if retries < (max_retries + 1):
                                    log.error("Command failed. Re-try #%d..." %
                                              retries)
                                    continue

                                log.error(
                                    "Package install command failed with error code %d"
                                    % status)
                                ok, ans = tui.enter_yes_no(
                                    "Would you like to retry installing the missing package(s)"
                                )

                                if not ok:
                                    sys.exit(0)

                                if ans:
                                    continue
                                else:
                                    log.warn(
                                        "Some HPLIP functionality might not function due to missing package(s)."
                                    )
                                    break
                            else:
                                break

                else:
                    packages_to_install = ' '.join(packages)
                    while True:
                        cmd = utils.cat(package_mgr_cmd)
                        log.debug("Package manager command: %s" % cmd)

                        log.info(
                            "Running '%s'\nPlease wait, this may take several minutes..."
                            % cmd)
                        status, output = utils.run(cmd, core.passwordObj)

                        if status != 0:
                            log.error(
                                "Package install command failed with error code %d"
                                % status)
                            ok, ans = tui.enter_yes_no(
                                "Would you like to retry installing the missing package(s)"
                            )

                            if not ok:
                                sys.exit(0)

                            if ans:
                                continue
                            else:
                                log.warn(
                                    "Some HPLIP functionality might not function due to missing package(s)."
                                )
                                break
                        else:
                            break

            if commands_to_run:
                for cmd in commands_to_run:
                    log.debug(cmd)
                    log.info(
                        "Running '%s'\nPlease wait, this may take several minutes..."
                        % cmd)
                    status, output = utils.run(cmd, core.passwordObj)

                    if status != 0:
                        log.error("Install command failed with error code %d" %
                                  status)
                        sys.exit(1)

            #
            # HPLIP REMOVE
            #
            num_req_missing = 0
            core.check_dependencies()
            for depend, desc, opt in core.missing_required_dependencies():
                log.error("A required dependency '%s (%s)' is still missing." %
                          (depend, desc))
                num_req_missing += 1

            if num_req_missing == 0 and core.hplip_present and core.selected_component == 'hplip' and core.distro_version_supported:
                path = utils.which('hp-uninstall')
                ok, choice = tui.enter_choice(
                    "HPLIP-%s exists, this may conflict with the new one being installed.\nDo you want to ('i'= Remove and Install*, 'o'= Overwrite, 'q'= Quit)?	:"
                    % (prev_hplip_version), ['i', 'o', 'q'], 'i')
                if not ok or choice == 'q':
                    log.error("User Exit")
                    sys.exit(0)
                elif choice == 'i':
                    #                    log.info("Uninstalling existing HPLIP-%s"%prev_hplip_version)
                    sts = core.uninstall(NON_INTERACTIVE_MODE)

                    if sts is False:
                        log.warn(
                            "Failed to uninstall existing HPLIP-%s. This installation will overwrite on existing HPLIP"
                            % prev_hplip_version)
                    else:
                        log.debug("HPLIP-%s is uninstalled successfully." %
                                  prev_hplip_version)

            #
            # POST-DEPEND
            #
            tui.title("RUNNING POST-PACKAGE COMMANDS")
            core.run_post_depend(progress_callback)
            log.info("OK")

            #
            # DEPENDENCIES RE-CHECK
            #
            tui.title("RE-CHECKING DEPENDENCIES")
            core.check_dependencies()

            num_req_missing = 0
            for depend, desc, opt in core.missing_required_dependencies():
                num_req_missing += 1
                log.error("A required dependency '%s (%s)' is still missing." %
                          (depend, desc))

            if num_req_missing:
                if num_req_missing > 1:
                    log.error(
                        "Installation cannot continue without these dependencies."
                    )
                else:
                    log.error(
                        "Installation cannot continue without this dependency."
                    )

                log.error(
                    "Please manually install this dependency and re-run this installer."
                )
                sys.exit(1)

            for depend, desc, required_for_opt, opt in core.missing_optional_dependencies(
            ):
                if required_for_opt:
                    log.warn(
                        "An optional dependency '%s (%s)' is still missing." %
                        (depend, desc))
                    log.warn("Option '%s' has been turned off." % opt)
                    core.selected_options[opt] = False
                else:
                    log.warn(
                        "An optional dependency '%s (%s)' is still missing." %
                        (depend, desc))
                    log.warn("Some features may not function as expected.")

            if not num_opt_missing and not num_req_missing:
                log.info("OK")

        #
        # INSTALL LOCATION
        #

        log.debug("Install location = %s" % core.install_location)

        #
        # BUILD AND INSTALL
        #

        if not auto:
            tui.title("READY TO BUILD AND INSTALL")
            if not tui.continue_prompt("Ready to perform build and install."):
                sys.exit(0)

        tui.title("PRE-BUILD COMMANDS")
        core.run_pre_build(progress_callback, distro_alternate_version)
        log.info("OK")

        tui.title("BUILD AND INSTALL")
        os.umask(0022)
        for cmd in core.build_cmds():
            log.info(
                "Running '%s'\nPlease wait, this may take several minutes..." %
                cmd)
            status, output = utils.run(cmd, core.passwordObj)

            if status != 0:
                if 'configure' in cmd:
                    log.error(
                        "Configure failed with error: %s" %
                        (CONFIGURE_ERRORS.get(status, CONFIGURE_ERRORS[1])))

                else:
                    log.error("'%s' command failed with status code %d" %
                              (cmd, status))

                sys.exit(0)
            else:
                log.info("Command completed successfully.")

            log.info("")

        log.info("\nBuild complete.")

        #
        # POST BUILD
        #

        tui.title("POST-BUILD COMMANDS")
        core.run_post_build(progress_callback, distro_alternate_version)
        try:
            from prnt import cups
            #This call is just to update the cups PPD cache file@ /var/cache/cups/ppds.dat. If this is not called, hp-setup picks incorrect ppd 1st time for some printers.
            cups.getSystemPPDs()
        except ImportError:
            log.error("Failed to Import Cups")

        #
        # OPEN MDNS MULTICAST PORT
        #
        user_conf = UserConfig()

        if core.selected_options['network']:
            open_mdns_port = core.get_distro_ver_data(
                'open_mdns_port', None, distro_alternate_version)
            if open_mdns_port:
                tui.title(
                    "OPEN MDNS/BONJOUR FIREWALL PORT (MULTICAST PORT 5353)")

                paragraph = "In order to setup your printer on the network using mDNS/Bonjour, it is required that your internet firewall allows connections on port 5353. If this port is blocked by the firewall, connection to network printers using mDNS/Bonjour will not be possible."

                for p in tui.format_paragraph(paragraph):
                    log.info(p)
                log.info("")

                ok, ans = tui.enter_yes_no(
                    "Do you wish to open this port on your internet firewall")
                if not ok: sys.exit(0)

                if ans:
                    services.run_open_mdns_port(core, core.passwordObj)
                else:
                    log.warn(
                        "Skipping firewall setup. If this port is blocked on your firewall when setting up network printers, use SLP discovery and device URIs with ?ip=x.x.x.x. When using hp-setup, choose 'SLP' discovery under 'Advanced'."
                    )

        #
        # Try to close running hp-systray (3.9.2 or later)
        #

        if current_version >= 0x030902:  # 3.9.2
            try:
                from dbus import SessionBus, lowlevel
            except ImportError:
                pass
            else:
                try:
                    args = [
                        '', '', EVENT_SYSTEMTRAY_EXIT, prop.username, 0, '', ''
                    ]
                    msg = lowlevel.SignalMessage('/',
                                                 'com.hplip.StatusService',
                                                 'Event')
                    msg.append(signature='ssisiss', *args)
                    tui.title("CLOSE HP_SYSTRAY")
                    log.info(
                        "Sending close message to hp-systray (if it is currently running)..."
                    )
                    SessionBus().send_message(msg)
                    time.sleep(0.5)
                except:
                    pass

        tui.title("HPLIP UPDATE NOTIFICATION")
        ok, choice = tui.enter_choice(
            "Do you want to check for HPLIP updates?. (y=yes*, n=no) : ",
            ['y', 'n'], 'y')
        if not ok or choice != 'y':
            user_conf.set('upgrade', 'notify_upgrade', 'false')
        else:
            user_conf.set('upgrade', 'notify_upgrade', 'true')

        user_conf.set('upgrade', 'last_upgraded_time', str(int(time.time())))
        user_conf.set('upgrade', 'pending_upgrade_time', '0')

        if prev_hplip_plugin_status != pluginhandler.PLUGIN_NOT_INSTALLED:
            tui.title("HPLIP PLUGIN UPDATE NOTIFICATION")
            ok, choice = tui.enter_choice(
                "HPLIP Plug-in's needs to be installed/updated. Do you want to update plug-in's?. (y=yes*, n=no) : ",
                ['y', 'n'], 'y')
            if ok and choice == 'y':
                ok, choice = tui.enter_choice(
                    "Do you want to install plug-in's in GUI mode?. (u=GUI mode*, i=Interactive mode) : ",
                    ['u', 'i'], 'u')
                if ok and choice == 'u':
                    if not services.run_hp_tools_with_auth(
                            'hp-plugin', core.passwordObj):
                        log.error(
                            "hp-plugin command failed. Please run hp-plugin manually."
                        )
                elif ok and choice == 'i':
                    plugin_cmd = core.passwordObj.getAuthCmd(
                    ) % 'hp-plugin  -i'
                    log.info("Running '%s' command...." % plugin_cmd)
                    if os_utils.execute(plugin_cmd) != 0:
                        log.error(
                            "hp-plugin command failed. Please run hp-plugin manually."
                        )
                else:
                    log.info(
                        log.bold(
                            "Please install hp plugin's manually, otherwise some functionality may break"
                        ))
            else:
                log.info(
                    log.bold(
                        "Please install hp plugin's manually, otherwise some functionality may break"
                    ))

        if core.selected_component == 'hplip':
            tui.title("RESTART OR RE-PLUG IS REQUIRED")
            cmd = "hp-setup"
            paragraph = """If you are installing a USB connected printer, and the printer was plugged in when you started this installer, you will need to either restart your PC or unplug and re-plug in your printer (USB cable only). If you choose to restart, run this command after restarting: %s  (Note: If you are using a parallel connection, you will have to restart your PC. If you are using network/wireless, you can ignore and continue).""" % cmd

            for p in tui.format_paragraph(paragraph):
                log.info(p)
            log.info("")

            ok, choice = tui.enter_choice(
                "Restart or re-plug in your printer (r=restart, p=re-plug in*, i=ignore/continue, q=quit) : ",
                ['r', 'p', 'i'], 'p')

            if not ok:
                tui.title("RE-STARTING HP_SYSTRAY")
                services.run_systray()
                sys.exit(0)

            if choice == 'r':
                log.note("")
                log.note(
                    "IMPORTANT! Make sure to save all work in all open applications before restarting!"
                )

                ok, ans = tui.enter_yes_no(log.bold("Restart now"), 'n')
                if not ok:
                    tui.title("RE-STARTING HP_SYSTRAY")
                    services.run_systray()
                    sys.exit(0)
                if ans:
                    ok = services.restart(core.passwordObj)
                    if not ok:
                        log.error(
                            "Restart failed. Please restart using the system menu."
                        )

                tui.title("RE-STARTING HP_SYSTRAY")
                services.run_systray()
                sys.exit(0)

            elif choice == 'p':  # 'p'
                if not tui.continue_prompt(
                        "Please unplug and re-plugin your printer now. "):
                    tui.title("RE-STARTING HP_SYSTRAY")
                    services.run_systray()
                    sys.exit(0)

        #
        # SETUP PRINTER
        #
        if core.selected_component == 'hplip':
            tui.title("PRINTER SETUP")

            if auto:
                install_printer = True
            else:
                ok, install_printer = tui.enter_yes_no(
                    "Would you like to setup a printer now")
                if not ok:
                    tui.title("RE-STARTING HP_SYSTRAY")
                    services.run_systray()
                    sys.exit(0)

            if install_printer:
                log.info(
                    "Please make sure your printer is connected and powered on at this time."
                )
                ok, choice = tui.enter_choice(
                    "Do you want to setup printer in GUI mode? (u=GUI mode*, i=Interactive mode) : ",
                    ['u', 'i'], 'u')
                if ok and choice == 'u':
                    if not services.run_hp_tools_with_auth(
                            'hp-setup', core.passwordObj):
                        log.error(
                            "hp-setup failed. Please run hp-setup manually.")

                elif ok and choice == 'i':
                    setup_cmd = core.passwordObj.getAuthCmd() % 'hp-setup  -i'
                    log.info("Running '%s' command...." % setup_cmd)
                    if os_utils.execute(setup_cmd) != 0:
                        log.error(
                            "hp-setup failed. Please run hp-setup manually.")

        tui.title("RE-STARTING HP_SYSTRAY")
        services.run_systray()
    except KeyboardInterrupt:
        log.info("")
        log.error("Aborted.")

    sys.exit(0)
def start(language,
          auto=True,
          test_depends=False,
          test_unknown=False,
          assume_network=False,
          max_retries=3,
          enable=None,
          disable=None):
    try:
        core = CoreInstall(MODE_INSTALLER, INTERACTIVE_MODE)
        current_version = prop.installed_version_int
        log.debug("Currently installed version: 0x%06x" % current_version)
        core.enable = enable
        core.disable = disable

        if core.running_as_root():
            log.error(
                "You are running the installer as root. It is highly recommended that you run the installer as"
            )
            log.error(
                "a regular (non-root) user. Do you still wish to continue?")

            ok, ans = tui.enter_yes_no(log.bold("Continue with installation"),
                                       'n')
            if not ans or not ok:
                sys.exit(1)

        if auto:
            log.note(
                "Running in automatic mode. The most common options will be selected."
            )

        log.info("")
        log.note(
            "Defaults for each question are maked with a '*'. Press <enter> to accept the default."
        )

        if not auto:
            tui.title("INSTALLATION MODE")
            log.info(
                "Automatic mode will install the full HPLIP solution with the most common options."
            )
            log.info(
                "Custom mode allows you to choose installation options to fit specific requirements."
            )

            #if os.getenv('DISPLAY') and utils.find_browser() is not None:
            if 0:
                ok, choice = tui.enter_choice(
                    "\nPlease choose the installation mode (a=automatic*, c=custom, w=web installer, q=quit) : ",
                    ['a', 'c', 'w'], 'a')
            else:
                ok, choice = tui.enter_choice(
                    "\nPlease choose the installation mode (a=automatic*, c=custom, q=quit) : ",
                    ['a', 'c'], 'a')

            if not ok: sys.exit(0)

            if choice == 'a':
                auto = True

            elif choice == 'w':
                import web_install
                log.debug("Starting web browser installer...")
                web_install.start(language)
                return

        log.info("\nInitializing. Please wait...")
        core.init()

        if test_unknown:
            core.distro_name = 'unknown'
            core.distro = 0
            core.distro_version = 0

        #
        # HPLIP vs. HPIJS INSTALLATION
        #

        #if not auto:
        #tui.title("INSTALL TYPE")
        #log.info("For most users, it is recommended to install HPLIP with full support (scanning, faxing, toolbox, etc).")
        #log.info("For servers or minimal installations, you can also install print support only (HPIJS only).")

        #ok, choice = tui.enter_choice("\nInstall full hplip support (recommended) or print-only support (f=full hplip support*, p=printing only support, q=quit) ?",
        #['f', 'p'], 'f')
        #if not ok: sys.exit(0)
        #if choice  == 'p':
        #core.selected_component = 'hpijs'

        #log.debug(core.selected_component)
        core.selected_component = 'hplip'

        #
        # INTRODUCTION
        #

        tui.title("INTRODUCTION")

        if core.selected_component == 'hplip':
            log.info(
                "This installer will install HPLIP version %s on your computer."
                % core.version_public)
            #core.hpijs_build = False
        #else:
        #log.info("This installer will install HPIJS version %s on your computer." % core.version_public)
        #core.hpijs_build = True

        log.info(
            "Please close any running package management systems now (YaST, Adept, Synaptic, Up2date, etc)."
        )

        ##        #
        ##        # RELEASE NOTES
        ##        #
        ##
        ##        if not auto:
        ##            if os.getenv('DISPLAY'):
        ##                tui.title("VIEW RELEASE NOTES")
        ##                log.info("Release notes from this version are available as a web (HTML) document.")
        ##                log.info("The release notes file will be shown in a separate web browser window.")
        ##
        ##                ok, ans = tui.enter_yes_no("\nWould you like to view the release notes for this version of HPLIP", 'n')
        ##
        ##                if ok and ans:
        ##                    log.info("Displaying release notes in a browser window...")
        ##                    core.show_release_notes_in_browser()
        ##
        ##                if not ok:
        ##                    sys.exit(0)

        # For testing, mark all dependencies missing
        if test_depends:
            for d in core.have_dependencies:
                core.have_dependencies[d] = False

        num_req_missing = core.count_num_required_missing_dependencies()
        num_opt_missing = core.count_num_optional_missing_dependencies()

        #
        # CONFIRM AND SELECT DISTRO NAME AND VERSION
        #

        tui.title("DISTRO/OS CONFIRMATION")

        if core.distro_known():
            log.info("Distro appears to be %s %s.\n" % (core.get_distro_data(
                'display_name', '(unknown)'), core.distro_version))

        log.debug("Distro = %s Distro Name = %s Display Name= %s Version = %s Supported = %s" % \
            (core.distro, core.distro_name, core.distros[core.distro_name]['display_name'], \
            core.distro_version, core.distro_version_supported))

        distro_ok, ok = False, True
        if core.distro_known():
            ok, distro_ok = tui.enter_yes_no(
                'Is "%s %s" your correct distro/OS and version' %
                (core.get_distro_data('display_name',
                                      '(unknown)'), core.distro_version))

        if not ok:
            sys.exit(0)

        core.distro_changed()

        if not distro_ok:
            tui.title("DISTRO/OS SELECTION")
            core.distro, core.distro_version = DISTRO_UNKNOWN, DISTRO_VER_UNKNOWN

            log.info(
                log.bold(
                    "\nChoose the name of the distro/OS that most closely matches your system:\n"
                ))

            max_name = 0
            for d in core.distros_index:
                dd = core.distros[core.distros_index[d]]
                if dd['display']:
                    max_name = max(max_name, len(dd['display_name']))

            formatter = utils.TextFormatter((
                {
                    'width': 4
                },
                {
                    'width': max_name,
                    'margin': 2
                },
            ))

            log.info(formatter.compose(("Num.", "Distro/OS Name")))
            log.info(formatter.compose(('-' * 4, '-' * (max_name))))

            d_temp = {}
            x = 0
            for d in core.distros_index:
                dd = core.distros[core.distros_index[d]]

                if dd['display']:
                    d_temp[x] = d
                    log.info(formatter.compose((str(x), dd['display_name'])))
                    x += 1

            ok, y = tui.enter_range(
                "\nEnter number 0...%d (q=quit) ?" % (x - 1), 0, x - 1)
            if not ok: sys.exit(0)

            core.distro = d_temp[y]
            core.distro_name = core.distros_index[core.distro]
            distro_display_name = core.distros[
                core.distro_name]['display_name']
            log.debug("Distro = %s Distro Name = %s Display Name= %s" %
                      (core.distro, core.distro_name, distro_display_name))

            if core.distro != DISTRO_UNKNOWN:
                versions = core.distros[core.distro_name]['versions'].keys()
                versions.sort(lambda x, y: core.sort_vers(x, y))

                log.info(
                    log.bold(
                        '\nChoose the version of "%s" that most closely matches your system:\n'
                        % distro_display_name))

                formatter = utils.TextFormatter((
                    {
                        'width': 4
                    },
                    {
                        'width': 40,
                        'margin': 2
                    },
                ))

                log.info(formatter.compose(("Num.", "Distro/OS Version")))
                log.info(formatter.compose(('-' * 4, '-' * 40)))

                log.info(formatter.compose(("0", "Unknown or not listed")))

                x = 1
                for ver in versions:
                    ver_info = core.distros[core.distro_name]['versions'][ver]

                    if ver_info['code_name'] and ver_info['release_date']:
                        text = ver + ' ("' + ver_info[
                            'code_name'] + '", Released ' + ver_info[
                                'release_date'] + ')'

                    elif ver_info['code_name']:
                        text = ver + ' ("' + ver_info['code_name'] + '")'

                    elif ver_info['release_date']:
                        text = ver + ' (Released ' + ver_info[
                            'release_date'] + ')'

                    else:
                        text = ver

                    if not ver_info['supported']:
                        text += " [Unsupported]"

                    log.info(formatter.compose((str(x), text)))
                    x += 1

                ok, core.distro_version_int = tui.enter_range(
                    "\nEnter number 0...%d (q=quit) ?" % (x - 1), 0, x - 1)
                if not ok: sys.exit(0)

                if core.distro_version_int == 0:
                    core.distro_version = DISTRO_VER_UNKNOWN
                    core.distro_version_supported = False
                else:
                    core.distro_version = versions[core.distro_version_int - 1]
                    core.distro_version_supported = core.get_ver_data(
                        'supported', False)

                log.debug("Distro = %s Distro Name = %s Display Name= %s Version = %s Supported = %s" % \
                    (core.distro, core.distro_name, core.distros[core.distro_name]['display_name'], \
                    core.distro_version, core.distro_version_supported))

                core.distro_changed()

                log.info("\nDistro set to: %s %s" % (core.get_distro_data(
                    'display_name', '(unknown)'), core.distro_version))

            if core.distro == DISTRO_UNKNOWN or not core.distro_version_supported:
                log.error(
                    "The distribution/OS that you are running is not supported. This installer\ncannot install an unsupported distribution. Please check your distribution/OS\nand re-run this installer or perform a manual installation."
                )
                if num_req_missing:
                    log.error(
                        "The following REQUIRED dependencies are missing and need to be installed:"
                    )

                    for d, desc, opt in core.missing_required_dependencies():
                        log.error("Missing REQUIRED dependency: %s (%s)" %
                                  (d, desc))

                for d, desc, req, opt in core.missing_optional_dependencies():
                    if req:
                        log.warning(
                            "Missing OPTIONAL dependency: %s (%s) [Required for option '%s']"
                            % (d, desc, opt))
                    else:
                        log.warning(
                            "Missing OPTIONAL dependency: %s (%s) [Optional for option '%s']"
                            % (d, desc, opt))

                sys.exit(1)

        #
        # SELECT OPTIONS TO INSTALL
        #

        if not auto:
            tui.title("SELECT HPLIP OPTIONS")
            log.info(
                "You can select which HPLIP options to enable. Some options require extra dependencies."
            )
            log.info("")
            num_opt_missing = core.select_options(option_question_callback)

        #else: # auto
        #ok, enable_par = tui.enter_yes_no("Would you like to enable support for parallel (LPT:) connected printers?", 'n')
        #if not ok: sys.exit(0)

        #core.selected_options['parallel'] = enable_par

        #if enable_par:
        #log.info("Parallel support enabled.")
        else:
            enable_par = False
            core.selected_options['parallel'] = False

        log.debug("Req missing=%d Opt missing=%d HPLIP=%s Component=%s" % \
            (num_req_missing, num_opt_missing, core.hplip_present, core.selected_component))

        #
        # COLLECT SUPERUSER PASSWORD
        #
        if not core.running_as_root():
            su_sudo = core.get_distro_data('su_sudo')
            if su_sudo == "sudo":
                tui.title("ENTER USER PASSWORD")
                ok = core.check_password(password_user_entry,
                                         progress_callback)
            else:
                tui.title("ENTER ROOT/SUPERUSER PASSWORD")
                ok = core.check_password(password_entry, progress_callback)

            if not ok:
                log.error("3 incorrect attempts. Exiting.")
                sys.exit(1)

        #
        # INSTALLATION NOTES
        #

        if core.distro_supported():
            distro_notes = core.get_distro_data('notes', '').strip()
            ver_notes = core.get_ver_data('notes', '').strip()

            if distro_notes or ver_notes:
                tui.title("INSTALLATION NOTES")

                if distro_notes:
                    log.info(distro_notes)

                if ver_notes:
                    log.info(ver_notes)

                log.info("")

                if not tui.continue_prompt(
                        "Please read the installation notes."):
                    sys.exit(0)

        #
        # PRE-INSTALL COMMANDS
        #
        tui.title("RUNNING PRE-INSTALL COMMANDS")
        if core.run_pre_install(progress_callback):  # some cmds were run...
            num_req_missing = core.count_num_required_missing_dependencies()
            num_opt_missing = core.count_num_optional_missing_dependencies()
        log.info("OK")

        #
        # REQUIRED DEPENDENCIES INSTALL
        #

        depends_to_install = []
        if num_req_missing:
            tui.title("INSTALL MISSING REQUIRED DEPENDENCIES")

            log.warn("There are %d missing REQUIRED dependencies." %
                     num_req_missing)
            log.notice(
                "Installation of dependencies requires an active internet connection."
            )

            for depend, desc, option in core.missing_required_dependencies():
                log.warning("Missing REQUIRED dependency: %s (%s)" %
                            (depend, desc))

                ok = False
                packages, commands = core.get_dependency_data(depend)
                log.debug("Packages: %s" % ','.join(packages))
                log.debug("Commands: %s" % ','.join(commands))

                if core.distro_version_supported and (packages or commands):
                    if auto:
                        answer = True
                    else:
                        ok, answer = tui.enter_yes_no(
                            "\nWould you like to have this installer install the missing dependency"
                        )
                        if not ok: sys.exit(0)

                    if answer:
                        ok = True
                        log.debug(
                            "Adding '%s' to list of dependencies to install." %
                            depend)
                        depends_to_install.append(depend)

                else:
                    log.warn(
                        "This installer cannot install '%s' for your distro/OS and/or version."
                        % depend)

                if not ok:
                    log.error(
                        "Installation cannot continue without this dependency. Please manually install this dependency and re-run this installer."
                    )
                    sys.exit(0)

                #log.info("-"*10)
                #log.info("")

        #
        # OPTIONAL dependencies
        #

        if num_opt_missing:
            tui.title("INSTALL MISSING OPTIONAL DEPENDENCIES")
            log.warn("There are %d missing OPTIONAL dependencies." %
                     num_opt_missing)

            log.notice(
                "Installation of dependencies requires an active internet connection."
            )

            for depend, desc, required_for_opt, opt in core.missing_optional_dependencies(
            ):

                if required_for_opt:
                    log.warning(
                        "Missing REQUIRED dependency for option '%s': %s (%s)"
                        % (opt, depend, desc))

                else:
                    log.warning(
                        "Missing OPTIONAL dependency for option '%s': %s (%s)"
                        % (opt, depend, desc))

                installed = False
                packages, commands = core.get_dependency_data(depend)
                log.debug("Packages: %s" % ','.join(packages))
                log.debug("Commands: %s" % ','.join(commands))

                if core.distro_version_supported and (packages or commands):
                    if auto:
                        answer = True
                    else:
                        ok, answer = tui.enter_yes_no(
                            "\nWould you like to have this installer install the missing dependency"
                        )
                        if not ok: sys.exit(0)

                    if answer:
                        log.debug(
                            "Adding '%s' to list of dependencies to install." %
                            depend)
                        depends_to_install.append(depend)

                    else:
                        log.warning(
                            "Missing dependencies may effect the proper functioning of HPLIP. Please manually install this dependency after you exit this installer."
                        )
                        log.warning(
                            "Note: Options that have REQUIRED dependencies that are missing will be turned off."
                        )

                        if required_for_opt:
                            log.warn("Option '%s' has been turned off." % opt)
                            core.selected_options[opt] = False
                else:
                    log.warn(
                        "This installer cannot install '%s' for your distro/OS and/or version."
                        % depend)

                    if required_for_opt:
                        log.warn("Option '%s' has been turned off." % opt)
                        core.selected_options[opt] = False

                #log.info("-"*10)
                #log.info("")

        log.debug("Dependencies to install: %s" % depends_to_install)

        if core.distro_version_supported and \
            (depends_to_install or core.hplip_present) and \
            core.selected_component == 'hplip':

            #
            # CHECK FOR RUNNING PACKAGE MANAGER
            #

            pid, cmdline = core.check_pkg_mgr()
            while pid:
                ok, user_input = tui.enter_choice(
                    "A package manager '%s' appears to be running. Please quit the package manager and press enter to continue (i=ignore, r=retry*, f=force, q=quit) :"
                    % cmdline, ['i', 'r', 'q', 'f'], 'r')

                if not ok: sys.exit(0)

                if user_input == 'i':
                    log.warn(
                        "Ignoring running package manager. Some package operations may fail."
                    )
                    break

                if user_input == 'f':
                    ok, ans = tui.enter_yes_no(
                        "\nForce quit of package manager '%s'" % cmdline, 'y')

                    if not ok: sys.exit(0)

                    if ans:
                        cmd = core.su_sudo() % ("kill %d" % pid)
                        status, output = core.run(cmd)

                        if status != 0:
                            log.error(
                                "Failed to kill process. You may need to manually quit the program."
                            )

                pid, cmdline = core.check_pkg_mgr()

            #
            # CHECK FOR ACTIVE NETWORK CONNECTION
            #

            if not assume_network:
                tui.title("CHECKING FOR NETWORK CONNECTION")

                if not core.check_network_connection():
                    log.error(
                        "\nThe network appears to be unreachable. Installation cannot complete without access to"
                    )
                    log.error(
                        "distribution repositories. Please check the network and try again."
                    )
                    sys.exit(1)
                else:
                    log.info("Network connection present.")

            #
            # PRE-DEPEND
            #

            tui.title("RUNNING PRE-PACKAGE COMMANDS")
            core.run_pre_depend(progress_callback)
            log.info("OK")

            #
            # INSTALL PACKAGES AND RUN COMMANDS
            #

            tui.title("DEPENDENCY AND CONFLICT RESOLUTION")

            packages = []
            commands_to_run = []
            package_mgr_cmd = core.get_distro_data('package_mgr_cmd')

            # HACK!
            individual_pkgs = True
            if package_mgr_cmd.startswith('xterm'):
                individual_pkgs = False

            if package_mgr_cmd:
                log.debug("Preparing to install packages and run commands...")

                for d in depends_to_install:
                    log.debug("*** Processing dependency: %s" % d)
                    pkgs, commands = core.get_dependency_data(d)

                    if pkgs:
                        log.debug(
                            "Package(s) '%s' will be installed to satisfy dependency '%s'."
                            % (','.join(pkgs), d))

                        packages.extend(pkgs)

                    if commands:
                        log.debug(
                            "Command(s) '%s' will be run to satisfy dependency '%s'."
                            % (','.join(commands), d))

                        commands_to_run.extend(commands)

            else:
                log.error("Invalid package manager")

            log.debug("Packages: %s" % packages)
            log.debug("Commands: %s" % commands_to_run)
            log.debug("Install individual packages: %s" % individual_pkgs)

            if package_mgr_cmd and packages:
                if individual_pkgs:
                    for packages_to_install in packages:
                        retries = 0
                        while True:
                            cmd = utils.cat(package_mgr_cmd)
                            log.debug("Package manager command: %s" % cmd)

                            log.info(
                                "Running '%s'\nPlease wait, this may take several minutes..."
                                % cmd)
                            status, output = core.run(cmd)

                            if status != 0:
                                retries += 1
                                if retries < (max_retries + 1):
                                    log.error("Command failed. Re-try #%d..." %
                                              retries)
                                    continue

                                log.error(
                                    "Package install command failed with error code %d"
                                    % status)
                                ok, ans = tui.enter_yes_no(
                                    "Would you like to retry installing the missing package(s)"
                                )

                                if not ok:
                                    sys.exit(0)

                                if ans:
                                    continue
                                else:
                                    log.warn(
                                        "Some HPLIP functionality might not function due to missing package(s)."
                                    )
                                    break
                            else:
                                break

                else:
                    packages_to_install = ' '.join(packages)
                    while True:
                        cmd = utils.cat(package_mgr_cmd)
                        log.debug("Package manager command: %s" % cmd)

                        log.info(
                            "Running '%s'\nPlease wait, this may take several minutes..."
                            % cmd)
                        status, output = core.run(cmd)

                        if status != 0:
                            log.error(
                                "Package install command failed with error code %d"
                                % status)
                            ok, ans = tui.enter_yes_no(
                                "Would you like to retry installing the missing package(s)"
                            )

                            if not ok:
                                sys.exit(0)

                            if ans:
                                continue
                            else:
                                log.warn(
                                    "Some HPLIP functionality might not function due to missing package(s)."
                                )
                                break
                        else:
                            break

            if commands_to_run:
                for cmd in commands_to_run:
                    log.debug(cmd)
                    log.info(
                        "Running '%s'\nPlease wait, this may take several minutes..."
                        % cmd)
                    status, output = core.run(cmd)

                    if status != 0:
                        log.error("Install command failed with error code %d" %
                                  status)
                        sys.exit(1)

            ##
            ## HPOJ REMOVAL
            ##

            #if core.hpoj_present and core.selected_component == 'hplip' and core.distro_version_supported:
            #log.error("HPOJ is installed and/or running. HPLIP is not compatible with HPOJ.")
            #failed = True
            #hpoj_remove_cmd = core.get_distro_data('hpoj_remove_cmd')

            #if hpoj_remove_cmd:
            #if auto:
            #answer = True
            #else:
            #ok, answer = tui.enter_yes_no("\nWould you like to have this installer attempt to uninstall HPOJ")

            #if not ok: sys.exit(0)

            #if answer:
            #failed = core.remove_hpoj(progress_callback)

            #if failed:
            #log.error("HPOJ removal failed. Please manually stop/remove/uninstall HPOJ and then re-run this installer.")
            #sys.exit(1)
            #else:
            #log.error("Please stop/remove/uninstall HPOJ and then re-run this installer.")
            #sys.exit(1)

            #else:
            #log.error("Please stop/remove/uninstall HPOJ and then re-run this installer.")
            #sys.exit(1)

            #
            # HPLIP REMOVE
            #

            if core.hplip_present and core.selected_component == 'hplip' and core.distro_version_supported:
                failed = True
                log.warn(
                    "A previous install of HPLIP is installed and/or running.")

                hplip_remove_cmd = core.get_distro_data('hplip_remove_cmd')
                if hplip_remove_cmd:
                    if auto:
                        answer = True
                    else:
                        ok, answer = tui.enter_yes_no(
                            "\nWould you like to have this installer attempt to uninstall the previously installed HPLIP"
                        )
                    if not ok: sys.exit(0)

                    if answer:
                        failed = core.remove_hplip(progress_callback)

                else:
                    log.error(
                        "The previously installed version of HPLIP may conflict with the new one being installed."
                    )
                    log.error(
                        "It is recommended that you quit this installer, and manually remove HPLIP before continuing."
                    )
                    sys.exit(0)

                if failed:
                    log.warn(
                        "HPLIP removal failed. The previous install may have been installed using a tarball or this installer."
                    )
                    log.warn(
                        "Continuing to run installer - this installation should overwrite the previous one."
                    )

            #
            # POST-DEPEND
            #

            tui.title("RUNNING POST-PACKAGE COMMANDS")
            core.run_post_depend(progress_callback)
            log.info("OK")

            #
            # DEPENDENCIES RE-CHECK
            #

            tui.title("RE-CHECKING DEPENDENCIES")
            core.check_dependencies()

            num_req_missing = 0
            for depend, desc, opt in core.missing_required_dependencies():
                num_req_missing += 1
                log.error("A required dependency '%s (%s)' is still missing." %
                          (depend, desc))

            if num_req_missing:
                if num_req_missing > 1:
                    log.error(
                        "Installation cannot continue without these dependencies."
                    )
                else:
                    log.error(
                        "Installation cannot continue without this dependency."
                    )

                log.error(
                    "Please manually install this dependency and re-run this installer."
                )
                sys.exit(1)

            for depend, desc, required_for_opt, opt in core.missing_optional_dependencies(
            ):
                if required_for_opt:
                    log.warn(
                        "An optional dependency '%s (%s)' is still missing." %
                        (depend, desc))
                    log.warn("Option '%s' has been turned off." % opt)
                    core.selected_options[opt] = False
                else:
                    log.warn(
                        "An optional dependency '%s (%s)' is still missing." %
                        (depend, desc))
                    log.warn("Some features may not function as expected.")

            if not num_opt_missing and not num_req_missing:
                log.info("OK")

        #
        # INSTALL LOCATION
        #

        log.debug("Install location = %s" % core.install_location)

        #
        # BUILD AND INSTALL
        #

        if not auto:
            tui.title("READY TO BUILD AND INSTALL")
            if not tui.continue_prompt("Ready to perform build and install."):
                sys.exit(0)

        tui.title("PRE-BUILD COMMANDS")
        core.run_pre_build(progress_callback)
        log.info("OK")

        tui.title("BUILD AND INSTALL")

        os.umask(0022)
        for cmd in core.build_cmds():
            log.info(
                "Running '%s'\nPlease wait, this may take several minutes..." %
                cmd)
            status, output = core.run(cmd)

            if status != 0:
                if 'configure' in cmd:
                    log.error(
                        "Configure failed with error: %s" %
                        CONFIGURE_ERRORS.get(status, CONFIGURE_ERRORS[1]))

                else:
                    log.error("'%s' command failed with status code %d" %
                              (cmd, status))

                sys.exit(0)
            else:
                log.info("Command completed successfully.")

            log.info("")

        log.info("\nBuild complete.")

        #
        # POST BUILD
        #

        tui.title("POST-BUILD COMMANDS")
        core.run_post_build(progress_callback)

        #
        # OPEN MDNS MULTICAST PORT
        #

        if core.selected_options['network']:
            open_mdns_port = core.get_distro_ver_data('open_mdns_port')
            if open_mdns_port:
                tui.title(
                    "OPEN MDNS/BONJOUR FIREWALL PORT (MULTICAST PORT 5353)")

                paragraph = "In order to setup your printer on the network using mDNS/Bonjour, it is required that your internet firewall allows connections on port 5353. If this port is blocked by the firewall, connection to network printers using mDNS/Bonjour will not be possible."

                for p in tui.format_paragraph(paragraph):
                    log.info(p)
                log.info("")

                ok, ans = tui.enter_yes_no(
                    "Do you wish to open this port on your internet firewall")
                if not ok: sys.exit(0)

                if ans:
                    core.run_open_mdns_port()
                else:
                    log.warn(
                        "Skipping firewall setup. If this port is blocked on your firewall when setting up network printers, use SLP discovery and device URIs with ?ip=x.x.x.x. When using hp-setup, choose 'SLP' discovery under 'Advanced'."
                    )

        #
        # Try to close running hp-systray (3.9.2 or later)
        #

        if current_version >= 0x030902:  # 3.9.2
            try:
                # dBus
                #import dbus
                from dbus import SessionBus, lowlevel
            except ImportError:
                #log.error("Unable to load DBus")
                pass
            else:
                try:
                    args = [
                        '', '', EVENT_SYSTEMTRAY_EXIT, prop.username, 0, '', ''
                    ]
                    msg = lowlevel.SignalMessage('/',
                                                 'com.hplip.StatusService',
                                                 'Event')
                    msg.append(signature='ssisiss', *args)
                    tui.title("CLOSE HP_SYSTRAY")
                    log.info(
                        "Sending close message to hp-systray (if it is currently running)..."
                    )
                    SessionBus().send_message(msg)
                except:
                    pass

        # Restart or re-plugin if necessary (always True in 2.7.9+)
        if core.selected_component == 'hplip':
            tui.title("RESTART OR RE-PLUG IS REQUIRED")
            cmd = "hp-setup"
            paragraph = """If you are installing a USB connected printer, and the printer was plugged in when you started this installer, you will need to either restart your PC or unplug and re-plug in your printer (USB cable only). If you choose to restart, run this command after restarting: %s  (Note: If you are using a parallel connection, you will have to restart your PC. If you are using network/wireless, you can ignore and continue).""" % cmd

            for p in tui.format_paragraph(paragraph):
                log.info(p)
            log.info("")

            ok, choice = tui.enter_choice(
                "Restart or re-plug in your printer (r=restart, p=re-plug in*, i=ignore/continue, q=quit) : ",
                ['r', 'p', 'i'], 'p')

            if not ok: sys.exit(0)

            if choice == 'r':
                log.note("")
                log.note(
                    "IMPORTANT! Make sure to save all work in all open applications before restarting!"
                )

                ok, ans = tui.enter_yes_no(log.bold("Restart now"), 'n')
                if not ok: sys.exit(0)
                if ans:
                    ok = core.restart()
                    if not ok:
                        log.error(
                            "Restart failed. Please restart using the system menu."
                        )

                sys.exit(0)

            elif choice == 'p':  # 'p'
                if not tui.continue_prompt(
                        "Please unplug and re-plugin your printer now. "):
                    sys.exit(0)

        #
        # SETUP PRINTER
        #
        if core.selected_component == 'hplip':
            tui.title("PRINTER SETUP")

            if auto:
                install_printer = True
            else:
                ok, install_printer = tui.enter_yes_no(
                    "Would you like to setup a printer now")
                if not ok: sys.exit(0)

            if install_printer:
                log.info(
                    "Please make sure your printer is connected and powered on at this time."
                )
                if not core.run_hp_setup():
                    log.error("hp-setup failed. Please run hp-setup manually.")

    except KeyboardInterrupt:
        log.info("")
        log.error("Aborted.")

    sys.exit(0)
Example #12
0
def start(language, auto=True, test_depends=False,
          test_unknown=False, assume_network=False,
          max_retries=3, enable=None, disable=None):
    try:
        core =  CoreInstall(MODE_INSTALLER, INTERACTIVE_MODE)
        current_version = prop.installed_version_int
        log.debug("Currently installed version: 0x%06x" % current_version)
        core.enable = enable
        core.disable = disable

        if core.running_as_root():
            log.error("You are running the installer as root. It is highly recommended that you run the installer as")
            log.error("a regular (non-root) user. Do you still wish to continue?")

            ok, ans = tui.enter_yes_no(log.bold("Continue with installation"), 'n')
            if not ans or not ok:
                sys.exit(1)

        if auto:
            log.note("Running in automatic mode. The most common options will be selected.")

        log.info("")
        log.note("Defaults for each question are maked with a '*'. Press <enter> to accept the default.")
        core.init()
        vrs =core.get_distro_data('versions_list')
        Is_Manual_Distro = False
        distro_alternate_version=None
        if core.distro_version not in vrs and len(vrs):
            distro_alternate_version= vrs[len(vrs)-1]
            if core.is_auto_installer_support(distro_alternate_version):
                log.error("%s-%s version is not supported, so all dependencies may not be installed. However trying to install using %s-%s version packages." \
                                                   %(core.distro_name, core.distro_version, core.distro_name, distro_alternate_version))
                ok, choice = tui.enter_choice("\nPress 'y' to continue auto installation. Press 'n' to quit auto instalation(y=yes, n=no*): ",['y','n'],'n')
                if not ok or choice =='n':
                    log.info("Installation exit")
                    sys.exit()
            else:
                # Even previous distro is not supported
                Is_Manual_Distro = True
        elif not core.is_auto_installer_support():
              # This distro is not supported
            Is_Manual_Distro = True


        if Is_Manual_Distro:
            log.error("Auto installation is not supported for '%s' distro so all dependencies may not be installed. \nPlease install manually as mentioned in 'http://hplipopensource.com/hplip-web/install/manual/index.html' web-site"% core.distro_name)
            ok, choice = tui.enter_choice("\nPress 'y' to continue auto installation. Press 'n' to quit auto instalation(y=yes, n=no*): ",['y','n'],'n')
            if not ok or choice =='n':
                log.info("Installation exit")
                sys.exit()

        if not auto:
            tui.title("INSTALLATION MODE")
            log.info("Automatic mode will install the full HPLIP solution with the most common options.")
            log.info("Custom mode allows you to choose installation options to fit specific requirements.")

            #if os.getenv('DISPLAY') and utils.find_browser() is not None:
            if 0:
                ok, choice = tui.enter_choice("\nPlease choose the installation mode (a=automatic*, c=custom, w=web installer, q=quit) : ",
                    ['a', 'c', 'w'], 'a')
            else:
                ok, choice = tui.enter_choice("\nPlease choose the installation mode (a=automatic*, c=custom, q=quit) : ",
                    ['a', 'c'], 'a')

            if not ok: sys.exit(0)

            if choice == 'a':
                auto = True

            elif choice == 'w':
                import web_install
                log.debug("Starting web browser installer...")
                web_install.start(language)
                return

        log.info("\nInitializing. Please wait...")
        prev_hplip_version= sys_conf.get("hplip","version","0.0.0")


        if test_unknown:
            core.distro_name = 'unknown'
            core.distro = 0
            core.distro_version = 0


        #
        # HPLIP INSTALLATION
        #
        core.selected_component = 'hplip'

        #
        # INTRODUCTION
        #

        tui.title("INTRODUCTION")

        if core.selected_component == 'hplip':
            log.info("This installer will install HPLIP version %s on your computer." % core.version_public)

        log.info("Please close any running package management systems now (YaST, Adept, Synaptic, Up2date, etc).")


        # For testing, mark all dependencies missing
        if test_depends:
            for d in core.have_dependencies:
                core.have_dependencies[d] = False

        num_req_missing = core.count_num_required_missing_dependencies()
        num_opt_missing = core.count_num_optional_missing_dependencies()


        #
        # CONFIRM AND SELECT DISTRO NAME AND VERSION
        #

        tui.title("DISTRO/OS CONFIRMATION")


        if core.distro_known():
            log.info("Distro appears to be %s %s.\n" % (core.get_distro_data('display_name', '(unknown)'), core.distro_version))

        log.debug("Distro = %s Distro Name = %s Display Name= %s Version = %s Supported = %s" % \
            (core.distro, core.distro_name, core.distros[core.distro_name]['display_name'], \
            core.distro_version, core.distro_version_supported))

        distro_ok, ok = False, True
        if core.distro_known():
            ok, distro_ok = tui.enter_yes_no('Is "%s %s" your correct distro/OS and version'
                % (core.get_distro_data('display_name', '(unknown)'), core.distro_version))

        if not ok:
            sys.exit(0)

        if distro_alternate_version:
            core.distro_version = distro_alternate_version

        core.distro_changed()

        if not distro_ok:
            tui.title("DISTRO/OS SELECTION")
            core.distro, core.distro_version = DISTRO_UNKNOWN, DISTRO_VER_UNKNOWN

            log.info(log.bold("\nChoose the name of the distro/OS that most closely matches your system:\n"))

            max_name = 0
            for d in core.distros_index:
                dd = core.distros[core.distros_index[d]]
                if dd['display']:
                    max_name = max(max_name, len(dd['display_name']))

            formatter = utils.TextFormatter(
                    (
                        {'width': 4},
                        {'width': max_name, 'margin': 2},
                    )
                )

            log.info(formatter.compose(("Num.", "Distro/OS Name")))
            log.info(formatter.compose(('-'*4, '-'*(max_name))))

            d_temp = {}
            x = 0
            for d in core.distros_index:
                dd = core.distros[core.distros_index[d]]

                if dd['display']:
                    d_temp[x] = d
                    log.info(formatter.compose((str(x), dd['display_name'])))
                    x += 1

            ok, y = tui.enter_range("\nEnter number 0...%d (q=quit) ?" % (x-1), 0, x-1)
            if not ok: sys.exit(0)

            core.distro = d_temp[y]
            core.distro_name = core.distros_index[core.distro]
            distro_display_name = core.distros[core.distro_name]['display_name']
            log.debug("Distro = %s Distro Name = %s Display Name= %s" %
                (core.distro, core.distro_name, distro_display_name))

            if core.distro != DISTRO_UNKNOWN:
                versions = core.distros[core.distro_name]['versions'].keys()
                versions.sort(lambda x, y: core.sort_vers(x, y))

                log.info(log.bold('\nChoose the version of "%s" that most closely matches your system:\n' % distro_display_name))

                formatter = utils.TextFormatter(
                        (
                            {'width': 4},
                            {'width': 40, 'margin': 2},
                        )
                    )

                log.info(formatter.compose(("Num.", "Distro/OS Version")))
                log.info(formatter.compose(('-'*4, '-'*40)))

                log.info(formatter.compose(("0", "Unknown or not listed")))

                x = 1
                for ver in versions:
                    ver_info = core.distros[core.distro_name]['versions'][ver]

                    if ver_info['code_name'] and ver_info['release_date']:
                        text = ver + ' ("' + ver_info['code_name'] + '", Released ' + ver_info['release_date'] + ')'

                    elif ver_info['code_name']:
                        text = ver + ' ("' + ver_info['code_name'] + '")'

                    elif ver_info['release_date']:
                        text = ver + ' (Released ' + ver_info['release_date'] + ')'

                    else:
                        text = ver

                    if not ver_info['supported']:
                        text += " [Unsupported]"

                    log.info(formatter.compose((str(x), text)))
                    x += 1

                ok, core.distro_version_int = tui.enter_range("\nEnter number 0...%d (q=quit) ?" %
                    (x-1), 0, x-1)
                if not ok: sys.exit(0)

                if core.distro_version_int == 0:
                    core.distro_version = DISTRO_VER_UNKNOWN
                    core.distro_version_supported = False
                else:
                    core.distro_version = versions[core.distro_version_int - 1]
                    core.distro_version_supported = core.get_ver_data('supported', False)

                log.debug("Distro = %s Distro Name = %s Display Name= %s Version = %s Supported = %s" % \
                    (core.distro, core.distro_name, core.distros[core.distro_name]['display_name'], \
                    core.distro_version, core.distro_version_supported))

                core.distro_changed()

                log.info("\nDistro set to: %s %s" %
                    (core.get_distro_data('display_name', '(unknown)'), core.distro_version))


#            if core.distro == DISTRO_UNKNOWN or not core.distro_version_supported:
            if core.distro == DISTRO_UNKNOWN:
                log.error("The distribution/OS that you are running is not supported. This installer\ncannot install an unsupported distribution. Please check your distribution/OS\nand re-run this installer or perform a manual installation.")
                if num_req_missing:
                    log.error("The following REQUIRED dependencies are missing and need to be installed:")

                    for d, desc, opt in core.missing_required_dependencies():
                        log.error("Missing REQUIRED dependency: %s (%s)" % (d, desc))

                for d, desc, req, opt in core.missing_optional_dependencies():
                    if req:
                        log.warning("Missing OPTIONAL dependency: %s (%s) [Required for option '%s']" % (d, desc, opt))
                    else:
                        log.warning("Missing OPTIONAL dependency: %s (%s) [Optional for option '%s']" % (d, desc, opt))

                sys.exit(1)


        #
        # SELECT OPTIONS TO INSTALL
        #

        if not auto:
            tui.title("SELECT HPLIP OPTIONS")
            log.info("You can select which HPLIP options to enable. Some options require extra dependencies.")
            log.info("")
            num_opt_missing = core.select_options(option_question_callback)

        else:
            enable_par = False
            core.selected_options['parallel'] = False



        #
        # COLLECT SUPERUSER PASSWORD
        #
        if not core.running_as_root():
            su_sudo = core.get_distro_data('su_sudo')
            if su_sudo == "sudo":
                tui.title("ENTER USER PASSWORD")
                ok = core.check_password(password_user_entry, progress_callback)
            else:
                tui.title("ENTER ROOT/SUPERUSER PASSWORD")
                ok = core.check_password(password_entry, progress_callback)

            if not ok:
                log.error("3 incorrect attempts. (or) Insufficient permissions(i.e. try with sudo user).\nExiting.")
                sys.exit(1)


        # INSTALLATION NOTES
        #

        if core.is_auto_installer_support(distro_alternate_version):
            distro_notes = core.get_distro_data('notes', '').strip()
            ver_notes = core.get_ver_data('notes', '',distro_alternate_version).strip()

            if distro_notes or ver_notes:
                tui.title("INSTALLATION NOTES")

                if distro_notes:
                    log.info(distro_notes)

                if ver_notes:
                    log.info(ver_notes)

                log.info("")

                if not tui.continue_prompt("Please read the installation notes."):
                    sys.exit(0)

        #
        # PRE-INSTALL COMMANDS
        #
        tui.title("RUNNING PRE-INSTALL COMMANDS")
        if core.run_pre_install(progress_callback, distro_alternate_version): # some cmds were run...
            num_req_missing = core.count_num_required_missing_dependencies()
            num_opt_missing = core.count_num_optional_missing_dependencies()
        log.info("OK")

        #
        # REQUIRED DEPENDENCIES INSTALL
        #
        package_mgr_cmd = core.get_distro_data('package_mgr_cmd')
        depends_to_install = []
        if num_req_missing:
            tui.title("INSTALL MISSING REQUIRED DEPENDENCIES")

            log.warn("There are %d missing REQUIRED dependencies." % num_req_missing)
            log.notice("Installation of dependencies requires an active internet connection.")

            for depend, desc, option in core.missing_required_dependencies():
                log.warning("Missing REQUIRED dependency: %s (%s)" % (depend, desc))

                ok = False
                packages, commands = core.get_dependency_data(depend,distro_alternate_version)
                log.debug("Packages: %s" % ','.join(packages))
                log.debug("Commands: %s" % ','.join(commands))

#                if core.distro_version_supported and (packages or commands):
                if package_mgr_cmd and (packages or commands):
                    if auto:
                        answer = True
                    else:
                        ok, answer = tui.enter_yes_no("\nWould you like to have this installer install the missing dependency")
                        if not ok: sys.exit(0)

                    if answer:
                        ok = True
                        log.debug("Adding '%s' to list of dependencies to install." % depend)
                        depends_to_install.append(depend)

                else:
                    log.warn("This installer cannot install '%s' for your distro/OS and/or version." % depend)

                if not ok:
                    log.error("Installation cannot continue without this dependency. Please manually install this dependency and re-run this installer.")
                    sys.exit(0)

        #
        # OPTIONAL dependencies
        #

        if num_opt_missing:
            tui.title("INSTALL MISSING OPTIONAL DEPENDENCIES")
            log.warn("There are %d missing OPTIONAL dependencies." % num_opt_missing)

            log.notice("Installation of dependencies requires an active internet connection.")

            for depend, desc, required_for_opt, opt in core.missing_optional_dependencies():

                if required_for_opt:
                    log.warning("Missing REQUIRED dependency for option '%s': %s (%s)" % (opt, depend, desc))

                else:
                    log.warning("Missing OPTIONAL dependency for option '%s': %s (%s)" % (opt, depend, desc))

                installed = False
                packages, commands = core.get_dependency_data(depend,distro_alternate_version)
                log.debug("Packages: %s" % ','.join(packages))
                log.debug("Commands: %s" % ','.join(commands))


#                if core.distro_version_supported and (packages or commands):
                if package_mgr_cmd and (packages or commands):
                    if auto:
                        answer = True
                    else:
                        ok, answer = tui.enter_yes_no("\nWould you like to have this installer install the missing dependency")
                        if not ok: sys.exit(0)

                    if answer:
                        log.debug("Adding '%s' to list of dependencies to install." % depend)
                        depends_to_install.append(depend)

                    else:
                        log.warning("Missing dependencies may effect the proper functioning of HPLIP. Please manually install this dependency after you exit this installer.")
                        log.warning("Note: Options that have REQUIRED dependencies that are missing will be turned off.")

                        if required_for_opt:
                            log.warn("Option '%s' has been turned off." % opt)
                            core.selected_options[opt] = False
                else:
                    log.warn("This installer cannot install '%s' for your distro/OS and/or version." % depend)

                    if required_for_opt:
                        log.warn("Option '%s' has been turned off." % opt)
                        core.selected_options[opt] = False



        log.debug("Dependencies to install: %s  hplip_present:%s" % (depends_to_install, core.hplip_present))

#        if core.distro_version_supported and \
#            (depends_to_install or core.hplip_present) and \
#            core.selected_component == 'hplip':

        if package_mgr_cmd and \
            (depends_to_install or core.hplip_present) and \
            core.selected_component == 'hplip':
            #
            # CHECK FOR RUNNING PACKAGE MANAGER
            #

            pid, cmdline = core.check_pkg_mgr()
            while pid:
                ok, user_input = tui.enter_choice("A package manager '%s' appears to be running. Please quit the package manager and press enter to continue (i=ignore, r=retry*, f=force, q=quit) :"
                    % cmdline, ['i', 'r', 'q', 'f'], 'r')

                if not ok: sys.exit(0)

                if user_input == 'i':
                    log.warn("Ignoring running package manager. Some package operations may fail.")
                    break

                if user_input == 'f':
                    ok, ans = tui.enter_yes_no("\nForce quit of package manager '%s'" % cmdline, 'y')

                    if not ok: sys.exit(0)

                    if ans:
                        cmd = core.su_sudo() % ("kill %d" % pid)
                        status, output = core.run(cmd)

                        if status != 0:
                            log.error("Failed to kill process. You may need to manually quit the program.")

                pid, cmdline = core.check_pkg_mgr()


            #
            # CHECK FOR ACTIVE NETWORK CONNECTION
            #
            if not assume_network:
                tui.title("CHECKING FOR NETWORK CONNECTION")

                if not core.check_network_connection():
                    log.error("The network appears to be unreachable. Installation may not resolve all dependencies without access to distribution repositories.")
                    ok, choice = tui.enter_choice("Do you want to continue installation without network?. Press 'y' for YES. Press 'n' for NO (y=yes*, n=no) : ",['y', 'n'], 'y')
                    if not ok or choice == 'n':
                        log.info("Please connect network and try again")
                        sys.exit(1)
                    else:
                        log.debug("Continuing installation without network")
                else:
                    log.info("Network connection present.")

            #
            # PRE-DEPEND
            #

            tui.title("RUNNING PRE-PACKAGE COMMANDS")
            core.run_pre_depend(progress_callback,distro_alternate_version)
            log.info("OK")

            #
            # INSTALL PACKAGES AND RUN COMMANDS
            #

            tui.title("DEPENDENCY AND CONFLICT RESOLUTION")

            packages = []
            commands_to_run = []
            package_mgr_cmd = core.get_distro_data('package_mgr_cmd')

            # HACK!
            individual_pkgs = True
            if package_mgr_cmd.startswith('xterm'):
                individual_pkgs = False

            if package_mgr_cmd:
                log.debug("Preparing to install packages and run commands...")

                for d in depends_to_install:
                    log.debug("*** Processing dependency: %s" % d)
                    pkgs, commands = core.get_dependency_data(d,distro_alternate_version)

                    if pkgs:
                        log.debug("Package(s) '%s' will be installed to satisfy dependency '%s'." %
                            (','.join(pkgs), d))

                        packages.extend(pkgs)

                    if commands:
                        log.debug("Command(s) '%s' will be run to satisfy dependency '%s'." %
                            (','.join(commands), d))

                        commands_to_run.extend(commands)

            else:
                log.error("Invalid package manager")

            log.debug("Packages: %s" % packages)
            log.debug("Commands: %s" % commands_to_run)
            log.debug("Install individual packages: %s" % individual_pkgs)

            if package_mgr_cmd and packages:
                if individual_pkgs:
                    for packages_to_install in packages:
                        retries = 0
                        while True:
                            cmd = utils.cat(package_mgr_cmd)
                            log.debug("Package manager command: %s" % cmd)

                            log.info("Running '%s'\nPlease wait, this may take several minutes..." % cmd)
                            status, output = core.run(cmd)

                            if status != 0:
                                retries += 1
                                if retries < (max_retries+1):
                                    log.error("Command failed. Re-try #%d..." % retries)
                                    continue

                                log.error("Package install command failed with error code %d" % status)
                                ok, ans = tui.enter_yes_no("Would you like to retry installing the missing package(s)")

                                if not ok:
                                    sys.exit(0)

                                if ans:
                                    continue
                                else:
                                    log.warn("Some HPLIP functionality might not function due to missing package(s).")
                                    break
                            else:
                                break

                else:
                    packages_to_install = ' '.join(packages)
                    while True:
                        cmd = utils.cat(package_mgr_cmd)
                        log.debug("Package manager command: %s" % cmd)

                        log.info("Running '%s'\nPlease wait, this may take several minutes..." % cmd)
                        status, output = core.run(cmd)

                        if status != 0:
                            log.error("Package install command failed with error code %d" % status)
                            ok, ans = tui.enter_yes_no("Would you like to retry installing the missing package(s)")

                            if not ok:
                                sys.exit(0)

                            if ans:
                                continue
                            else:
                                log.warn("Some HPLIP functionality might not function due to missing package(s).")
                                break
                        else:
                            break

            if commands_to_run:
                for cmd in commands_to_run:
                    log.debug(cmd)
                    log.info("Running '%s'\nPlease wait, this may take several minutes..." % cmd)
                    status, output = core.run(cmd)

                    if status != 0:
                        log.error("Install command failed with error code %d" % status)
                        sys.exit(1)




            #
            # HPLIP REMOVE
            #
            num_req_missing = 0
            core.check_dependencies()
            for depend, desc, opt in core.missing_required_dependencies():
                log.error("A required dependency '%s (%s)' is still missing." % (depend, desc))
                num_req_missing += 1

            if num_req_missing == 0 and core.hplip_present and core.selected_component == 'hplip' and core.distro_version_supported:
                path = utils.which('hp-uninstall')
                ok, choice = tui.enter_choice("HPLIP-%s exists, this may conflict with the new one being installed.\nDo you want to ('i'= Remove and Install, 'o'= Overwrite*, 'q'= Quit)?	:"%(prev_hplip_version),['i','o','q'],'o')
                if not ok or choice=='q':
                    log.error("User Exit")
                    sys.exit(0)
                elif choice == 'i':
#                    log.info("Uninstalling existing HPLIP-%s"%prev_hplip_version)
                    sts =core.uninstall(NON_INTERACTIVE_MODE)

                    if sts is False: 
                        log.warn("Failed to uninstall existing HPLIP-%s. This installation will overwrite on existing HPLIP" %prev_hplip_version)
                    else:
                        log.debug("HPLIP-%s is uninstalled successfully." %prev_hplip_version)

            #
            # POST-DEPEND
            #
            tui.title("RUNNING POST-PACKAGE COMMANDS")
            core.run_post_depend(progress_callback)
            log.info("OK")


            #
            # DEPENDENCIES RE-CHECK
            #
            tui.title("RE-CHECKING DEPENDENCIES")
            core.check_dependencies()

            num_req_missing = 0
            for depend, desc, opt in core.missing_required_dependencies():
                num_req_missing += 1
                log.error("A required dependency '%s (%s)' is still missing." % (depend, desc))

            if num_req_missing:
                if num_req_missing > 1:
                    log.error("Installation cannot continue without these dependencies.")
                else:
                    log.error("Installation cannot continue without this dependency.")

                log.error("Please manually install this dependency and re-run this installer.")
                sys.exit(1)

            for depend, desc, required_for_opt, opt in core.missing_optional_dependencies():
                if required_for_opt:
                    log.warn("An optional dependency '%s (%s)' is still missing." % (depend, desc))
                    log.warn("Option '%s' has been turned off." % opt)
                    core.selected_options[opt] = False
                else:
                    log.warn("An optional dependency '%s (%s)' is still missing." % (depend, desc))
                    log.warn("Some features may not function as expected.")


            if not num_opt_missing and not num_req_missing:
                log.info("OK")

        #
        # INSTALL LOCATION
        #

        log.debug("Install location = %s" % core.install_location)


        #
        # BUILD AND INSTALL
        #

        if not auto:
            tui.title("READY TO BUILD AND INSTALL")
            if not tui.continue_prompt("Ready to perform build and install."):
                sys.exit(0)

        tui.title("PRE-BUILD COMMANDS")
        core.run_pre_build(progress_callback, distro_alternate_version)
        log.info("OK")

        tui.title("BUILD AND INSTALL")

        os.umask(0022)
        for cmd in core.build_cmds():
            log.info("Running '%s'\nPlease wait, this may take several minutes..." % cmd)
            status, output = core.run(cmd)

            if status != 0:
                if 'configure' in cmd:
                    log.error("Configure failed with error: %s" % CONFIGURE_ERRORS.get(status, CONFIGURE_ERRORS[1]))

                else:
                    log.error("'%s' command failed with status code %d" % (cmd, status))

                sys.exit(0)
            else:
                log.info("Command completed successfully.")

            log.info("")

        log.info("\nBuild complete.")

        #
        # POST BUILD
        #

        tui.title("POST-BUILD COMMANDS")
        core.run_post_build(progress_callback, distro_alternate_version)
        try:
            from prnt import cups
            #This call is just to update the cups PPD cache file@ /var/cache/cups/ppds.dat. If this is not called, hp-setup picks incorrect ppd 1st time for some printers. 
            cups.getSystemPPDs()
        except ImportError:
            log.error("Failed to Import Cups")

        #
        # OPEN MDNS MULTICAST PORT
        #
        user_conf = UserConfig()
        
        if core.selected_options['network']:
            open_mdns_port = core.get_distro_ver_data('open_mdns_port',None,distro_alternate_version)
            if open_mdns_port:
                tui.title("OPEN MDNS/BONJOUR FIREWALL PORT (MULTICAST PORT 5353)")

                paragraph = "In order to setup your printer on the network using mDNS/Bonjour, it is required that your internet firewall allows connections on port 5353. If this port is blocked by the firewall, connection to network printers using mDNS/Bonjour will not be possible."

                for p in tui.format_paragraph(paragraph):
                    log.info(p)
                log.info("")

                ok, ans = tui.enter_yes_no("Do you wish to open this port on your internet firewall")
                if not ok: sys.exit(0)

                if ans:
                    core.run_open_mdns_port()
                else:
                    log.warn("Skipping firewall setup. If this port is blocked on your firewall when setting up network printers, use SLP discovery and device URIs with ?ip=x.x.x.x. When using hp-setup, choose 'SLP' discovery under 'Advanced'.")


        #
        # Try to close running hp-systray (3.9.2 or later)
        #

        if current_version >= 0x030902: # 3.9.2
            try:
                from dbus import SessionBus, lowlevel
            except ImportError:
                pass
            else:
                try:
                    args = ['', '', EVENT_SYSTEMTRAY_EXIT, prop.username, 0, '', '']
                    msg = lowlevel.SignalMessage('/', 'com.hplip.StatusService', 'Event')
                    msg.append(signature='ssisiss', *args)
                    tui.title("CLOSE HP_SYSTRAY")
                    log.info("Sending close message to hp-systray (if it is currently running)...")
                    SessionBus().send_message(msg)
                    time.sleep(0.5)
                except:
                    pass
	
        tui.title("HPLIP UPDATE NOTIFICATION")
        ok, choice = tui.enter_choice("Do you want to check for HPLIP updates?. (y=yes*, n=no) : ",['y', 'n'], 'y')
        if not ok or choice != 'y':
            user_conf.set('upgrade', 'notify_upgrade', 'false')
        else:
            user_conf.set('upgrade', 'notify_upgrade', 'true')

        user_conf.set('upgrade','last_upgraded_time',str(int(time.time())))
        user_conf.set('upgrade','pending_upgrade_time','0')


        if core.selected_component == 'hplip':
            tui.title("RESTART OR RE-PLUG IS REQUIRED")
            cmd = "hp-setup"
            paragraph = """If you are installing a USB connected printer, and the printer was plugged in when you started this installer, you will need to either restart your PC or unplug and re-plug in your printer (USB cable only). If you choose to restart, run this command after restarting: %s  (Note: If you are using a parallel connection, you will have to restart your PC. If you are using network/wireless, you can ignore and continue).""" % cmd

            for p in tui.format_paragraph(paragraph):
                log.info(p)
            log.info("")

            ok, choice = tui.enter_choice("Restart or re-plug in your printer (r=restart, p=re-plug in*, i=ignore/continue, q=quit) : ",
                ['r', 'p', 'i'], 'p')

            if not ok: 
                start_systray()
                sys.exit(0)

            if choice == 'r':
                log.note("")
                log.note("IMPORTANT! Make sure to save all work in all open applications before restarting!")

                ok, ans = tui.enter_yes_no(log.bold("Restart now"), 'n')
                if not ok: 
                    start_systray()
                    sys.exit(0)
                if ans:
                    ok = core.restart()
                    if not ok:
                        log.error("Restart failed. Please restart using the system menu.")

                start_systray()
                sys.exit(0)

            elif choice == 'p': # 'p'
                if not tui.continue_prompt("Please unplug and re-plugin your printer now. "):
                    start_systray()
                    sys.exit(0)


        #
        # SETUP PRINTER
        #
        if core.selected_component == 'hplip':
            tui.title("PRINTER SETUP")

            if auto:
                install_printer = True
            else:
                ok, install_printer = tui.enter_yes_no("Would you like to setup a printer now")
                if not ok:
                    start_systray()
                    sys.exit(0)

            if install_printer:
                log.info("Please make sure your printer is connected and powered on at this time.")
                if not core.run_hp_setup():
                    log.error("hp-setup failed. Please run hp-setup manually.")

        start_systray()
    except KeyboardInterrupt:
        log.info("")
        log.error("Aborted.")

    sys.exit(0)
Example #13
0
    def run(self):
        #results = {} # {'file' : error_code,...}

        STATE_DONE = 0
        STATE_ABORTED = 10
        STATE_SUCCESS = 20
        STATE_BUSY = 25
        STATE_READ_SENDER_INFO = 30
        STATE_PRERENDER = 40
        STATE_COUNT_PAGES = 50
        STATE_NEXT_RECIPIENT = 60
        STATE_COVER_PAGE = 70
        STATE_SINGLE_FILE = 80
        STATE_MERGE_FILES = 90
        STATE_SINGLE_FILE = 100
        STATE_SEND_FAX = 110
        STATE_CLEANUP = 120
        STATE_ERROR = 130

        next_recipient = self.next_recipient_gen()

        state = STATE_READ_SENDER_INFO
        self.rendered_file_list = []

        while state != STATE_DONE: # --------------------------------- Fax state machine
            if self.check_for_cancel():
                state = STATE_ABORTED

            log.debug("STATE=(%d, 0, 0)" % state)

            if state == STATE_ABORTED: # --------------------------------- Aborted (10, 0, 0)
                log.error("Aborted by user.")
                self.write_queue((STATUS_IDLE, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_SUCCESS: # --------------------------------- Success (20, 0, 0)
                log.debug("Success.")
                self.write_queue((STATUS_COMPLETED, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_ERROR: # --------------------------------- Error (130, 0, 0)
                log.error("Error, aborting.")
                self.write_queue((STATUS_ERROR, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_BUSY: # --------------------------------- Busy (25, 0, 0)
                log.error("Device busy, aborting.")
                self.write_queue((STATUS_BUSY, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_READ_SENDER_INFO: # --------------------------------- Get sender info (30, 0, 0)
                log.debug("%s State: Get sender info" % ("*"*20))
                state = STATE_PRERENDER
                try:
                    try:
                        self.dev.open()
                    except Error as e:
                        log.error("Unable to open device (%s)." % e.msg)
                        state = STATE_ERROR
                    else:
                        try:
                            self.sender_name = self.dev.station_name
                            log.debug("Sender name=%s" % self.sender_name)
                            self.sender_fax = self.dev.phone_num
                            log.debug("Sender fax=%s" % self.sender_fax)
                        except Error:
                            log.error("HTTP GET failed!")
                            state = STATE_ERROR

                finally:
                    self.dev.close()


            elif state == STATE_PRERENDER: # --------------------------------- Pre-render non-G4 files (40, 0, 0)
                log.debug("%s State: Pre-render non-G4 files" % ("*"*20))
                state = self.pre_render(STATE_COUNT_PAGES)

            elif state == STATE_COUNT_PAGES: # --------------------------------- Get total page count (50, 0, 0)
                log.debug("%s State: Get total page count" % ("*"*20))
                state = self.count_pages(STATE_NEXT_RECIPIENT)

            elif state == STATE_NEXT_RECIPIENT: # --------------------------------- Loop for multiple recipients (60, 0, 0)
                log.debug("%s State: Next recipient" % ("*"*20))
                state = STATE_COVER_PAGE

                try:
                    recipient = next(next_recipient)
                    log.debug("Processing for recipient %s" % recipient['name'])
                    self.write_queue((STATUS_SENDING_TO_RECIPIENT, 0, recipient['name']))
                except StopIteration:
                    state = STATE_SUCCESS
                    log.debug("Last recipient.")
                    continue

                recipient_file_list = self.rendered_file_list[:]


            elif state == STATE_COVER_PAGE: # --------------------------------- Create cover page (70, 0, 0)
                log.debug("%s State: Render cover page" % ("*"*20))
                state = self.cover_page(recipient)


            elif state == STATE_SINGLE_FILE: # --------------------------------- Special case for single file (no merge) (80, 0, 0)
                log.debug("%s State: Handle single file" % ("*"*20))
                state = self.single_file(STATE_SEND_FAX)

            elif state == STATE_MERGE_FILES: # --------------------------------- Merge multiple G4 files (90, 0, 0)
                log.debug("%s State: Merge multiple files" % ("*"*20))
                state = self.merge_files(STATE_SEND_FAX)

            elif state == STATE_SEND_FAX: # --------------------------------- Send fax state machine (110, 0, 0)
                log.debug("%s State: Send fax" % ("*"*20))
                state = STATE_NEXT_RECIPIENT

                FAX_SEND_STATE_DONE = 0
                FAX_SEND_STATE_ABORT = 10
                FAX_SEND_STATE_ERROR = 20
                FAX_SEND_STATE_BUSY = 25
                FAX_SEND_STATE_SUCCESS = 30
                FAX_SEND_STATE_DEVICE_OPEN = 40
                FAX_SEND_STATE_BEGINJOB = 50
                FAX_SEND_STATE_DOWNLOADPAGES = 60
                FAX_SEND_STATE_ENDJOB = 70
                FAX_SEND_STATE_CANCELJOB = 80
                FAX_SEND_STATE_CLOSE_SESSION = 170

                monitor_state = False
                fax_send_state = FAX_SEND_STATE_DEVICE_OPEN

                while fax_send_state != FAX_SEND_STATE_DONE:

                    if self.check_for_cancel():
                        log.error("Fax send aborted.")
                        fax_send_state = FAX_SEND_STATE_ABORT

                    if monitor_state:
                        fax_state = self.getFaxDownloadState()
                        if not fax_state in (pml.UPDN_STATE_XFERACTIVE, pml.UPDN_STATE_XFERDONE):
                            log.error("D/L error state=%d" % fax_state)
                            fax_send_state = FAX_SEND_STATE_ERROR
                            state = STATE_ERROR

                    log.debug("STATE=(%d, %d, 0)" % (STATE_SEND_FAX, fax_send_state))

                    if fax_send_state == FAX_SEND_STATE_ABORT: # -------------- Abort (110, 10, 0)
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CANCELJOB
                        state = STATE_ABORTED

                    elif fax_send_state == FAX_SEND_STATE_ERROR: # -------------- Error (110, 20, 0)
                        log.error("Fax send error.")
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        state = STATE_ERROR

                    elif fax_send_state == FAX_SEND_STATE_BUSY: # -------------- Busy (110, 25, 0)
                        log.error("Fax device busy.")
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        state = STATE_BUSY

                    elif fax_send_state == FAX_SEND_STATE_SUCCESS: # -------------- Success (110, 30, 0)
                        log.debug("Fax send success.")
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        state = STATE_NEXT_RECIPIENT

                    elif fax_send_state == FAX_SEND_STATE_DEVICE_OPEN: # -------------- Device open (110, 40, 0)
                        log.debug("%s State: Open device" % ("*"*20))
                        fax_send_state = FAX_SEND_STATE_BEGINJOB
                        try:
                            self.dev.open()
                        except Error as e:
                            log.error("Unable to open device (%s)." % e.msg)
                            fax_send_state = FAX_SEND_STATE_ERROR
                        else:
                            if self.dev.device_state == DEVICE_STATE_NOT_FOUND:
                                fax_send_state = FAX_SEND_STATE_ERROR

                    elif fax_send_state == FAX_SEND_STATE_BEGINJOB: # -------------- BeginJob (110, 50, 0)
                        log.debug("%s State: BeginJob" % ("*"*20))

                        try:
                            ff = open(self.f, 'rb')
                        except IOError:
                            log.error("Unable to read fax file.")
                            fax_send_state = FAX_SEND_STATE_ERROR
                            continue

                        try:
                            header = ff.read(FILE_HEADER_SIZE)
                        except IOError:
                            log.error("Unable to read fax file.")
                            fax_send_state = FAX_SEND_STATE_ERROR
                            continue

                        magic, version, total_pages, hort_dpi, vert_dpi, page_size, \
                            resolution, encoding, reserved1, reserved2 = self.decode_fax_header(header)

                        if magic != b'hplip_g3':
                            log.error("Invalid file header. Bad magic.")
                            fax_send_state = FAX_SEND_STATE_ERROR
                        else:
                            log.debug("Magic=%s Ver=%d Pages=%d hDPI=%d vDPI=%d Size=%d Res=%d Enc=%d" %
                                      (magic, version, total_pages, hort_dpi, vert_dpi, page_size,
                                       resolution, encoding))

                        job_id = self.job_id
                        delay = 0
                        faxnum = recipient['fax']
                        speeddial = 0

                        if resolution == RESOLUTION_STD:
                            res = "STANDARD"
                        elif resolution == RESOLUTION_FINE:
                            res = "FINE"
                        elif resolution == RESOLUTION_300DPI:
                            res = "SUPERFINE"

                        soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><Fax:BeginJob xmlns:Fax="urn:Fax"><ticket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Fax:Ticket"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$job_id</jobId><resolution xsi:type="Fax:Resolution">$res</resolution><delay xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$delay</delay><phoneNumber xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$faxnum</phoneNumber><speedDial xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$speeddial</speedDial></ticket></Fax:BeginJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                        data = self.format_http(soap.encode('utf-8'))

                        log.log_data(data)

                        if log.is_debug():
                            open('beginjob.log', 'wb').write(data)
                        self.dev.openSoapFax()
                        self.dev.writeSoapFax(data)
                        ret = BytesIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass

                        ret = ret.getvalue()

                        if log.is_debug():
                            open('beginjob_ret.log', 'wb').write(ret)
                        log.log_data(ret)
                        self.dev.closeSoapFax()

                        if self.get_error_code(ret.decode('utf-8')) == HTTP_OK:
                            fax_send_state = FAX_SEND_STATE_DOWNLOADPAGES
                        else:
                            fax_send_state = FAX_SEND_STATE_ERROR


                    elif fax_send_state == FAX_SEND_STATE_DOWNLOADPAGES: # -------------- DownloadPages (110, 60, 0)
                        log.debug("%s State: DownloadPages" % ("*"*20))
                        page = BytesIO()
                        for p in range(total_pages):

                            if self.check_for_cancel():
                                fax_send_state = FAX_SEND_STATE_ABORT

                            if fax_send_state == FAX_SEND_STATE_ABORT:
                                break

                            try:
                                header = ff.read(PAGE_HEADER_SIZE)
                            except IOError:
                                log.error("Unable to read fax file.")
                                fax_send_state = FAX_SEND_STATE_ERROR
                                continue

                            page_num, ppr, rpp, bytes_to_read, thumbnail_bytes, reserved2 = \
                                self.decode_page_header(header)

                            log.debug("Page=%d PPR=%d RPP=%d BPP=%d Thumb=%d" %
                                      (page_num, ppr, rpp, bytes_to_read, thumbnail_bytes))

                            if ppr != PIXELS_PER_LINE:
                                log.error("Pixels per line (width) must be %d!" % PIXELS_PER_LINE)

                            page.write(ff.read(bytes_to_read))
                            thumbnail = ff.read(thumbnail_bytes) # thrown away for now (should be 0 read)
                            page.seek(0)

                            try:
                                data = page.read(bytes_to_read)
                            except IOError:
                                log.error("Unable to read fax file.")
                                fax_send_state = FAX_SEND_STATE_ERROR
                                break

                            if data == b'':
                                log.error("No data!")
                                fax_send_state = FAX_SEND_STATE_ERROR
                                break

                            height = rpp
                            job_id = self.job_id

                            soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:DownloadPage xmlns:Fax="urn:Fax"><height xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:positiveInteger">$height</height></Fax:DownloadPage></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                            m = dime.Message()
                            m.add_record(dime.Record(b"cid:id0", b"http://schemas.xmlsoap.org/soap/envelope/",
                                dime.TYPE_T_URI, to_bytes_utf8(soap)))

                            m.add_record(dime.Record(b"", b"image/g4fax", dime.TYPE_T_MIME, data))

                            output = BytesIO()
                            m.generate(output)
                            data = self.format_http(output.getvalue(), content_type="application/dime")
                            log.log_data(data)
                            if log.is_debug():
                                           open('downloadpages%d.log' % p, 'wb').write(data)
                            try:
                                self.dev.writeSoapFax(data)
                            except Error:
                                fax_send_state = FAX_SEND_STATE_ERROR

                            ret = BytesIO()

                            try:
                                while self.dev.readSoapFax(8192, ret, timeout=5):
                                    pass
                            except Error:
                                fax_send_state = FAX_SEND_STATE_ERROR

                            ret = ret.getvalue()

                            if log.is_debug():
                                open('downloadpages%d_ret.log' % p, 'wb').write(ret)

                            log.log_data(ret)
                            self.dev.closeSoapFax()

                            if self.get_error_code(ret.decode('utf-8')) != HTTP_OK:
                                fax_send_state = FAX_SEND_STATE_ERROR
                                break

                            page.truncate(0)
                            page.seek(0)

                        else:
                            fax_send_state = FAX_SEND_STATE_ENDJOB


                    elif fax_send_state == FAX_SEND_STATE_ENDJOB: # -------------- EndJob (110, 70, 0)
                        log.debug("%s State: EndJob" % ("*"*20))

                        job_id = self.job_id

                        soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:EndJob xmlns:Fax="urn:Fax"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">$job_id</jobId></Fax:EndJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                        data = self.format_http(soap.encode('utf-8'))

                        log.log_data(data)

                        if log.is_debug():
                            open('endjob.log', 'wb').write(data)

                        self.dev.writeSoapFax(data)
                        ret = BytesIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass

                        ret = ret.getvalue()

                        if log.is_debug():
                            open('endjob_ret.log', 'wb').write(ret)

                        log.log_data(ret)
                        self.dev.closeSoapFax()

                        if self.get_error_code(ret.decode('utf-8')) == HTTP_OK:
                            fax_send_state = FAX_SEND_STATE_SUCCESS
                        else:
                            fax_send_state = FAX_SEND_STATE_ERROR

                    elif fax_send_state == FAX_SEND_STATE_CANCELJOB: # -------------- CancelJob (110, 80, 0)
                        log.debug("%s State: CancelJob" % ("*"*20))

                        job_id = self.job_id

                        soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:CancelJob xmlns:Fax="urn:Fax"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">$job_id</jobId></Fax:CancelJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                        data = self.format_http(soap.encode('utf-8'))

                        log.log_data(data)

                        if log.is_debug():
                            open('canceljob.log', 'wb').write(data)

                        self.dev.writeSoapFax(data)
                        ret = BytesIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass

                        ret = ret.getvalue()

                        if log.is_debug():
                            open('canceljob_ret.log', 'wb').write(ret)

                        log.log_data(ret)
                        self.dev.closeSoapFax()

                        if self.get_error_code(ret.decode('utf-8')) == HTTP_OK:
                            fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        else:
                            fax_send_state = FAX_SEND_STATE_ERROR


                    elif fax_send_state == FAX_SEND_STATE_CLOSE_SESSION: # -------------- Close session (110, 170, 0)
                        log.debug("%s State: Close session" % ("*"*20))
                        log.debug("Closing session...")

                        try:
                            mm.close()
                        except NameError:
                            pass

                        try:
                            ff.close()
                        except NameError:
                            pass

                        time.sleep(1)

                        self.dev.closeSoapFax()
                        self.dev.close()

                        fax_send_state = FAX_SEND_STATE_DONE # Exit inner state machine


            elif state == STATE_CLEANUP: # --------------------------------- Cleanup (120, 0, 0)
                log.debug("%s State: Cleanup" % ("*"*20))

                if self.remove_temp_file:
                    log.debug("Removing merged file: %s" % self.f)
                    try:
                        os.remove(self.f)
                        log.debug("Removed")
                    except OSError:
                        log.debug("Not found")

                state = STATE_DONE # Exit outer state machine
Example #14
0
                                      (magic, version, total_pages, hort_dpi, vert_dpi, page_size,
                                       resolution, encoding))

                        job_id = self.job_id
                        delay = 0
                        faxnum = recipient['fax'].encode('ascii')
                        speeddial = 0

                        if resolution == RESOLUTION_STD:
                            res = "STANDARD"
                        elif resolution == RESOLUTION_FINE:
                            res = "FINE"
                        elif resolution == RESOLUTION_300DPI:
                            res = "SUPERFINE"

                        soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><Fax:BeginJob xmlns:Fax="urn:Fax"><ticket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Fax:Ticket"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$job_id</jobId><resolution xsi:type="Fax:Resolution">$res</resolution><delay xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$delay</delay><phoneNumber xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$faxnum</phoneNumber><speedDial xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$speeddial</speedDial></ticket></Fax:BeginJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                        data = self.format_http(soap)
                        log.log_data(data)

                        if log.is_debug():
                            file('beginjob.log', 'w').write(data)

                        self.dev.openSoapFax()
                        self.dev.writeSoapFax(data)
                        ret = cStringIO.StringIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass

                        ret = ret.getvalue()
Example #15
0
                if core.dependencies[d][0]:
                    log.error("NOT FOUND! This is a REQUIRED%s dependency. Please make sure that this dependency is installed before installing or running HPLIP." % s)
                else:
                    log.warn("NOT FOUND! This is an OPTIONAL%s dependency. Some HPLIP functionality may not function properly." %s)

                if core.distro_supported():
                    packages_to_install, commands = core.get_dependency_data(d)

                    commands_to_run = []

                    if packages_to_install:
                        package_mgr_cmd = core.get_distro_data('package_mgr_cmd')

                        if package_mgr_cmd:
                            packages_to_install = ' '.join(packages_to_install)
                            commands_to_run.append(utils.cat(package_mgr_cmd))

                    if commands:
                        commands_to_run.extend(commands)

                    overall_commands_to_run.extend(commands_to_run)

                    if len(commands_to_run) == 1:
                        log.info("To install this dependency, execute this command:")
                        log.info(commands_to_run[0])

                    elif len(commands_to_run) > 1:
                        log.info("To install this dependency, execute these commands:")
                        for c in commands_to_run:
                            log.info(c)
Example #16
0
                    log.warn(
                        "NOT FOUND! This is an OPTIONAL%s dependency. Some HPLIP functionality may not function properly."
                        % s)

                if core.distro_supported():
                    packages_to_install, commands = core.get_dependency_data(d)

                    commands_to_run = []

                    if packages_to_install:
                        package_mgr_cmd = core.get_distro_data(
                            'package_mgr_cmd')

                        if package_mgr_cmd:
                            packages_to_install = ' '.join(packages_to_install)
                            commands_to_run.append(utils.cat(package_mgr_cmd))

                    if commands:
                        commands_to_run.extend(commands)

                    overall_commands_to_run.extend(commands_to_run)

                    if len(commands_to_run) == 1:
                        log.info(
                            "To install this dependency, execute this command:"
                        )
                        log.info(commands_to_run[0])

                    elif len(commands_to_run) > 1:
                        log.info(
                            "To install this dependency, execute these commands:"
Example #17
0
    def __getPluginFilesList(self, src_dir):
        if not os.path.exists(src_dir+"/plugin.spec"):
            log.warn("%s/plugin.spec file doesn't exists."%src_dir)
            return []

        cwd = os.getcwd()
        os.chdir(src_dir)
        
        plugin_spec = ConfigBase("plugin.spec")
        products = plugin_spec.keys("products")

        BITNESS = utils.getBitness()
        ENDIAN = utils.getEndian()
        PPDDIR = sys_conf.get('dirs', 'ppd')
        DRVDIR = sys_conf.get('dirs', 'drv')
        HOMEDIR = sys_conf.get('dirs', 'home')
        DOCDIR = sys_conf.get('dirs', 'doc')
        CUPSBACKENDDIR = sys_conf.get('dirs', 'cupsbackend')
        CUPSFILTERDIR = sys_conf.get('dirs', 'cupsfilter')
        RULESDIR = '/etc/udev/rules.d'
        BIN = sys_conf.get('dirs', 'bin')

        # Copying plugin.spec file to home dir.
        if src_dir != HOMEDIR:
            shutil.copyfile(src_dir+"/plugin.spec", HOMEDIR+"/plugin.spec")
            os.chmod(HOMEDIR+"/plugin.spec",0644)

        processor = utils.getProcessor()
        if processor == 'power_machintosh':
            ARCH = 'ppc'
        else:
            ARCH = 'x86_%d' % BITNESS

        if BITNESS == 64:
            SANELIBDIR = '/usr/lib64/sane'
            LIBDIR = '/usr/lib64'
        else:
            SANELIBDIR = '/usr/lib/sane'
            LIBDIR = '/usr/lib'

        copies = []

        for PRODUCT in products:
            MODEL = PRODUCT.replace('hp-', '').replace('hp_', '')
            for s in plugin_spec.get("products", PRODUCT).split(','):

                if not plugin_spec.has_section(s):
                    log.error("Missing section [%s]" % s)
                    os.chdir(cwd)
                    return []

                src = plugin_spec.get(s, 'src', '')
                trg = plugin_spec.get(s, 'trg', '')
                link = plugin_spec.get(s, 'link', '')

                if not src:
                    log.error("Missing 'src=' value in section [%s]" % s)
                    os.chdir(cwd)
                    return []

                if not trg:
                    log.error("Missing 'trg=' value in section [%s]" % s)
                    os.chdir(cwd)
                    return []

                src = os.path.basename(utils.cat(src))
                trg = utils.cat(trg)

                if link:
                    link = utils.cat(link)

                copies.append((src, trg, link))

        copies = utils.uniqueList(copies)
        copies.sort()
        os.chdir(cwd)
        return copies
Example #18
0
    def run(self):
        #results = {} # {'file' : error_code,...}

        STATE_DONE = 0
        STATE_ABORTED = 10
        STATE_SUCCESS = 20
        STATE_BUSY = 25
        STATE_READ_SENDER_INFO = 30
        STATE_PRERENDER = 40
        STATE_COUNT_PAGES = 50
        STATE_NEXT_RECIPIENT = 60
        STATE_COVER_PAGE = 70
        STATE_SINGLE_FILE = 80
        STATE_MERGE_FILES = 90
        STATE_SINGLE_FILE = 100
        STATE_SEND_FAX = 110
        STATE_CLEANUP = 120
        STATE_ERROR = 130

        next_recipient = self.next_recipient_gen()

        state = STATE_READ_SENDER_INFO
        self.rendered_file_list = []

        while state != STATE_DONE: # --------------------------------- Fax state machine
            if self.check_for_cancel():
                state = STATE_ABORTED

            log.debug("STATE=(%d, 0, 0)" % state)

            if state == STATE_ABORTED: # --------------------------------- Aborted (10, 0, 0)
                log.error("Aborted by user.")
                self.write_queue((STATUS_IDLE, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_SUCCESS: # --------------------------------- Success (20, 0, 0)
                log.debug("Success.")
                self.write_queue((STATUS_COMPLETED, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_ERROR: # --------------------------------- Error (130, 0, 0)
                log.error("Error, aborting.")
                self.write_queue((STATUS_ERROR, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_BUSY: # --------------------------------- Busy (25, 0, 0)
                log.error("Device busy, aborting.")
                self.write_queue((STATUS_BUSY, 0, ''))
                state = STATE_CLEANUP


            elif state == STATE_READ_SENDER_INFO: # --------------------------------- Get sender info (30, 0, 0)
                log.debug("%s State: Get sender info" % ("*"*20))
                state = STATE_PRERENDER
                try:
                    try:
                        self.dev.open()
                    except Error as e:
                        log.error("Unable to open device (%s)." % e.msg)
                        state = STATE_ERROR
                    else:
                        try:
                            self.sender_name = self.dev.station_name
                            log.debug("Sender name=%s" % self.sender_name)
                            self.sender_fax = self.dev.phone_num
                            log.debug("Sender fax=%s" % self.sender_fax)
                        except Error:
                            log.error("HTTP GET failed!")
                            state = STATE_ERROR

                finally:
                    self.dev.close()


            elif state == STATE_PRERENDER: # --------------------------------- Pre-render non-G4 files (40, 0, 0)
                log.debug("%s State: Pre-render non-G4 files" % ("*"*20))
                state = self.pre_render(STATE_COUNT_PAGES)

            elif state == STATE_COUNT_PAGES: # --------------------------------- Get total page count (50, 0, 0)
                log.debug("%s State: Get total page count" % ("*"*20))
                state = self.count_pages(STATE_NEXT_RECIPIENT)

            elif state == STATE_NEXT_RECIPIENT: # --------------------------------- Loop for multiple recipients (60, 0, 0)
                log.debug("%s State: Next recipient" % ("*"*20))
                state = STATE_COVER_PAGE

                try:
                    recipient = next(next_recipient)
                    log.debug("Processing for recipient %s" % recipient['name'])
                    self.write_queue((STATUS_SENDING_TO_RECIPIENT, 0, recipient['name']))
                except StopIteration:
                    state = STATE_SUCCESS
                    log.debug("Last recipient.")
                    continue

                recipient_file_list = self.rendered_file_list[:]


            elif state == STATE_COVER_PAGE: # --------------------------------- Create cover page (70, 0, 0)
                log.debug("%s State: Render cover page" % ("*"*20))
                state = self.cover_page(recipient)


            elif state == STATE_SINGLE_FILE: # --------------------------------- Special case for single file (no merge) (80, 0, 0)
                log.debug("%s State: Handle single file" % ("*"*20))
                state = self.single_file(STATE_SEND_FAX)

            elif state == STATE_MERGE_FILES: # --------------------------------- Merge multiple G4 files (90, 0, 0)
                log.debug("%s State: Merge multiple files" % ("*"*20))
                state = self.merge_files(STATE_SEND_FAX)

            elif state == STATE_SEND_FAX: # --------------------------------- Send fax state machine (110, 0, 0)
                log.debug("%s State: Send fax" % ("*"*20))
                state = STATE_NEXT_RECIPIENT

                FAX_SEND_STATE_DONE = 0
                FAX_SEND_STATE_ABORT = 10
                FAX_SEND_STATE_ERROR = 20
                FAX_SEND_STATE_BUSY = 25
                FAX_SEND_STATE_SUCCESS = 30
                FAX_SEND_STATE_DEVICE_OPEN = 40
                FAX_SEND_STATE_BEGINJOB = 50
                FAX_SEND_STATE_DOWNLOADPAGES = 60
                FAX_SEND_STATE_ENDJOB = 70
                FAX_SEND_STATE_CANCELJOB = 80
                FAX_SEND_STATE_CLOSE_SESSION = 170

                monitor_state = False
                fax_send_state = FAX_SEND_STATE_DEVICE_OPEN

                while fax_send_state != FAX_SEND_STATE_DONE:

                    if self.check_for_cancel():
                        log.error("Fax send aborted.")
                        fax_send_state = FAX_SEND_STATE_ABORT

                    if monitor_state:
                        fax_state = self.getFaxDownloadState()
                        if not fax_state in (pml.UPDN_STATE_XFERACTIVE, pml.UPDN_STATE_XFERDONE):
                            log.error("D/L error state=%d" % fax_state)
                            fax_send_state = FAX_SEND_STATE_ERROR
                            state = STATE_ERROR

                    log.debug("STATE=(%d, %d, 0)" % (STATE_SEND_FAX, fax_send_state))

                    if fax_send_state == FAX_SEND_STATE_ABORT: # -------------- Abort (110, 10, 0)
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CANCELJOB
                        state = STATE_ABORTED

                    elif fax_send_state == FAX_SEND_STATE_ERROR: # -------------- Error (110, 20, 0)
                        log.error("Fax send error.")
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        state = STATE_ERROR

                    elif fax_send_state == FAX_SEND_STATE_BUSY: # -------------- Busy (110, 25, 0)
                        log.error("Fax device busy.")
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        state = STATE_BUSY

                    elif fax_send_state == FAX_SEND_STATE_SUCCESS: # -------------- Success (110, 30, 0)
                        log.debug("Fax send success.")
                        monitor_state = False
                        fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        state = STATE_NEXT_RECIPIENT

                    elif fax_send_state == FAX_SEND_STATE_DEVICE_OPEN: # -------------- Device open (110, 40, 0)
                        log.debug("%s State: Open device" % ("*"*20))
                        fax_send_state = FAX_SEND_STATE_BEGINJOB
                        try:
                            self.dev.open()
                        except Error as e:
                            log.error("Unable to open device (%s)." % e.msg)
                            fax_send_state = FAX_SEND_STATE_ERROR
                        else:
                            if self.dev.device_state == DEVICE_STATE_NOT_FOUND:
                                fax_send_state = FAX_SEND_STATE_ERROR

                    elif fax_send_state == FAX_SEND_STATE_BEGINJOB: # -------------- BeginJob (110, 50, 0)
                        log.debug("%s State: BeginJob" % ("*"*20))

                        try:
                            ff = open(self.f, 'rb')
                        except IOError:
                            log.error("Unable to read fax file.")
                            fax_send_state = FAX_SEND_STATE_ERROR
                            continue

                        try:
                            header = ff.read(FILE_HEADER_SIZE)
                        except IOError:
                            log.error("Unable to read fax file.")
                            fax_send_state = FAX_SEND_STATE_ERROR
                            continue

                        magic, version, total_pages, hort_dpi, vert_dpi, page_size, \
                            resolution, encoding, reserved1, reserved2 = self.decode_fax_header(header)

                        if magic != b'hplip_g3':
                            log.error("Invalid file header. Bad magic.")
                            fax_send_state = FAX_SEND_STATE_ERROR
                        else:
                            log.debug("Magic=%s Ver=%d Pages=%d hDPI=%d vDPI=%d Size=%d Res=%d Enc=%d" %
                                      (magic, version, total_pages, hort_dpi, vert_dpi, page_size,
                                       resolution, encoding))

                        job_id = self.job_id
                        delay = 0
                        faxnum = recipient['fax']
                        speeddial = 0

                        if resolution == RESOLUTION_STD:
                            res = "STANDARD"
                        elif resolution == RESOLUTION_FINE:
                            res = "FINE"
                        elif resolution == RESOLUTION_300DPI:
                            res = "SUPERFINE"

                        soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><Fax:BeginJob xmlns:Fax="urn:Fax"><ticket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Fax:Ticket"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$job_id</jobId><resolution xsi:type="Fax:Resolution">$res</resolution><delay xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$delay</delay><phoneNumber xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$faxnum</phoneNumber><speedDial xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$speeddial</speedDial></ticket></Fax:BeginJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                        data = self.format_http(soap.encode('utf-8'))

                        log.log_data(data)

                        if log.is_debug():
                            open('beginjob.log', 'wb').write(data)
                        self.dev.openSoapFax()
                        self.dev.writeSoapFax(data)
                        ret = BytesIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass

                        ret = ret.getvalue()

                        if log.is_debug():
                            open('beginjob_ret.log', 'wb').write(ret)
                        log.log_data(ret)
                        self.dev.closeSoapFax()

                        if self.get_error_code(ret.decode('utf-8')) == HTTP_OK:
                            fax_send_state = FAX_SEND_STATE_DOWNLOADPAGES
                        else:
                            fax_send_state = FAX_SEND_STATE_ERROR


                    elif fax_send_state == FAX_SEND_STATE_DOWNLOADPAGES: # -------------- DownloadPages (110, 60, 0)
                        log.debug("%s State: DownloadPages" % ("*"*20))
                        page = BytesIO()
                        for p in range(total_pages):

                            if self.check_for_cancel():
                                fax_send_state = FAX_SEND_STATE_ABORT

                            if fax_send_state == FAX_SEND_STATE_ABORT:
                                break

                            try:
                                header = ff.read(PAGE_HEADER_SIZE)
                            except IOError:
                                log.error("Unable to read fax file.")
                                fax_send_state = FAX_SEND_STATE_ERROR
                                continue

                            page_num, ppr, rpp, bytes_to_read, thumbnail_bytes, reserved2 = \
                                self.decode_page_header(header)

                            log.debug("Page=%d PPR=%d RPP=%d BPP=%d Thumb=%d" %
                                      (page_num, ppr, rpp, bytes_to_read, thumbnail_bytes))

                            if ppr != PIXELS_PER_LINE:
                                log.error("Pixels per line (width) must be %d!" % PIXELS_PER_LINE)

                            page.write(ff.read(bytes_to_read))
                            thumbnail = ff.read(thumbnail_bytes) # thrown away for now (should be 0 read)
                            page.seek(0)

                            try:
                                data = page.read(bytes_to_read)
                            except IOError:
                                log.error("Unable to read fax file.")
                                fax_send_state = FAX_SEND_STATE_ERROR
                                break

                            if data == b'':
                                log.error("No data!")
                                fax_send_state = FAX_SEND_STATE_ERROR
                                break

                            height = rpp
                            job_id = self.job_id

                            soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:DownloadPage xmlns:Fax="urn:Fax"><height xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:positiveInteger">$height</height></Fax:DownloadPage></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                            m = dime.Message()
                            m.add_record(dime.Record(b"cid:id0", b"http://schemas.xmlsoap.org/soap/envelope/",
                                dime.TYPE_T_URI, to_bytes_utf8(soap)))

                            m.add_record(dime.Record(b"", b"image/g4fax", dime.TYPE_T_MIME, data))

                            output = BytesIO()
                            m.generate(output)
                            data = self.format_http(output.getvalue(), content_type="application/dime")
                            log.log_data(data)
                            if log.is_debug():
                                           open('downloadpages%d.log' % p, 'wb').write(data)
                            try:
                                self.dev.writeSoapFax(data)
                            except Error:
                                fax_send_state = FAX_SEND_STATE_ERROR

                            ret = BytesIO()

                            try:
                                while self.dev.readSoapFax(8192, ret, timeout=5):
                                    pass
                            except Error:
                                fax_send_state = FAX_SEND_STATE_ERROR

                            ret = ret.getvalue()

                            if log.is_debug():
                                open('downloadpages%d_ret.log' % p, 'wb').write(ret)

                            log.log_data(ret)
                            self.dev.closeSoapFax()

                            if self.get_error_code(ret.decode('utf-8')) != HTTP_OK:
                                fax_send_state = FAX_SEND_STATE_ERROR
                                break

                            page.truncate(0)
                            page.seek(0)

                        else:
                            fax_send_state = FAX_SEND_STATE_ENDJOB


                    elif fax_send_state == FAX_SEND_STATE_ENDJOB: # -------------- EndJob (110, 70, 0)
                        log.debug("%s State: EndJob" % ("*"*20))

                        job_id = self.job_id

                        soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:EndJob xmlns:Fax="urn:Fax"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">$job_id</jobId></Fax:EndJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                        data = self.format_http(soap.encode('utf-8'))

                        log.log_data(data)

                        if log.is_debug():
                            open('endjob.log', 'wb').write(data)

                        self.dev.writeSoapFax(data)
                        ret = BytesIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass

                        ret = ret.getvalue()

                        if log.is_debug():
                            open('endjob_ret.log', 'wb').write(ret)

                        log.log_data(ret)
                        self.dev.closeSoapFax()

                        if self.get_error_code(ret.decode('utf-8')) == HTTP_OK:
                            fax_send_state = FAX_SEND_STATE_SUCCESS
                        else:
                            fax_send_state = FAX_SEND_STATE_ERROR

                    elif fax_send_state == FAX_SEND_STATE_CANCELJOB: # -------------- CancelJob (110, 80, 0)
                        log.debug("%s State: CancelJob" % ("*"*20))

                        job_id = self.job_id

                        soap = utils.cat(
"""<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:CancelJob xmlns:Fax="urn:Fax"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">$job_id</jobId></Fax:CancelJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""")

                        data = self.format_http(soap.encode('utf-8'))

                        log.log_data(data)

                        if log.is_debug():
                            open('canceljob.log', 'wb').write(data)

                        self.dev.writeSoapFax(data)
                        ret = BytesIO()

                        while self.dev.readSoapFax(8192, ret, timeout=5):
                            pass

                        ret = ret.getvalue()

                        if log.is_debug():
                            open('canceljob_ret.log', 'wb').write(ret)

                        log.log_data(ret)
                        self.dev.closeSoapFax()

                        if self.get_error_code(ret.decode('utf-8')) == HTTP_OK:
                            fax_send_state = FAX_SEND_STATE_CLOSE_SESSION
                        else:
                            fax_send_state = FAX_SEND_STATE_ERROR


                    elif fax_send_state == FAX_SEND_STATE_CLOSE_SESSION: # -------------- Close session (110, 170, 0)
                        log.debug("%s State: Close session" % ("*"*20))
                        log.debug("Closing session...")

                        try:
                            mm.close()
                        except NameError:
                            pass

                        try:
                            ff.close()
                        except NameError:
                            pass

                        time.sleep(1)

                        self.dev.closeSoapFax()
                        self.dev.close()

                        fax_send_state = FAX_SEND_STATE_DONE # Exit inner state machine


            elif state == STATE_CLEANUP: # --------------------------------- Cleanup (120, 0, 0)
                log.debug("%s State: Cleanup" % ("*"*20))

                if self.remove_temp_file:
                    log.debug("Removing merged file: %s" % self.f)
                    try:
                        os.remove(self.f)
                        log.debug("Removed")
                    except OSError:
                        log.debug("Not found")

                state = STATE_DONE # Exit outer state machine