summaryrefslogtreecommitdiff
path: root/branding/scripts/vendorize.py
blob: c600967429f2083e6f9deafecc8ba78e9387718a (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3

import datetime
import os
import sys

from string import Template
import configparser

OUTFILE = 'config.go'
INFILE = 'config.go.tmpl'
CONFIGFILE = 'config/vendor.conf'
SCRIPT_NAME = 'vendorize'


def getDefaultProvider(config):
    provider = os.environ.get('PROVIDER')
    if provider:
        print('[+] Got provider {} from environemnt'.format(provider))
    else:
        print('[+] Using default provider from config file')
        provider = config['default']['provider']
    return provider


def getProviderData(provider, config):
    print("[+] Configured provider:", provider)

    c = config[provider]
    d = dict()

    keys = ('name', 'applicationName', 'binaryName',
            'providerURL', 'tosURL', 'helpURL',
            'donateURL', 'apiURL', 'geolocationAPI', 'caCertString')

    for value in keys:
        d[value] = c.get(value)

    d['timeStamp'] = '{:%Y-%m-%d %H:%M:%S}'.format(
        datetime.datetime.now())

    return d


def addCaData(data, configfile):
    provider = data.get('name').lower()
    folder, f = os.path.split(configfile)
    caFile = os.path.join(folder, provider + '-ca.crt')
    if not os.path.isfile(caFile):
        bail('[!] Cannot find CA file in {path}'.format(path=caFile))
    with open(caFile) as ca:
        data['caCertString'] = ca.read().strip()


def writeOutput(data, infile, outfile):

    with open(infile) as infile:
        s = Template(infile.read())

    with open(outfile, 'w') as outf:
        outf.write(s.substitute(data))


def bail(msg=None):
    if not msg:
        print('Usage: {scriptname}.py <template> <config> <output>'.format(
            scriptname=SCRIPT_NAME))
    else:
        print(msg)
    sys.exit(1)


if __name__ == "__main__":
    infile = outfile = ""

    if len(sys.argv) > 4:
        bail()

    elif len(sys.argv) == 1:
        infile = INFILE
        outfile = OUTFILE
        configfile = CONFIGFILE
    else:
        try:
            infile = sys.argv[1]
            configfile = sys.argv[2]
            outfile = sys.argv[3]
        except IndexError:
            bail()

    if not os.path.isfile(infile):
        bail('[!] Cannot find template in {path}'.format(
            path=os.path.abspath(infile)))
    elif not os.path.isfile(configfile):
        bail('[!] Cannot find config in {path}'.format(
            path=os.path.abspath(configfile)))
    else:
        print('[+] Using {path} as template'.format(
            path=os.path.abspath(infile)))
        print('[+] Using {path} as config'.format(
            path=os.path.abspath(configfile)))

    config = configparser.ConfigParser()
    config.read(configfile)

    provider = getDefaultProvider(config)
    data = getProviderData(provider, config)
    addCaData(data, configfile)
    writeOutput(data, infile, outfile)

    print('[+] Wrote configuration for {provider} to {outf}'.format(
        provider=data.get('name'),
        outf=os.path.abspath(outfile)))