modified: myjupyterlab.sh
[GalaxyCodeBases.git] / etc / agfw / autoproxy-gfwlist / addChecksum.pl
blob57c92a24e2bf1e755704e9c7af64786cb47c80f5
1 #!/usr/bin/perl
3 #############################################################################
4 # This is a reference script to add checksums to downloadable #
5 # subscriptions. The checksum will be validated by AutoProxy on download #
6 # and checksum mismatches (broken downloads) will be rejected. #
7 # #
8 # To add a checksum to a subscription file, run the script like this: #
9 # #
10 # perl addChecksum.pl subscription.txt #
11 # #
12 # Note: your subscription file should be saved in UTF-8 encoding, otherwise #
13 # the generated checksum might be incorrect. #
14 # #
15 #############################################################################
17 use strict;
18 use warnings;
19 use Digest::MD5 qw(md5_base64);
20 use File::stat;
21 use POSIX qw(locale_h);
22 use POSIX qw(strftime);
24 die "Usage: $^X $0 subscription.txt\n" unless @ARGV;
26 my $file = $ARGV[0];
27 my $data = readFile($file);
29 # Remove already existing checksum
30 $data =~ s/^.*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n//gmi;
32 # Update timestamp
33 setlocale(LC_TIME, "C");
34 my $timestamp = strftime("%a, %d %b %Y %H:%M:%S %z", localtime(stat($file)->mtime));
35 $data =~ s/^!\s*Last Modified:.*$/! Last Modified: $timestamp/mi;
37 # Calculate new checksum: remove all CR symbols and empty
38 # lines and get an MD5 checksum of the result (base64-encoded,
39 # without the trailing = characters).
40 my $checksumData = $data;
41 $checksumData =~ s/\r//g;
42 $checksumData =~ s/\n+/\n/g;
44 # Calculate new checksum
45 my $checksum = md5_base64($checksumData);
47 # Insert checksum into the file
48 $data =~ s/(\r?\n)/$1! Checksum: $checksum$1/;
50 writeFile($file, $data);
52 sub readFile
54 my $file = shift;
56 open(local *FILE, "<", $file) || die "Could not read file '$file'";
57 binmode(FILE);
58 local $/;
59 my $result = <FILE>;
60 close(FILE);
62 return $result;
65 sub writeFile
67 my ($file, $contents) = @_;
69 open(local *FILE, ">", $file) || die "Could not write file '$file'";
70 binmode(FILE);
71 print FILE $contents;
72 close(FILE);