[CherryPick] Input Method upversion
[framework/web/webkit-efl.git] / Tools / Scripts / update-webkit-dependency
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2005, 2006, 2007 Apple Computer, Inc.  All rights reserved.
4 # Copyright (C) 2011 Carl Lobo.  All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1.  Redistributions of source code must retain the above copyright
11 #     notice, this list of conditions and the following disclaimer. 
12 # 2.  Redistributions in binary form must reproduce the above copyright
13 #     notice, this list of conditions and the following disclaimer in the
14 #     documentation and/or other materials provided with the distribution. 
15 # 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 #     its contributors may be used to endorse or promote products derived
17 #     from this software without specific prior written permission. 
18 #
19 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 # Updates a development environment to the new WebKitAuxiliaryLibrary
31
32 use strict;
33 use warnings;
34
35 use File::Find;
36 use File::Spec;
37 use File::Temp ();
38 use FindBin;
39 use HTTP::Date qw(str2time);
40 use POSIX;
41 use lib $FindBin::Bin;
42 use webkitdirs;
43
44 if ($#ARGV != 1) {
45     die <<EOF;
46 Usage:
47         update-webkit-dependancy <URL with the dependancy zip file> <*prefix dir inside zip without filename>
48
49         * If filename is requirements.zip and the contents of the zipfile are "requirements/x" then prefix = "."
50         * If filename is xyz.zip and the contents of the zipfile are xyz/abc/x" then prefix = "abc"
51         * x is lib or include or bin.
52 EOF
53 }
54
55 sub lastModifiedToUnixTime($);
56 sub getLibraryName($);
57
58 # Time in seconds that the new zip file must be newer than the old for us to
59 # consider them to be different. If the difference in modification time is less
60 # than this threshold, we assume that the files are the same. We need this
61 # because the zip file is served from a set of mirrors with slightly different
62 # Last-Modified times.
63 my $newnessThreshold = 30;
64
65 my $libsURL = shift;
66 my $prefixInZip = shift;
67 my $sourceDir = sourceDir();
68 my $file = getLibraryName($libsURL);
69 my $zipFile = "$file.zip"; 
70 my $webkitLibrariesDir = toUnixPath($ENV{'WEBKITLIBRARIESDIR'}) || "$sourceDir/WebKitLibraries/win";
71 my $tmpRelativeDir = File::Temp::tempdir("webkitlibsXXXXXXX", TMPDIR => 1, CLEANUP => 1);
72 my $tmpAbsDir = File::Spec->rel2abs($tmpRelativeDir);
73
74 print "Checking Last-Modified date of $zipFile...\n";
75
76 my $result = system "curl -s -I -k --sslv3 $libsURL | grep Last-Modified > \"$tmpAbsDir/$file.headers\"";
77
78 if (WEXITSTATUS($result)) {
79
80     #Note: Neither GitHub nor DropBox emit the Last-Modified HTTP header, so fall back to a file
81         #containing the necessary information if we do not receive the information in our initial query.
82     my $headerURL = $libsURL;
83     $headerURL =~ s/\.zip$/\.headers/;
84
85     $result = system "curl -k --sslv3 -o \"$tmpAbsDir/$file.headers\" $headerURL";
86
87     if (WEXITSTATUS($result)) {
88         print STDERR "Couldn't check Last-Modified date of new $zipFile.\n";
89         print STDERR "Please ensure that $libsURL is reachable.\n";
90
91         if (! -f "$webkitLibrariesDir/$file.headers") {
92             print STDERR "Unable to check Last-Modified date and no version of $file to fall back to.\n";
93             exit 1;
94         }
95
96         print STDERR "Falling back to existing version of $file.\n";
97         exit 0;
98     }
99 }
100
101 if (open NEW, "$tmpAbsDir/$file.headers") {
102     my $new = lastModifiedToUnixTime(<NEW>);
103     close NEW;
104
105     if (defined $new && open OLD, "$webkitLibrariesDir/$file.headers") {
106         my $old = lastModifiedToUnixTime(<OLD>);
107         close OLD;
108         if (defined $old && abs($new - $old) < $newnessThreshold) {
109             print "Current $file is up to date\n";
110             exit 0;
111         }
112     }
113 }
114
115 print "Downloading $zipFile...\n\n";
116 $result = system "curl -k --sslv3 -o \"$tmpAbsDir/$zipFile\" $libsURL";
117 die "Couldn't download $zipFile!" if $result;
118
119 $result = system "unzip", "-q", "-d", $tmpAbsDir, "$tmpAbsDir/$zipFile";
120 die "Couldn't unzip $zipFile." if $result;
121
122 print "\nInstalling $file...\n";
123
124 sub wanted
125 {
126     my $relativeName = File::Spec->abs2rel($File::Find::name, "$tmpAbsDir/$file/$prefixInZip");
127     my $destination = "$webkitLibrariesDir/$relativeName";
128
129     if (-d $_) {
130         mkdir $destination;
131         return;
132     }
133
134     system "cp", $_, $destination;
135 }
136
137 File::Find::find(\&wanted, "$tmpAbsDir/$file");
138
139 $result = system "mv", "$tmpAbsDir/$file.headers", $webkitLibrariesDir;
140 print STDERR "Couldn't move $file.headers to $webkitLibrariesDir" . ".\n" if $result;
141
142 print "The $file has been sucessfully installed in\n $webkitLibrariesDir\n";
143 exit;
144
145 sub toUnixPath
146 {
147     my $path = shift;
148     return unless $path;
149     chomp($path = `cygpath -u '$path'`);
150     return $path;
151 }
152
153 sub lastModifiedToUnixTime($)
154 {
155     my ($str) = @_;
156
157     $str =~ /^Last-Modified: (.*)$/ or return;
158     return str2time($1);
159 }
160
161 sub getLibraryName($)
162 {
163     my $url = shift;
164     $url =~ m#/([^/]+)\.zip$#;
165     return $1;
166 }
167