summaryrefslogtreecommitdiff
path: root/scripts/build-config
blob: b350f692932a285bbfccec05f8f5c309e9f9be61 (plain)
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
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# Copyright (C) 2015 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# File: build-config
# Purpose:
#  This script serves the same purpose as ./configure in traditional
#   autoconf setups.
#  It takes build configuration options from command line, checks them,
#  builds a config dictionary, augments it with some default and/or
#  computed values and saves it to build/build-config.json
#  for other build scripts to read.

import argparse
import re
import sys
import os
import getpass
import platform
import json
import distutils.dir_util

import defaults

# argparse converts hyphens to underscores,
# so for lookups in the original options hash we have to
# convert them back
def field_to_option(s):
    return re.sub(r'_', '-', s)

def get_default_build_by():
    return "{user}@{host}".format(user= getpass.getuser(), host=platform.node())


# Options dict format:
# '$option_name_without_leading_dashes': { ('$help_string', $default_value_generator_thunk, $value_checker_thunk) }
options = {
   'architecture': ('Image target architecture (amd64 or i586)', lambda: 'amd64', lambda x: x in ['amd64', 'i586']),
   'build-by': ('Builder identifier (e.g. jrandomhacker@example.net)', get_default_build_by, None),
   'debian-mirror': ('Debian repository mirror for ISO build', lambda: defaults.DEBIAN_MIRROR, None),
   'pbuilder-debian-mirror': ('Debian repository mirror for pbuilder env bootstrap', lambda: defaults.DEBIAN_MIRROR, None),
   'build-type': ('Build type, release or development', lambda: 'development', lambda x: x in ['release', 'development'])
}

# Create the option parser
parser = argparse.ArgumentParser()
for k, v in options.items():
    parser.add_argument('--' + k, type=str, help=v[0], default=v[1]())

args = vars(parser.parse_args())

# Validate options
for k, v in args.items():
    key = field_to_option(k)
    func = options[key][2]
    if func is not None:
        if not func(v):
            print("{v} is not a valid value for --{o} option".format(o=key, v=v))
            sys.exit(1)

# Some fixup for mirror settings.
# The idea is: if --debian-mirror is specified but --pbuilder-debian-mirror is not,
# use the --debian-mirror value for both lb and pbuilder bootstrap
if (args['debian_mirror'] != defaults.DEBIAN_MIRROR) and \
   (args['pbuilder_debian_mirror'] == defaults.DEBIAN_MIRROR):
    args['pbuilder_debian_mirror'] = args['debian-mirror']

# Populate some defaults that are not configurable,
# but that are handy to have in the options hash
args['distribution'] = defaults.DEBIAN_DISTRIBUTION
args['build_dir'] = os.path.join(os.getcwd(), defaults.BUILD_DIR)
args['pbuilder_config'] = defaults.PBUILDER_CONFIG


# Save to file
distutils.dir_util.mkpath(defaults.BUILD_DIR)

with open(defaults.BUILD_CONFIG, 'w') as f:
    json.dump(args, f)