def main():
    """
    Datasets that are older than the specified cutoff and for which the tool_id
    contains the specified text will be marked as deleted in user's history and
    the user will be notified by email using the specified template file.
    """
    parser = OptionParser()
    parser.add_option("-d",
                      "--days",
                      dest="days",
                      action="store",
                      type="int",
                      help="number of days (60)",
                      default=60)
    parser.add_option("--tool_id",
                      default=None,
                      help="Text to match against tool_id"
                      "Default: match all")
    parser.add_option("--template",
                      default=None,
                      help="Mako Template file to use as email "
                      "Variables are 'cutoff' for the cutoff in days, "
                      "'email' for users email and "
                      "'datasets' which is a list of tuples "
                      "containing 'dataset' and 'history' names. "
                      "Default: admin_cleanup_deletion_template.txt")
    parser.add_option("-i",
                      "--info_only",
                      action="store_true",
                      dest="info_only",
                      help="info about the requested action",
                      default=False)
    parser.add_option("-e",
                      "--email_only",
                      action="store_true",
                      dest="email_only",
                      help="Send emails only, don't delete",
                      default=False)
    parser.add_option("--smtp",
                      default=None,
                      help="SMTP Server to use to send email. "
                      "Default: [read from galaxy ini file]")
    parser.add_option("--fromaddr",
                      default=None,
                      help="From address to use to send email. "
                      "Default: [read from galaxy ini file]")
    (options, args) = parser.parse_args()
    ini_file = args[0]

    config_parser = ConfigParser.ConfigParser({'here': os.getcwd()})
    config_parser.read(ini_file)
    config_dict = {}
    for key, value in config_parser.items("app:main"):
        config_dict[key] = value

    if options.smtp is not None:
        config_dict['smtp_server'] = options.smtp
    if config_dict.get('smtp_server') is None:
        parser.error("SMTP Server must be specified as an option (--smtp) "
                     "or in the config file (smtp_server)")

    if options.fromaddr is not None:
        config_dict['error_email_to'] = options.fromaddr
    if config_dict.get('error_email_to') is None:
        parser.error("From address must be specified as an option "
                     "(--fromaddr) or in the config file "
                     "(error_email_to)")

    scriptdir = os.path.dirname(os.path.abspath(__file__))
    template_file = options.template
    if template_file is None:
        default_template = os.path.join(scriptdir,
                                        'admin_cleanup_deletion_template.txt')
        sample_template_file = "%s.sample" % default_template
        if os.path.exists(default_template):
            template_file = default_template
        elif os.path.exists(sample_template_file):
            print "Copying %s to %s" % (sample_template_file, default_template)
            shutil.copyfile(sample_template_file, default_template)
            template_file = default_template
        else:
            parser.error(
                "Default template (%s) or sample template (%s) not "
                "found, please specify template as an option "
                "(--template)." % default_template, sample_template_file)
    elif not os.path.exists(template_file):
        parser.error("Specified template file (%s) not found." % template_file)

    config = galaxy.config.Configuration(**config_dict)

    app = CleanupDatasetsApplication(config)
    cutoff_time = datetime.utcnow() - timedelta(days=options.days)
    now = strftime("%Y-%m-%d %H:%M:%S")

    print "##########################################"
    print "\n# %s - Handling stuff older than %i days" % (now, options.days)

    if options.info_only:
        print "# Displaying info only ( --info_only )\n"
    elif options.email_only:
        print "# Sending emails only, not deleting ( --email_only )\n"

    administrative_delete_datasets(app,
                                   cutoff_time,
                                   options.days,
                                   tool_id=options.tool_id,
                                   template_file=template_file,
                                   config=config,
                                   email_only=options.email_only,
                                   info_only=options.info_only)
    app.shutdown()
    sys.exit(0)
def main():
    """
    Datasets that are older than the specified cutoff and for which the tool_id
    contains the specified text will be marked as deleted in user's history and
    the user will be notified by email using the specified template file.
    """
    usage = "usage: %prog [options] galaxy.ini"
    parser = OptionParser(usage=usage)
    parser.add_option("-d", "--days", dest="days", action="store",
                      type="int", help="number of days (60)", default=60)
    parser.add_option("--tool_id", default=None,
                      help="Text to match against tool_id"
                      "Default: match all")
    parser.add_option("--template", default=None,
                      help="Mako Template file to use as email "
                      "Variables are 'cutoff' for the cutoff in days, "
                      "'email' for users email and "
                      "'datasets' which is a list of tuples "
                      "containing 'dataset' and 'history' names. "
                      "Default: admin_cleanup_deletion_template.txt")
    parser.add_option("-i", "--info_only", action="store_true",
                      dest="info_only", help="info about the requested action",
                      default=False)
    parser.add_option("-e", "--email_only", action="store_true",
                      dest="email_only", help="Send emails only, don't delete",
                      default=False)
    parser.add_option("--smtp", default=None,
                      help="SMTP Server to use to send email. "
                      "Default: [read from galaxy ini file]")
    parser.add_option("--fromaddr", default=None,
                      help="From address to use to send email. "
                      "Default: [read from galaxy ini file]")
    (options, args) = parser.parse_args()
    if len(args) != 1 :
        parser.print_help()
        sys.exit()
    ini_file = args[0]

    config_parser = ConfigParser.ConfigParser({'here': os.getcwd()})
    config_parser.read(ini_file)
    config_dict = {}
    for key, value in config_parser.items("app:main"):
        config_dict[key] = value

    if options.smtp is not None:
        config_dict['smtp_server'] = options.smtp
    if config_dict.get('smtp_server') is None:
        parser.error("SMTP Server must be specified as an option (--smtp) "
                     "or in the config file (smtp_server)")

    if options.fromaddr is not None:
        config_dict['email_from'] = options.fromaddr
    if config_dict.get('email_from') is None:
        parser.error("From address must be specified as an option "
                     "(--fromaddr) or in the config file "
                     "(email_from)")

    scriptdir = os.path.dirname(os.path.abspath(__file__))
    template_file = options.template
    if template_file is None:
        default_template = os.path.join(scriptdir,
                                        'admin_cleanup_deletion_template.txt')
        sample_template_file = "%s.sample" % default_template
        if os.path.exists(default_template):
            template_file = default_template
        elif os.path.exists(sample_template_file):
            print "Copying %s to %s" % (sample_template_file, default_template)
            shutil.copyfile(sample_template_file, default_template)
            template_file = default_template
        else:
            parser.error("Default template (%s) or sample template (%s) not "
                         "found, please specify template as an option "
                         "(--template)." % default_template,
                         sample_template_file)
    elif not os.path.exists(template_file):
        parser.error("Specified template file (%s) not found." % template_file)

    config = galaxy.config.Configuration(**config_dict)

    app = CleanupDatasetsApplication(config)
    cutoff_time = datetime.utcnow() - timedelta(days=options.days)
    now = strftime("%Y-%m-%d %H:%M:%S")

    print "##########################################"
    print "\n# %s - Handling stuff older than %i days" % (now, options.days)

    if options.info_only:
        print "# Displaying info only ( --info_only )\n"
    elif options.email_only:
        print "# Sending emails only, not deleting ( --email_only )\n"

    administrative_delete_datasets(
        app, cutoff_time, options.days, tool_id=options.tool_id,
        template_file=template_file, config=config,
        email_only=options.email_only, info_only=options.info_only)
    app.shutdown()
    sys.exit(0)
def main():
    """
    Datasets that are older than the specified cutoff and for which the tool_id
    contains the specified text will be marked as deleted in user's history and
    the user will be notified by email using the specified template file.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('legacy_config', metavar='CONFIG', type=str,
                        default=None,
                        nargs='?',
                        help='config file (legacy, use --config instead)')
    parser.add_argument("-d", "--days", dest="days", action="store",
                        type=int, help="number of days (60)", default=60)
    parser.add_argument("--tool_id", default=None,
                        help="Text to match against tool_id"
                        "Default: match all")
    parser.add_argument("--template", default=None,
                        help="Mako Template file to use as email "
                        "Variables are 'cutoff' for the cutoff in days, "
                        "'email' for users email and "
                        "'datasets' which is a list of tuples "
                        "containing 'dataset' and 'history' names. "
                        "Default: admin_cleanup_deletion_template.txt")
    parser.add_argument("-i", "--info_only", action="store_true",
                        dest="info_only", help="info about the requested action",
                        default=False)
    parser.add_argument("-e", "--email_only", action="store_true",
                        dest="email_only", help="Send emails only, don't delete",
                        default=False)
    parser.add_argument("--smtp", default=None,
                        help="SMTP Server to use to send email. "
                        "Default: [read from galaxy ini file]")
    parser.add_argument("--fromaddr", default=None,
                        help="From address to use to send email. "
                        "Default: [read from galaxy ini file]")
    populate_config_args(parser)

    args = parser.parse_args()
    config_override = None
    if args.legacy_config:
        config_override = args.legacy_config

    app_properties = app_properties_from_args(args, legacy_config_override=config_override)

    if args.smtp is not None:
        app_properties['smtp_server'] = args.smtp
    if app_properties.get('smtp_server') is None:
        parser.error("SMTP Server must be specified as an option (--smtp) "
                     "or in the config file (smtp_server)")

    if args.fromaddr is not None:
        app_properties['email_from'] = args.fromaddr
    if app_properties.get('email_from') is None:
        parser.error("From address must be specified as an option "
                     "(--fromaddr) or in the config file "
                     "(email_from)")

    scriptdir = os.path.dirname(os.path.abspath(__file__))
    template_file = args.template
    if template_file is None:
        default_template = os.path.join(scriptdir,
                                        'admin_cleanup_deletion_template.txt')
        sample_template_file = "%s.sample" % default_template
        if os.path.exists(default_template):
            template_file = default_template
        elif os.path.exists(sample_template_file):
            print("Copying %s to %s" % (sample_template_file, default_template))
            shutil.copyfile(sample_template_file, default_template)
            template_file = default_template
        else:
            parser.error("Default template (%s) or sample template (%s) not "
                         "found, please specify template as an option "
                         "(--template)." % default_template,
                         sample_template_file)
    elif not os.path.exists(template_file):
        parser.error("Specified template file (%s) not found." % template_file)

    config = galaxy.config.Configuration(**app_properties)

    app = CleanupDatasetsApplication(config)
    cutoff_time = datetime.utcnow() - timedelta(days=args.days)
    now = strftime("%Y-%m-%d %H:%M:%S")

    print("##########################################")
    print("\n# %s - Handling stuff older than %i days" % (now, args.days))

    if args.info_only:
        print("# Displaying info only ( --info_only )\n")
    elif args.email_only:
        print("# Sending emails only, not deleting ( --email_only )\n")

    administrative_delete_datasets(
        app, cutoff_time, args.days, tool_id=args.tool_id,
        template_file=template_file, config=config,
        email_only=args.email_only, info_only=args.info_only)
    app.shutdown()
    sys.exit(0)
def main():
    """
    Datasets that are older than the specified cutoff and for which the tool_id
    contains the specified text will be marked as deleted in user's history and
    the user will be notified by email using the specified template file.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('legacy_config',
                        metavar='CONFIG',
                        type=str,
                        default=None,
                        nargs='?',
                        help='config file (legacy, use --config instead)')
    parser.add_argument("-d",
                        "--days",
                        dest="days",
                        action="store",
                        type=int,
                        help="number of days (60)",
                        default=60)
    parser.add_argument("--tool_id",
                        default=None,
                        help="Text to match against tool_id"
                        "Default: match all")
    parser.add_argument("--template",
                        default=None,
                        help="Mako Template file to use as email "
                        "Variables are 'cutoff' for the cutoff in days, "
                        "'email' for users email and "
                        "'datasets' which is a list of tuples "
                        "containing 'dataset' and 'history' names. "
                        "Default: admin_cleanup_deletion_template.txt")
    parser.add_argument("-i",
                        "--info_only",
                        action="store_true",
                        dest="info_only",
                        help="info about the requested action",
                        default=False)
    parser.add_argument("-e",
                        "--email_only",
                        action="store_true",
                        dest="email_only",
                        help="Send emails only, don't delete",
                        default=False)
    parser.add_argument("--smtp",
                        default=None,
                        help="SMTP Server to use to send email. "
                        "Default: [read from galaxy ini file]")
    parser.add_argument("--fromaddr",
                        default=None,
                        help="From address to use to send email. "
                        "Default: [read from galaxy ini file]")
    populate_config_args(parser)

    args = parser.parse_args()
    config_override = None
    if args.legacy_config:
        config_override = args.legacy_config

    app_properties = app_properties_from_args(
        args, legacy_config_override=config_override)

    if args.smtp is not None:
        app_properties['smtp_server'] = args.smtp
    if app_properties.get('smtp_server') is None:
        parser.error("SMTP Server must be specified as an option (--smtp) "
                     "or in the config file (smtp_server)")

    if args.fromaddr is not None:
        app_properties['email_from'] = args.fromaddr
    if app_properties.get('email_from') is None:
        parser.error("From address must be specified as an option "
                     "(--fromaddr) or in the config file "
                     "(email_from)")

    scriptdir = os.path.dirname(os.path.abspath(__file__))
    template_file = args.template
    if template_file is None:
        default_template = os.path.join(scriptdir,
                                        'admin_cleanup_deletion_template.txt')
        sample_template_file = "%s.sample" % default_template
        if os.path.exists(default_template):
            template_file = default_template
        elif os.path.exists(sample_template_file):
            print("Copying %s to %s" %
                  (sample_template_file, default_template))
            shutil.copyfile(sample_template_file, default_template)
            template_file = default_template
        else:
            parser.error(
                "Default template (%s) or sample template (%s) not "
                "found, please specify template as an option "
                "(--template)." % default_template, sample_template_file)
    elif not os.path.exists(template_file):
        parser.error("Specified template file (%s) not found." % template_file)

    config = galaxy.config.Configuration(**app_properties)

    app = CleanupDatasetsApplication(config)
    cutoff_time = datetime.utcnow() - timedelta(days=args.days)
    now = strftime("%Y-%m-%d %H:%M:%S")

    print("##########################################")
    print("\n# %s - Handling stuff older than %i days" % (now, args.days))

    if args.info_only:
        print("# Displaying info only ( --info_only )\n")
    elif args.email_only:
        print("# Sending emails only, not deleting ( --email_only )\n")

    administrative_delete_datasets(app,
                                   cutoff_time,
                                   args.days,
                                   tool_id=args.tool_id,
                                   template_file=template_file,
                                   config=config,
                                   email_only=args.email_only,
                                   info_only=args.info_only)
    app.shutdown()
    sys.exit(0)