Build example

Take a look at this example build.py file that use the ksconf.builder module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
#
# KSCONF Official example app building script
#
# NOTE:  Keep in mind that this is all very experimental and subject to change.
import sys
from pathlib import Path

from ksconf.builder import QUIET, VERBOSE, BuildManager, default_cli
from ksconf.builder.steps import clean_build, copy_files, pip_install

manager = BuildManager()

APP_FOLDER = "TA-my_technology"
SPL_NAME = "ta_my_technology-{{version}}.tgz"
SOURCE_DIR = "."

REQUIREMENTS = "requirements.txt"

# Files that support the build process, but don't end up in the tarball.
BUILD_FILES = [
    REQUIREMENTS,
]

COPY_FILES = [
    "README.md",
    "bin/*.py",
    "default/",
    "metadata/*.meta",
    "static/",
    "lookups/*.csv",
    "appserver/",
    "README/*.spec",
] + BUILD_FILES


@manager.cache([REQUIREMENTS], ["lib/"], timeout=7200)
def python_packages(step):
    # Reuse shared function from ksconf.build.steps
    pip_install(step, REQUIREMENTS, "lib",
                handle_dist_info="remove")


def package_spl(step):
    top_dir = step.dist_path.parent
    release_path = top_dir / ".release_path"
    release_name = top_dir / ".release_name"
    step.run(sys.executable, "-m", "ksconf", "package",
             "--file", step.dist_path / SPL_NAME,   # Path to created tarball
             "--app-name", APP_FOLDER,              # Top-level directory name
             "--block-local",                       # VC build, no 'local' folder
             "--release-file", str(release_path),
             ".")
    # Provide the dist file as a short name too (used by some CI/CD tools)
    path = release_path.read_text()
    short_name = Path(path).name
    release_name.write_text(short_name)


def build(step, args):
    """ Build process """
    # Step 1:  Clean/create build folder
    clean_build(step)

    # Step 2:  Copy files from source to build folder
    copy_files(step, COPY_FILES)

    # Step 3:  Install Python package dependencies
    python_packages(step)

    # Step 4:  Make tarball
    package_spl(step)


if __name__ == '__main__':
    # Tell build manager where stuff lives
    manager.set_folders(SOURCE_DIR, "build", "dist")

    # Launch build CLI
    default_cli(manager, build)

Usage notes

  • BuildManager - is used to help orchestrate the build process.
  • step is an instance of BuildStep, which is passed as the first argument to all the of step-service functions. This class assists with logging, and directing all activities to the correct paths.
  • There’s no interal interface for ksconf package yet, hence another instance of Python is launched on line 48. This is done using the module execution mode of Python, which is a slightly more reliable way of launching ksconf from within itself. For whatever that’s worth.