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