[tor-commits] [tor-messenger-build/master] Add a script to clean obsolete build files and docker images

boklm at torproject.org boklm at torproject.org
Wed Nov 12 19:06:17 UTC 2014


commit 87fc563253e0d09fb23c4942158b5fb6ec832490
Author: Nicolas Vigier <boklm at torproject.org>
Date:   Wed Nov 12 19:59:33 2014 +0100

    Add a script to clean obsolete build files and docker images
    
    The script can be run with 'make clean-old'.
    
    It needs to compute all current filenames and docker image names (which
    themself depend on sha256sum of a lot of things), so it takes some time
    to run.
---
 Makefile                     |    3 ++
 projects/docker-image/config |    3 +-
 rbm.conf                     |    4 ++
 tools/clean-old              |  106 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 115 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 9ef012f..50dc2f0 100644
--- a/Makefile
+++ b/Makefile
@@ -14,3 +14,6 @@ submodule-update:
 fetch:
 	$(rbm) fetch
 
+clean-old: submodule-update
+	./tools/clean-old
+
diff --git a/projects/docker-image/config b/projects/docker-image/config
index 82e45c8..b84de53 100644
--- a/projects/docker-image/config
+++ b/projects/docker-image/config
@@ -1,6 +1,7 @@
 filename: '[% sha256(c("pre")).substr(0, 12) %]'
 remote_docker: 1
-docker_save_image: 'tor-messenger_[% GET ENV.RBM_BUILDNAME ? ENV.RBM_BUILDNAME : ENV.USER ? ENV.USER : c("uid") %]:[% c("filename") %]'
+docker_image_prefix: 'tor-messenger_[% GET ENV.RBM_BUILDNAME ? ENV.RBM_BUILDNAME : ENV.USER ? ENV.USER : c("uid") %]'
+docker_save_image: '[% c("docker_image_prefix") %]:[% c("filename") %]'
 pkg_type: build
 
 distributions:
diff --git a/rbm.conf b/rbm.conf
index 483fe58..ce4c16e 100644
--- a/rbm.conf
+++ b/rbm.conf
@@ -31,6 +31,10 @@ var:
        %]
     build:
     [% c("build", { filename => 'f', output_dir => '/out' }) %]
+  input_files_list: |
+    [% FOREACH file IN c("input_files_by_name").keys.sort -%]
+    [% c("input_files_by_name/" _ file) %]
+    [% END -%]
 
 targets:
   notarget: linux-x86_64
diff --git a/tools/clean-old b/tools/clean-old
new file mode 100755
index 0000000..e3be7fa
--- /dev/null
+++ b/tools/clean-old
@@ -0,0 +1,106 @@
+#!/usr/bin/perl -w
+use strict;
+use Cwd qw(getcwd);
+use IO::CaptureOutput qw(capture_exec);
+use FindBin;
+
+my $rbm = "$FindBin::Bin/../rbm/rbm";
+my $projects_dir = "$FindBin::Bin/../projects";
+my @targets = qw(linux-x86_64 linux-i686 windows-i686 osx-x86_64);
+my @sig_ext = qw(.sig .gpg .asc);
+my $docker_image_prefix = docker_image_prefix();
+
+sub clean_project_build_files {
+    my ($project) = @_;
+    my $old_cwd = getcwd;
+    chdir "$FindBin::Bin/../out/$project";
+    my @files = grep { -f $_ } glob '*';
+    chdir $old_cwd;
+    my %keep_files;
+    foreach my $target (@targets) {
+        my ($out, $err, $success) = capture_exec($rbm,
+            'showconf', $project, 'filename', "--target=$target");
+        return unless $success;
+        chomp $out;
+        $keep_files{$out} = 1;
+        ($out, $err, $success) = capture_exec($rbm,
+            'showconf', $project, 'var/input_files_list', "--target=$target");
+        next unless $success;
+        my @input_files = split("\n", $out);
+        foreach my $input_file (@input_files) {
+            chomp $input_file;
+            next unless $input_file;
+            $keep_files{$input_file} = 1;
+            foreach my $ext (qw(sig gpg asc)) {
+                $keep_files{"$input_file.$ext"} = 1;
+            }
+        }
+    }
+    foreach my $file (@files) {
+        next if $keep_files{$file};
+        print "Removing $file\n";
+        unlink "$FindBin::Bin/../out/$project/$file";
+    }
+}
+
+sub clean_build_files {
+    chdir $projects_dir;
+    foreach my $project (glob '*') {
+        next if $project eq 'docker-image';
+        next unless -f "$project/config";
+        print "Cleaning project: $project\n";
+        clean_project_build_files($project);
+    }
+}
+
+sub docker_image_prefix {
+    my ($out, $err, $success) = capture_exec($rbm, 'showconf', 'docker-image',
+                                              'docker_image_prefix');
+    die "Cannot get docker image prefix" unless $success;
+    chomp $out;
+    return $out;
+}
+
+sub remove_docker_image {
+    my ($image_name) = @_;
+    my $image = "$docker_image_prefix:$image_name";
+    print "Removing docker image $image\n";
+    my ($out, $err, $success) = capture_exec('docker', 'rmi', '-f', $image);
+    if (!$success) {
+        print STDERR "Error removing docker image $image\n";
+        return;
+    }
+    unlink "$FindBin::Bin/../out/docker-image/$image_name";
+}
+
+sub clean_docker_images {
+    chdir "$FindBin::Bin/../out/docker-image" or return;
+    print "Cleaning docker images\n";
+    my @docker_files = grep { -f $_ } glob '*';
+    my %keep_docker_files;
+    chdir $projects_dir;
+    foreach my $project (glob '*') {
+        next if $project eq 'docker-image';
+        next unless -f "$project/config";
+        foreach my $target (@targets) {
+            my ($out, $err, $success) = capture_exec($rbm, 'showconf', $project,
+                'remote_docker', "--target=$target");
+            next unless $success;
+            chomp $out;
+            next unless $out;
+            ($out, $err, $success) = capture_exec($rbm, 'showconf', $project,
+                'docker_image', "--target=$target");
+            next unless $success;
+            chomp $out;
+            my (undef, $image_file) = split(':', $out);
+            $keep_docker_files{$image_file} = 1;
+        }
+    }
+    foreach my $docker_file (@docker_files) {
+        next if $keep_docker_files{$docker_file};
+        remove_docker_image($docker_file);
+    }
+}
+
+clean_build_files;
+clean_docker_images;



More information about the tor-commits mailing list