summaryrefslogtreecommitdiff
path: root/branding/scripts/gen-qtinstaller
blob: 5b4d103d4e778bfaa345d9f7159af5ab4d0c4a6e (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3

import configparser
import json
import os
import sys
import time

from string import Template

from provider import getDefaultProviders
from provider import getProviderData

OS_CONFIG = {
    'osx': """
    <!-- osx -->
    <TargetDir>/Applications/$APPNAME</TargetDir>
    <StartMenuDir>$APPNAME.app</StartMenuDir>
    <RunProgram>open</RunProgram>
    <RunProgramDescription>Start $APPNAME now!</RunProgramDescription>
      <RunProgramArguments>
        <Argument>@TargetDir@/$APPNAME.app</Argument>
      </RunProgramArguments>

    <WizardStyle>mac</WizardStyle>
    <!-- end osx -->
""",
    'windows': """
    <!-- windows -->
    <TargetDir>@ApplicationsDir@/$APPNAME</TargetDir>
    <StartMenuDir>$APPNAME</StartMenuDir>

    <RunProgram>@TargetDir@/$BINNAME.exe</RunProgram>
    <RunProgramArguments>
    </RunProgramArguments>

    <WizardStyle>Modern</WizardStyle>
    <!-- end windows -->
"""
}

def getData():
    config = configparser.ConfigParser()
    configPath = os.path.join(VENDOR_PATH, 'vendor.conf')
    config.read(configPath)

    provider = os.environ.get('PROVIDER')
    if not provider:
        provider = getDefaultProviders(config)[0]
    return getProviderData(provider, config)

def generateQtInstallerBoilerplate(data, platform, outDir):
    generateConfig(data, platform, outDir)
    generatePackageMetadata(data, platform, outDir)

def generateConfig(data, platform, outDir):
    templateData = {
        'APPNAME': data['applicationName'],
        'BINNAME': data['binaryName'],
        'VERSION': getVersion(),
        'TIMESTAMP': time.strftime('%Y-%m-%d'),
    }

    platformStr = OS_CONFIG[platform]
    platBlock = Template(platformStr).substitute(**templateData)
    templateData['PLATFORM_BLOCK'] = platBlock

    renderTemplate(
         templatePath='../templates/qtinstaller/config/config.xml',
         outPath=os.path.join(outDir, 'config/config.xml'),
         data=templateData)

def generatePackageMetadata(data, platform, outDir):
    appname = 'applicationName'
    templateData = {
        'APPNAME': data[appname],
        'BINNAME': data['binaryName'],
        'VERSION': getVersion(),
        'TIMESTAMP': time.strftime('%Y-%m-%d')
    }
    if platform == "windows":
        p = data[appname] + " for Windows"
    elif platform == "osx":
        p = data[appname] + " for OSX"
    elif platform == "linux":
        p = data[appname] + " for GNU/Linux"
    else:
        p = data[appname]
    templateData['APPNAME_PLATFORM'] = p

    renderTemplate(
         templatePath='../templates/qtinstaller/packages/bitmaskvpn/meta/package.xml',
         outPath=os.path.join(outDir, 'packages/bitmaskvpn/meta/package.xml'),
         data=templateData)

    renderTemplate(
         templatePath='../templates/qtinstaller/packages/bitmaskvpn/meta/install.js',
         outPath=os.path.join(outDir, 'packages/bitmaskvpn/meta/install.js'),
         data=templateData)

def renderTemplate(templatePath=None, outPath=None, data=None):
    with open(os.path.join(here(), templatePath), 'r') as f:
        t = f.read()	
    rendered = Template(t).substitute(**data)
    os.makedirs(os.path.dirname(outPath), exist_ok=True)
    with open(outPath, 'w') as out:
        out.write(rendered)

def here():
    return os.path.abspath(os.path.dirname(__file__))

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

def getVersion():
    return os.environ.get('VERSION', 'unknown')

if __name__ == "__main__":
    VENDOR_PATH = os.environ.get('VENDOR_PATH')

    if len(sys.argv) != 3:
        bail()
    platform = sys.argv[1]
    outDir = sys.argv[2]
    print("[+] Generating qtinstaller files...")
    data = getData()
    generateQtInstallerBoilerplate(data, platform, outDir)