##// END OF EJS Templates
configitems: declare items in a TOML file...
configitems: declare items in a TOML file Mercurial ships with Rust code that also needs to read from the config. Having a way of presenting `configitems` to both Python and Rust is needed to prevent duplication, drift, and have the appropriate devel warnings. Abstracting away from Python means choosing a config format. No single format is perfect, and I have yet to come across a developer that doesn't hate all of them in some way. Since we have a strict no-dependencies policy for Mercurial, we either need to use whatever comes with Python, vendor a library, or implement a custom format ourselves. Python stdlib means using JSON, which doesn't support comments and isn't great for humans, or `configparser` which is an obscure, untyped format that nobody uses and doesn't have a commonplace Rust parser. Implementing a custom format is error-prone, tedious and subject to the same issues as picking an existing format. Vendoring opens us to the vast array of common config formats. The ones being picked for most modern software are YAML and TOML. YAML is older and common in the Python community, but TOML is much simpler and less error-prone. I would much rather be responsible for the <1000 lines of `tomli`, on top of TOML being the choice of the Rust community, with robust crates for reading it. The structure of `configitems.toml` is explained inline.

File last commit:

r44058:99e231af default
r51655:c51b178b default
Show More
hg-docker
129 lines | 3.4 KiB | text/plain | TextLexer
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 #!/usr/bin/env python3
#
# Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
import argparse
import pathlib
import shutil
import subprocess
import sys
Gregory Szorc
black: blacken scripts...
r44058
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 def get_docker() -> str:
docker = shutil.which('docker.io') or shutil.which('docker')
if not docker:
print('could not find docker executable')
return 1
try:
out = subprocess.check_output([docker, '-h'], stderr=subprocess.STDOUT)
if b'Jansens' in out:
Gregory Szorc
black: blacken scripts...
r44058 print(
'%s is the Docking System Tray; try installing docker.io'
% docker
)
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 sys.exit(1)
except subprocess.CalledProcessError as e:
print('error calling `%s -h`: %s' % (docker, e.output))
sys.exit(1)
Gregory Szorc
black: blacken scripts...
r44058 out = subprocess.check_output([docker, 'version'], stderr=subprocess.STDOUT)
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476
lines = out.splitlines()
if not any(l.startswith((b'Client:', b'Client version:')) for l in lines):
print('`%s version` does not look like Docker' % docker)
sys.exit(1)
if not any(l.startswith((b'Server:', b'Server version:')) for l in lines):
print('`%s version` does not look like Docker' % docker)
sys.exit(1)
return docker
Gregory Szorc
black: blacken scripts...
r44058
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 def get_dockerfile(path: pathlib.Path, args: list) -> bytes:
with path.open('rb') as fh:
df = fh.read()
for k, v in args:
Mathias De Mare
packaging: allow running packaging with custom uid+gid for CentOS...
r41270 df = df.replace(bytes('%%%s%%' % k.decode(), 'utf-8'), v)
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476
return df
Gregory Szorc
black: blacken scripts...
r44058
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 def build_docker_image(dockerfile: pathlib.Path, params: list, tag: str):
"""Build a Docker image from a templatized Dockerfile."""
docker = get_docker()
dockerfile_path = pathlib.Path(dockerfile)
dockerfile = get_dockerfile(dockerfile_path, params)
print('building Dockerfile:')
print(dockerfile.decode('utf-8', 'replace'))
args = [
docker,
'build',
Gregory Szorc
black: blacken scripts...
r44058 '--build-arg',
'http_proxy',
'--build-arg',
'https_proxy',
'--tag',
tag,
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 '-',
]
print('executing: %r' % args)
Mathias De Mare
hg-docker: fix Python 3.4 compatibility (for CentOS 7)...
r41269 p = subprocess.Popen(args, stdin=subprocess.PIPE)
p.communicate(input=dockerfile)
if p.returncode:
raise subprocess.CalledProcessException(
Gregory Szorc
black: blacken scripts...
r44058 p.returncode,
'failed to build docker image: %s %s' % (p.stdout, p.stderr),
)
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476
def command_build(args):
build_args = []
for arg in args.build_arg:
k, v = arg.split('=', 1)
build_args.append((k.encode('utf-8'), v.encode('utf-8')))
Gregory Szorc
black: blacken scripts...
r44058 build_docker_image(pathlib.Path(args.dockerfile), build_args, args.tag)
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476
def command_docker(args):
print(get_docker())
Gregory Szorc
black: blacken scripts...
r44058
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 def main() -> int:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title='subcommands')
build = subparsers.add_parser('build', help='Build a Docker image')
build.set_defaults(func=command_build)
Gregory Szorc
black: blacken scripts...
r44058 build.add_argument(
'--build-arg',
action='append',
default=[],
help='Substitution to perform in Dockerfile; ' 'format: key=value',
)
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 build.add_argument('dockerfile', help='path to Dockerfile to use')
build.add_argument('tag', help='Tag to apply to created image')
docker = subparsers.add_parser('docker-path', help='Resolve path to Docker')
docker.set_defaults(func=command_docker)
args = parser.parse_args()
return args.func(args)
Gregory Szorc
black: blacken scripts...
r44058
Gregory Szorc
packaging: replace dockerlib.sh with a Python script...
r38476 if __name__ == '__main__':
sys.exit(main())