#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import argparse
import sys

sys.dont_write_bytecode = True

from common import testinfra

def main():
    parser = argparse.ArgumentParser(description='Test infrastructure: scan and update status of pull requests on github')
    parser.add_argument('-d', '--dry', action="store_true", default=False,
                        help='Don''t actually change anything on github')
    parser.add_argument('--except', dest='except_context', action='store_true',
                        help='Scan for anything except the specified context')
    parser.add_argument('context', action='store', nargs='?',
                        help='The test context to scan for tasks')
    opts = parser.parse_args()

    github = testinfra.GitHub()

    for entry in github.scan(not opts.dry, opts.context, opts.except_context):
        sys.stdout.write("{0}: {1}\n".format(int(entry.priority), entry.task.description()))

    for image, wait_time in github.scan_image_wait_times().items():
        # Images with wait_time <= 0 are already included in the list
        # of tasks above, so we don't need to list them here.
        if wait_time > 0:
            sys.stdout.write("{0} will be refreshed in {1:.1f} days.\n".format(image, wait_time))

if __name__ == '__main__':
    sys.exit(main())
