Beispiel #1
0
def do_bash_checks(file_name, module_name, all_checks, strict_checks):
    """Run app bash checks"""

    # First attempt to execute.
    if not utils.is_executable(file_name):
        log.e(TAG, "[FAIL] Module is not marked executable!")
        return -1

    # Next attempt to auto parse
    item = pm.parse_bash_module(file_name, module_name)

    if item is None:
        log.e(TAG, "[FAIL] Auto parse failed!")
        return -1

    if do_auto_checks(item, strict_checks) != 0:
        return -1

    if all_checks:
        log.d(TAG, "Running checkbashisms...")
        if run_command("checkbashisms -f \"%s\"" % file_name) != 0:
            log.e(TAG, "[FAIL] checkbashisms failed.")
            return -1

        log.d(TAG, "Running shellcheck...")
        if run_command("shellcheck \"%s\"" % file_name) != 0:
            log.e(TAG, "[FAIL] shellcheck failed.")
            return -1

    log.i(TAG, "[PASS] All checks passed!")

    return 0
Beispiel #2
0
    def do_included_upgrade(self, args):

        """Perform upgrade of only included"""

        parser = ArgumentParser(prog='upgrade included',
                                description='Update the bundled TAR.')
        parser.add_argument('--branch', dest='branch',
                            default=BRANCH_DEFAULT,
                            help="Specify the branch to pull from.")

        parsed_args = parser.parse_args(args)

        log.i(self.name, "Performing included upgrade...")

        upgrade_tar_url = UPGRADE_TAR_TEMPLATE % parsed_args.branch

        # First pull
        tar_name = self.__download_file(upgrade_tar_url, 'dtf_included.tar')
        if tar_name is None:
            log.e(self.name, "Unable to download: %s" % upgrade_tar_url)
            return -1

        # Now we perform the reconfiguration
        return autoconfig.initialize_from_tar(tar_name, is_full=False,
                                              clean_up=True)
Beispiel #3
0
def test_info_suppress():
    """Log an informational message with logging at 2"""

    log.LOG_LEVEL_STDOUT = 2
    log.LOG_LEVEL_FILE = 2

    log.i(TAG, "This is information!")
Beispiel #4
0
def delete_library(name, force=False):

    """Remove a library"""

    rtn = 0

    # First check if we know about the library
    if is_library_installed(name):

        item = Item()
        item.name = name
        item.type = TYPE_LIBRARY

        installed_item = __load_item(item)

        # Prompt for removal
        if not force:

            if __prompt_delete(installed_item):
                log.d(TAG, "User would like to remove")
                rtn = __do_library_delete(installed_item)
            else:
                log.i(TAG, "Library deletion skipped.")
        # Force
        else:
            log.d(TAG, "Forcing component removal.")
            rtn = __do_library_delete(installed_item)
    else:
        log.e(TAG, "No library installed with this name.")
        rtn = -1

    return rtn
Beispiel #5
0
def install_single_package(item, force=False):

    """Install a single package"""

    rtn = 0

    try:
        # First check if we know about this package
        if is_binary_installed(item.name):

            # Prompt for install.
            if not force:
                print ("[WARNING] An item with this name is already installed."
                      " See details below.")

                installed_item = __load_item(item)

                if __prompt_install(item, installed_item):
                    log.d(TAG, "User would like to install")
                    rtn = __do_single_package_install(item)
                else:
                    log.i(TAG, "Package installation skipped.")
            # Force
            else:
                log.d(TAG, "Forcing component installation.")
                rtn = __do_single_package_install(item)
        else:
            log.d(TAG, "This is a new package, installing.")
            rtn = __do_single_package_install(item)

    except KeyError:
        log.w(TAG, "Error checking if the package was installed. Skipping")
        rtn = -4

    return rtn
Beispiel #6
0
def do_python_checks(file_name, module_name, all_checks, strict_checks):
    """Run all python checks"""

    # First attempt to auto parse
    item = pm.parse_python_module(file_name, module_name)

    if item is None:
        log.e(TAG, "[FAIL] Auto parse failed!")
        return -1

    if do_auto_checks(item, strict_checks) != 0:
        return -1

    if all_checks:
        log.d(TAG, "Running pylint...")
        if run_command("pylint \"%s\"" % file_name) != 0:
            log.e(TAG, "[FAIL] pylint failed.")
            return -1

        log.d(TAG, "Running flake8...")
        if run_command("flake8 \"%s\"" % file_name) != 0:
            log.e(TAG, "[FAIL] flake8 failed.")
            return -1

    log.i(TAG, "[PASS] All checks passed!")

    return 0
Beispiel #7
0
def delete_package(name, force=False):

    """Remove a package"""

    rtn = 0

    # First check if we know about the package
    if is_package_installed(name):

        item = Item()
        item.name = name
        item.type = TYPE_PACKAGE

        installed_item = __load_item(item)

        # Prompt for removal
        if not force:

            if __prompt_delete(installed_item):
                log.d(TAG, "User would like to remove")
                rtn = __do_package_delete(installed_item)
            else:
                log.i(TAG, "Package deletion skipped.")
        # Force
        else:
            log.d(TAG, "Forcing component removal.")
            rtn = __do_package_delete(installed_item)
    else:
        log.e(TAG, "No package installed with this name.")
        rtn = -1

    return rtn
Beispiel #8
0
def delete_library(name, force=False):
    """Remove a library"""

    rtn = 0

    # First check if we know about the library
    if is_library_installed(name):

        item = dtf.core.item.Item()
        item.name = name
        item.type = dtf.core.item.TYPE_LIBRARY

        installed_item = __load_item(item)

        # Prompt for removal
        if not force:

            if __prompt_delete(installed_item):
                log.d(TAG, "User would like to remove")
                rtn = __do_library_delete(installed_item)
            else:
                log.i(TAG, "Library deletion skipped.")
        # Force
        else:
            log.d(TAG, "Forcing component removal.")
            rtn = __do_library_delete(installed_item)
    else:
        log.e(TAG, "No library installed with this name.")
        rtn = -1

    return rtn
Beispiel #9
0
    def do_core_upgrade(self, args):

        """Perform upgrade of dtf"""

        parser = ArgumentParser(prog='upgrade core',
                                description='Update dtf framework.')
        parser.add_argument('--reconfigure', action='store_true',
                            help="Just reconfig (post upgrade).")
        parser.add_argument('--branch', dest='branch',
                            default=BRANCH_DEFAULT,
                            help="Specify the branch to pull from.")
        parsed_args = parser.parse_args(args)

        # First, is this just a reconfig?
        if parsed_args.reconfigure:
            log.i(self.name, "Performing reconfiguration...")
            return autoconfig.initialize_from_local(is_full=True)

        log.i(self.name, "Downloading update script...")

        upgrade_script_url = UPGRADE_SCRIPT_TEMPLATE % parsed_args.branch

        upgrade_script_name = self.__download_file(upgrade_script_url,
                                                   'dtf_upgrade.sh')
        if upgrade_script_name is None:
            log.e(self.name, "Unable to download: %s" % upgrade_script_url)
            return -1

        log.i(self.name, "Update script downloaded. To complete install run:")
        log.i(self.name, "  chmod u+x %s" % upgrade_script_name)
        log.i(self.name, "  %s" % upgrade_script_name)

        return 0
Beispiel #10
0
Datei: pm.py Projekt: 5l1v3r1/dtf
    def do_export(self, args):
        """Perform an export"""

        rtn = 0

        parser = ArgumentParser(prog='pm export',
                                description='Export installed content.')
        parser.add_argument('output_name',
                            type=str,
                            help='The output file name.')

        parsed_args = parser.parse_args(args)

        output_name = parsed_args.output_name

        if os.path.isfile(output_name):
            log.e(TAG, "Output file already exists!")
            return -1

        # Generate a list of populated items.
        export_items = self.generate_export_items()

        if len(export_items) == 0:
            log.e(TAG, "Nothing to export!")
            return -2

        export_zip = mp.ExportZip(output_name)

        for item in export_items:
            export_zip.add_item(item)

        export_zip.finalize()
        log.i(TAG, "Export completed!")

        return rtn
Beispiel #11
0
def delete_package(name, force=False):
    """Remove a package"""

    rtn = 0

    # First check if we know about the package
    if is_package_installed(name):

        item = dtf.core.item.Item()
        item.name = name
        item.type = dtf.core.item.TYPE_PACKAGE

        installed_item = __load_item(item)

        # Prompt for removal
        if not force:

            if __prompt_delete(installed_item):
                log.d(TAG, "User would like to remove")
                rtn = __do_package_delete(installed_item)
            else:
                log.i(TAG, "Package deletion skipped.")
        # Force
        else:
            log.d(TAG, "Forcing component removal.")
            rtn = __do_package_delete(installed_item)
    else:
        log.e(TAG, "No package installed with this name.")
        rtn = -1

    return rtn
Beispiel #12
0
def test_info():
    """Log an informational message"""

    log.LOG_LEVEL_STDOUT = 5
    log.LOG_LEVEL_FILE = 5

    log.i(TAG, "This is informational!")
Beispiel #13
0
def delete_module(name, force=False):
    """Remove a module"""

    rtn = 0

    # First check if we know about the module
    if is_module_installed(name):

        item = Item()
        item.name = name
        item.type = TYPE_MODULE

        installed_item = __load_item(item)

        # Prompt for removal
        if not force:

            if __prompt_delete(installed_item):
                log.d(TAG, "User would like to remove")
                rtn = __do_module_delete(installed_item)
            else:
                log.i(TAG, "Module deletion skipped.")
        # Force
        else:
            log.d(TAG, "Forcing component removal.")
            rtn = __do_module_delete(installed_item)
    else:
        log.e(TAG, "No module installed with this name.")
        rtn = -1

    return rtn
Beispiel #14
0
def install_single_package(item, force=False):
    """Install a single package"""

    rtn = 0

    try:
        # First check if we know about this package
        if is_binary_installed(item.name):

            # Prompt for install.
            if not force:
                print(
                    "[WARNING] An item with this name is already installed."
                    " See details below.")

                installed_item = __load_item(item)

                if __prompt_install(item, installed_item):
                    log.d(TAG, "User would like to install")
                    rtn = __do_single_package_install(item)
                else:
                    log.i(TAG, "Package installation skipped.")
            # Force
            else:
                log.d(TAG, "Forcing component installation.")
                rtn = __do_single_package_install(item)
        else:
            log.d(TAG, "This is a new package, installing.")
            rtn = __do_single_package_install(item)

    except KeyError:
        log.w(TAG, "Error checking if the package was installed. Skipping")
        rtn = -4

    return rtn
Beispiel #15
0
def do_checks(file_name, all_checks, strict_checks):
    """Perform checks"""

    # Update path to include libs
    update_path()

    if not os.path.isfile(file_name):
        log.e(TAG, "[FAIL] %s is not a file." % file_name)
        return -1

    base_name = os.path.basename(file_name)
    module_name = os.path.splitext(base_name)[0]

    log.d(TAG, "Full File: %s" % file_name)
    log.d(TAG, "Base name: %s" % base_name)
    log.d(TAG, "Module name: %s" % module_name)

    # First, is this python, or bash?
    if pm.is_python_module(file_name, module_name):
        log.i(TAG, "[PASS] Is python, doing python checks...")
        return do_python_checks(file_name, module_name, all_checks,
                                strict_checks)
    elif pm.is_bash_module(file_name):
        log.i(TAG, "[PASS] Is bash, doing bash checks...")
        return do_bash_checks(file_name, module_name, all_checks,
                              strict_checks)
    else:
        log.e(TAG, "[FAIL] This is not recognized as either python or bash!")
        return -2
Beispiel #16
0
Datei: pm.py Projekt: 5l1v3r1/dtf
    def parse_and_install_single(self, args, single_type):
        """Parse and install single item"""

        force_mode = args.force

        # Check for auto-mode:
        if args.single_auto:
            # Only modules can be auto-parsed
            if single_type == TYPE_MODULE:
                log.i(TAG, "Attempting to auto parse...")

                item = self.auto_parse_module(args)
                if item is None:
                    log.e(TAG, "Error autoparsing module!")
                    return -9
            else:
                log.e(TAG, "Autoparse is only available for modules!")
                return -4
        # Not auto
        else:
            item = self.parse_single_item(args)
            if item is None:
                log.e(TAG, "Error parsing single item!")
                return -5

        return packagemanager.install_single(item, force=force_mode)
Beispiel #17
0
    def do_export(self, args):
        """Perform an export"""

        rtn = 0

        parser = ArgumentParser(prog='pm export',
                                description='Export installed content.')
        parser.add_argument('output_name',
                            type=str,
                            help='The output file name.')

        parsed_args = parser.parse_args(args)

        output_name = parsed_args.output_name

        if os.path.isfile(output_name):
            log.e(TAG, "Output file already exists!")
            return -1

        # Generate a list of populated items.
        export_items = self.generate_export_items()

        if len(export_items) == 0:
            log.e(TAG, "Nothing to export!")
            return -2

        output_zip = zipfile.ZipFile(output_name,
                                     'w',
                                     compression=zipfile.ZIP_DEFLATED)

        # Generate the XML
        export_manifest = tempfile.NamedTemporaryFile()

        rtn = self.generate_export_xml(export_items, export_manifest)
        if rtn != 0:
            log.e(TAG, "Unable to generate export manifest!")
            output_zip.close()
            return rtn

        # Add the manifest
        output_zip.write(export_manifest.name, packagemanager.MANIFEST_NAME)

        export_manifest.close()

        # Finally, add the content
        rtn = self.add_export_content(export_items, output_zip)
        if rtn != 0:
            log.e(TAG, "Unable to add content to the export ZIP!")
            output_zip.close()
            return rtn

        output_zip.close()

        log.i(TAG, "Export completed!")

        return rtn
Beispiel #18
0
    def do_export(self, args):

        """Perform an export"""

        rtn = 0

        parser = ArgumentParser(prog='pm export',
                                description='Export installed content.')
        parser.add_argument('output_name', type=str,
                                help='The output file name.')

        parsed_args = parser.parse_args(args)

        output_name = parsed_args.output_name

        if os.path.isfile(output_name):
            log.e(TAG, "Output file already exists!")
            return -1

        # Generate a list of populated items.
        export_items = self.generate_export_items()

        if len(export_items) == 0:
            log.e(TAG, "Nothing to export!")
            return -2

        output_zip = zipfile.ZipFile(output_name, 'w',
                                     compression=zipfile.ZIP_DEFLATED)

        # Generate the XML
        export_manifest = tempfile.NamedTemporaryFile()

        rtn = self.generate_export_xml(export_items, export_manifest)
        if rtn != 0:
            log.e(TAG, "Unable to generate export manifest!")
            output_zip.close()
            return rtn

        # Add the manifest
        output_zip.write(export_manifest.name, packagemanager.MANIFEST_NAME)

        export_manifest.close()

        # Finally, add the content
        rtn = self.add_export_content(export_items, output_zip)
        if rtn != 0:
            log.e(TAG, "Unable to add content to the export ZIP!")
            output_zip.close()
            return rtn

        output_zip.close()

        log.i(TAG, "Export completed!")

        return rtn
Beispiel #19
0
def do_version_checks(item, strict):
    """Run version checks"""

    if item.version is None:
        log.w(TAG, "[WARN] Version is none, this should be set!")
        if strict:
            return -1
    elif not dtf.core.item.is_valid_version(item.version):
        log.e(TAG, "[FAIL] invalid version (must be semvar)")
        return -1
    else:
        log.i(TAG, "[PASS] Valid version.")

    return 0
Beispiel #20
0
    def execute(self, args):
        """Main module executor"""

        print('Are you sure you want to delete the dtf project in this '
              'directory? This cannot be reversed! [y/N]'),

        inp = raw_input()

        if inp.lower() == 'y':
            os.remove(utils.CONFIG_FILE_NAME)
            log.i(TAG, "Reset complete!")
            return 0
        else:
            log.w(TAG, "Reset aborted.")
            return -1
Beispiel #21
0
    def execute(self, args):  # pylint: disable=unused-argument,no-self-use
        """Main module executor"""

        print(
            'Are you sure you want to delete the dtf project in this '
            'directory? This cannot be reversed! [y/N]',
            end=" ")

        inp = compat.raw_input()

        if inp.lower() == 'y':
            os.remove(utils.CONFIG_FILE_NAME)
            log.i(TAG, "Reset complete!")
            return 0
        else:
            log.w(TAG, "Reset aborted.")
            return -1
Beispiel #22
0
    def do_create(self, args):
        """Create the archive"""

        zip_name = ""

        if len(args) == 0:
            zip_name = "%s.zip" % prop.get_prop('Info', 'version-string')

        else:
            zip_name = args.pop()

        log.i(self.name, "Archiving to '%s'..." % zip_name)

        rtn = self.make_zip(zip_name)

        if rtn != 0:
            log.e(self.name, "Unable to archive project!")

        return rtn
Beispiel #23
0
def do_other_checks(item, strict):
    """Run rest of the auto checkks"""

    if item.about is None:
        log.w(TAG, "[WARN] About string is none, this should be set!")
        if strict:
            return -1
    else:
        log.i(TAG, "[PASS] Valid about.")

    # Check Author
    if item.author is None:
        log.w(TAG, "[WARN] Author is none, this should be set!")
        if strict:
            return -1
    else:
        log.i(TAG, "[PASS] Valid author.")

    return 0
Beispiel #24
0
def __install_zip_binary(zip_file, item, force_mode):
    """Install a binary from a ZIP"""

    rtn = 0

    # Does the resource even exist?
    if not file_in_zip(zip_file, item.local_name):
        log.w(
            TAG, "'%s' defined, but local file '%s' does not exist!" %
            (item.name, item.local_name))
        return -1

    try:
        # First check if we know about this binary
        if is_binary_installed(item.name):

            # Prompt for install.
            if not force_mode:
                print(
                    "[WARNING] An item with this name is already installed."
                    " See details below.")

                installed_item = __load_item(item)

                if __prompt_install(item, installed_item):
                    log.d(TAG, "User would like to install")
                    rtn = __do_zip_binary_install(zip_file, item)
                else:
                    log.i(TAG, "Binary installation skipped.")
            # Force
            else:
                log.d(TAG, "Forcing component installation.")
                rtn = __do_zip_binary_install(zip_file, item)
        else:
            log.d(TAG, "This is a new binary, installing.")
            rtn = __do_zip_binary_install(zip_file, item)

    except KeyError:
        log.w(TAG, "Error checking if the binary was installed. Skipping")
        rtn = -4

    return rtn
Beispiel #25
0
def __do_single_package_install(item):
    """Perform single package installation"""

    name = item.name
    local_name = item.local_name
    install_name = item.install_name

    # First copy the new tree.
    if copy_tree(local_name, install_name, DTF_PACKAGES_DIR) != 0:
        log.e(TAG, "Error copying package '%s'" % (local_name))
        return -1

    # Update database
    if __update_package(item) == 0:
        log.e(TAG,
              "Failed to update package '%s' details in database." % (name))
        return -2

    log.i(TAG, "Package '%s' installed successfully!" % name)
    return 0
Beispiel #26
0
def __do_single_module_install(item):
    """Perform single module installation"""

    name = item.name
    local_name = item.local_name
    install_name = item.install_name

    # First copy the new file.
    if copy_file(local_name, install_name, DTF_MODULES_DIR) != 0:
        log.e(TAG, "Error copying module '%s'" % (local_name))
        return -1

    # Update database
    if __update_module(item) == 0:
        log.e(TAG,
              "Failed to update module '%s' details in database." % (name))
        return -2

    log.i(TAG, "Module '%s' installed successfully!" % name)
    return 0
Beispiel #27
0
def __do_single_library_install(item):
    """Perform single library installation"""

    name = item.name
    local_name = item.local_name
    install_name = item.install_name

    # First copy the new tree.
    if copy_tree(local_name, install_name, DTF_LIBRARIES_DIR) != 0:
        log.e(TAG, "Error copying library '%s'" % (local_name))
        return -1

    # Update database
    if __update_library(item) == 0:
        log.e(TAG,
              "Failed to update library '%s' details in database." % (name))
        return -2

    log.i(TAG, "Library '%s' installed successfully!" % name)
    return 0
Beispiel #28
0
def __install_zip_binary(zip_file, item, force_mode):

    """Install a binary from a ZIP"""

    rtn = 0

    # Does the resource even exist?
    if not file_in_zip(zip_file, item.local_name):
        log.w(TAG, "'%s' defined, but local file '%s' does not exist!"
            % (item.name, item.local_name))
        return -1

    try:
        # First check if we know about this binary
        if is_binary_installed(item.name):

            # Prompt for install.
            if not force_mode:
                print ("[WARNING] An item with this name is already installed."
                      " See details below.")

                installed_item = __load_item(item)

                if __prompt_install(item, installed_item):
                    log.d(TAG, "User would like to install")
                    rtn = __do_zip_binary_install(zip_file, item)
                else:
                    log.i(TAG, "Binary installation skipped.")
            # Force
            else:
                log.d(TAG, "Forcing component installation.")
                rtn = __do_zip_binary_install(zip_file, item)
        else:
            log.d(TAG, "This is a new binary, installing.")
            rtn = __do_zip_binary_install(zip_file, item)

    except KeyError:
        log.w(TAG, "Error checking if the binary was installed. Skipping")
        rtn = -4

    return rtn
Beispiel #29
0
def __do_single_package_install(item):

    """Perform single package installation"""

    name = item.name
    local_name = item.local_name
    install_name = item.install_name

    # First copy the new tree.
    if copy_tree(local_name, install_name, DTF_PACKAGES_DIR) != 0:
        log.e(TAG, "Error copying package '%s'" % (local_name))
        return -1

    # Update database
    if __update_package(item) == 0:
        log.e(TAG, "Failed to update package '%s' details in database."
                % (name))
        return -2

    log.i(TAG, "Package '%s' installed successfully!" % name)
    return 0
Beispiel #30
0
def __do_single_module_install(item):

    """Perform single module installation"""

    name = item.name
    local_name = item.local_name
    install_name = item.install_name

    # First copy the new file.
    if copy_file(local_name, install_name, DTF_MODULES_DIR) != 0:
        log.e(TAG, "Error copying module '%s'" % (local_name))
        return -1

    # Update database
    if __update_module(item) == 0:
        log.e(TAG, "Failed to update module '%s' details in database."
                % (name))
        return -2

    log.i(TAG, "Module '%s' installed successfully!" % name)
    return 0
Beispiel #31
0
def __do_single_library_install(item):

    """Perform single library installation"""

    name = item.name
    local_name = item.local_name
    install_name = item.install_name

    # First copy the new tree.
    if copy_tree(local_name, install_name, DTF_LIBRARIES_DIR) != 0:
        log.e(TAG, "Error copying library '%s'" % (local_name))
        return -1

    # Update database
    if __update_library(item) == 0:
        log.e(TAG, "Failed to update library '%s' details in database."
                % (name))
        return -2

    log.i(TAG, "Library '%s' installed successfully!" % name)
    return 0
Beispiel #32
0
def do_first_run_process():
    """Perform the file time run install"""

    log.i(TAG, "First time launch of dtf detected...")

    # Set things up if they haven't been already
    if packagemanager.create_data_dirs() != 0:
        log.e(TAG, "Unable to setup dtf data directories!")
        return -4

    if not os.path.isfile(dtfglobals.DTF_DB):
        if packagemanager.initialize_db() != 0:
            log.e(TAG, "Error creating and populating dtf db!!")
            return -7

    if autoconfig.initialize_from_local(is_full=True) != 0:
        log.e(TAG, "Unable to initialize global settings!")
        return -5

    log.i(TAG, "Initial auto setup is completed!")
    return 0
Beispiel #33
0
    def do_init(self):
        """Perform the initialization"""

        log.i(TAG, "Project initialization started.")

        signal.signal(signal.SIGINT, self.do_shutdown)

        if os.path.isfile(utils.CONFIG_FILE_NAME):
            log.e(TAG, "Configuration file already exists!")
            return -1

        compat.raw_input("\nPlease connect test device "
                         "(press Enter to continue) ")

        # This might get in the way.
        try:
            del os.environ['ANDROID_SERIAL']
        except KeyError:
            pass

        self.adb = adb.DtfAdb(no_serial=True)

        log.i(TAG, "Restarting adb...")
        self.adb.kill_server()
        self.adb.start_server()

        log.i(TAG, "Waiting for a device to be connected...")

        time.sleep(1)

        init_device = self.determine_device()
        if init_device is None:
            log.e(TAG, "Error determining device.")
            return -2

        # Is this device offline?
        if init_device['status'] != adb.STATUS_DEVICE:
            log.e(TAG, "Cannot initialize offline/bootloader device!")
            log.e(TAG, "Try either: ")
            log.e(TAG, "    1. Run: adb kill-server && dtf init")
            log.e(TAG, "    2. Reboot the device.")
            return -3

        # Initialize device
        if self.initialize_device(init_device) != 0:
            log.e(TAG, "Error initializing device!")
            return -4

        log.i(TAG, "Device initialization complete!")
        return 0
Beispiel #34
0
def install_zip(zip_file_name, force=False):

    """Install a ZIP file"""

    rtn = 0
    manifest_data = None
    manifest_root = None

    zip_f = ZipFile(zip_file_name, 'r')

    log.d(TAG, "Parsing manifest file...")

    # Get Manifest
    if not file_in_zip(zip_f, MANIFEST_NAME):
        log.e(TAG, "Error extracting '%s' from ZIP archive - does it exist?"
                % (MANIFEST_NAME))
        return -3

    manifest_data = zip_f.read(MANIFEST_NAME)

    # Read Manifest
    try:
        manifest_root = etree.XML(manifest_data)
    except etree.XMLSyntaxError as err:
        log.e(TAG, "Error parsing XML file '%s'! Exiting."
                                                % MANIFEST_NAME)
        return -4

    # Processing Stuff
    items = manifest_root.xpath("/Items/Item")

    rtn = __process_zip_items(zip_f, items, force)

    if rtn == 0:
        log.i(TAG, "ZIP content '%s' installed successfully!"
                                            % zip_file_name)
    else:
        log.e(TAG, "Unable to install ZIP file: %d" % rtn)

    return rtn
Beispiel #35
0
    def do_upload(self, args):

        """Upload file to dtf client directory"""

        parser = ArgumentParser(prog='client upload',
                        description='Upload file to device.')
        parser.add_argument('--path', metavar="val", dest='upload_path',
                        default=DEFAULT_PATH, help="Specify a upload point.")
        parser.add_argument('file_name', type=str,
                         help='The file to upload.')

        args = parser.parse_args(args)

        file_name = args.file_name
        upload_path = args.upload_path

        if not isfile(file_name):
            log.e(self.name, "File does not exist: %s" % file_name)
            return -1

        log.i(self.name, "Waiting for device to be connected...")
        self.adb.wait_for_device()

        # Is client installed?
        if not self.adb.is_installed(DTF_CLIENT):
            log.e(self.name, "dtf Client is not installed!")
            return -1

        self.adb.push(file_name, upload_path)

        upload_file_name = "%s/%s" % (upload_path, file_name)
        dtf_upload_path = "/data/data/%s/" % DTF_CLIENT

        cmd = ("run-as %s cp %s %s" %
                (DTF_CLIENT, upload_file_name, dtf_upload_path))

        self.adb.shell_command(cmd)

        return 0
Beispiel #36
0
def install_zip(zip_file_name, force=False):
    """Install a ZIP file"""

    rtn = 0
    manifest_data = None
    manifest_root = None

    zip_f = ZipFile(zip_file_name, 'r')

    log.d(TAG, "Parsing manifest file...")

    # Get Manifest
    if not file_in_zip(zip_f, MANIFEST_NAME):
        log.e(
            TAG, "Error extracting '%s' from ZIP archive - does it exist?" %
            (MANIFEST_NAME))
        return -3

    manifest_data = zip_f.read(MANIFEST_NAME)

    # Read Manifest
    try:
        manifest_root = etree.XML(manifest_data)
    except etree.XMLSyntaxError as err:
        log.e(TAG, "Error parsing XML file '%s'! Exiting." % MANIFEST_NAME)
        return -4

    # Processing Stuff
    items = manifest_root.xpath("/Items/Item")

    rtn = __process_zip_items(zip_f, items, force)

    if rtn == 0:
        log.i(TAG, "ZIP content '%s' installed successfully!" % zip_file_name)
    else:
        log.e(TAG, "Unable to install ZIP file: %d" % rtn)

    return rtn
Beispiel #37
0
    def do_upload(self, args):

        """Upload file to dtf client directory"""

        parser = ArgumentParser(prog="client upload", description="Upload file to device.")
        parser.add_argument(
            "--path", metavar="val", dest="upload_path", default=DEFAULT_PATH, help="Specify a upload point."
        )
        parser.add_argument("file_name", type=str, help="The file to upload.")

        args = parser.parse_args(args)

        file_name = args.file_name
        upload_path = args.upload_path

        if not isfile(file_name):
            log.e(self.name, "File does not exist: %s" % file_name)
            return -1

        log.i(self.name, "Waiting for device to be connected...")
        self.adb.wait_for_device()

        # Is client installed?
        if not self.adb.is_installed(DTF_CLIENT):
            log.e(self.name, "dtf Client is not installed!")
            return -1

        self.adb.push(file_name, upload_path)

        upload_file_name = "%s/%s" % (upload_path, file_name)
        dtf_upload_path = "/data/data/%s/" % DTF_CLIENT

        cmd = "run-as %s cp %s %s" % (DTF_CLIENT, upload_file_name, dtf_upload_path)

        self.adb.shell_command(cmd)

        return 0
Beispiel #38
0
    def do_remove(self):

        """Uninstall the dtf client"""

        log.i(self.name, "Waiting for device to be connected...")
        self.adb.wait_for_device()

        log.i(self.name, "Removing dtf client...")
        self.adb.uninstall(DTF_CLIENT)

        prop.del_prop('Info', 'busybox')

        log.i(self.name, "dtf client removed!")

        return 0
Beispiel #39
0
    def do_remove(self):

        """Uninstall the dtf client"""

        log.i(self.name, "Waiting for device to be connected...")
        self.adb.wait_for_device()

        log.i(self.name, "Removing dtf client...")
        self.adb.uninstall(DTF_CLIENT)

        prop.del_prop("Info", "busybox")

        log.i(self.name, "dtf client removed!")

        return 0
Beispiel #40
0
def __generic_install(item, force_mode, new_only, check_function,
                      install_function, install_args):
    """Generic check and caller"""

    try:
        # First check if we know about this item
        if check_function(item.name):

            log.d(TAG, "Item exists, need to check")

            # If forced, don't even check.
            if force_mode:
                log.i(
                    TAG, "Forcing component installation: %s (%s)" %
                    (item.name, item.type))
                return install_function(*install_args)

            installed_item = __load_item(item)

            # Ok, next, we check the versions.
            if dtf.core.item.item_is_newer(installed_item, item):
                log.i(
                    TAG, "Upgrading %s from v%s to v%s" %
                    (item.name, installed_item.version, item.version))
                return install_function(*install_args)

            elif new_only:
                log.w(TAG, "Skipping due to older version: %s" % item.name)
                return 0

            # Otherwise we need to prompt
            else:
                print("[WARNING] An item with this name is already installed."
                      " See details below.")

                if __prompt_install(item, installed_item):
                    log.d(TAG, "User would like to install")
                    return install_function(*install_args)
                else:
                    log.w(TAG, "Installation skipped.")
                    return 0
        else:
            log.i(TAG, "Installing new item: %s (%s)" % (item.name, item.type))
            return install_function(*install_args)

    except KeyError:
        log.w(TAG, "Error checking if the item was installed. Skipping")
        return -4
Beispiel #41
0
    def do_install(self):

        """Install the dtf client on device"""

        log.i(self.name, "Waiting for device to be connected...")
        self.adb.wait_for_device()

        log.i(self.name, "Removing old client if it exists...")
        self.adb.uninstall(DTF_CLIENT)

        log.i(self.name, "Installing dtf client...")

        self.adb.install(DTF_CLIENT_PATH)

        cmd = "am startservice -a com.dtf.action.name.INITIALIZE"
        self.adb.shell_command(cmd)

        busybox_path = "/data/data/%s/files/busybox" % DTF_CLIENT
        prop.set_prop('Info', 'busybox', busybox_path)

        log.i(self.name, "dtf client installed.")

        return 0
Beispiel #42
0
    def do_install(self):

        """Install the dtf client on device"""

        log.i(self.name, "Waiting for device to be connected...")
        self.adb.wait_for_device()

        log.i(self.name, "Removing old client if it exists...")
        self.adb.uninstall(DTF_CLIENT)

        log.i(self.name, "Installing dtf client...")

        self.adb.install(DTF_CLIENT_PATH)

        cmd = "am startservice -a com.dtf.action.name.INITIALIZE"
        self.adb.shell_command(cmd)

        busybox_path = "/data/data/%s/files/busybox" % DTF_CLIENT
        prop.set_prop("Info", "busybox", busybox_path)

        log.i(self.name, "dtf client installed.")

        return 0
Beispiel #43
0
def purge():

    """Perform database purge"""

    log.i(TAG, "Starting purge....")

    dtf_db = sqlite3.connect(DTF_DB)
    cur = dtf_db.cursor()

    # Remove Binaries
    sql = ('SELECT name, install_name '
           'FROM binaries')

    for row in cur.execute(sql):

        binary_name = row[0]
        install_name = row[1]
        full_path = DTF_BINARIES_DIR + install_name
        log.d(TAG, "Removing binary '%s'" % binary_name)

        if delete_file(full_path) != 0:
            log.e(TAG, "Error removing binary file! Continuing.")

    # Remove Libraries
    sql = ('SELECT name, install_name '
           'FROM libraries')

    for row in cur.execute(sql):

        library_name = row[0]
        install_name = row[1]
        full_path = DTF_LIBRARIES_DIR + install_name
        log.d(TAG, "Removing library '%s'" % library_name)

        if delete_tree(full_path) != 0:
            log.e(TAG, "Error removing library! Continuing.")

    # Remove Modules
    sql = ('SELECT name, install_name '
           'FROM modules')

    for row in cur.execute(sql):

        module_name = row[0]
        install_name = row[1]
        full_path = DTF_MODULES_DIR + install_name
        log.d(TAG, "Removing module '%s'" % module_name)

        if delete_file(full_path) != 0:
            log.e(TAG, "Error removing module file! Continuing.")

    # Remove Packages
    sql = ('SELECT name, install_name '
           'FROM packages')

    for row in cur.execute(sql):

        package_name = row[0]
        install_name = row[1]
        full_path = DTF_PACKAGES_DIR + install_name
        log.d(TAG, "Removing package '%s'" % package_name)

        if delete_tree(full_path) != 0:
            log.e(TAG, "Error removing package! Continuing.")

    # Drop the DB.
    cur.execute("DROP TABLE IF EXISTS binaries")
    cur.execute("DROP TABLE IF EXISTS libraries")
    cur.execute("DROP TABLE IF EXISTS modules")
    cur.execute("DROP TABLE IF EXISTS packages")

    dtf_db.commit()

    # Rebuilding
    if initialize_db() != 0:
        log.e(TAG, "Unable to re-create dtf db!")
        return -1

    log.i(TAG, "Purge complete!")
    return 0
Beispiel #44
0
    def do_install(self, args):

        """Attempt to install new content"""

        parser = ArgumentParser(prog='pm install',
                            description='Install a item or DTF ZIP of items.')
        parser.add_argument('--zip', dest='zipfile', default=None,
                            help='Install a DTF ZIP file containing items.')
        parser.add_argument('--single', metavar="ITEM", dest='single_type',
                            default=None, help='Install a single item.')
        parser.add_argument('--name', metavar="val", dest='single_name',
                            default=None, help="Item name [SINGLE ONLY].")
        parser.add_argument('--local_name', metavar="val",
                            dest='single_local_name', default=None,
                            help="Item local name [SINGLE ONLY].")
        parser.add_argument('--install_name', metavar="val",
                            dest='single_install_name', default=None,
                            help="Item install name [SINGLE ONLY].")
        parser.add_argument('--version', metavar="val", dest='single_version',
                            default=None,
                            help="Item version (#.# format) [SINGLE ONLY].")
        parser.add_argument('--author', nargs='+', metavar="val",
                            dest='single_author', default=None,
                            help="Item author (email is fine). [SINGLE ONLY].")
        parser.add_argument('--about', nargs='+', metavar="val",
                            dest='single_about', default=None,
                            help="About string for a module. [SINGLE ONLY].")
        parser.add_argument('--health', metavar="val", dest='single_health',
                            default=None, help="Item health [SINGLE ONLY].")
        parser.add_argument('--auto', dest='single_auto', action='store_const',
                            const=True, default=False,
                            help="Automatically parse module [SINGLE ONLY].")
        parser.add_argument('--force', dest='force', action='store_const',
                            const=True, default=False,
                            help="Force installation of component(s).")

        parsed_args = parser.parse_args(args)

        zip_file_name = parsed_args.zipfile
        single_type = parsed_args.single_type
        force_mode = parsed_args.force

        if zip_file_name is not None and single_type is not None:
            log.e(TAG, "Cannot install both DTF ZIP and single item. Exiting.")
            return -1

        if zip_file_name is None and single_type is None:
            log.e(TAG, "ZIP mode or single item mode not detected. Exiting.")
            return -2

        # Install zip.
        if zip_file_name is not None:
            if zipfile.is_zipfile(zip_file_name):
                return packagemanager.install_zip(zip_file_name,
                                                  force=force_mode)

            else:
                log.e(TAG, "'%s' is not a valid ZIP file or does not exist."
                        % (zip_file_name))
                return -3

        # Install single.
        else:
            # Check for auto-mode:
            if parsed_args.single_auto:
                # Only modules can be auto-parsed
                if single_type == TYPE_MODULE:
                    log.i(TAG, "Attempting to auto parse...")

                    item = self.auto_parse_module(parsed_args)
                    if item is None:
                        log.e(TAG, "Error autoparsing module!")
                        return -9
                else:
                    log.e(TAG, "Autoparse is only available for modules!")
                    return -4
            # Not auto
            else:
                item = self.parse_single_item(parsed_args)
                if item is None:
                    log.e(TAG, "Error parsing single item!")
                    return -5

            # Now do the installation.
            if single_type == TYPE_BINARY:
                return packagemanager.install_single_binary(item,
                                                            force=force_mode)
            elif single_type == TYPE_LIBRARY:
                return packagemanager.install_single_library(item,
                                                            force=force_mode)
            elif single_type == TYPE_MODULE:
                return packagemanager.install_single_module(item,
                                                            force=force_mode)
            elif single_type == TYPE_PACKAGE:
                return packagemanager.install_single_package(item,
                                                            force=force_mode)
Beispiel #45
0
    def do_init(self):

        """Perform the initialization"""

        log.i(TAG, "Project initialization started.")

        signal.signal(signal.SIGINT, self.do_shutdown)

        if os.path.isfile(utils.CONFIG_FILE_NAME):
            log.e(TAG, "Configuration file already exists!")
            return -1

        raw_input("\nPlease connect the test device (press Enter to continue) ")

        # This might get in the way.
        try:
            del os.environ['ANDROID_SERIAL']
        except KeyError:
            pass

        self.adb = DtfAdb.DtfAdb(no_serial=True)

        log.i(TAG, "Restarting adb...")
        self.adb.kill_server()
        self.adb.start_server()

        log.i(TAG, "Waiting for a device to be connected...")

        time.sleep(1)

        devices = self.get_devices()

        if len(devices) == 0:
            log.e(TAG, "No devices found, exiting.")
            return -2

        elif len(devices) == 1:
            serial = devices[0]
            res = raw_input("Got serial '%s', is this correct? [Y/n] "
                                % serial)
            if res.lower() == 'n':
                log.e(TAG, "Initialization aborted.")
                return -3
        else:
            print "Found many devices. Please select from the following list:"

            i = 1
            for device in devices:
                print "#%d. %s" % (i, device)
                i += 1

            res = raw_input("\nWhich device #? ")

            try:
                int_res = int(res)
                serial = devices[int_res - 1]
            except (ValueError, IndexError):
                log.e(TAG, "Invalid input!")
                return -4

        if self.initialize_device(serial) != 0:
            log.e(TAG, "Error initializing device!")
            return -5
        else:
            log.i(TAG, "Device initialization complete!")
            return 0