git.iko.soy/typeable/nixpkgs
on master help sign in
copy-tarballs.pl: Add --exclude flag
commit4f4ba0d3f44681085aee84977f333be2cedf7c80 authorEelco Dolstra <eelco.dolstra@logicblox.com> dateMon 9 May 2016, 14:55 (11 years ago) parent1717d11
1 file changed+47−16
maintainers/scripts/copy-tarballs.pl+47−16
maintainers/scripts/copy-tarballs.pl+47−16
@@ -5,7 +5,7 @@
55# content-addressed cache used by fetchurl as a fallback for when
66# upstream tarballs disappear or change. Usage:
77#
8−# 1) To upload a single file:
8+# 1) To upload one or more files:
99#
1010# $ copy-tarballs.pl --file /path/to/tarball.tar.gz
1111#
@@ -22,12 +22,38 @@ use JSON;
2222use Net::Amazon::S3;
2323use Nix::Store;
2424
25−isValidPath("/nix/store/foo"); # FIXME: forces Nix::Store initialisation
25+isValidPath("/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"); # FIXME: forces Nix::Store initialisation
26+
27+sub usage {
28+ die "Syntax: $0 [--dry-run] [--exclude REGEXP] [--expr EXPR | --file FILES...]\n";
29+}
30+
31+my $dryRun = 0;
32+my $expr;
33+my @fileNames;
34+my $exclude;
35+
36+while (@ARGV) {
37+ my $flag = shift @ARGV;
38+
39+ if ($flag eq "--expr") {
40+ $expr = shift @ARGV or die "--expr requires an argument";
41+ } elsif ($flag eq "--file") {
42+ @fileNames = @ARGV;
43+ last;
44+ } elsif ($flag eq "--dry-run") {
45+ $dryRun = 1;
46+ } elsif ($flag eq "--exclude") {
47+ $exclude = shift @ARGV or die "--exclude requires an argument";
48+ } else {
49+ usage();
50+ }
51+}
2652
2753
2854# S3 setup.
29−my $aws_access_key_id = $ENV{'AWS_ACCESS_KEY_ID'} or die;
30−my $aws_secret_access_key = $ENV{'AWS_SECRET_ACCESS_KEY'} or die;
55+my $aws_access_key_id = $ENV{'AWS_ACCESS_KEY_ID'} or die "AWS_ACCESS_KEY_ID not set\n";
56+my $aws_secret_access_key = $ENV{'AWS_SECRET_ACCESS_KEY'} or die "AWS_SECRET_ACCESS_KEY not set\n";
3157
3258my $s3 = Net::Amazon::S3->new(
3359 { aws_access_key_id => $aws_access_key_id,
@@ -37,12 +63,15 @@ my $s3 = Net::Amazon::S3->new(
3763
3864my $bucket = $s3->bucket("nixpkgs-tarballs") or die;
3965
40−my $cacheFile = "/tmp/copy-tarballs-cache";
66+my $doWrite = 0;
67+my $cacheFile = ($ENV{"HOME"} or die "\$HOME is not set") . "/.cache/nix/copy-tarballs";
4168my %cache;
4269$cache{$_} = 1 foreach read_file($cacheFile, err_mode => 'quiet', chomp => 1);
70+$doWrite = 1;
4371
4472END() {
45− write_file($cacheFile, map { "$_\n" } keys %cache);
73+ File::Path::mkpath(dirname($cacheFile), 0, 0755);
74+ write_file($cacheFile, map { "$_\n" } keys %cache) if $doWrite;
4675}
4776
4877sub alreadyMirrored {
@@ -87,11 +116,9 @@ sub uploadFile {
87116 $cache{$mainKey} = 1;
88117}
89118
90−my $op = shift @ARGV;
91−
92−if ($op eq "--file") {
119+if (scalar @fileNames) {
93120 my $res = 0;
94− foreach my $fn (@ARGV) {
121+ foreach my $fn (@fileNames) {
95122 eval {
96123 if (alreadyMirrored("sha512", hashFile("sha512", 0, $fn))) {
97124 print STDERR "$fn is already mirrored\n";
@@ -100,17 +127,16 @@ if ($op eq "--file") {
100127 }
101128 };
102129 if ($@) {
103− warn "$@\n";
130+ warn "$@";
104131 $res = 1;
105132 }
106133 }
107134 exit $res;
108135}
109136
110−elsif ($op eq "--expr") {
137+elsif (defined $expr) {
111138
112139 # Evaluate find-tarballs.nix.
113− my $expr = $ARGV[0] // die "$0: --expr requires a Nix expression\n";
114140 my $pid = open(JSON, "-|", "nix-instantiate", "--eval", "--json", "--strict",
115141 "<nixpkgs/maintainers/scripts/find-tarballs.nix>",
116142 "--arg", "expr", $expr);
@@ -126,7 +152,7 @@ elsif ($op eq "--expr") {
126152 # Check every fetchurl call discovered by find-tarballs.nix.
127153 my $mirrored = 0;
128154 my $have = 0;
129− foreach my $fetch (@{$fetches}) {
155+ foreach my $fetch (sort { $a->{url} cmp $b->{url} } @{$fetches}) {
130156 my $url = $fetch->{url};
131157 my $algo = $fetch->{type};
132158 my $hash = $fetch->{hash};
@@ -142,6 +168,8 @@ elsif ($op eq "--expr") {
142168 next;
143169 }
144170
171+ next if defined $exclude && $url =~ /$exclude/;
172+
145173 if (alreadyMirrored($algo, $hash)) {
146174 $have++;
147175 next;
@@ -151,7 +179,10 @@ elsif ($op eq "--expr") {
151179
152180 print STDERR "mirroring $url ($storePath)...\n";
153181
154− next if $ENV{DRY_RUN};
182+ if ($dryRun) {
183+ $mirrored++;
184+ next;
185+ }
155186
156187 # Substitute the output.
157188 if (!isValidPath($storePath)) {
@@ -184,5 +215,5 @@ elsif ($op eq "--expr") {
184215}
185216
186217else {
187− die "Syntax: $0 --file FILENAMES... | --expr EXPR\n";
218+ usage();
188219}