Skip to content

Commit

Permalink
feat(init): add a subcommand to create an example config
Browse files Browse the repository at this point in the history
  • Loading branch information
k-dovgan authored Sep 21, 2020
1 parent 1ee5e87 commit 2b0b8a1
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 23 deletions.
3 changes: 2 additions & 1 deletion doc/args.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ via ``python3.7 -m universum``.
| * -f='test 1:!unit test 1' - run all steps with 'test 1' substring in their names except those
containing 'unit test 1'
{poll,submit,nonci,github-handler} : @replace
{create-config,poll,submit,nonci,github-handler} : @replace
| :doc:`universum init <init>`
| :doc:`universum poll <args_poll>`
| :doc:`universum submit <args_submit>`
| :doc:`universum nonci <args_nonci>`
Expand Down
3 changes: 3 additions & 0 deletions doc/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ to this module in `config_path` member of its input settings.
The project is free to use whatever it needs in the configuration file; just remember,
all the calculations are done on config processing, not step execution.

To :doc:`create an example config and get a command to launch it with Universum <init>`,
run ``python3.7 -m universum init``.


Project configuration
---------------------
Expand Down
6 changes: 6 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ The goal of this project is to provide unified approach for adding continuous in
to any project. It currently supports Perforce, Git, Gerrit, Swarm, Jenkins and TeamCity.

Sometimes `Universum` system can be referred to as the framework or just CI.

To install Universum, make sure to :doc:`meet prerequisites <prerequisites>` and then simply run
``pip3.7 install -U universum`` from command line.

To :doc:`create an example config and generate a command line to run it with Universum <init>`,
run ``python3.7 -m universum init``.
11 changes: 11 additions & 0 deletions doc/init.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
:orphan:

Initialize Universum
--------------------

If you have Universum installed via ``pip3.7 install -U universum`` or downloaded it from GitHub and changed
working directory to project root, run ``python3.7 -m universum init`` to create an example configuration file,
that can be :doc:`updated according to your project needs <configuring>` later.

Running this command will not only create a configuration file, but will also provide you with a command line to
execute it with Universum.
16 changes: 16 additions & 0 deletions tests/test_create_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pathlib import Path
import subprocess


def test_create_config():
result = subprocess.run(["python3.7", "-m", "universum", "init"], capture_output=True, check=True)
new_command = ''
for line in result.stdout.splitlines():
if line.startswith(b'$ '): # result.stdout is a byte string
new_command = line.lstrip(b'$ ')
new_result = subprocess.run(new_command.split(), capture_output=True, check=True)
Path(".universum.py").unlink()
artifacts = Path("artifacts")
artifacts.joinpath("CONFIGS_DUMP.txt").unlink()
artifacts.rmdir()
assert 'Hello world' in str(new_result.stdout)
4 changes: 3 additions & 1 deletion universum/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import __version__, __title__
from .api import Api
from .github_handler import GithubHandler
from .config_creator import ConfigCreator
from .lib.ci_exception import SilentAbortException
from .lib.gravity import define_arguments_recursive, construct_component
from .lib.module_arguments import ModuleArgumentParser, ModuleNamespace, IncorrectParameterError
Expand All @@ -23,7 +24,7 @@ def define_arguments() -> ModuleArgumentParser:
define_arguments_recursive(Main, parser)

subparsers = parser.add_subparsers(title="Additional commands",
metavar="{poll,submit,nonci,github-handler}",
metavar="{init,poll,submit,nonci,github-handler}",
help="Use 'universum <subcommand> --help' for more info")

def define_command(klass, command):
Expand All @@ -33,6 +34,7 @@ def define_command(klass, command):
define_arguments_recursive(klass, command_parser)

define_command(Api, "api")
define_command(ConfigCreator, "init")
define_command(Poll, "poll")
define_command(Submit, "submit")
define_command(Nonci, "nonci")
Expand Down
21 changes: 2 additions & 19 deletions universum/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from types import TracebackType
from typing import Optional

import sys

from .modules.api_support import ApiSupport
from .modules.output.output import MinimalOut
from .lib.gravity import Module, Dependency
from .lib.module_arguments import ModuleArgumentParser
from .lib.utils import format_traceback

__all__ = ["Api"]

Expand All @@ -28,21 +25,7 @@ def __init__(self, *args, **kwargs) -> None:
sys.stderr.write(str(error) + '\n')
sys.exit(2)

class MinimalOut:
@staticmethod
def log(line: str) -> None:
pass

@staticmethod
def report_build_problem(problem: str) -> None:
pass

@staticmethod
def log_exception(exc: Exception) -> None:
ex_traceback: Optional[TracebackType] = sys.exc_info()[2]
sys.stderr.write("Unexpected error.\n" + format_traceback(exc, ex_traceback))

self.out = MinimalOut()
self.out = MinimalOut(silent=True)

def execute(self) -> None:
if self.settings.action == "file-diff":
Expand Down
34 changes: 34 additions & 0 deletions universum/config_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pathlib import Path

from .modules.output.output import MinimalOut
from .lib.gravity import Module
__all__ = ["ConfigCreator"]


class ConfigCreator(Module):
description: str = "Create a dummy project"

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.out = MinimalOut()

def execute(self) -> None:
config_name = ".universum.py"
self.out.log(f"Creating an example configuration file '{config_name}'")

config = Path(config_name)
config.write_text("""#!/usr/bin/env python3.7
from universum.configuration_support import Variations
configs = Variations([dict(name='Show directory contents', command=['ls', '-la']),
dict(name='Print a line', command=['bash', '-c', 'echo Hello world'])])
if __name__ == '__main__':
print(configs.dump())
""")
self.out.log("To run with Universum, execute the following command:\n"
"$ python3.7 -m universum nonci --launcher-config-path={}".format(config_name))

def finalize(self) -> None:
pass
25 changes: 23 additions & 2 deletions universum/modules/output/output.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from typing import ClassVar, Union
import sys
from types import TracebackType
from typing import ClassVar, Optional, Union

from ...lib.gravity import Module, Dependency
from ...lib import utils
from .terminal_based_output import TerminalBasedOutput
from .teamcity_output import TeamcityOutput

__all__ = [
"HasOutput"
"HasOutput",
"MinimalOut"
]


Expand Down Expand Up @@ -70,3 +73,21 @@ class HasOutput(Module):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.out: Output = self.out_factory()


class MinimalOut:
def __init__(self, silent=False):
self.silent = silent

def log(self, line: str) -> None:
if not self.silent:
print(line)

@staticmethod
def report_build_problem(problem: str) -> None:
pass

@staticmethod
def log_exception(exc: Exception) -> None:
ex_traceback: Optional[TracebackType] = sys.exc_info()[2]
sys.stderr.write("Unexpected error.\n" + utils.format_traceback(exc, ex_traceback))

0 comments on commit 2b0b8a1

Please sign in to comment.