forked from szTheory/exifcleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_exiftool.pl
executable file
·382 lines (295 loc) · 9.58 KB
/
update_exiftool.pl
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env perl
# Download the latest version of ExifTool for Unix and Windows
# and verify their checksums.
#
# The Unix version is taken from the source archive, removes
# extra help files to reduce filesize, and squashes it down into
# a single Perl file.
#
# The Windows version is a prepacked EXE that is simply extracted
# from the zip archive.
package UpdateExifTool 1.0;
use strict; #complain when a variable is used before declaration
use warnings; #output run-time warnings to catch bugs early
use diagnostics; #verbose warnings, consumes memory so disable in production
use autodie; #functions throw exception on failure instead of returning false
use utf8; #enable UTF-8 in source code
use open qw(:std :utf8); #set default encoding of filehandles to UTF-8
use constant EXIFTOOL_BASE_URL => 'https://exiftool.org/';
use constant CHECKSUMS_URL => EXIFTOOL_BASE_URL . 'checksums.txt';
use constant DOWNLOADS_WORKING_DIR => 'exiftool_downloads';
use constant RESOURCES_DIR => '.resources';
use constant BIN_DIR_UNIX => RESOURCES_DIR . '/nix/bin';
use constant BIN_DIR_WINDOWS => RESOURCES_DIR . '/win/bin';
use constant COMMAND_PRINT_SIGNAL => '------> ';
use constant COMMAND_SIGNAL_COLOR => 'bright_green';
use constant COMMAND_SUCCESS_COLOR => 'bright_green';
use constant COMMAND_ERROR_COLOR => 'bright_red';
use constant COMMAND_OUTPUT_COLOR => 'bold blue';
use constant BANNER_OUTPUT_COLOR => 'bold cyan';
use File::Path qw(make_path remove_tree);
use Term::ANSIColor;
sub print_output {
my $output = shift;
print color(COMMAND_OUTPUT_COLOR);
print "$output";
print color('reset');
return;
}
sub print_success {
my $text = shift;
print color(COMMAND_SUCCESS_COLOR);
print "$text\n";
print color('reset');
return;
}
sub print_error {
my $text = shift;
print color(COMMAND_ERROR_COLOR);
print "$text\n";
print color('reset');
return;
}
sub header {
my $text = shift;
my $banner = q{-} x length($text);
print "\n";
print color(BANNER_OUTPUT_COLOR);
print "$banner\n";
print "$text\n";
print "$banner\n";
print color('reset');
return;
}
sub print_command_signal {
print color(COMMAND_SIGNAL_COLOR);
print COMMAND_PRINT_SIGNAL;
print color('reset');
return;
}
sub print_command {
my @command = @_;
print_command_signal();
print_output( join( ' ', @command ) . "\n" );
return;
}
sub run_command {
my @command = @_;
print_command(@command);
system(@command) == 0 or die "system @command failed: $?";
return;
}
sub make_dir {
my $dir_path = shift;
print_command_signal();
print color(COMMAND_OUTPUT_COLOR);
make_path( $dir_path, { verbose => 1 } );
print color('reset');
return;
}
sub remove_dir {
my $dir_path = shift;
print_command( 'remove_tree(' . $dir_path . ')' );
remove_tree($dir_path);
return;
}
# Example checksum file output:
#
# SHA1(Image-ExifTool-12.01.tar.gz)= 140f014e7686ed80528b919d64c4de0a869e59aa
# SHA1(exiftool-12.01.zip)= a28c3f943165d1eec3ff69bb665390e340686ec6
# SHA1(ExifTool-12.01.dmg)= 327fd67f60fd7f62742d4ddb2f9999da13dc785f
# MD5 (Image-ExifTool-12.01.tar.gz) = 6980a6d435f83c0af060148a354acf24
# MD5 (exiftool-12.01.zip) = e11260548ebff70a3ce27d48e46dfe94
# MD5 (ExifTool-12.01.dmg) = 7a41e56901564f9bd4eb3f907846c118
sub get_checksum_file_text {
my $command = 'curl ' . CHECKSUMS_URL;
print_command($command);
return qx($command);
}
sub get_code_zip_info {
my $checksum_file_text = shift;
my ( $filename, $sha1 ) =
$checksum_file_text =~ /SHA1\((Image-ExifTool-[\w.]+tar[.]gz)\)= (\w+)/m;
return ( $filename, $sha1 );
}
sub get_windows_exe_info {
my $checksum_file_text = shift;
my ( $filename, $sha1 ) =
$checksum_file_text =~ /SHA1\((exiftool-[\w.]+zip)\)= (\w+)/m;
return ( $filename, $sha1 );
}
sub download_file {
my $filename = shift;
my $url = EXIFTOOL_BASE_URL . $filename;
my @command = (
'wget', '--no-clobber', '--directory-prefix', DOWNLOADS_WORKING_DIR, $url
);
run_command(@command);
return;
}
sub verify_checksum {
my ( $filename, $sha1 ) = @_;
my $command = 'shasum ' . DOWNLOADS_WORKING_DIR . "/$filename";
print_command($command);
my $output = qx($command);
my ($calculated_sha1) = split( ' ', $output );
print $calculated_sha1;
my $is_match = $sha1 eq $calculated_sha1;
if ( $sha1 eq $calculated_sha1 ) {
print_success(" ... Match!\n");
}
else {
die "\n!!! Did NOT match SHA1 from ExifTool website: $sha1 !!!\n";
}
return;
}
sub extract_source_code {
my $gzip_filename = shift;
my @command = (
'tar', '-xvf', DOWNLOADS_WORKING_DIR . "/$gzip_filename",
'-C', DOWNLOADS_WORKING_DIR
);
run_command(@command);
return;
}
sub extract_windows_exe {
my $zip_filename = shift;
my @command = (
'unzip', '-d', DOWNLOADS_WORKING_DIR, '-o',
DOWNLOADS_WORKING_DIR . "/$zip_filename"
);
run_command(@command);
return;
}
sub remove_old_binaries {
# remove old Unix lib dir
remove_dir( BIN_DIR_UNIX . '/lib' );
# remove old Unix `exiftool` bin
my $remove_path_bin_unix = BIN_DIR_UNIX . '/exiftool';
if ( -e $remove_path_bin_unix ) {
my @command = ( 'rm', $remove_path_bin_unix );
run_command(@command);
}
else {
print_output("No pre-existing Unix binary to remove\n");
}
# remove old Windows `exiftool.exe`
my $remove_path_bin_win = BIN_DIR_WINDOWS . '/exiftool.exe';
if ( -e $remove_path_bin_win ) {
my @command = ( 'rm', $remove_path_bin_win );
run_command(@command);
}
else {
print_output("No pre-existing Windows binary to remove\n");
}
return;
}
# The Unix version of ExifTool only needs `exiftool` and the `lib` dir.
# In order to keep package size down we only copy these over to the
# ExifCleaner bin dir.
sub copy_unix_binary {
my $code_archive_filename = shift;
my ($code_dir_name) = $code_archive_filename =~ /^(.+)[.]tar[.]gz$/;
my $from_dir = DOWNLOADS_WORKING_DIR . "/$code_dir_name";
# move lib dir
my @command = ( 'cp', '-R', "$from_dir/lib", BIN_DIR_UNIX );
run_command(@command);
# move `exiftool` base Perl file
@command = ( 'cp', "$from_dir/exiftool", BIN_DIR_UNIX );
run_command(@command);
return;
}
sub verify_successful_install {
my $command = BIN_DIR_UNIX . '/exiftool -ver';
my $version = qx($command);
if ($version) {
print "\n";
print_success("Success! Updated to ExifTool $version\n");
}
else {
print_error(
"Error while attempting to verify ExifTool install with $command\n");
}
return;
}
# The Windows ExifTool binary is just an .exe file. We have to
# rename it from `exiftool(-k).exe` to `exiftool.exe` and move
# it to the ExifCleaner Windows bin dir.
sub copy_windows_binary {
my $from_path = DOWNLOADS_WORKING_DIR . '/exiftool(-k).exe';
my $to_path = BIN_DIR_WINDOWS . '/exiftool.exe';
my @command = ( 'cp', $from_path, $to_path );
run_command(@command);
return;
}
sub is_exiftool_already_downloaded {
my ( $code_filename, $windows_version_filename ) = @_;
my $code_path = DOWNLOADS_WORKING_DIR . "/$code_filename";
my $windows_path = DOWNLOADS_WORKING_DIR . "/$windows_version_filename";
my $download_folder_exists = -d DOWNLOADS_WORKING_DIR;
my $code_downloaded = -e $code_path;
my $windows_version_downloaded = -e $windows_path;
return
$download_folder_exists
&& $code_downloaded
&& $windows_version_downloaded;
}
sub run {
my $cache_downloads_working_dir = shift;
header('Fetching ExifTool SHA1 checksums from website');
my $checksum_file_text = get_checksum_file_text();
my ( $code_filename, $code_sha1 ) = get_code_zip_info($checksum_file_text);
my ( $windows_version_filename, $windows_sha1 ) =
get_windows_exe_info($checksum_file_text);
my $exiftool_already_downloaded =
is_exiftool_already_downloaded( $code_filename, $windows_version_filename );
print_output("$code_filename - $code_sha1\n");
print_output("$windows_version_filename - $windows_sha1\n");
header('Recreate downloads working directory');
if ( $cache_downloads_working_dir && $exiftool_already_downloaded ) {
print_command(
"Keeping existing downloads working directory since download caching is enabled"
);
}
else {
remove_dir(DOWNLOADS_WORKING_DIR);
make_dir(DOWNLOADS_WORKING_DIR);
}
header('Downloading files');
if ( $cache_downloads_working_dir && $exiftool_already_downloaded ) {
print_command( "Skipping download since the downloads working directory '"
. DOWNLOADS_WORKING_DIR
. "' already exists and download caching is enabled" );
}
else {
download_file($code_filename);
download_file($windows_version_filename);
}
header('Verifying SHA1 checksums');
verify_checksum( $code_filename, $code_sha1 );
verify_checksum( $windows_version_filename, $windows_sha1 );
header('Extracting archives');
extract_source_code($code_filename);
extract_windows_exe($windows_version_filename);
header('Removing old binaries');
remove_old_binaries();
header('Moving fresh binaries');
copy_unix_binary($code_filename);
copy_windows_binary();
header('Clean up downloads working directory');
if ($cache_downloads_working_dir) {
print_command(
"Keeping downloads working directory since caching is enabled.");
}
else {
remove_dir(DOWNLOADS_WORKING_DIR);
}
return;
}
# Pass the command line argument --cache-downloads-working-dir
# to cache the downloads working directory to avoid repeated
# downloads from the exiftool server. Useful for CI
my $cache_downloads_working_dir = $ARGV[0] eq "--cache-downloads-working-dir";
run($cache_downloads_working_dir);
verify_successful_install();
1;