@task
def cleanup():
  assert env.host_string

  # Search for active env.
  active_env_rev = active_env()
  assert active_env_rev, 'No active env, this could be a problem; aborting.'
  active_env_date = hg_revision_timestamp(active_env_rev)
  assert active_env_date, 'Could not determine timestamp for active env revision %s; aborting.' % active_env_rev

  fprint('Current active environment is revision %s (%s)' %
         (active_env_rev, util.TimestampUTCToISO8601(active_env_date)))

  # Search for, and iterate over, all environments.
  installed_env_revs = run('ls -d ~/env.*')
  for r in installed_env_revs.split():
    if not r.strip():
      continue
    rev = get_file_suffix('env', r)
    if not rev:
      continue
    if rev == active_env_rev:
      continue
    ts = hg_revision_timestamp(rev)
    if not ts:
      continue
    if ts >= active_env_date:
      fprint('Env revision %s (%s) newer than active env revision %s (%s); skipping.' %
             (rev, util.TimestampUTCToISO8601(ts), active_env_rev, util.TimestampUTCToISO8601(active_env_date)))
      continue

    answer = fprompt('Delete unused environment revision %s (%s)?' % (rev, util.TimestampUTCToISO8601(ts)),
                     default='N', validate='[yYnN]')
    if answer == 'n' or answer == 'N':
      continue
    run('rm -r -f env.%s prod-requirements.txt.%s python-package.%s' % (rev, rev, rev))
    fprint('Deleted environment revision %s (%s)' % (rev, util.TimestampUTCToISO8601(ts)))

  # Search for active code.
  active_code_rev = active_code()
  assert active_code_rev, 'No active code, this could be a problem; aborting.'
  active_code_date = hg_revision_timestamp(active_code_rev)
  assert active_code_date, 'Could not determine timestamp for active code revision %s; aborting.' % active_code_rev

  fprint('Current active code is revision %s (%s)' %
         (active_code_rev, util.TimestampUTCToISO8601(active_code_date)))

  # Search for, and iterate over, all code.
  installed_code_revs = run('ls -d ~/viewfinder.*')
  for r in installed_code_revs.split():
    if not r.strip():
      continue
    rev = get_file_suffix('viewfinder', r)
    if not rev:
      continue
    if rev == active_code_rev:
      continue
    ts = hg_revision_timestamp(rev)
    if not ts:
      continue
    if ts >= active_code_date:
      fprint('Code revision %s (%s) newer than active code revision %s (%s); skipping.' %
             (rev, util.TimestampUTCToISO8601(ts), active_code_rev, util.TimestampUTCToISO8601(active_code_date)))
      continue

    answer = fprompt('Delete unused code revision %s (%s)?' % (rev, util.TimestampUTCToISO8601(ts)),
                     default='N', validate='[yYnN]')
    if answer == 'n' or answer == 'N':
      continue
    run('rm -r -f viewfinder.%s' % rev)
    fprint('Deleted code revision %s (%s)' % (rev, util.TimestampUTCToISO8601(ts)))
Cleanup old env and code.
dataset/ETHPy150Open viewfinderco/viewfinder/fabfile.py/cleanup