Commit 828ef07f authored by Adrian DC's avatar Adrian DC
Browse files

chore: features: turn the 'launcher' into a pipeline feature class

parent fadb5063
Loading
Loading
Loading
Loading
Loading
+4 −4
Changes for gitlabci_local/cli/main.py: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ from ..menu import selector
from ..engines.engine import supported as engine_supported
from ..features.images import ImagesFeature
from ..features.jobs import JobsFeature
from ..features.pipelines import JobsPipelines
from ..package.bundle import Bundle
from ..package.settings import Settings
from ..package.updates import Updates
@@ -19,7 +20,6 @@ from ..package.version import Version
from ..parser import reader
from ..parsers.gitlab import GitLab
from ..prints.colors import Colors
from ..runner import launcher
from ..system.platform import Platform

# Main
@@ -239,11 +239,11 @@ def main():

    # Launch pipeline
    elif options.pipeline:
        result = launcher(options, jobs)
        result = JobsPipelines(jobs, options).launch()

    # Launch jobs
    elif options.names:
        result = launcher(options, jobs)
        result = JobsPipelines(jobs, options).launch()

    # Select jobs
    elif interactive:
@@ -252,7 +252,7 @@ def main():
    # Launch all jobs
    elif options.all:
        options.pipeline = True
        result = launcher(options, jobs)
        result = JobsPipelines(jobs, options).launch()

    # Unsupported case
    else:
+110 −0
Changes for gitlabci_local/features/pipelines.py: 110 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env python3

# Standard libraries
from time import time

# Components
from ..prints.colors import Colors
from ..runner import runner
from ..system.platform import Platform
from ..types.lists import Lists

# JobsPipelines class
class JobsPipelines:

    # Members
    __jobs = None
    __options = None

    # Constructor
    def __init__(self, jobs, options):

        # Prepare jobs
        self.__jobs = jobs

        # Prepare options
        self.__options = options

    # Launch
    def launch(self):

        # Variables
        jobs_status = {'jobs_count': 0, 'quiet': True, 'time_launcher': time()}
        result = None

        # Run selected jobs
        for job in self.__jobs:

            # Filter jobs list
            if not self.__options.pipeline and not Lists.match(
                    self.__options.names, job, ignore_case=self.__options.ignore_case,
                    no_regex=self.__options.no_regex):
                continue

            # Filter stages list
            if self.__options.pipeline and self.__options.names and not Lists.match(
                    self.__options.names, self.__jobs[job]['stage'],
                    ignore_case=self.__options.ignore_case,
                    no_regex=self.__options.no_regex):
                continue

            # Filter manual jobs
            job_manual = (self.__jobs[job]['when'] == 'manual')
            if job_manual and not self.__options.manual and not Lists.match(
                    self.__options.names, job, ignore_case=self.__options.ignore_case,
                    no_regex=self.__options.no_regex):
                continue

            # Filter disabled jobs
            if self.__jobs[job]['options']['disabled']:
                continue

            # Raise initial result
            if result is None:
                result = True

            # Run job
            attempt = 0
            expected = result
            jobs_status['jobs_count'] += 1
            result = runner(self.__options, self.__jobs[job], result, jobs_status)

            # Retry job if allowed
            if expected and not result and self.__jobs[job]['retry'] > 0:
                while not result and attempt < self.__jobs[job]['retry']:
                    attempt += 1
                    result = runner(self.__options, self.__jobs[job], expected,
                                    jobs_status)

        # Non quiet jobs
        if not jobs_status['quiet']:

            # Pipeline jobs footer
            if jobs_status['jobs_count'] > 1:

                # Evaluate duration total time
                time_total_duration = time() - jobs_status['time_launcher']
                time_total_seconds = '%.0f second%s' % (
                    time_total_duration % 60, 's' if time_total_duration % 60 > 1 else '')
                time_total_minutes = ''
                if time_total_duration >= 60:
                    time_total_minutes = '%.0f minute%s ' % (
                        time_total_duration / 60,
                        's' if time_total_duration / 60 > 1 else '')
                time_total_string = time_total_minutes + time_total_seconds

                # Final footer
                print(' %s> Pipeline: %s in %s total%s' %
                      (Colors.YELLOW, Colors.BOLD + 'Success' if result else Colors.RED +
                       'Failure', time_total_string, Colors.RESET))
                print(' ')
                print(' ')
                Platform.flush()

            # Simple job footer
            else:
                print(' ')
                Platform.flush()

        # Result
        return bool(result)
+2 −2
Changes for gitlabci_local/menu.py: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -11,11 +11,11 @@ from PyInquirer import prompt as PyInquirer_prompt
from PyInquirer import Separator as PyInquirer_Separator

# Components
from .features.pipelines import JobsPipelines
from .package.bundle import Bundle
from .package.patcher import Patcher
from .prints.colors import Colors
from .prints.menus import Menus
from .runner import launcher
from .system.platform import Platform
from .types.dicts import Dicts
from .types.lists import Lists
@@ -133,7 +133,7 @@ def selector(options, jobs):

    # Launch jobs
    if options.names:
        result = launcher(options, jobs)
        result = JobsPipelines(jobs, options).launch()

    # Result
    return result
+0 −82
Changes for gitlabci_local/runner.py: 0 added lines, 82 removed lines.
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@ from .engines.engine import Engine
from .prints.colors import Colors
from .system.platform import Platform
from .types.files import Files
from .types.lists import Lists
from .types.paths import Paths
from .types.volumes import Volumes

@@ -25,87 +24,6 @@ __MARKER_RESULT = '__GITLAB_CI_LOCAL_RESULT__'
# Variables
__engine = None # pylint: disable=invalid-name

# Launcher
def launcher(options, jobs):

    # Variables
    jobs_status = {'jobs_count': 0, 'quiet': True, 'time_launcher': time()}
    result = None

    # Run selected jobs
    for job in jobs:

        # Filter jobs list
        if not options.pipeline and not Lists.match(options.names, job,
                                                    ignore_case=options.ignore_case,
                                                    no_regex=options.no_regex):
            continue

        # Filter stages list
        if options.pipeline and options.names and not Lists.match(
                options.names, jobs[job]['stage'], ignore_case=options.ignore_case,
                no_regex=options.no_regex):
            continue

        # Filter manual jobs
        job_manual = (jobs[job]['when'] == 'manual')
        if job_manual and not options.manual and not Lists.match(
                options.names, job, ignore_case=options.ignore_case,
                no_regex=options.no_regex):
            continue

        # Filter disabled jobs
        if jobs[job]['options']['disabled']:
            continue

        # Raise initial result
        if result is None:
            result = True

        # Run job
        attempt = 0
        expected = result
        jobs_status['jobs_count'] += 1
        result = runner(options, jobs[job], result, jobs_status)

        # Retry job if allowed
        if expected and not result and jobs[job]['retry'] > 0:
            while not result and attempt < jobs[job]['retry']:
                attempt += 1
                result = runner(options, jobs[job], expected, jobs_status)

    # Non quiet jobs
    if not jobs_status['quiet']:

        # Pipeline jobs footer
        if jobs_status['jobs_count'] > 1:

            # Evaluate duration total time
            time_total_duration = time() - jobs_status['time_launcher']
            time_total_seconds = '%.0f second%s' % (time_total_duration % 60, 's' if
                                                    time_total_duration % 60 > 1 else '')
            time_total_minutes = ''
            if time_total_duration >= 60:
                time_total_minutes = '%.0f minute%s ' % (
                    time_total_duration / 60, 's' if time_total_duration / 60 > 1 else '')
            time_total_string = time_total_minutes + time_total_seconds

            # Final footer
            print(' %s> Pipeline: %s in %s total%s' %
                  (Colors.YELLOW, Colors.BOLD + 'Success' if result else Colors.RED +
                   'Failure', time_total_string, Colors.RESET))
            print(' ')
            print(' ')
            Platform.flush()

        # Simple job footer
        else:
            print(' ')
            Platform.flush()

    # Result
    return bool(result)

# Runner
def runner(options, job_data, last_result, jobs_status):