File: make-macpkg

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (128 lines) | stat: -rwxr-xr-x 3,935 bytes parent folder | download
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
#!/usr/bin/env python
"""Script to create a mac pkg installer.

This script assumes that an executable has been produced previously
by the sibling script make-pyinstaller.
"""

import argparse
import os
import shutil
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from utils import extract_zip, run, tmp_dir

ROOT = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
PKG_DIR = os.path.join(ROOT, 'macpkg')
SCRIPTS_DIR = os.path.join(PKG_DIR, 'scripts')
RESOURCES_DIR = os.path.join(PKG_DIR, 'resources')
DISTRIBUTION_PATH = os.path.join(PKG_DIR, 'distribution.xml')

TEMP_PKG_NAME = 'aws-cli.pkg'
PKG_NAME = 'AWS-CLI-Installer.pkg'
EXE_ZIP_NAME = 'awscli-exe.zip'
EXE_NAME = 'aws'


def make_pkg(pkg_name, src_exe):
    with tmp_dir() as workdir:
        extract_zip(src_exe, workdir)
        stage_files(workdir)
        do_make_pkg(workdir, pkg_name)


def stage_files(workdir):
    # Stage all the distribution files inside a directory named aws-cli
    stage_dir = os.path.join(workdir, 'stage')
    os.makedirs(stage_dir)
    dist_dir = os.path.join(workdir, 'aws', 'dist')
    cli_dir = os.path.join(stage_dir, 'aws-cli')
    shutil.move(dist_dir, cli_dir)


def do_make_pkg(workdir, pkg_name):
    version = get_version(workdir)
    print(
        run(
            (
                'pkgbuild --identifier com.amazon.aws.cli2 '
                '--root ./stage '
                '--scripts %s '
                '--version %s '
                '%s'
            )
            % (SCRIPTS_DIR, version, TEMP_PKG_NAME),
            cwd=workdir,
        )
    )
    with tmp_dir() as formatted_resource_dir:
        render_resources(
            formatted_resource_dir, RESOURCES_DIR, {'version': version}
        )
        print(
            run(
                ('productbuild --distribution %s --resources %s %s')
                % (DISTRIBUTION_PATH, formatted_resource_dir, PKG_NAME),
                cwd=workdir,
            )
        )
    shutil.copyfile(os.path.join(workdir, PKG_NAME), pkg_name)


def get_version(output_dir):
    exe_dir = os.path.join(output_dir, 'stage', 'aws-cli')
    exe_path = os.path.join(exe_dir, EXE_NAME)
    result = run('%s --version' % exe_path, cwd=exe_dir).strip()
    version = result.split(' ')[0].split('/')[1]
    return version


def render_resources(workdir, resource_dir, variables):
    for filename in os.listdir(resource_dir):
        path = os.path.join(resource_dir, filename)
        if path.endswith('.tmpl'):
            # This is a templated resource. Format its content with the
            # variables and write it out to a filepath with the .tmpl
            # extension stripped.
            dst = os.path.join(workdir, filename[:-5])
            with open(path) as f:
                content = f.read()
            formatted_content = content.format(**variables)
            with open(dst, 'w') as f:
                f.write(formatted_content)
        else:
            # No formatting needed just copy the file.
            dst = os.path.join(workdir, filename)
            shutil.copy2(path, dst)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        '--output',
        default=os.path.join(ROOT, 'dist', PKG_NAME),
        help=(
            'The output PKG name. By default, this will be '
            '"dist/%s" in the root of the awscli.' % PKG_NAME
        ),
    )
    parser.add_argument(
        '--src-exe',
        default=os.path.join(ROOT, 'dist', EXE_ZIP_NAME),
        help=(
            'The exe used to build the PKG. By default, this will be the'
            '"dist/%s" zipfile in the root of the awscli.' % EXE_ZIP_NAME
        ),
    )
    args = parser.parse_args()
    output = os.path.abspath(args.output)
    src_exe = os.path.abspath(args.src_exe)
    make_pkg(output, src_exe)


if __name__ == "__main__":
    main()