Gentoo Archives: gentoo-commits

From: "Zac Medico (zmedico)" <zmedico@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] portage r10864 - main/trunk/pym/portage/dbapi
Date: Tue, 01 Jul 2008 08:10:30
Message-Id: E1KDawM-0001od-2F@stork.gentoo.org
1 Author: zmedico
2 Date: 2008-07-01 08:10:17 +0000 (Tue, 01 Jul 2008)
3 New Revision: 10864
4
5 Modified:
6 main/trunk/pym/portage/dbapi/vartree.py
7 Log:
8 Split out a write_contents() function and a vardbapi.removeFromContents()
9 function. This is refactoring of code from the blocker file collision
10 contents handling in dblink.treewalk(). Also, there is a new
11 dblink._match_contents() method derived from isowner(). It returns the
12 exact path from the contents file that matches the given path, regardless
13 of path differences due to things such as symlinks.
14
15
16 Modified: main/trunk/pym/portage/dbapi/vartree.py
17 ===================================================================
18 --- main/trunk/pym/portage/dbapi/vartree.py 2008-07-01 07:26:37 UTC (rev 10863)
19 +++ main/trunk/pym/portage/dbapi/vartree.py 2008-07-01 08:10:17 UTC (rev 10864)
20 @@ -891,6 +891,35 @@
21 return dblink(category, pf, self.root,
22 self.settings, vartree=self.vartree)
23
24 + def removeFromContents(self, pkg, paths, relative_paths=True):
25 + """
26 + @param pkg: cpv for an installed package
27 + @type pkg: string
28 + @param paths: paths of files to remove from contents
29 + @type paths: iterable
30 + """
31 + if not hasattr(pkg, "getcontents"):
32 + pkg = self._dblink(pkg)
33 + root = self.root
34 + root_len = len(root) - 1
35 + new_contents = pkg.getcontents().copy()
36 + contents_key = None
37 +
38 + for filename in paths:
39 + filename = normalize_path(filename)
40 + if relative_paths:
41 + relative_filename = filename
42 + else:
43 + relative_filename = filename[root_len:]
44 + contents_key = pkg._match_contents(relative_filename, root)
45 + if contents_key:
46 + del new_contents[contents_key]
47 +
48 + if contents_key:
49 + f = atomic_ofstream(os.path.join(pkg.dbdir, "CONTENTS"))
50 + write_contents(new_contents, root, f)
51 + f.close()
52 +
53 class _owners_cache(object):
54 """
55 This class maintains an hash table that serves to index package
56 @@ -1922,7 +1951,7 @@
57 #remove self from vartree database so that our own virtual gets zapped if we're the last node
58 self.vartree.zap(self.mycpv)
59
60 - def isowner(self,filename, destroot):
61 + def isowner(self, filename, destroot):
62 """
63 Check if a file belongs to this package. This may
64 result in a stat call for the parent directory of
65 @@ -1941,12 +1970,25 @@
66 1. True if this package owns the file.
67 2. False if this package does not own the file.
68 """
69 + return bool(self._match_contents(filename, destroot))
70 +
71 + def _match_contents(self, filename, destroot):
72 + """
73 + The matching contents entry is returned, which is useful
74 + since the path may differ from the one given by the caller,
75 + due to symlinks.
76 +
77 + @rtype: String
78 + @return: the contents entry corresponding to the given path, or False
79 + if the file is not owned by this package.
80 + """
81 +
82 destfile = normalize_path(
83 os.path.join(destroot, filename.lstrip(os.path.sep)))
84
85 pkgfiles = self.getcontents()
86 if pkgfiles and destfile in pkgfiles:
87 - return True
88 + return destfile
89 if pkgfiles:
90 basename = os.path.basename(destfile)
91 if self._contents_basenames is None:
92 @@ -1996,7 +2038,7 @@
93 for p_path in p_path_list:
94 x = os.path.join(p_path, basename)
95 if x in pkgfiles:
96 - return True
97 + return x
98
99 return False
100
101 @@ -2661,33 +2703,8 @@
102 contents = self.getcontents()
103 destroot_len = len(destroot) - 1
104 for blocker in blockers:
105 - blocker_contents = blocker.getcontents()
106 - collisions = []
107 - for filename in blocker_contents:
108 - relative_filename = filename[destroot_len:]
109 - if self.isowner(relative_filename, destroot):
110 - collisions.append(filename)
111 - if not collisions:
112 - continue
113 - for filename in collisions:
114 - del blocker_contents[filename]
115 - f = atomic_ofstream(os.path.join(blocker.dbdir, "CONTENTS"))
116 - for filename in sorted(blocker_contents):
117 - entry_data = blocker_contents[filename]
118 - entry_type = entry_data[0]
119 - relative_filename = filename[destroot_len:]
120 - if entry_type == "obj":
121 - entry_type, mtime, md5sum = entry_data
122 - line = "%s %s %s %s\n" % \
123 - (entry_type, relative_filename, md5sum, mtime)
124 - elif entry_type == "sym":
125 - entry_type, mtime, link = entry_data
126 - line = "%s %s -> %s %s\n" % \
127 - (entry_type, relative_filename, link, mtime)
128 - else: # dir, dev, fif
129 - line = "%s %s\n" % (entry_type, relative_filename)
130 - f.write(line)
131 - f.close()
132 + self.vartree.dbapi.removeFromContents(blocker, iter(contents),
133 + relative_paths=False)
134
135 self.vartree.dbapi._add(self)
136 contents = self.getcontents()
137 @@ -3098,6 +3115,27 @@
138 "Is this a regular package (does it have a CATEGORY file? A dblink can be virtual *and* regular)"
139 return os.path.exists(os.path.join(self.dbdir, "CATEGORY"))
140
141 +def write_contents(contents, root, f):
142 + """
143 + Write contents to any file like object. The file will be left open.
144 + """
145 + root_len = len(root) - 1
146 + for filename in sorted(contents):
147 + entry_data = contents[filename]
148 + entry_type = entry_data[0]
149 + relative_filename = filename[root_len:]
150 + if entry_type == "obj":
151 + entry_type, mtime, md5sum = entry_data
152 + line = "%s %s %s %s\n" % \
153 + (entry_type, relative_filename, md5sum, mtime)
154 + elif entry_type == "sym":
155 + entry_type, mtime, link = entry_data
156 + line = "%s %s -> %s %s\n" % \
157 + (entry_type, relative_filename, link, mtime)
158 + else: # dir, dev, fif
159 + line = "%s %s\n" % (entry_type, relative_filename)
160 + f.write(line)
161 +
162 def tar_contents(contents, root, tar, protect=None, onProgress=None):
163 from portage.util import normalize_path
164 import tarfile
165
166 --
167 gentoo-commits@l.g.o mailing list