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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# Copyright 2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$
import errno
import os
from portage.util import grabfile, write_atomic, ensure_dirs
from portage.const import PRIVATE_PATH, USER_CONFIG_PATH
from portage.locks import lockfile, unlockfile
from portage import portage_gid
from portage.sets.base import PackageSet, EditablePackageSet
from portage.sets import SetConfigError
from portage.env.loaders import ItemFileLoader, KeyListFileLoader
from portage.env.validators import ValidAtomValidator
from portage import dep_getkey, cpv_getkey
__all__ = ["StaticFileSet", "ConfigFileSet", "WorldSet"]
class StaticFileSet(EditablePackageSet):
_operations = ["merge", "unmerge"]
def __init__(self, filename):
super(StaticFileSet, self).__init__()
self._filename = filename
self._mtime = None
self.description = "Package set loaded from file %s" % self._filename
self.loader = ItemFileLoader(self._filename, ValidAtomValidator)
metadata = grabfile(self._filename + ".metadata")
key = None
value = []
for line in metadata:
line = line.strip()
if len(line) == 0 and key != None:
setattr(self, key, " ".join(value))
key = None
elif line[-1] == ":" and key == None:
key = line[:-1].lower()
value = []
elif key != None:
value.append(line)
else:
pass
else:
if key != None:
setattr(self, key, " ".join(value))
def write(self):
write_atomic(self._filename, "\n".join(sorted(self._atoms))+"\n")
def load(self):
try:
mtime = os.stat(self._filename).st_mtime
except (OSError, IOError):
mtime = None
if (not self._loaded or self._mtime != mtime):
try:
data, errors = self.loader.load()
for fname in errors:
for e in errors[fname]:
self.errors.append(fname+": "+e)
except EnvironmentError, e:
if e.errno != errno.ENOENT:
raise
del e
data = {}
self._setAtoms(data.keys())
self._mtime = mtime
def singleBuilder(self, options, setconfig):
if not "filename" in options:
raise SetConfigError("no filename specified")
return ConfigFileSet(options[filename])
singleBuilder = classmethod(singleBuilder)
def multiBuilder(self, options, setconfig):
rValue = {}
directory = options.get("directory", \
os.path.join(setconfig.settings["PORTAGE_CONFIGROOT"], USER_CONFIG_PATH.lstrip(os.sep), "sets"))
name_pattern = options.get("name_pattern", "sets/$name")
if not "$name" in name_pattern and not "${name}" in name_pattern:
raise SetConfigError("name_pattern doesn't include $name placeholder")
if os.path.isdir(directory):
for filename in os.listdir(directory):
if filename.endswith(".metadata"):
continue
myname = name_pattern.replace("$name", filename)
myname = myname.replace("${name}", filename)
rValue[myname] = StaticFileSet(os.path.join(directory, filename))
return rValue
multiBuilder = classmethod(multiBuilder)
class ConfigFileSet(PackageSet):
def __init__(self, filename):
super(ConfigFileSet, self).__init__()
self._filename = filename
self.description = "Package set generated from %s" % self._filename
self.loader = KeyListFileLoader(self._filename, ValidAtomValidator)
def load(self):
data, errors = self.loader.load()
self._setAtoms(data.keys())
def singleBuilder(self, options, setconfig):
if not "filename" in options:
raise SetConfigError("no filename specified")
return ConfigFileSet(options[filename])
singleBuilder = classmethod(singleBuilder)
def multiBuilder(self, options, setconfig):
rValue = {}
directory = options.get("directory", \
os.path.join(setconfig.settings["PORTAGE_CONFIGROOT"], USER_CONFIG_PATH.lstrip(os.sep)))
name_pattern = options.get("name_pattern", "sets/package_$suffix")
if not "$suffix" in name_pattern and not "${suffix}" in name_pattern:
raise SetConfigError("name_pattern doesn't include $suffix placeholder")
for suffix in ["keywords", "use", "mask", "unmask"]:
myname = name_pattern.replace("$suffix", suffix)
myname = myname.replace("${suffix}", suffix)
rValue[myname] = ConfigFileSet(os.path.join(directory, "package."+suffix))
return rValue
multiBuilder = classmethod(multiBuilder)
class WorldSet(StaticFileSet):
description = "Set of packages that were directly installed by the user"
def __init__(self, root):
super(WorldSet, self).__init__(os.path.join(os.sep, root, PRIVATE_PATH.lstrip(os.sep), "world"))
self._lock = None
def _ensure_dirs(self):
ensure_dirs(os.path.dirname(self._filename), gid=portage_gid, mode=02750, mask=02)
def lock(self):
self._ensure_dirs()
self._lock = lockfile(self._filename, wantnewlockfile=1)
def unlock(self):
unlockfile(self._lock)
self._lock = None
def cleanPackage(self, vardb, cpv):
self.lock()
worldlist = list(self.getAtoms()) # loads latest from disk
mykey = cpv_getkey(cpv)
newworldlist = []
for x in worldlist:
if dep_getkey(x) == mykey:
matches = vardb.match(x, use_cache=0)
if not matches:
#zap our world entry
pass
elif len(matches) == 1 and matches[0] == cpv:
#zap our world entry
pass
else:
#others are around; keep it.
newworldlist.append(x)
else:
#this doesn't match the package we're unmerging; keep it.
newworldlist.append(x)
self.replace(newworldlist)
self.unlock()
def singleBuilder(self, options, setconfig):
return WorldSet(setconfig.settings["ROOT"])
singleBuilder = classmethod(singleBuilder)
|