| 1 | |
|---|
| 2 | import warnings |
|---|
| 3 | warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning) |
|---|
| 4 | |
|---|
| 5 | import apt_inst |
|---|
| 6 | import apt_pkg |
|---|
| 7 | import commands |
|---|
| 8 | import sys |
|---|
| 9 | from progress import TextProgress |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | apt_pkg.init() |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | def readAptInstalled(): |
|---|
| 16 | progress = TextProgress() |
|---|
| 17 | cache = apt_pkg.GetCache(progress) |
|---|
| 18 | packages = cache.Packages |
|---|
| 19 | |
|---|
| 20 | systemDepends= "" |
|---|
| 21 | for package in packages: |
|---|
| 22 | if package.CurrentVer != None: |
|---|
| 23 | systemDepends += package.Name + ", " |
|---|
| 24 | |
|---|
| 25 | if len(systemDepends) > 2: |
|---|
| 26 | return systemDepends[:-2] |
|---|
| 27 | else: |
|---|
| 28 | return "" |
|---|
| 29 | |
|---|
| 30 | def readAptInstalled2List(): |
|---|
| 31 | progress = TextProgress() |
|---|
| 32 | cache = apt_pkg.GetCache(progress) |
|---|
| 33 | packages = cache.Packages |
|---|
| 34 | |
|---|
| 35 | systemDepends= [] |
|---|
| 36 | for package in packages: |
|---|
| 37 | if package.CurrentVer != None: |
|---|
| 38 | systemDepends.append(package.Name) |
|---|
| 39 | |
|---|
| 40 | return systemDepends |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | def getCachePackages(ignore=True): |
|---|
| 44 | progress = TextProgress() |
|---|
| 45 | cache = apt_pkg.GetCache(progress) |
|---|
| 46 | records = apt_pkg.GetPkgRecords(cache) |
|---|
| 47 | packages = cache.Packages |
|---|
| 48 | |
|---|
| 49 | pkgList = [] |
|---|
| 50 | |
|---|
| 51 | for pkg in packages: |
|---|
| 52 | for pkgInfo in pkg.VersionList: |
|---|
| 53 | for packFile, index in pkgInfo.FileList: |
|---|
| 54 | records.Lookup((packFile,index)) |
|---|
| 55 | if packFile.IndexType != 'Debian Package Index' and ignore: |
|---|
| 56 | continue |
|---|
| 57 | pkgList.append([None, pkg.Name, pkgInfo.VerStr, records.ShortDesc]) |
|---|
| 58 | |
|---|
| 59 | pkgList.sort() |
|---|
| 60 | return pkgList |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | def pkgInfo(package, info): |
|---|
| 64 | try: |
|---|
| 65 | controlFile = apt_inst.debExtractControl(open(package)) |
|---|
| 66 | sections = apt_pkg.ParseSection(controlFile) |
|---|
| 67 | |
|---|
| 68 | if info == "Depends": |
|---|
| 69 | return apt_pkg.ParseDepends(sections["Depends"]) |
|---|
| 70 | else: |
|---|
| 71 | return sections[info] |
|---|
| 72 | except: |
|---|
| 73 | return "" |
|---|
| 74 | |
|---|
| 75 | def isInstalled(package): |
|---|
| 76 | try: |
|---|
| 77 | pkg = pkgInfo(package, 'Package') |
|---|
| 78 | |
|---|
| 79 | dpkg_query = commands.getoutput('dpkg-query --show ' + pkg) |
|---|
| 80 | |
|---|
| 81 | if (dpkg_query[0:len(pkg)] == pkg): |
|---|
| 82 | versionInstalled = dpkg_query.split('\t')[1] |
|---|
| 83 | if not versionInstalled: return False |
|---|
| 84 | return True |
|---|
| 85 | else: |
|---|
| 86 | return False |
|---|
| 87 | except: |
|---|
| 88 | return False |
|---|
| 89 | |
|---|
| 90 | def equalVersion(package): |
|---|
| 91 | try: |
|---|
| 92 | pkg = pkgInfo(package, 'Package') |
|---|
| 93 | installed = isInstalled(pkg) |
|---|
| 94 | if not installed: return False |
|---|
| 95 | |
|---|
| 96 | dpkg_query = commands.getoutput('dpkg-query --show ' + pkg) |
|---|
| 97 | |
|---|
| 98 | versionInstalled = dpkg_query.split('\t')[1] |
|---|
| 99 | versionInPackage = pkgInfo(package, 'Version') |
|---|
| 100 | if versionInPackage == versionInstalled: |
|---|
| 101 | return True |
|---|
| 102 | return False |
|---|
| 103 | except: |
|---|
| 104 | return False |
|---|
| 105 | |
|---|
| 106 | def upgradable(package): |
|---|
| 107 | try: |
|---|
| 108 | pkg = pkgInfo(package, 'Package') |
|---|
| 109 | if (not isInstalled(pkg)) or equalVersion(pkg): return False |
|---|
| 110 | |
|---|
| 111 | dpkg_query = commands.getoutput('dpkg-query --show ' + pkg) |
|---|
| 112 | versionInstalled = dpkg_query.split('\t')[1] |
|---|
| 113 | versionInPackage = pkgInfo(package, 'Version') |
|---|
| 114 | |
|---|
| 115 | ltC ='if dpkg --compare-versions "'+ versionInstalled + '" lt "' + \ |
|---|
| 116 | versionInPackage + '";then echo yes; fi ' |
|---|
| 117 | if commands.getoutput(ltC) == 'yes': |
|---|
| 118 | return True |
|---|
| 119 | return False |
|---|
| 120 | except: |
|---|
| 121 | return False |
|---|
| 122 | |
|---|
| 123 | def install(package): |
|---|
| 124 | dpkg_install = commands.getoutput("gksu 'gdebi -n -q %s'" %(package)) |
|---|
| 125 | if len(dpkg_install.split("\n")) > 1: |
|---|
| 126 | isSuccess = isInstalled(package) |
|---|
| 127 | if isSuccess: |
|---|
| 128 | return True |
|---|
| 129 | else: |
|---|
| 130 | return False |
|---|
| 131 | else: |
|---|
| 132 | return False |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | def stripDepends(depList): |
|---|
| 137 | depResult = "" |
|---|
| 138 | for L in depList: |
|---|
| 139 | for tuplo in L: |
|---|
| 140 | if tuplo[0] != "" and tuplo[1] == "": |
|---|
| 141 | depResult += tuplo[0] + ", " |
|---|
| 142 | elif tuplo[0] != "" and tuplo[1] != "": |
|---|
| 143 | depResult += tuplo[0] + " (" + tuplo[2] + " " + tuplo[1] + "), " |
|---|
| 144 | |
|---|
| 145 | if len(depResult) > 2: |
|---|
| 146 | return depResult[:-2] |
|---|
| 147 | else: |
|---|
| 148 | return "" |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | def setDescription(SDesc, LDesc): |
|---|
| 152 | Desc = SDesc + "\n .\n" |
|---|
| 153 | tmpLDesc = LDesc.split("\n") |
|---|
| 154 | for e in tmpLDesc: |
|---|
| 155 | if e == "": |
|---|
| 156 | Desc += " .\n" |
|---|
| 157 | else: |
|---|
| 158 | Desc += " " + e + "\n" |
|---|
| 159 | |
|---|
| 160 | return Desc |
|---|
| 161 | |
|---|
| 162 | def getDependsFromDic(Dic): |
|---|
| 163 | depends = "" |
|---|
| 164 | for key, value in Dic.items(): |
|---|
| 165 | depends += key + ", " |
|---|
| 166 | |
|---|
| 167 | if len(depends) > 2: |
|---|
| 168 | return depends[:-2] |
|---|
| 169 | else: |
|---|
| 170 | return "" |
|---|
| 171 | |
|---|
| 172 | def getDependsFromList(list): |
|---|
| 173 | depends = "" |
|---|
| 174 | for l in list: |
|---|
| 175 | depends += l + ", " |
|---|
| 176 | |
|---|
| 177 | if len(depends) > 2: |
|---|
| 178 | return depends[:-2] |
|---|
| 179 | else: |
|---|
| 180 | return "" |
|---|
| 181 | |
|---|
| 182 | def getShortDesc(desc): |
|---|
| 183 | if desc: |
|---|
| 184 | try: |
|---|
| 185 | tmpDesc = desc.split("\n") |
|---|
| 186 | if "Description: " in tmpDesc[0]: |
|---|
| 187 | finalDesc = tmpDesc[0].split("Description: ") |
|---|
| 188 | else: |
|---|
| 189 | return tmpDesc[0] |
|---|
| 190 | return finalDesc[1] |
|---|
| 191 | except: |
|---|
| 192 | return "" |
|---|
| 193 | else: |
|---|
| 194 | return "" |
|---|
| 195 | |
|---|
| 196 | def getLongDesc(desc): |
|---|
| 197 | final = "" |
|---|
| 198 | if desc: |
|---|
| 199 | try: |
|---|
| 200 | tmpDesc = desc.split("\n") |
|---|
| 201 | if len(tmpDesc) > 1: |
|---|
| 202 | for i in range(len(tmpDesc) - 1): |
|---|
| 203 | final += tmpDesc[i+1] + "\n" |
|---|
| 204 | return final |
|---|
| 205 | except: |
|---|
| 206 | pass |
|---|
| 207 | else: |
|---|
| 208 | return "" |
|---|
| 209 | |
|---|
| 210 | def shortDescFromDic(list, pkg, ver): |
|---|
| 211 | for dic in list: |
|---|
| 212 | if dic['Package'] == pkg and dic['Version'] == ver: |
|---|
| 213 | return dic['ShortDesc'] |
|---|
| 214 | |
|---|
| 215 | return "" |
|---|
| 216 | |
|---|
| 217 | def longDescFromDic(list, pkg, ver): |
|---|
| 218 | for dic in list: |
|---|
| 219 | if dic['Package'] == pkg and dic['Version'] == ver: |
|---|
| 220 | return dic['LongDesc'] |
|---|
| 221 | |
|---|
| 222 | return "" |
|---|
| 223 | |
|---|
| 224 | def getSectionPackages(section, list): |
|---|
| 225 | Packages = [] |
|---|
| 226 | for dic in list: |
|---|
| 227 | if dic['Section'] == section: |
|---|
| 228 | Packages.append(dic) |
|---|
| 229 | return Packages |
|---|
| 230 | |
|---|
| 231 | def main(): |
|---|
| 232 | args = sys.argv[1:] |
|---|
| 233 | if len(args) != 1: |
|---|
| 234 | print 'usage: python test.py infile.xml' |
|---|
| 235 | sys.exit(-1) |
|---|
| 236 | print install(args[0]) |
|---|
| 237 | |
|---|
| 238 | if __name__ == '__main__': |
|---|
| 239 | main() |
|---|