#!/var/www/rpm2python/venv/bin/python
""" Utility to update rpmfind database """
###############################################################################
#Programmer: Orcan Ogetbil    orcan@nbcs.rutgers.edu                          #
#Date: 08/12/2010                                                             #
#Filename: populate-rpmfind-db                                                #
#                                                                             #
#                                                                             #
#       Copyright 2010 Orcan Ogetbil                                          #
#                                                                             #
#    This program is free software; you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation; either version 2 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program 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 General Public License for more details.                             #
#                                                                             #
###############################################################################
import sys
sys.path.append('/var/www/rpm2python/rpm2python')
import _mysql
import _mysql_exceptions
from MySQLdb.constants import FIELD_TYPE
import os
import rcommon
import populatedb
from optparse import OptionParser
import logging
import string
import time

my_config_file='/etc/populate-rpmfind-db.cfg'
""" Utility to update rpmfind database """
os.umask(002)
myapp = rcommon.AppHandler(verifyuser=True,config_file=my_config_file)
parser = OptionParser("usage: %prog [options]")
parser.add_option("-r", "--rebuild",
                  default=False,
                  action="store_true",
                  help="Cleans and rebuilds the whole database.")
parser.add_option("-v",
                  "--verbose",
                  default=False,
                  action="store_true",
                  help="Verbose output")
parser.add_option("-d", "--distver",
                  action="store", type="string", dest="distver", default="",
                  help="Populate only specific distver (give option as number)")

(options, args) = parser.parse_args(sys.argv[1:])
myapp.create_lock()

if options.verbose:
    verbosity = logging.DEBUG
else:
    verbosity = logging.INFO
myapp.init_logger(verbosity)

if options.rebuild: 
    my_conv = { FIELD_TYPE.LONG: int }
    db_host = myapp.config.get("rpmdb", "host")
    db_user = myapp.config.get("rpmdb", "user")
    db_pw   = myapp.config.get("rpmdb", "password")
    db_name = myapp.config.get("rpmdb", "name")

    dbase = _mysql.connect(db_host, db_user, db_pw, db_name, conv=my_conv)
    
    populatedb.clean_database(myapp, dbase)
    populatedb.create_tables(myapp, dbase)

    dbase.close()

distname = myapp.config.get("repositories", "distname_nice")
alldistvers = myapp.config.get("repositories", "alldistvers").split()
if options.distver == "":
  for distver in alldistvers:
      myapp.distver = distver
      myapp.logger.info("Populating rpmfind database for {0} {1}...".format(
          distname, distver))
      populatedb.update_db(myapp)
else:
  myapp.distver = options.distver
  myapp.logger.info("Populating rpmfind database for {0} {1}...".format(
      distname, options.distver))
  populatedb.update_db(myapp)
  
timerun = myapp.time_run()
myapp.logger.info("\nSuccess! Time run: " + str(timerun) + " s")

myapp.exit()
