source: projects/update-watch/tags/2.0.0/check-upgrades.lua @ 3774

Revision 3774, 5.6 KB checked in by kazutaka, 13 years ago (diff)

tags/1.xからインポート

  • Property svn:executable set to *
Line 
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
5rex = require("rex_posix")
6
7basefile = "/var/tmp/update-watch-info."
8filename = basefile.."tmp"
9
10-- Function to extract real package name from allow-duplicated packages.
11function realname(name)
12    local s, e, name = string.find(name, "(.+)#")
13    return name
14end
15
16-- Open output file that store list of to-be changed packages,
17file = io.open(filename, "w")
18if not file then
19    print("error: can't open output file at "..filename)
20    return
21end
22
23-- Get list ofmanually held packages and store it to local table.
24-- those packages should be ignored by update-watch.
25
26local holdlist = confgetlist("RPM::Hold") 
27
28for i, expr in ipairs(holdlist) do
29    holdlist[i] = rex.new(expr)
30end
31
32-- Mark whole system for upgrading to enable executing
33-- apt-get upgrade --simulate for non root users.
34
35markupgrade()
36
37-- *NOTE* bellow block is copied from allow-duplicated.lua
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.
46local updatelist = confgetlist("RPM::Allow-Duplicated-Upgrade")
47for i, expr in ipairs(updatelist) do
48    updatelist[i] = rex.new(expr)
49end
50
51if 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
131end
132----------<end copied block>----------
133
134-- Print upgrade/new install/remove/hold package name if exist.
135-- *NOTE: newinstall package appeares as upgrade one too. naze...
136
137local new = {}
138local upgrade = {}
139local remove = {}
140local hold = {}
141local dup = {}
142
143for i, pkg in pairs(pkglist()) do
144    -- Check new and update package
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
151    --- Check remove package
152    if statremove(pkg) then
153        table.insert(remove,string.format("remove:%s\n",pkgname(pkg)))
154    end
155
156    --- Check hold package
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
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   
185end
186
187local function write(index, str)
188    file:write(str)
189end
190table.foreach(new, write)
191table.foreach(upgrade, write)
192table.foreach(remove, write)
193table.foreach(hold, write)
194table.foreach(dup, write)
195file:close()
196
197os.execute("mv "..filename.." "..basefile.."`id -u`")
Note: See TracBrowser for help on using the repository browser.