16c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#!/usr/bin/perl -w
26c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# Copyright (C) 2010 Google Inc. All rights reserved.
36c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#
46c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# Redistribution and use in source and binary forms, with or without
56c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# modification, are permitted provided that the following conditions
66c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# are met:
76c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#
86c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# 1.  Redistributions of source code must retain the above copyright
96c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#     notice, this list of conditions and the following disclaimer. 
106c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# 2.  Redistributions in binary form must reproduce the above copyright
116c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#     notice, this list of conditions and the following disclaimer in the
126c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#     documentation and/or other materials provided with the distribution. 
136c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# 3.  Neither the name of Apple Inc. ("Apple") nor the names of
146c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#     its contributors may be used to endorse or promote products derived
156c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#     from this software without specific prior written permission. 
166c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#
176c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
186c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
196c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
206c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
216c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
226c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
236c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
246c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
286c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse strict;
296c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
306c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse File::Basename;
316c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse File::Spec;
326c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse File::Temp qw(tempdir);
336c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse FindBin;
34545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochuse Getopt::Long;
356c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
366c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse lib $FindBin::Bin;
376c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse webkitdirs;
386c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenuse VCSUtils;
396c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
406c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenmy $macPythonURL = "http://www.python.org/ftp/python/2.6.5/python-2.6.5-macosx10.3-2010-03-24.dmg";
416c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenmy $macPythonMD5 = "84489bba813fdbb6041b69d4310a86da";
426c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenmy $macPythonInstallerName = "Python.mpkg";
436c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
446c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen# We could use a consistent download location, like the source or build directory.
45e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarkemy $tempDirectory = File::Temp::tempdir("WebKitPythonXXXX", TMPDIR => 1, CLEANUP => 1);
466c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenmy $downloadDirectory = $tempDirectory;
476c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenmy $mountPoint = File::Spec->join($tempDirectory, "mount");
486c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
496c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub checkPythonVersion()
506c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
516c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    # Will exit 0 if Python is 2.5 or greater, non-zero otherwise.
526c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    `python -c "import sys;sys.exit(sys.version_info[:2] < (2,5))"`;
536c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return exitStatus($?) == 0;
546c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
556c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
566c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub downloadFileToPath($$)
576c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
586c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my ($remoteURL, $localPath) = @_;
596c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "Downloading $remoteURL to $localPath\n";
606c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $exitCode = system("curl", "-o", $localPath, $remoteURL);
616c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return exitStatus($exitCode) == 0;
626c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
636c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
646c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub checkMD5($$)
656c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
666c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my ($path, $expectedMD5) = @_;
676c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $md5Output = `md5 -q "$path"`;
686c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    chomp($md5Output);
696c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $isValid = $md5Output eq $expectedMD5;
706c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "'$md5Output' does not match expected: '$expectedMD5'\n" unless $isValid;
716c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return $isValid;
726c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
736c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
746c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub mountDMG($$)
756c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
766c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my ($dmgPath, $mountPoint) = @_;
776c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "Mounting $dmgPath at $mountPoint\n";
786c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return system("hdiutil", "attach", "-mountpoint", $mountPoint, "-nobrowse", $dmgPath) == 0;
796c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
806c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
816c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub unmountDMG($)
826c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
836c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my ($mountPoint) = @_;
846c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "Unmounting disk image from $mountPoint\n";
856c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $exitCode = system("hdiutil", "detach", $mountPoint);
866c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return exitStatus($exitCode) == 0;
876c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
886c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
896c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub runInstaller($)
906c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
916c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my ($installerPackage) = @_;
926c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "sudo will now ask for your password to run the Python installer.\n";
936c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "The installer will install Python in /Library/Frameworks/Python.framework\n";
946c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "and add symlinks from /usr/local/bin.\n";
956c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return system("sudo", "installer", "-verbose", "-pkg", $installerPackage, "-target", "/") == 0;
966c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
976c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
986c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub downloadAndMountMacPythonDMG($$)
996c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
1006c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my ($pythonURL, $pythonMD5) = @_;
1016c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $localFilename = basename($pythonURL);
1026c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $localPath = File::Spec->join($downloadDirectory, $localFilename);
1036c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    
1046c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    downloadFileToPath($pythonURL, $localPath) or die "Failed to download $pythonURL";
1056c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    checkMD5($localPath, $pythonMD5) or die "MD5 check failed on $localPath";
1066c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return mountDMG($localPath, $mountPoint);
1076c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
1086c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
1096c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub installMacPython()
1106c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
1116c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    downloadAndMountMacPythonDMG($macPythonURL, $macPythonMD5) or die "Failed to download and mount disk image.";
1126c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "Mounted python install image at: $mountPoint\n";
1136c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $installerPackage = File::Spec->join($mountPoint, $macPythonInstallerName);
1146c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    my $installSuccess = runInstaller($installerPackage);
1156c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    unmountDMG($mountPoint) or die "Failed to unmount disk image from $mountPoint";
1166c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return $installSuccess;
1176c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
1186c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
1196c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsensub main()
1206c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen{
121545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    my $checkOnly = 0;
122545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    my $showHelp = 0;
123545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    my $getOptionsResult = GetOptions(
124545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        'check-only!' => \$checkOnly,
125545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        'help|h' => \$showHelp,
126545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    );
127545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    if (!$getOptionsResult || $showHelp) {
128545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        print STDERR <<HELP;
129545e470e52f0ac6a3a072bf559c796b42c6066b6Ben MurdochUsage: $0 [options]
130545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch  --check-only        Check python version only.
131545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch  -h|--help           Show this help message.
132545e470e52f0ac6a3a072bf559c796b42c6066b6Ben MurdochHELP
133545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        return 1;
134545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    }
1356c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    # Congrats, your Python is fine.
1366c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    return 0 if checkPythonVersion();
137545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch
138545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    return 1 if $checkOnly;
139545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch
1406c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    if (!isTiger()) {
141545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        print "Your Python version is insufficient to run WebKit's Python code.  Please update.\n";
1426c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen        print "See http://trac.webkit.org/wiki/PythonGuidelines for more info.\n";
1436c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen        return 1;
1446c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    }
1456c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
1466c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    installMacPython() or die "Failed to install Python.";
1476c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
1486c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    checkPythonVersion() or die "Final version check failed, must have failed to update Python";
1496c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen    print "Successfully updated python.\n";
1506c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen}
1516c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
1526c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsenexit(main());
153