Ejemplo n.º 1
0
    def launch_convertion(self):
        """Convert build steps to target convertion.
        """

        Log.debug("Converting...")
        Converter.check() and Converter.save(self.steps)
        Log.debug("Conversion done!")
Ejemplo n.º 2
0
 def install_packages(self, cmd, packages):
     installed = 0
     for pkg in packages:
         install_cmd = cmd_split(cmd.format(packages=pkg))
         install_output = Popen(install_cmd)
         while install_output.poll() is None:
             sleep(0.5)
         output = install_output.returncode
         Log.debug("Running (npm) %s ... %r" % (install_cmd, output == 0))
         if 0 == output:
             installed += 1
     return installed
Ejemplo n.º 3
0
 def install_packages(self, cmd, packages):
     installed = 0
     for pkg in packages:
         install_cmd = cmd_split(cmd.format(packages=pkg))
         install_output = Popen(install_cmd)
         while install_output.poll() is None:
             sleep(0.5)
         output = install_output.returncode
         log_status = (self.env.os_name, install_cmd, output == 0)
         Log.debug("Running (%s) %s ... %r" % log_status)
         if 0 == output:
             installed += 1
     return installed
Ejemplo n.º 4
0
def scan_lookup(statements, lookup, length=80):
    """Lookup a statement with example usage.

    Args:
        statements (Statement): Statement instance will all known statements.
        lookup           (str): String to lookup after in all statements.
    """

    Log.debug("Lookup after %s ..." % lookup)
    known_statements = statements.get_actions()
    actions = []

    # Check available statements
    if len(known_statements) == 0:
        return Log.error("Statements are missing")

    # Search in all statemnts
    for stmt in known_statements:
        if lookup.lower() in stmt.parse_description().lower():
            if stmt not in actions:
                actions.append(stmt)
        else:
            for exp in stmt.parse_statements():
                if lookup.lower() in exp.lower():
                    if stmt not in actions:
                        actions.append(stmt)

    # Handle missing action
    if len(actions) == 0:
        return Log.error("Nothing found for '%s'" % lookup)

    # Print lookup
    for action in actions:
        delimiter = "-" * (length)
        description = action.parse_description()
        desc = u"\033[96m %-{}s \033[0m".format(length - 2) % description
        print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
        print(u"\033[90m|\033[0m" + desc + "\033[90m|\033[0m")
        print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
        for text in action.parse_statements():
            stmt = u"\033[95m %-{}s \033[0m".format(length - 6) % text.strip()
            print(u"\033[90m|   |\033[0m" + stmt + "\033[90m|\033[0m")
        print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
        for text in action.parse("sample"):
            sample = u"\033[93m %-{}s \033[0m".format(length -
                                                      6) % text.strip()
            print(u"\033[90m|   |\033[0m" + sample + "\033[90m|\033[0m")

    # Done
    print(u"\033[90m|%-{}s|\033[0m".format(length - 4) % delimiter)
    Log.debug("Done...")
Ejemplo n.º 5
0
 def run(self, srv=None, *args, **kwargs):
     cmd = StopService.check_systemd()
     if cmd is None:
         return self.fail("Unsupported OS: %s" % self.env.os_name)
     try:
         service_cmd = cmd.format(service=srv)
         log_status = (self.env.os_name, service_cmd)
         Log.debug("Service OS (%s) stop: %s ..." % log_status)
         service_output = Popen(cmd_split(service_cmd))
         while service_output.poll() is None:
             sleep(0.5)
         if 0 != service_output.returncode:
             return self.fail(u"Service '%s' => failed to stop" % srv)
         self.success(u"Service '%s' => stopped" % srv)
     except CalledProcessError as e:
         self.fail(e.output)
     except Exception as e:
         self.fail(str(e))
Ejemplo n.º 6
0
 def run(self, srv=None, *args, **kwargs):
     cmd = StatusService.check_systemd()
     if cmd is None:
         return self.fail("Unsupported OS: %s" % self.env.os_name)
     try:
         service_cmd = cmd.format(service=srv)
         log_status = (self.env.os_name, service_cmd)
         Log.debug("Service OS (%s) status: %s ..." % log_status)
         ok, status = StatusService.get_service_status(service_cmd)
         output = u"Service '%s' => %s" % (srv, status)
         if ok:
             self.success(output)
         else:
             self.fail(output)
     except CalledProcessError as e:
         self.fail(e.output)
     except Exception as e:
         self.fail(str(e))
Ejemplo n.º 7
0
    def parse(self, newlines=0):
        """Parse build steps file.

        Loop through all steps and check for topics and instructions.
        """

        Log.debug("Guide has %d lines" % len(self.content))
        self.last_guide = Guide()
        while self.has_next():
            line = self.get_line(strip=True)
            if len(line) == 0:
                newlines += 1
            else:
                newlines = 0
            if newlines >= 2:
                self.last_topic = None
            self.check_topic(line)
            self.check_instruction(line).check_payload(self.next_content())
            self.next_line()
Ejemplo n.º 8
0
def analyze(statement, length=80):
    """Analyze all statements and print visual results.

    Returns:
        bool: True if all statements are ok, otherwise False.
    """

    lines = scan_statements(statement)
    results = self_analyze(lines)
    Log.debug("Listing all statements...")
    for action in statement.get_actions():
        try:
            text = action.parse_description()
        except Exception:
            text = "no description"
        delimiter = "-" * (length - 4)
        c_txt = u"\033[96m %-{}s \033[0m".format(length - 6)
        r_txt = u"\033[91m %-{}s \033[0m".format(length - 6)
        g_txt = u"\033[92m %-{}s \033[0m".format(length - 6)
        print(u"\033[90m|---|%-{}s|\033[0m".format(length - 4) % delimiter)
        print(u"\033[90m|   |\033[0m" + fmt(c_txt, text) + "\033[90m|\033[0m")
        print(u"\033[90m|---|%-{}s|\033[0m".format(length - 4) % delimiter)
        for line in action.parse_statements():
            if lines[line] > 1:
                status = UnicodeIcon.INVALID
                line_text = fmt(r_txt, line.strip())
            else:
                status = UnicodeIcon.VALID
                line_text = fmt(g_txt, line.strip())
            print_line = u"\033[90m|\033[0m " + status + u" \033[90m|\033[0m"
            print_line += line_text + u"\033[90m|\033[0m"
            print(print_line)
    print(u"\033[90m|---|%-{}s|\033[0m".format(length - 4) % delimiter)
    if not results:
        Log.error("Duplicated statements found!")
        for line, times in lines.iteritems():
            if times > 1:
                err = (times, line.strip())
                Log.error(" - line duplicated %d times: %s" % err)
        Log.error("Please correct these problems before running again.")
    else:
        Log.debug("Everything looks OK!")
    return results
Ejemplo n.º 9
0
    def prepare(cls):
        """Statement initialization.

        Loops through all supported actions, validates and maps statements with
        actions.

        Returns:
            bool: True if statements are mapped successful.
        """

        Log.debug("Preparing to scan %d actions" % len(cls.__actions))
        for action in cls.__actions:
            if not callable(action):
                raise SystemExit("Expected action to be callable")
            Log.debug("Scanning action: %s" % action.parse_description())
            for line in action.parse_statements():
                exp = compile(line.strip(), IGNORECASE | UNICODE)
                cls.statements.update({exp: action})
                Log.debug("Updating known patterns: %s" % exp.pattern)
        if len(cls.statements) < len(cls.__actions):
            raise SystemExit("Unable to map statements to action")
        cls.ready = True
        Log.debug("All statements are scanned")
Ejemplo n.º 10
0
    def launch_topic(self, ignore_fails=False, failed=False):
        """Launch topic and run all steps.
        """

        total_steps = len(self.topic.get_steps())
        Log.info("Preparing to run %d steps from topic..." % total_steps)

        # Loop steps and run each one
        start_time = default_timer()
        Log.debug("Setting start time: %s" % start_time)

        success_log = u"\033[92m\u2713 (Success)\033[0m \033[93m%s\033[0m"
        failed_log = u"\033[91m? (Failed)\033[0m \033[95m%s\033[0m"
        error_log = u"\033[91m? (Error) %s\033[0m"

        for self.last_step, step in enumerate(self.steps):
            step_count, step_desc = self.last_step + 1, step.get_description()
            Log.debug("Preparing step (%d) %s" % (step_count, step_desc))
            Log.info(u"Running \033[93m%s\033[0m ..." % step.get_step())
            try:
                success, output = step.run()
                if success:
                    Log.info(success_log % output)
                else:
                    Log.info(failed_log % output)
                    if not ignore_fails:
                        failed = True
                        Report.set_error(output)
                        break
                Report.inc_step(1)
            except KeyboardInterrupt:
                Log.info("Stopping current step...")
                Log.warn("Interrupting may lead to unexpected results")
                Log.warn("Stop master process at your own risk (PID %s)" % PID)
            except Exception as e:
                failed = True
                Report.set_error(e)
                Log.info(error_log % str(e))
                break

        stop_time = default_timer()
        Log.debug("Set stop time: %s" % stop_time)
        Log.debug("Runtime %ss" % (stop_time - start_time))

        # Save runtime
        Report.set_runtime(stop_time - start_time)

        # Wrap up...
        Log.debug("Done running steps...")
        title = self.topic.get_title()
        if failed:
            Log.info("An error occured while topic '%s' was running" % title)
            Report.set_status("Failed")
        else:
            Log.info("Topic '%s' has ran all steps with no errors" % title)
            Report.set_status("OK")
        Log.debug("Closing...")