-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.py
executable file
·155 lines (118 loc) · 5.09 KB
/
history.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008-2010, TUBITAK/UEKAE
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#
import os
import time
import gettext
__trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext
import pisilinux.pxml.autoxml as autoxml
import pisilinux.pxml.xmlfile as xmlfile
import pisilinux.context as ctx
__metaclass__ = autoxml.autoxml
class PackageInfo:
a_version = [autoxml.String, autoxml.mandatory]
a_release = [autoxml.String, autoxml.mandatory]
def __str__(self):
# FIXME: Do not get these from the config file
distro_id = ctx.config.values.general.distribution_id
arch = ctx.config.values.general.architecture
return "-".join((self.version, self.release, distro_id, arch))
class Repo:
a_operation = [autoxml.String, autoxml.mandatory]
t_Name = [autoxml.String, autoxml.mandatory]
t_Uri = [autoxml.String, autoxml.mandatory]
def __str__(self):
# "update", "remove", "add"
operation = ""
if self.operation == "update":
return _("%s repository is updated.") % self.name
elif self.operation == "add":
pass # TBD
elif self.operation == "remove":
pass # TBD
class Package:
a_operation = [autoxml.String, autoxml.mandatory]
a_type = [autoxml.String, autoxml.optional]
t_Name = [autoxml.String, autoxml.mandatory]
t_Before = [PackageInfo, autoxml.optional]
t_After = [PackageInfo, autoxml.optional]
def __str__(self):
# "upgrade", "remove", "install", "reinstall", "downgrade"
operation = ""
if self.operation == "upgrade":
if self.type == "delta":
return _("%s is upgraded from %s to %s with delta.") % (self.name, self.before, self.after)
else:
return _("%s is upgraded from %s to %s.") % (self.name, self.before, self.after)
elif self.operation == "remove":
return _("%s %s is removed.") % (self.name, self.before)
elif self.operation == "install":
return _("%s %s is installed.") % (self.name, self.after)
elif self.operation == "reinstall":
return _("%s %s is reinstalled.") % (self.name, self.after)
elif self.operation == "downgrade":
return _("%s is downgraded from %s to %s.") % (self.name, self.before, self.after)
else:
return ""
class Operation:
a_type = [autoxml.String, autoxml.mandatory]
a_date = [autoxml.String, autoxml.mandatory]
a_time = [autoxml.String, autoxml.mandatory]
t_Packages = [ [Package], autoxml.optional, "Package"]
t_Repos = [ [Repo], autoxml.optional, "Repository"]
def __str__(self):
return self.type
class History(xmlfile.XmlFile, metaclass=autoxml.autoxml):
tag = "PISI"
t_Operation = [Operation, autoxml.mandatory]
def create(self, operation):
if operation not in ["upgrade", "remove", "emerge", "install", "snapshot", "takeback", "repoupdate"]:
raise Exception(_("Unknown package operation"))
opno = self._get_latest()
self.histfile = "%s_%s.xml" % (opno, operation)
year, month, day, hour, minute = time.localtime()[0:5]
self.operation.type = operation
self.operation.date = "%s-%02d-%02d" % (year, month, day)
self.operation.time = "%02d:%02d" % (hour, minute)
self.operation.no = opno
def update_repo(self, name, uri, operation=None):
repo = Repo()
repo.operation = operation
repo.name = name
repo.uri = uri
self.operation.repos.append(repo)
# @param otype is currently only used to hold if an upgrade is from "delta"
def add(self, pkgBefore=None, pkgAfter=None, operation=None, otype=None):
if operation not in ["upgrade", "remove", "install", "reinstall", "downgrade", "snapshot"]:
raise Exception(_("Unknown package operation"))
package = Package()
package.operation = operation
package.type = otype
package.name = (pkgAfter and pkgAfter.name) or (pkgBefore and pkgBefore.name)
if not pkgBefore:
package.before = None
if not pkgAfter:
package.after = None
for histInfo, pkgInfo in [(package.before, pkgBefore), (package.after, pkgAfter)]:
if pkgInfo:
histInfo.version = str(pkgInfo.version)
histInfo.release = str(pkgInfo.release)
self.operation.packages.append(package)
def update(self):
self.write(os.path.join("%s/%s", ctx.config.history_dir(), self.histfile))
def _get_latest(self):
files = [h for h in os.listdir(ctx.config.history_dir()) if h.endswith(".xml")]
if not files:
return "001"
files.sort(lambda x,y:int(x.split("_")[0]) - int(y.split("_")[0]))
no, opxml = files[-1].split("_")
return "%03d" % (int(no) + 1)