prune-old-builds 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/perl -w
  2. # Copyright (c) 2019, The Tor Project, Inc.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. #
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. #
  16. # * Neither the names of the copyright owners nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. # 'prune-old-builds' is a script to prune old builds.
  32. #
  33. #
  34. # Usage:
  35. # $ ./prune-old-builds [options] <directory>
  36. #
  37. #
  38. # Available options:
  39. #
  40. # --dry-run
  41. # Don't delete anything, but say what would be deleted.
  42. #
  43. # --prefix <prefix>
  44. # Prefix of the directories to be removed. Default is 'tbb-nightly.'.
  45. #
  46. # --separator <c>
  47. # Separator character to separate the year, month, day in the directory
  48. # names. Default is '.'.
  49. #
  50. # --days <n>
  51. # Number of days that we should keep. Default is 6.
  52. #
  53. # --weeks <n>
  54. # Number of monday builds that we should keep. Default is 3.
  55. #
  56. # --months <n>
  57. # Number of 1st day of the month builds that we should keep.
  58. # Default is 3.
  59. use strict;
  60. use Getopt::Long;
  61. use DateTime;
  62. use DateTime::Duration;
  63. use File::Path qw(remove_tree);
  64. my %options = (
  65. days => 6,
  66. weeks => 3,
  67. months => 3,
  68. prefix => 'tbb-nightly.',
  69. separator => '.',
  70. );
  71. sub keep_builds {
  72. my %res;
  73. my $day = DateTime::Duration->new(days => 1);
  74. my $week = DateTime::Duration->new(weeks => 1);
  75. my $month = DateTime::Duration->new(months => 1);
  76. my $n = $options{days};
  77. my $dt = DateTime->now;
  78. while ($n) {
  79. $res{ $options{prefix} . $dt->ymd($options{separator}) } = 1;
  80. $dt = $dt - $day;
  81. $n--;
  82. }
  83. my $w = $options{weeks};
  84. while ($dt->day_of_week != 1) {
  85. $dt = $dt - $day;
  86. }
  87. while ($w) {
  88. $res{ $options{prefix} . $dt->ymd($options{separator}) } = 1;
  89. $dt = $dt - $week;
  90. $w--;
  91. }
  92. my $m = $options{months};
  93. $dt = DateTime->now;
  94. while ($dt->day != 1) {
  95. $dt = $dt - $day;
  96. }
  97. while ($m) {
  98. $res{ $options{prefix} . $dt->ymd($options{separator}) } = 1;
  99. $dt = $dt - $month;
  100. $m--;
  101. }
  102. return \%res;
  103. }
  104. sub clean_directory {
  105. my ($directory) = @_;
  106. my $k = keep_builds;
  107. chdir $directory || die "Error entering $directory";
  108. foreach my $file (glob "$options{prefix}*") {
  109. next unless $file =~ m/^$options{prefix}\d{4}$options{separator}\d{2}$options{separator}\d{2}$/;
  110. next if $k->{$file};
  111. if ($options{'dry-run'}) {
  112. print "Would remove $file\n";
  113. } else {
  114. remove_tree($file);
  115. }
  116. }
  117. }
  118. my @opts = qw(days=i weeks=i months=i prefix=s dry-run!);
  119. Getopt::Long::GetOptions(\%options, @opts);
  120. die "Missing argument: directory to clean" unless @ARGV;
  121. foreach my $dir (@ARGV) {
  122. clean_directory($dir);
  123. }