tools/support/dmg-license.py
author sheepluva
Sat, 03 Dec 2016 09:09:16 +0100
changeset 12103 966a9739812f
parent 9343 67552b2e20ca
permissions -rwxr-xr-x
copyToXYFromRect: fix pixels overflowing pixel lines in dest
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9343
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     1
#! /usr/bin/env python
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     2
"""
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     3
This script adds a license file to a DMG. Requires Xcode and a plain ascii text
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     4
license file.
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     5
Obviously only runs on a Mac.
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     6
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     7
Copyright (C) 2011 Jared Hobbs
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     8
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
     9
Permission is hereby granted, free of charge, to any person obtaining a copy
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    10
of this software and associated documentation files (the "Software"), to deal
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    11
in the Software without restriction, including without limitation the rights
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    13
copies of the Software, and to permit persons to whom the Software is
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    14
furnished to do so, subject to the following conditions:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    15
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    16
The above copyright notice and this permission notice shall be included in
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    17
all copies or substantial portions of the Software.
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    18
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    25
THE SOFTWARE.
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    26
"""
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    27
import os
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    28
import sys
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    29
import tempfile
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    30
import optparse
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    31
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    32
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    33
class Path(str):
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    34
    def __enter__(self):
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    35
        return self
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    36
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    37
    def __exit__(self, type, value, traceback):
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    38
        os.unlink(self)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    39
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    40
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    41
def mktemp(dir=None, suffix=''):
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    42
    (fd, filename) = tempfile.mkstemp(dir=dir, suffix=suffix)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    43
    os.close(fd)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    44
    return Path(filename)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    45
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    46
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    47
def main(options, args):
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    48
    dmgFile, license = args
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    49
    with mktemp('.') as tmpFile:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    50
        with open(tmpFile, 'w') as f:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    51
            f.write("""data 'LPic' (5000) {
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    52
    $"0002 0011 0003 0001 0000 0000 0002 0000"
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    53
    $"0000 000E 0006 0001 0005 0007 0000 0007"
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    54
    $"0008 0000 0047 0009 0000 0034 000A 0001"
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    55
    $"0035 000B 0001 0020 000C 0000 0011 000D"
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    56
    $"0000 005B 0004 0000 0033 000F 0001 000C"
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    57
    $"0010 0000 000B 000E 0000"
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    58
};\n\n""")
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    59
            with open(license, 'r') as l:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    60
                f.write('data \'TEXT\' (5002, "English") {\n')
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    61
                for line in l:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    62
                    if len(line) < 1000:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    63
                        f.write('    "' + line.strip().replace('"', '\\"') +
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    64
                                '\\n"\n')
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    65
                    else:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    66
                        for liner in line.split('.'):
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    67
                            f.write('    "' +
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    68
                                    liner.strip().replace('"', '\\"') +
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    69
                                    '. \\n"\n')
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    70
                f.write('};\n\n')
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    71
            f.write("""resource 'STR#' (5002, "English") {
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    72
    {
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    73
        "English",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    74
        "Agree",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    75
        "Disagree",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    76
        "Print",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    77
        "Save...",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    78
        "IMPORTANT - By clicking on the \\"Agree\\" button, you agree "
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    79
        "to be bound by the terms of the License Agreement.",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    80
        "Software License Agreement",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    81
        "This text cannot be saved. This disk may be full or locked, or the "
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    82
        "file may be locked.",
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    83
        "Unable to print. Make sure you have selected a printer."
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    84
    }
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    85
};""")
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    86
        os.system('/usr/bin/hdiutil unflatten -quiet "%s"' % dmgFile)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    87
        os.system('%s "%s/"*.r %s -a -o "%s"' %
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    88
                  (options.rez, options.flat_carbon, tmpFile, dmgFile))
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    89
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    90
        os.system('/usr/bin/hdiutil flatten -quiet "%s"' % dmgFile)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    91
        if options.compression is not None:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    92
            os.system('cp %s %s.temp.dmg' % (dmgFile, dmgFile))
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    93
            os.remove(dmgFile)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    94
            if options.compression == "bz2":
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    95
                os.system('hdiutil convert %s.temp.dmg -format UDBZ -o %s' %
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    96
                          (dmgFile, dmgFile))
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    97
            elif options.compression == "gz":
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    98
                os.system('hdiutil convert %s.temp.dmg -format ' % dmgFile +
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
    99
                          'UDZO -imagekey zlib-devel=9 -o %s' % dmgFile)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   100
            os.remove('%s.temp.dmg' % dmgFile)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   101
    print "Successfully added license to '%s'" % dmgFile
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   102
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   103
if __name__ == '__main__':
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   104
    parser = optparse.OptionParser()
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   105
    parser.set_usage("""%prog <dmgFile> <licenseFile> [OPTIONS]
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   106
  This program adds a software license agreement to a DMG file.
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   107
  It requires Xcode and a plain ascii text <licenseFile>.
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   108
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   109
  See --help for more details.""")
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   110
    parser.add_option(
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   111
        '--rez',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   112
        '-r',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   113
        action='store',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   114
        default='/Applications/Xcode.app/Contents/Developer/Tools/Rez',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   115
        help='The path to the Rez tool. Defaults to %default'
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   116
    )
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   117
    parser.add_option(
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   118
        '--flat-carbon',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   119
        '-f',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   120
        action='store',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   121
        default='/Applications/Xcode.app/Contents/Developer/Platforms'
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   122
                '/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk'
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   123
                '/Developer/Headers/FlatCarbon',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   124
        help='The path to the FlatCarbon headers. Defaults to %default'
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   125
    )
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   126
    parser.add_option(
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   127
        '--compression',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   128
        '-c',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   129
        action='store',
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   130
        choices=['bz2', 'gz'],
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   131
        default=None,
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   132
        help='Optionally compress dmg using specified compression type. '
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   133
             'Choices are bz2 and gz.'
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   134
    )
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   135
    options, args = parser.parse_args()
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   136
    cond = len(args) != 2 or not os.path.exists(options.rez) \
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   137
        or not os.path.exists(options.flat_carbon)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   138
    if cond:
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   139
        parser.print_usage()
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   140
        sys.exit(1)
67552b2e20ca import create-dmg script from https://github.com/andreyvit/yoursway-create-dmg
koda
parents:
diff changeset
   141
    main(options, args)