initial import
[vuplus_webkit] / 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 $libsURL | grep Last-Modified > \"$tmpAbsDir/$file.headers\"";
77
78 if (WEXITSTATUS($result)) {
79     print STDERR "Couldn't check Last-Modified date of new $zipFile.\n";
80     print STDERR "Please ensure that $libsURL is reachable.\n";
81
82     if (! -f "$webkitLibrariesDir/$file.headers") {
83         print STDERR "Unable to check Last-Modified date and no version of $file to fall back to.\n";
84         exit 1;
85     }
86
87     print STDERR "Falling back to existing version of $file.\n";
88     exit 0;
89 }
90
91 if (open NEW, "$tmpAbsDir/$file.headers") {
92     my $new = lastModifiedToUnixTime(<NEW>);
93     close NEW;
94
95     if (defined $new && open OLD, "$webkitLibrariesDir/$file.headers") {
96         my $old = lastModifiedToUnixTime(<OLD>);
97         close OLD;
98         if (defined $old && abs($new - $old) < $newnessThreshold) {
99             print "Current $file is up to date\n";
100             exit 0;
101         }
102     }
103 }
104
105 print "Downloading $zipFile...\n\n";
106 $result = system "curl -o \"$tmpAbsDir/$zipFile\" $libsURL";
107 die "Couldn't download $zipFile!" if $result;
108
109 $result = system "unzip", "-q", "-d", $tmpAbsDir, "$tmpAbsDir/$zipFile";
110 die "Couldn't unzip $zipFile." if $result;
111
112 print "\nInstalling $file...\n";
113
114 sub wanted
115 {
116     my $relativeName = File::Spec->abs2rel($File::Find::name, "$tmpAbsDir/$file/$prefixInZip");
117     my $destination = "$webkitLibrariesDir/$relativeName";
118
119     if (-d $_) {
120         mkdir $destination;
121         return;
122     }
123
124     system "cp", $_, $destination;
125 }
126
127 File::Find::find(\&wanted, "$tmpAbsDir/$file");
128
129 $result = system "mv", "$tmpAbsDir/$file.headers", $webkitLibrariesDir;
130 print STDERR "Couldn't move $file.headers to $webkitLibrariesDir" . ".\n" if $result;
131
132 print "The $file has been sucessfully installed in\n $webkitLibrariesDir\n";
133 exit;
134
135 sub toUnixPath
136 {
137     my $path = shift;
138     return unless $path;
139     chomp($path = `cygpath -u '$path'`);
140     return $path;
141 }
142
143 sub lastModifiedToUnixTime($)
144 {
145     my ($str) = @_;
146
147     $str =~ /^Last-Modified: (.*)$/ or return;
148     return str2time($1);
149 }
150
151 sub getLibraryName($)
152 {
153     my $url = shift;
154     $url =~ m#/([^/]+)\.zip$#;
155     return $1;
156 }
157