Source

Fapman / install.py

#-*- coding: UTF-8 -*-

##      install.py - pacakge installation, removing and reinstallation.
##
##
## Copyright (C) 2008 A2K <a2k0001@gmail.com>
##
## This file is part of fapman.
##
## fapman 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; version 3 or later.
##
## fapman 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.
##
## You should have received a copy of the GNU General Public License
## along with fapman.  If not, see <http://www.gnu.org/licenses/>.
##

import os
import shutil

from config import Config
from package import Package
from download import Download

class RepositoyError(Exception):
    pass
class DownloadError(Exception):
    pass

class Installer:
    def install(self, package):
        if package.REPO == 'local':
            self.reinstall(package)
            return True
        if package.installed()[0] == 0:
            self.reinstall(package)
            return True
        if package.REPO == 'aur':
            self.install_from_AUR(package)
            return True
        self.config = Config()
        dest = '/var/cache/fapman/pkg/' + package.FILENAME
        dbdir = os.path.join("/var/lib/pacman/local/",str(package))
        srcdir= os.path.join("/var/lib/pacman/sync/",package.REPO,str(package))
        if os.path.exists(dest):
            if os.path.getsize(dest) != int(package.CSIZE):
                self.download(package)
        else:
            self.download(package)
        try:
            os.mkdir(dbdir)
        except:
            pass
        os.system("/bin/tar -xv -C / -f "+dest+" > "+os.path.join(dbdir,"files"))
        shutil.copy(os.path.join(srcdir,"desc"),dbdir+"/desc")
        shutil.copy(os.path.join(srcdir,"depends"),dbdir+"/depends")
    def remove(self, package):
        dbdir = os.path.join("/var/lib/pacman/local/",str(package))
        files = open(dbdir+"/files").readlines()
        files = files[-2:0:-1]
        for line in files:
            file_ = "/"+line[:-1]
            if os.path.exists(file_):
                if os.path.isfile(file_):
                    try:
                        print os.remove(file_)
                    except:
                        pass
                else:
                    try:
                        os.removedirs(file_)
                    except:
                        pass
            else:
                print "File does not exists: %s" % file_
    def download(self, package):
        url = self.config['Servers'][package.REPO] + '/'+package.FILENAME
        dest = '/var/cache/fapman/pkg/' + package.FILENAME
        name = str(package)
        try:
            Download(url, dest, name)
            return True
        except:
            return False
    def reinstall(self, package):
        #there will be something here...
        pass
    def install_from_AUR(self, package):
        #there will be something here...
        pass

#from search import Search
#pk=Search().local("gajim")[0]
#print Installer().remove(pk)