root/trunk/src/repositoryhandler/repHandler.py @ 60

Revision 60, 7.0 KB (checked in by pcabido, 5 years ago)
  • Property svn:executable set to *
Line 
1# -*- coding: utf-8 -*-
2import warnings
3warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning)
4
5import apt
6import apt_pkg
7import apt_inst
8import os
9import shutil
10from util import util
11from util import aptutil
12
13apt_pkg.init()         
14
15class rephandle:
16   
17    def __init__(self, kitsSourcesList, tmpDir, localDir=""):
18        self._sourceList = kitsSourcesList
19        self._tmpDir = tmpDir
20        self._localDir = localDir
21        self._packages = []
22        self._localKits = []
23       
24    def update(self, deleteFiles=False):
25        try:
26            if deleteFiles:
27                for root, dirs, files in os.walk(self._tmpDir):
28                    for name in files:
29                        filename = os.path.join(root, name)
30                        if os.path.isfile(filename):
31                            os.remove(filename)
32                           
33                util.rmdir(self._tmpDir)
34           
35            util.mkdir(self._tmpDir)
36            util.mkdir(self._tmpDir + '/partial')
37            util.mkdir(self._tmpDir + '/sources.list.d')
38
39            #define opcoes apt
40            apt_pkg.Config.Set("Dir::Etc::sourceparts", self._tmpDir + '/sources.list.d')
41            apt_pkg.Config.Set("Dir::Etc::sourcelist", self._sourceList)
42            apt_pkg.Config.Set("Dir::State::Lists", self._tmpDir)
43           
44            cache = apt.Cache()
45            try:
46                res = cache.update(apt.progress.TextFetchProgress())
47            except:
48                return False
49           
50            return True
51        except IOError:
52            print "update: error\n"
53            return False
54   
55    def clean(self):
56        try:
57            for root, dirs, files in os.walk(self._tmpDir):
58                for name in files:
59                    filename = os.path.join(root, name)
60                    if os.path.isfile(filename):
61                        os.remove(filename)
62                           
63            util.rmdir(self._tmpDir)
64           
65            return True
66        except IOError:
67            print "clean: error\n"
68            return False
69       
70    def loadPackages(self, deleteFiles=True):
71        try:
72            self._packages = []
73            tmpListLine = []
74            for root, dirs, files in os.walk(self._tmpDir):
75                for name in files:       
76                   filename = os.path.join(root, name)
77                   if filename[-8:] == "Packages":
78                       try:
79                           tmpFile = open(filename, "r")
80                           tmpData = tmpFile.read()
81                           tmpListData = tmpData.split("Package: ")
82                           tmpFilename = filename.split("_")
83                           
84                           for pkg in tmpListData:
85                               if pkg != "":
86                                   tmpSplitDesc = pkg.split("Description: ")
87                                   tmpSplitDesc[1] = "Description: " + tmpSplitDesc[1]
88                                   tmpGlobalInfo = tmpSplitDesc[0].split("\n")
89                                   tmpGlobalInfo[0] = "Package: " + tmpGlobalInfo[0]
90                                   tmpDic = util.list2dic(tmpGlobalInfo)
91                                   #tmpDic['Description'] = tmpSplitDesc[1]
92                                   tmpDic['ShortDesc'] = aptutil.getShortDesc(tmpSplitDesc[1])
93                                   tmpDic['LongDesc'] = aptutil.getLongDesc(tmpSplitDesc[1])
94                                   tmpDic['Installed'] = False
95                                   self._packages.append(tmpDic)
96                       except:
97                           pass
98            #remove os ficheiros na directorias e subdirectorias
99            if deleteFiles:
100                for root, dirs, files in os.walk(self._tmpDir):
101                    for name in files:
102                        filename = os.path.join(root, name)
103                        if os.path.isfile(filename):
104                            os.remove(filename)
105                           
106                util.rmdir(self._tmpDir)
107           
108            return True
109        except IOError:
110            print "loadPackages: error\n"
111            return False
112   
113    def loadLocalKits(self):
114        try:
115            self._localKits = []
116            for root, dirs, files in os.walk(self._localDir):
117                for name in files:       
118                   filename = os.path.join(root, name)
119                   #check if extension is .deb
120                   tmpFilename = os.path.splitext(filename)
121                   if tmpFilename[1] == ".deb":
122                       try:
123                           tmpDic = {}
124                           controlFile = apt_inst.debExtractControl(open(filename))
125                           sections = apt_pkg.ParseSection(controlFile)
126                           #criar o dicionario
127                           tmpDic['Installed'] = False
128                           tmpDic['Package'] = sections["Package"]
129                           tmpDic['Priority'] = sections["Priority"]
130                           tmpDic['Section'] = sections["Section"]
131                           tmpDic['Version'] = sections["Version"]
132                           tmpDic['Architecture'] = sections["Architecture"]
133                           tmpDic['Maintainer'] = sections["Maintainer"]
134                           #tmpDic['Description'] = sections["Description"]
135                           tmpDic['ShortDesc'] = aptutil.getShortDesc(sections["Description"])
136                           tmpDic['LongDesc'] = aptutil.getLongDesc(sections["Description"])
137                           tmpDic['Depends'] = aptutil.stripDepends(apt_pkg.ParseDepends(sections["Depends"]))
138                           self._localKits.append(tmpDic)
139                       except:
140                           pass
141            return True       
142        except IOError:
143            print "error\n"
144            return False
145   
146    def setTmpDir(self, tmpDir):
147        self._tmpDir = tmpDir
148       
149    def checkInstalled(self):
150        installedPackages = aptutil.readAptInstalled().split(", ")
151        if installedPackages == []:
152            return
153        else:
154            for value in self._packages:
155                for pkg in installedPackages:
156                    if value['Package'] == pkg:
157                        value['Installed'] = True
158        return
159   
160    def getPackages(self):
161        return self._packages
162   
163    def getLocalKits(self):
164        return self._localKits
165   
166    def findPackages(self, strToFind, packageList):
167        matchingSearch = []
168        for dic in packageList:
169           if util.findStrInDic(strToFind, dic):
170               matchingSearch.append(dic)
171               
172        return matchingSearch
173   
174    def getSections(self):
175        sections = ['all']
176        for dic in self._packages:
177                sections.append(dic['Section'])
178               
179        for dic in self._localKits:
180                sections.append(dic['Section'])
181       
182        result = util.removeDuplicates(sections)
183        return result
184   
Note: See TracBrowser for help on using the browser.