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