aboutsummaryrefslogtreecommitdiff
path: root/auto_delete_nightly_test_data.py
blob: f990f12502a083d4b0ee582bb1ffc29182e633b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python

"""A crontab script to delete night test data."""
__author__ = 'shenhan@google.com (Han Shen)'

import datetime
import optparse
import os
import re
import sys

from utils import command_executer
from utils import constants
from utils import misc

DIR_BY_WEEKDAY = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')


def CleanNumberedDir(s, dry_run=False):
  """Deleted directories under each dated_dir."""
  chromeos_dirs = [os.path.join(s, x) for x in os.listdir(s)
                   if misc.IsChromeOsTree(os.path.join(s, x))]
  ce = command_executer.GetCommandExecuter()
  all_succeeded = True
  for cd in chromeos_dirs:
    if misc.DeleteChromeOsTree(cd, dry_run=dry_run):
      print 'Successfully removed chromeos tree "{0}".'.format(cd)
    else:
      all_succeeded = False
      print 'Failed to remove chromeos tree "{0}", please check.'.format(cd)

  ## Now delete the numbered dir Before forcibly removing the directory, just
  ## check 's' to make sure it is sane.
  if not re.search('^' + constants.CROSTC_WORKSPACE + '/(' +
                   '|'.join(DIR_BY_WEEKDAY) + ')', s):
    print 'Trying to delete an invalid dir, please check.'
    return False

  cmd = 'rm -fr {0}'.format(s)
  if dry_run:
    print cmd
  else:
    if ce.RunCommand(cmd, return_output=False, print_to_console=True,
                     terminated_timeout=480) == 0:
      print 'Successfully removed "{0}".'.format(s)
    else:
      all_succeeded = False
      print 'Failed to remove "{0}", please check.'.format(s)
  return all_succeeded


def CleanDatedDir(dated_dir, dry_run=False):
  # List subdirs under dir
  subdirs = [os.path.join(dated_dir, x) for x in os.listdir(dated_dir)
             if os.path.isdir(os.path.join(dated_dir, x))]
  all_succeeded = True
  for s in subdirs:
    if not CleanNumberedDir(s, dry_run):
      all_succeeded = False
  return all_succeeded


def ProcessArguments(argv):
  """Process arguments."""
  parser = optparse.OptionParser(
      description='Automatically delete nightly test data directories.',
      usage='auto_delete_nightly_test_data.py options')
  parser.add_option('-d', '--dry_run', dest='dry_run',
                    default=False, action='store_true',
                    help='Only print command line, do not execute anything.')
  parser.add_option('--days_to_perserve', dest='days_to_preserve', default=3,
                    help=('Specify the number of days, test data generated '
                          'on these days will *NOT* be deleted. '
                          'Defaults to 3.'))
  options, _ = parser.parse_args(argv)
  return options


def Main(argv):
  """Delete nightly test data directories."""
  options = ProcessArguments(argv)
  # Function 'isoweekday' returns 1(Monday) - 7 (Sunday).
  d = datetime.datetime.today().isoweekday()
  # We go back 1 week, delete from that day till we are
  # options.days_to_preserve away from today.
  s = d - 7
  e = d - options.days_to_preserve
  rv = 0
  for i in range(s + 1, e):
    if i <= 0:
      ## Wrap around if index is negative.  6 is from i + 7 - 1, because
      ## DIR_BY_WEEKDAY starts from 0, while isoweekday is from 1-7.
      dated_dir = DIR_BY_WEEKDAY[i+6]
    else:
      dated_dir = DIR_BY_WEEKDAY[i-1]
    rv += 0 if CleanDatedDir(os.path.join(
        constants.CROSTC_WORKSPACE, dated_dir), options.dry_run) else 1
  return rv


if __name__ == '__main__':
  retval = Main(sys.argv)
  sys.exit(retval)