1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this file,
3# You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import, print_function
6
7import errno
8import os
9import tempfile
10import tarfile
11import shutil
12import mozpack.path as mozpath
13from mozpack.dmg import create_dmg
14from mozbuild.repackaging.application_ini import get_application_ini_value
15
16
17def repackage_dmg(infile, output):
18
19    if not tarfile.is_tarfile(infile):
20        raise Exception("Input file %s is not a valid tarfile." % infile)
21
22    tmpdir = tempfile.mkdtemp()
23    try:
24        with tarfile.open(infile) as tar:
25            tar.extractall(path=tmpdir)
26
27        # Remove the /Applications symlink. If we don't, an rsync command in
28        # create_dmg() will break, and create_dmg() re-creates the symlink anyway.
29        try:
30            os.remove(mozpath.join(tmpdir, " "))
31        except OSError as e:
32            if e.errno != errno.ENOENT:
33                raise
34
35        volume_name = get_application_ini_value(
36            tmpdir, "App", "CodeName", fallback="Name"
37        )
38
39        # The extra_files argument is empty [] because they are already a part
40        # of the original dmg produced by the build, and they remain in the
41        # tarball generated by the signing task.
42        create_dmg(tmpdir, output, volume_name, [])
43
44    finally:
45        shutil.rmtree(tmpdir)
46