#!/usr/bin/python
#
# This file is a portion of the Red Hat Update Agent
# Copyright (c) 1999 - 2002 Red Hat, Inc.  Distributed under GPL
#
# Authors:
#       Cristian Gafton <gafton@redhat.com>
#       Adrian Likins   <alikins@redhat.com>
#
# $Id: sourcesConfig.py,v 1.7 2004/01/22 18:10:47 alikins Exp $

import os
import sys
import string

import config
import up2dateUtils

from rhpl.translate import _, N_

# The format for sources v1 is stupid. each entry can only be one line
# each different source type has different info (aieee!) # comment stuff out (duh)



class SourcesConfigFile:
    "class for parsing out the up2date/apt/yum src repo info"
    def __init__(self, filename = None):
        self.repos = []
        self.fileName = filename
        self.cfg = config.initUp2dateConfig()
        if self.fileName:
            self.load()

    def load(self, filename = None):
        if filename:
            self.fileName = filename
        if not self.fileName:
            return

        if not os.access(self.fileName, os.R_OK):
            print "warning: can't access %s" % self.fileName
            return

        f = open(self.fileName, "r")

        
	for line in f.readlines():
            # strip comments
            if '#' in line:
                line = line[:string.find(line, '#')]

            line = string.strip(line)
            if not line:
                continue

            data = string.split(line)
            repoType = data[0]
            if data[0] == "up2date":
                self.parseUp2date(line)
            if data[0] == "yum":
                self.parseYum(line)
            if data[0] == "apt":
                self.parseApt(line)
            if data[0] == "dir":
                self.parseDir(line)
            if data[0] == "bt":
                self.parseBt(line)
            if data[0] == "yum-mirror":
                self.parseYumMirror(line)
            if data[0] == "apt-mirror":
                self.parseAptMirror(line)
            if data[0] == "rpmmd":
                self.parseRpmmd(line)
                
        f.close()

    # in some cases, we want to readd the line that points at RHN
    def writeUp2date(self):
        # parse the config file into something editable
        f = open(self.fileName, "r")
        lines = f.readlines()
        index = 0
        for line in lines:
            if '#' in line:
                line = line[:string.find(line, '#')]
                
            line = string.rstrip(line)
            if not line:
                index = index + 1
                continue
            
            firstUsedLine = index
            break
        
        f.close()
        
        f = open(self.fileName, "w")

        lines.insert(firstUsedLine-1, "up2date default")
        buf = string.join(lines, '')
        f.write(buf)
        f.close()
        
        
    def parseUp2date(self,line):
        try:
            (tmp, url) = string.split(line)
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return
            
        if url == "default":
            self.repos.append({'type':'up2date', 'url':self.cfg['serverURL']})
        else:
            self.repos.append({'type':'up2date', 'url':url})

    def parseDir(self, line):
        try:
            (tmp, name, path) = string.split(line)
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return
        
        self.repos.append({'type':'dir','path':path, 'label':name})

    def parseYum(self, line):
        try:
            (tmp, name, url) = string.split(line)
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return
        arch = up2dateUtils.getUnameArch()
        url = string.replace(url, "$ARCH", arch)
        name = string.replace(name, "$ARCH", arch)
        self.repos.append({'type':'yum', 'url':url, 'label':name})

    def parseYumMirror(self, line):
        try:
            tmp = []
            tmp = string.split(line)
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return

        
        url = tmp[2]
        name = tmp[1]
        arch = up2dateUtils.getUnameArch()
        url = string.replace(url, "$ARCH", arch)
        name = string.replace(name, "$ARCH", arch)
        self.repos.append({'type':'yum-mirror', 'url':url, 'label':name})
        
    def parseAptMirror(self, line):
        try:
            tmp = []
            tmp = string.split(line)
            server = tmp[2]
            path = tmp[3]
            label = tmp[1]
            dists = tmp[4:]
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return

        
        arch = up2dateUtils.getUnameArch()
        url = string.replace(url, "$ARCH", arch)
        name = string.replace(name, "$ARCH", arch)
        for dist in dists:
            self.repos.append({'type':'apt-mirror', 'url':"%s/%s" (server, path),
                               'label':name, 'dist': dist})

    def parseBt(self, line):
        try:
            (tmp, name, url) = string.split(line)
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return
        self.repos.append({'type':'bt', 'url':url, 'label':name})

    def parseRpmmd(self, line):
        try:
            data = string.split(line)
            name = data[1]
            url = data[2]
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return

        arch = up2dateUtils.getUnameArch()
        url = string.replace(url, "$ARCH", arch)
        name = string.replace(name, "$ARCH", arch)
        self.repos.append({'type':'rpmmd', 'url':url, 'label':name})

    def parseApt(self, line):
        # of course, the debian one had to be weird
        # atm, we only support http one's
        try:
            data = string.split(line)
            name = data[1]
            server = data[2]
            path = data[3]
            dists = data[4:]
        except:
            print "Error parsing /etc/sysconfig/rhn/up2date"
            print "at line: %s" % line
            return
        # if multiple dists are appended, make them seperate
        # channels
        for dist in dists:
            self.repos.append({'type':'apt',
                          'url':'%s/%s' % (server, path),
                          'label': "%s-%s" % (name,dist),
                          'dist': dist})

global sources
sources = None
def getSources():
    global sources
    if sources:
        return sources
    scfg = SourcesConfigFile(filename="/etc/sysconfig/rhn/sources")
    sources = scfg.repos
    return sources
    

