source: projects/update-watch/trunk/check-upgrades.lua @ 227

Revision 227, 5.3 KB checked in by kazutaka, 15 years ago (diff)

重複バージョン検出ロジックの追加(中)

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