summaryrefslogtreecommitdiff
path: root/xpi-repack
blob: 080b0ad31f46b544f20533a52e9bff1e7df0383f (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
#!/usr/bin/python

# Copyright (c) 2010-2014, Benjamin Drung <bdrung@debian.org>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import argparse
import os
import subprocess
import sys
import tempfile

COMPRESSION_FORMATS = ["bz2", "gz", "xz"]
SCRIPT_NAME = "xpi-repack"


def remove_recursive(path):
    """equivalent to rm -rf path"""
    if os.path.exists(path):
        for i in os.listdir(path):
            full_path = os.path.join(path, i)
            if os.path.isdir(full_path):
                remove_recursive(full_path)
            else:
                os.remove(full_path)
        os.rmdir(path)


def repack_xpi(package, upstream_version, xpi_file, extension, verbose):
    # extract xpi file
    tmp_dir = tempfile.mkdtemp(prefix=SCRIPT_NAME+".")
    try:
        extract_dir = package + "-" + upstream_version
        full_extract_dir = os.path.join(tmp_dir, extract_dir)
        subprocess.check_call(["xpi-unpack", xpi_file, full_extract_dir])

        if not extension:
            # check, if source 3.0 (quilt) format is used
            extension = "gz"
            if os.path.isfile("debian/source/format"):
                source_format = open("debian/source/format").readline().strip()
                if source_format == "3.0 (quilt)":
                    extension = "xz"

        # pack source
        directory = os.path.realpath(os.path.dirname(xpi_file))
        tar_file = package + "_" + upstream_version + ".orig.tar." + extension
        full_tar_file = os.path.join(directory, tar_file)
        cmd = ["tar", "-ca", "-C", tmp_dir, "-f", full_tar_file, extract_dir]
        if verbose:
            print " ".join(cmd)
        subprocess.check_call(cmd)
    finally:
        # remove temporary directory
        remove_recursive(tmp_dir)


def get_source_package_name():
    if not os.path.isfile("debian/control"):
        sys.stderr.write(SCRIPT_NAME + ": Error: debian/control file is "
                         "missing. Please execute the script in a Debian "
                         "source package or provide a source package name.\n")
        sys.exit(1)
    lines = open("debian/control").readlines()
    package_lines = [x for x in lines if x.find("Source:") >= 0]
    packages = [x[x.find(":")+1:].strip() for x in package_lines]
    return packages[0]


def main():
    epilog = "See {prog}(1) for more info.".format(prog=SCRIPT_NAME)
    parser = argparse.ArgumentParser(epilog=epilog)
    parser.add_argument("xpi_file", metavar="<xpi-file>",
                        help=".xpi file that should be repacked")
    parser.add_argument("-p", "--package", help="specify source package name")
    parser.add_argument("-u", "--upstream-version", dest="version",
                        help="specify the upstream version")
    parser.add_argument("-f", "--format", choices=COMPRESSION_FORMATS,
                        help="compression format for the produced tarball")
    parser.add_argument("-v", "--verbose", action="store_true",
                        help="print more information")

    args = parser.parse_args()

    if not args.package:
        args.package = get_source_package_name()
    if not args.version:
        parser.error("Unknown upstream version. "
                     "You have to specify one with --upstream-version.")

    repack_xpi(args.package, args.version, args.xpi_file, args.format,
               args.verbose)

if __name__ == "__main__":
    main()