# Copyright (C) 2008 Libresoft Research Group
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors : Israel Herraiz <herraiz@gsyc.escet.urjc.es>

from mydatabase import MyDatabase
import os
import commands
import sys

class MyApp:

    def __init__(self,host,user,password,dbname):

        self.db = MyDatabase()

        self.db.user = user
        self.db.password = password
        self.db.host = host
        self.db.name = dbname

        self.db.connect()

    def run(self):

        # Get list of all ports
        portslist_command = "cd /usr/ports && find . -type d -depth 2 -print"
        ignorelist = ['.', \
                      'Tools', \
                      'work', \
                      'files', \
                      'Mk', \
                      'Templates', \
                      'distfiles']
        

        portslist = commands.getoutput(portslist_command).split('\n')

        # This dict contain each port name and its section
        portsdict = {}

        for port in portslist:
            try:
                section, name = port.lstrip('./').split('/')
            except ValueError:
                continue

            if not section in ignorelist:
                portsdict[name] = section

                print "Obtaining filenames port "+name+" from section "+section
                filenames = self.obtainPortFilesNames(name,section)

                # Store port name, section and tarfilename
                for f in filenames:
                    properties_dict = {'portname':name, \
                                       'portsection':section, \
                                       'tarfilename':f}
                
                    self.db.insertProject(properties_dict)

        self.db.connection.commit()

    def obtainPortFilesNames(self,name,section):

        portdir = os.path.join('/usr/ports',section,name)
        distinfo_fn = os.path.join(portdir,'distinfo')
        
        # Get list of files of this port
        if not os.path.exists(distinfo_fn):
            return []
        
        distinfo = open(distinfo_fn,'r')
        content = distinfo.readlines()
        distinfo.close()

        filenames = []

        for line in content:
            if 'SIZE (' in line:
                filename = line.split('(')[1].split(')')[0]

                filenames.append(filename)

        return filenames

if '__main__' == __name__:

    host = 'localhost'
    user = 'root'
    password = 'root'
    dbname = 'metrics_tse_herraiz'
    app = MyApp(host=host, 
                user=user, 
                password=password, 
                dbname=dbname)
    app.run()

