Build example

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

 1#!/usr/bin/env python
 2#
 3# KSCONF Official example app building script
 4#
 5from pathlib import Path
 6
 7from ksconf.builder import QUIET, VERBOSE, BuildManager, BuildStep, default_cli
 8from ksconf.builder.steps import clean_build, copy_files, pip_install
 9
10manager = BuildManager()
11
12APP_FOLDER = "TA-my_technology"
13SPL_NAME = "ta_my_technology-{{version}}.tgz"
14SOURCE_DIR = "."
15
16REQUIREMENTS = "requirements.txt"
17
18# Files that support the build process, but don't end up in the tarball.
19BUILD_FILES = [
20    REQUIREMENTS,
21]
22
23COPY_FILES = [
24    "README.md",
25    "bin/*.py",
26    "default/",
27    "metadata/*.meta",
28    "static/",
29    "lookups/*.csv",
30    "appserver/",
31    "README/*.spec",
32] + BUILD_FILES
33
34
35@manager.cache([REQUIREMENTS], ["lib/"], timeout=7200)
36def python_packages(step):
37    # Reuse shared function from ksconf.build.steps
38    pip_install(step, REQUIREMENTS, "lib",
39                handle_dist_info="remove")
40
41
42def package_spl(step: BuildStep):
43    log = step.get_logger()
44    top_dir = step.dist_path.parent
45    release_path = top_dir / ".release_path"
46    release_name = top_dir / ".release_name"
47    # Verbose message
48    log("Starting to package SPL file!", VERBOSE)
49    step.run_ksconf("package",
50                    "--file", step.dist_path / SPL_NAME,   # Path to created tarball
51                    "--app-name", APP_FOLDER,              # Top-level directory name
52                    "--block-local",                       # VC build, no 'local' folder
53                    "--release-file", str(release_path),
54                    ".")
55    # Provide the dist file as a short name too (used by some CI/CD tools)
56    path = release_path.read_text()
57    short_name = Path(path).name
58    release_name.write_text(short_name)
59    # Output message will be produced even in QUIET mode
60    log(f"Created SPL file:  {short_name}", QUIET)
61
62
63def build(step: BuildStep, args):
64    """ Build process """
65    # Step 1:  Clean/create build folder
66    clean_build(step)
67
68    # Step 2:  Copy files from source to build folder
69    copy_files(step, COPY_FILES)
70
71    # Step 3:  Install Python package dependencies
72    python_packages(step)
73
74    # Step 4:  Make tarball
75    package_spl(step)
76
77
78if __name__ == '__main__':
79    # Tell build manager where stuff lives
80    manager.set_folders(SOURCE_DIR, "build", "dist")
81
82    # Launch build CLI
83    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.