summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/repackaging/installer.py
blob: 9bd17613bffbe2e2deed3ca6e679a747fdfd85a4 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import shutil
import tempfile
import zipfile

import mozpack.path as mozpath

from mozbuild.action.exe_7z_archive import archive_exe
from mozbuild.util import ensureParentDir


def repackage_installer(
    topsrcdir, tag, setupexe, package, output, package_name, sfx_stub, use_upx
):
    if package and not zipfile.is_zipfile(package):
        raise Exception("Package file %s is not a valid .zip file." % package)
    if package is not None and package_name is None:
        raise Exception("Package name must be provided, if a package is provided.")
    if package is None and package_name is not None:
        raise Exception(
            "Package name must not be provided, if a package is not provided."
        )

    # We need the full path for the tag and output, since we chdir later.
    tag = mozpath.realpath(tag)
    output = mozpath.realpath(output)
    ensureParentDir(output)

    tmpdir = tempfile.mkdtemp()
    old_cwd = os.getcwd()
    try:
        if package:
            z = zipfile.ZipFile(package)
            z.extractall(tmpdir)
            z.close()

        # Copy setup.exe into the root of the install dir, alongside the
        # package.
        shutil.copyfile(setupexe, mozpath.join(tmpdir, mozpath.basename(setupexe)))

        # archive_exe requires us to be in the directory where the package is
        # unpacked (the tmpdir)
        os.chdir(tmpdir)

        sfx_package = mozpath.join(topsrcdir, sfx_stub)

        archive_exe(package_name, tag, sfx_package, output, use_upx)

    finally:
        os.chdir(old_cwd)
        shutil.rmtree(tmpdir)