- initial import of revision 374 from cnc
[apt.git] / contrib / log / log.lua
1 -- This script will log the changes being introduced by
2 -- the transaction which is going to be run, and is based
3 -- on an idea of Panu Matilainen. It must be plugged
4 -- in the slots Scripts::PM::Pre/Post.
5 --
6 -- Author: Gustavo Niemeyer <niemeyer@conectiva.com>
7
8 if confget("APT::Log::Enable/b", "false") == "false" then
9     return
10 end
11
12 confset("Dir::Log", "/var/log", true)
13 confset("Dir::Log::transaction", "apt.log", true)
14 filename = confget("Dir::Log::transaction/f", "/var/log/apt.log")
15
16 if script_slot == "Scripts::PM::Pre" then
17     file = io.open(filename, "a+")
18     if not file then
19         print("error: can't open log file at "..filename)
20         return
21     end
22     local removing = {}
23     local installing = {}
24     local reinstalling = {}
25     local downgrading = {}
26     local upgrading = {}
27     for i, pkg in ipairs(pkglist()) do
28         if statkeep(pkg) then
29             -- Do nothing
30         elseif statreinstall(pkg) then
31             table.insert(reinstalling,
32                          string.format("ReInstalling %s %s\n",
33                                        pkgname(pkg),
34                                        verstr(pkgvercur(pkg))))
35         elseif statremove(pkg) then
36             table.insert(removing,
37                          string.format("Removing %s %s\n",
38                                        pkgname(pkg),
39                                        verstr(pkgvercur(pkg))))
40         elseif statnewinstall(pkg) then
41             table.insert(installing,
42                          string.format("Installing %s %s\n",
43                                        pkgname(pkg),
44                                        verstr(pkgverinst(pkg))))
45         elseif statdowngrade(pkg) then
46             table.insert(downgrading,
47                          string.format("Downgrading %s %s to %s\n",
48                                        pkgname(pkg),
49                                        verstr(pkgvercur(pkg)),
50                                        verstr(pkgverinst(pkg))))
51         elseif statupgrade(pkg) then
52             table.insert(upgrading,
53                          string.format("Upgrading %s %s to %s\n",
54                                        pkgname(pkg),
55                                        verstr(pkgvercur(pkg)),
56                                        verstr(pkgverinst(pkg))))
57         end
58     end
59     file:write("Transaction starting at ", os.date(), "\n")
60     local function write(index, str)
61         file:write(str)
62     end
63     table.foreach(removing, write)
64     table.foreach(installing, write)
65     table.foreach(reinstalling, write)
66     table.foreach(downgrading, write)
67     table.foreach(upgrading, write)
68     file:close()
69 elseif script_slot == "Scripts::PM::Post" then
70     if transaction_success then
71         word = "succeeded"
72     else
73         word = "failed"
74     end
75     file = io.open(filename, "a+")
76     file:write("Transaction ", word, " at ", os.date(), "\n")
77     file:close()
78 end
79
80 -- vim:ts=4:sw=4:et