clean-old 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use IO::CaptureOutput qw(capture_exec);
  4. use FindBin;
  5. use lib "$FindBin::Bin/../rbm/lib";
  6. use RBM;
  7. use File::Slurp qw(read_dir write_file read_file);
  8. use Getopt::Long;
  9. my %options;
  10. my @options_list = qw(dry-run! list-used-files=s project=s target=s@);
  11. Getopt::Long::GetOptionsFromArray(\@ARGV, \%options, @options_list) || exit 1;
  12. sub clean_file {
  13. my ($file, $used_files) = @_;
  14. return if $used_files->{$file};
  15. if (-d $file) {
  16. my @l = read_dir($file);
  17. foreach my $subfile (@l) {
  18. clean_file("$file/$subfile", $used_files);
  19. }
  20. @l = read_dir($file);
  21. rmdir $file unless @l;
  22. } else {
  23. print "Removing file $file\n";
  24. unlink $file unless $options{'dry-run'};
  25. }
  26. }
  27. sub get_project_input_files {
  28. my ($project, @targets) = @_;
  29. print "Getting input files for $project ", join(' ', @targets), "\n";
  30. $RBM::config->{run}{target} = \@targets;
  31. my $res = RBM::project_config($project, 'input_files_paths',
  32. {error_if_undef => 1});
  33. return ref $res eq 'ARRAY' ? @$res : ();
  34. }
  35. sub get_input_files {
  36. my ($c) = @_;
  37. my @files;
  38. foreach my $p (@$c) {
  39. my $tmp = File::Temp->new();
  40. my @args = ("$FindBin::Bin/$FindBin::Script", '--list-used-files', $tmp,
  41. '--project', $p->{project});
  42. foreach my $target (@{$p->{target}}) {
  43. push @args, ('--target', $target);
  44. }
  45. RBM::exit_error 'Error getting used files' unless system(@args) == 0;
  46. push @files, map { chomp; $_ } read_file($tmp);
  47. }
  48. return @files;
  49. }
  50. RBM::load_config;
  51. RBM::load_system_config;
  52. RBM::load_local_config;
  53. RBM::set_default_env;
  54. if ($options{'list-used-files'}) {
  55. if (!$options{project} || !$options{target}) {
  56. RBM::exit_error 'Missing option project or target';
  57. }
  58. my @files = get_project_input_files($options{project}, @{$options{target}});
  59. write_file($options{'list-used-files'}, join("\n", @files));
  60. exit 0;
  61. }
  62. my $clean = RBM::project_config('clean', 'var/clean');
  63. if (!$clean) {
  64. print STDERR "Clean configuration is missing. ",
  65. "You should add it to rbm.local.conf.\n";
  66. exit 1;
  67. }
  68. my @files = get_input_files($clean->{HEAD}) if $clean->{HEAD};
  69. foreach my $branch (keys %$clean) {
  70. next if $branch eq 'HEAD';
  71. print "Checking out $branch branch\n";
  72. RBM::exit_error("Error checking out $branch branch")
  73. unless system('git', 'checkout', $branch) == 0;
  74. RBM::exit_error("Error running git submodule update --init")
  75. unless system('git', 'submodule', 'update', '--init') == 0;
  76. push @files, get_input_files($clean->{$branch});
  77. RBM::exit_error('Error checking out @{-1}')
  78. unless system('git', 'checkout', '@{-1}') == 0;
  79. RBM::exit_error("Error running git submodule update --init")
  80. unless system('git', 'submodule', 'update', '--init') == 0;
  81. }
  82. my %used_files = map { $_ => 1 } @files;
  83. my $outdir = $RBM::config->{basedir} . '/out';
  84. clean_file($outdir, \%used_files);