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
|
# 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
from portage.locks import lockfile, unlockfile
from portage import portage_gid
from portage.sets.dbapi import PackageSet, EditablePackageSet
from portage.env.config import ConfigLoaderKlass, GenericFile
from portage.env.loaders import ItemFileLoader, KeyListFileLoader
from portage.env.validators import PackagesFileValidator, ValidAtomValidator
class StaticFileSet(EditablePackageSet):
_operations = ["merge", "unmerge"]
def __init__(self, name, filename, data = None):
super(StaticFileSet, self).__init__(name)
self._filename = filename
self._mtime = None
self.description = "Package set loaded from file %s" % self._filename
self.data = data
if data is None:
self.data = ConfigLoaderKlass(ItemFileLoader(filename=self._filename,
validator=PackagesFileValidator))
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:
self.data.load()
except EnvironmentError, e:
if e.errno != errno.ENOENT:
raise
del e
self._setAtoms(self.data.keys())
self._mtime = mtime
class ConfigFileSet(PackageSet):
def __init__(self, name, filename, loader=None, validator=None):
super(ConfigFileSet, self).__init__(name)
self._filename = filename
self.description = "Package set generated from %s" % self._filename
if loader is None:
self.loader = GenericFile(filename=self._filename)
else:
self.loader = loader(filename, validator)
def load(self):
self.loader.load()
self._setAtoms(self.loader.keys())
class WorldSet(StaticFileSet):
description = "Set of packages that were directly installed by the user"
def __init__(self, name, root):
super(WorldSet, self).__init__(name, os.path.join(os.sep, root, PRIVATE_PATH, "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
|