[227] | 1 | -- This is a helper script for update-watch. |
---|
| 2 | -- Run apt-get and extract packaged that to be installed, |
---|
| 3 | -- updated, removed and duplicated. |
---|
[175] | 4 | |
---|
[1036] | 5 | rex = require("rex_posix") |
---|
| 6 | |
---|
[175] | 7 | basefile = "/var/tmp/update-watch-info." |
---|
| 8 | filename = basefile.."tmp" |
---|
| 9 | |
---|
| 10 | -- Function to extract real package name from allow-duplicated packages. |
---|
| 11 | function realname(name) |
---|
| 12 | local s, e, name = string.find(name, "(.+)#") |
---|
| 13 | return name |
---|
| 14 | end |
---|
| 15 | |
---|
[227] | 16 | -- Open output file that store list of to-be changed packages, |
---|
[175] | 17 | file = io.open(filename, "w") |
---|
| 18 | if not file then |
---|
| 19 | print("error: can't open output file at "..filename) |
---|
| 20 | return |
---|
| 21 | end |
---|
| 22 | |
---|
[230] | 23 | -- Get list ofmanually held packages and store it to local table. |
---|
| 24 | -- those packages should be ignored by update-watch. |
---|
| 25 | |
---|
| 26 | local holdlist = confgetlist("RPM::Hold") |
---|
| 27 | |
---|
| 28 | for i, expr in ipairs(holdlist) do |
---|
| 29 | holdlist[i] = rex.new(expr) |
---|
| 30 | end |
---|
| 31 | |
---|
[227] | 32 | -- Mark whole system for upgrading to enable executing |
---|
[175] | 33 | -- apt-get upgrade --simulate for non root users. |
---|
| 34 | |
---|
| 35 | markupgrade() |
---|
| 36 | |
---|
[227] | 37 | -- *NOTE* bellow block is copied from allow-duplicated.lua |
---|
[175] | 38 | -- to handle allow-duplicated packages properly. |
---|
| 39 | ----------<start copied block>---------- |
---|
| 40 | -- Automatically install newer versions of all packages which |
---|
| 41 | -- are registered in the Allow-Duplicated scheme and are matched |
---|
| 42 | -- by the regular expressions in RPM::Allow-Duplicated-Upgrade. |
---|
| 43 | |
---|
| 44 | -- Compile expressions with package names which should |
---|
| 45 | -- be considered for upgrade. |
---|
| 46 | local updatelist = confgetlist("RPM::Allow-Duplicated-Upgrade") |
---|
| 47 | for i, expr in ipairs(updatelist) do |
---|
| 48 | updatelist[i] = rex.new(expr) |
---|
| 49 | end |
---|
| 50 | |
---|
| 51 | if table.getn(updatelist) ~= 0 then |
---|
| 52 | |
---|
| 53 | -- Gather information about Allow-Duplicated packges. |
---|
| 54 | local canddups = {} |
---|
| 55 | local curdups = {} |
---|
| 56 | for i, pkg in pairs(pkglist()) do |
---|
| 57 | local name = realname(pkgname(pkg)) |
---|
| 58 | if name then |
---|
| 59 | if pkgvercur(pkg) then |
---|
| 60 | if not curdups[name] then |
---|
| 61 | curdups[name] = {} |
---|
| 62 | end |
---|
| 63 | table.insert(curdups[name], |
---|
| 64 | verstr(pkgvercur(pkg))) |
---|
| 65 | elseif pkgvercand(pkg) then |
---|
| 66 | if not canddups[name] then |
---|
| 67 | canddups[name] = {} |
---|
| 68 | end |
---|
| 69 | table.insert(canddups[name], |
---|
| 70 | verstr(pkgvercand(pkg))) |
---|
| 71 | end |
---|
| 72 | end |
---|
| 73 | end |
---|
| 74 | |
---|
| 75 | -- Compile expressions with package names which should be hold. |
---|
| 76 | local holdlist = confgetlist("RPM::Hold") |
---|
| 77 | for i, expr in ipairs(holdlist) do |
---|
| 78 | holdlist[i] = rex.new(expr) |
---|
| 79 | end |
---|
| 80 | |
---|
| 81 | -- Remove packages without any matches in updatelist, or with |
---|
| 82 | -- any matches in holdlist. |
---|
| 83 | for name, _ in pairs(curdups) do |
---|
| 84 | local found = false |
---|
| 85 | for i, expr in ipairs(updatelist) do |
---|
| 86 | if expr:match(name) then |
---|
| 87 | found = true |
---|
| 88 | break |
---|
| 89 | end |
---|
| 90 | end |
---|
| 91 | if found then |
---|
| 92 | for i, expr in ipairs(holdlist) do |
---|
| 93 | if expr:match(name) then |
---|
| 94 | found = false |
---|
| 95 | break |
---|
| 96 | end |
---|
| 97 | end |
---|
| 98 | end |
---|
| 99 | if not found then |
---|
| 100 | curdups[name] = nil |
---|
| 101 | end |
---|
| 102 | end |
---|
| 103 | |
---|
| 104 | -- Mark the newest packages for installation. |
---|
| 105 | for name, _ in pairs(curdups) do |
---|
| 106 | if canddups[name] then |
---|
| 107 | -- Check the best candidate version. |
---|
| 108 | local bestver = nil |
---|
| 109 | for i, ver in ipairs(canddups[name]) do |
---|
| 110 | if not bestver or |
---|
| 111 | verstrcmp(bestver, ver) == -1 then |
---|
| 112 | bestver = ver |
---|
| 113 | end |
---|
| 114 | end |
---|
| 115 | |
---|
| 116 | -- Now check if it's newer than all installed |
---|
| 117 | -- versions. |
---|
| 118 | for i, ver in ipairs(curdups[name]) do |
---|
| 119 | if verstrcmp(ver, bestver) == 1 then |
---|
| 120 | bestver = nil |
---|
| 121 | break |
---|
| 122 | end |
---|
| 123 | end |
---|
| 124 | |
---|
| 125 | -- Finally, mark it for installation. |
---|
| 126 | if bestver then |
---|
| 127 | markinstall(name.."#"..bestver) |
---|
| 128 | end |
---|
| 129 | end |
---|
| 130 | end |
---|
| 131 | end |
---|
| 132 | ----------<end copied block>---------- |
---|
| 133 | |
---|
[227] | 134 | -- Print upgrade/new install/remove/hold package name if exist. |
---|
[175] | 135 | -- *NOTE: newinstall package appeares as upgrade one too. naze... |
---|
| 136 | |
---|
| 137 | local new = {} |
---|
| 138 | local upgrade = {} |
---|
| 139 | local remove = {} |
---|
| 140 | local hold = {} |
---|
[227] | 141 | local dup = {} |
---|
[175] | 142 | |
---|
| 143 | for i, pkg in pairs(pkglist()) do |
---|
[227] | 144 | -- Check new and update package |
---|
[175] | 145 | if statnewinstall(pkg) then |
---|
| 146 | table.insert(new,string.format("new:%s\n",pkgname(pkg))) |
---|
| 147 | elseif statupgrade(pkg) then |
---|
| 148 | table.insert(upgrade,string.format("upgrade:%s\n",pkgname(pkg))) |
---|
| 149 | end |
---|
| 150 | |
---|
[227] | 151 | --- Check remove package |
---|
[175] | 152 | if statremove(pkg) then |
---|
| 153 | table.insert(remove,string.format("remove:%s\n",pkgname(pkg))) |
---|
| 154 | end |
---|
| 155 | |
---|
[227] | 156 | --- Check hold package |
---|
[175] | 157 | if pkgvercur(pkg) ~= nil and pkgvercand(pkg) ~= nil and statupgrade(pkg) == nil then |
---|
| 158 | local found = false |
---|
| 159 | for i, expr in ipairs(holdlist) do |
---|
| 160 | if expr:match(pkgname(pkg)) then |
---|
| 161 | found = true |
---|
| 162 | end |
---|
| 163 | end |
---|
| 164 | if not found then |
---|
| 165 | table.insert(hold,string.format("hold:%s\n",pkgname(pkg))) |
---|
| 166 | end |
---|
| 167 | end |
---|
[227] | 168 | |
---|
| 169 | --- check duplicate package |
---|
| 170 | local name = realname(pkgname(pkg)) |
---|
| 171 | if name then |
---|
| 172 | local found = false |
---|
| 173 | for i, expr in ipairs(updatelist) do |
---|
| 174 | if expr:match(name) then |
---|
| 175 | found = true |
---|
| 176 | break |
---|
| 177 | end |
---|
| 178 | end |
---|
| 179 | if not found then |
---|
| 180 | table.insert(dup,string.format("duplicate:%s\n",pkgname(pkg))) |
---|
| 181 | end |
---|
| 182 | end |
---|
| 183 | |
---|
| 184 | |
---|
[175] | 185 | end |
---|
| 186 | |
---|
| 187 | local function write(index, str) |
---|
| 188 | file:write(str) |
---|
| 189 | end |
---|
| 190 | table.foreach(new, write) |
---|
| 191 | table.foreach(upgrade, write) |
---|
| 192 | table.foreach(remove, write) |
---|
| 193 | table.foreach(hold, write) |
---|
[227] | 194 | table.foreach(dup, write) |
---|
[175] | 195 | file:close() |
---|
| 196 | |
---|
| 197 | os.execute("mv "..filename.." "..basefile.."`id -u`") |
---|