mypythontools_cicd.cicd package

Run cicd functions from other modules in particular order and provide configuration.

This module can be used for example in running deploy pipelines or githooks (some code automatically executed before commit). This module can run the tests, edit library version, generate rst files for docs, push to git or deploy app to pypi.

All of that can be done with one function call - with cicd_pipeline function that run other functions, or you can use functions separately.

mypythontools_cicd.cicd.default_pipeline_config

Default values for pipeline. If something changes here, it will change in all the repos. You can edit any values in pipeline. Intellisense and help tooltip should help.

Type

PipelineConfig

Examples:

From python script

Create folder utils, create push_script.py inside, add:

from mypythontools_cicd.cicd import cicd_pipeline, default_pipeline_config

if __name__ == "__main__":
    cicd_pipeline(config=default_pipeline_config)

With terminal command

Run code like this in terminal:

mypythontools_cicd --test True --deploy True

If you need to ensure that it will run from particular venv, prepend path_to_python/python.exe -m

VS Code Task example

You can push changes with single click with all the hooks displaying results in your terminal. All params changing every push (like git message or tag) can be configured on the beginning and therefore you don’t need to wait for test finish. Default values can be also used, so in small starting projects, push is actually very fast.

Here you can find all the tasks with with interactive version and git message config

https://github.com/Malachov/Software-settings/blob/master/VS-Code/tasks.json

Add the tasks to global tasks.json. Here is example of one such a task

{
  "version": "2.0.0",
  "tasks": [
    {
        "label": "Push to PyPi",
        "command": "${command:python.interpreterPath}",
        "args": ["-m", "mypythontools_cicd", "--do_only", "reformat"]
    },
  ]
}

Git hooks example

Create folder git_hooks with git hook file - for pre commit name must be pre-commit (with no extension). Hooks in git folder are gitignored by default (and hooks is not visible on first sight).

Then add hook to git settings - run in terminal (last arg is path (created folder)):

$ git config core.hooksPath git_hooks

In created folder on first two lines copy this:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

Then just import any function from here and call with desired params. E.g.

class mypythontools_cicd.cicd.PipelineConfig(*args, **kwargs)[source]

Bases: Config

Allow to setup CICD pipeline.

property allowed_branches

Pipeline runs only on defined branches.

Type

None | Sequence[str]

Default

[“master”, “main”]

config_fields: ConfigFields
property deploy

Deploy to PYPI.

TWINE_USERNAME and TWINE_PASSWORD are used for authorization.

Type

bool

Default

False

property do_only

Run just single function from pipeline, ignore the others.

Type
Literal[

None, “prepare_venvs”, “reformat”, “test”, “docs”, “sync_requirements”, “git_commit_all”, “git_push”, “deploy”

]

Default

None

Reason for why to call it form here and not directly is to be able to use sys args or single command line entrypoint.

property docs

Define whether generate sphinx apidoc and generate rst files for documentation.

Type

bool

Default

True

Some files in docs source can be deleted - check docs docstrings for details.

property git_commit_all

Whether take all the changes in repository and create a commit with these changes.

Note

!!! Be cautious here if using with git_push !!!

Type

None | str

Default

‘New commit’

property git_push

Whether push to repository.

Type

bool

Default

True

property prepare_venvs

Create venvs with defined versions.

Type

None | list[str]

Default

None

property prepare_venvs_path

Prepare venvs in defined path.

Type

str

Default

“venv”

property reformat

Reformat all python files with black. Setup parameters in pyproject.toml.

Type

bool

Default

True

property set_version

Overwrite __version__ in __init__.py.

Type

str

Default

‘increment’.

Version has to be in format like ‘1.0.3’ three digits and two dots. If ‘None’, nothing will happen. If ‘increment’, than it will be updated by 0.0.1..

property sync_requirements

Check requirements.txt and update all the libraries.

Type

None | Literal[“infer”] | PathLike | Sequence[PathLike]

Default

None

You can use path to requirements, list of paths. If “infer”, auto detected (all requirements), not recursively, only on defined path.

property sync_requirements_path

Define the root if using just names or relative path, and not found.

Type

PathLike

Default

PROJECT_PATHS.root

It’s also necessary when using another referenced files. If inferring files, it’s used to search. Defaults to PROJECT_PATHS.root.

property tag

Tag. E.g ‘v1.1.2’. If ‘__version__’, get the version.

Type

str | None

Default

‘__version__’

property tag_message

Tag message.

Type

str

Default

‘New version’

property venv

Used venv. Now used only in “deploy” function for build.

Type

None | Venv

Default

Venv(“venv”)

property verbosity

Pipeline runs only on defined branches.

Type

Literal[0, 1, 2]

Default

1

mypythontools_cicd.cicd.cicd_pipeline(config: ~mypythontools_cicd.cicd.cicd_internal.PipelineConfig = <mypythontools_cicd.cicd.cicd_internal.PipelineConfig object>) None[source]

Run pipeline for pushing and deploying an app or a package.

Can run tests, generate rst files for sphinx docs, push to github and deploy to pypi. All params can be configured not only with function params, but also from command line with params and therefore callable from terminal and optimal to run from IDE (for example with creating simple VS Code task).

Some function suppose some project structure (where are the docs, where is __init__.py etc.). If you are issuing some error, try functions directly, find necessary paths in parameters and set paths that are necessary in paths module.

Note

Beware, that by default, it creates a commit and add all the changes, not only the staged ones.

When using sys args for boolean values, always define True or False.

There is command line entrypoint called mypythontools_cicd. After mypythontools is installed, you can use it in terminal like:

mypythontools_cicd --do_only reformat
Parameters

config (PipelineConfig, optional) – PipelineConfig object with CICD pipeline configuration. Just import default_pipeline_config and use intellisense and help tooltip with description. It is also possible to configure all the params with CLI args from terminal. Defaults to default_pipeline_config.

Example

Recommended use is from IDE (for example with Tasks in VS Code). Check utils docs for how to use it. You can also use it from python…

from mypythontools_cicd.cicd import cicd_pipeline, default_pipeline_config

default_pipeline_config.deploy = True
# default_pipeline_config.do_only = ""


if __name__ == "__main__":
    # All the parameters can be overwritten via CLI args
    cicd_pipeline(config=default_pipeline_config)

It’s also possible to use CLI and configure it via args. This example just push repo to PyPi.

python path-to-project/utils/push_script.py --do_only deploy

Another way how to run it is to use IDE. For example in VS Code you can use Tasks. Check cicd docs for examples.