/[gentoo-src]/portage/pym/portage_contents.py
Gentoo

Contents of /portage/pym/portage_contents.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download) (as text)
Sat Feb 26 06:35:20 2005 UTC (10 years, 3 months ago) by jstubbs
Branch: MAIN
CVS Tags: HEAD
Branch point for: portage_2_1
Changes since 1.3: +2 -1 lines
File MIME type: text/x-python
Brought forward changes from portage_2_0

1 # portage_contents.py -- (Persistent) Contents File Management
2 # Copyright 1998-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo-src/portage/pym/portage_contents.py,v 1.3 2004/10/04 14:07:40 vapier Exp $
5 cvs_id_string="$Id: portage_contents.py,v 1.3.2.1 2005/01/16 02:35:33 carpaski Exp $"[5:-2]
6
7 import os,string,types,sys,copy
8 import portage_exception
9 import portage_const
10
11 #import gettext
12 #gettext_t = gettext.translation('portage.contents', portage_const.LOCALE_DATA_PATH)
13 #_ = gettext_t.ugettext
14 def _(mystr):
15 return mystr
16
17
18 FILES_KEY = "\0FILES\0"
19 OWNER_KEY = "\0OWNER\0"
20
21
22 def ContentsHandler(filename):
23 infile = open(filename)
24 myfiles = []
25 mydirs = []
26
27 mylines = infile.readlines()
28 infile.close()
29 for line in mylines:
30 if line[-1] == '\n':
31 line = line[:-1]
32 parts = string.split(line)
33
34 mytype = parts[0]
35 mytarget = None
36
37 if mytype in ["dir","dev","fif"]:
38 mypath = string.join(parts[1:])
39 elif mytype == "obj":
40 mypath = string.join(parts[1:-2])
41 elif mytype == "sym":
42 sl = string.join(parts[1:-1])
43 sl = string.split(sl, " -> ")
44
45 mypath = sl[0]
46 mytarget = sl[1]
47 else:
48 print _("Unknown type:"),mytype
49
50 if mytype in ["dir"]:
51 mydirs.append(mypath)
52 else:
53 myfiles.append(mypath)
54
55 mydirs.sort()
56 myfiles.sort()
57 return myfiles,mydirs
58
59 class PathLookupTable:
60 """Creates a temporary lookup table for paths from files."""
61 def __init__(self,dbname):
62 #if not self.validLocation(dbname):
63 # raise portage_exception.InvalidLocation, dbname
64 #self.base = copy.deepcopy(dbname)
65
66 self.files = []
67 self.pathtree = {}
68
69 def addFromFile(self, filename, handler):
70 if type(handler) != types.FunctionType:
71 raise portage_exception.IncorrectParameter, _("Handler of type '%(type)s' not 'function'") % {"type": type(handler)}
72
73 filelist,dirlist = handler(filename)
74 filestat = os.stat(filename)
75
76 if type(filelist) != types.ListType:
77 raise portage_exception.InvalidDataType, _("%(handler)s returned an invalid file list") % {"handler": handler.__name__}
78 if type(dirlist) != types.ListType:
79 raise portage_exception.InvalidDataType, _("%(handler)s returned an invalid directory list") % {"handler": handler.__name__}
80
81 for x in filelist:
82 if not x:
83 continue
84 x = os.path.normpath(x)
85 if len(x) > 1:
86 if x[:2] == "//":
87 x = x[1:]
88 if type(x) != types.StringType:
89 raise portage_exception.InvalidDataType, _("%(handler)s returned an invalid subelement in dataset") % {"handler": handler.__name__}
90 xs = string.split(x, "/")
91 self.addFilePath(xs,filename)
92
93 for x in dirlist:
94 if not x:
95 continue
96 x = os.path.normpath(x)
97 if len(x) > 1:
98 if x[:2] == "//":
99 x = x[1:]
100 if type(x) != types.StringType:
101 raise portage_exception.InvalidDataType, _("%(handler)s returned an invalid subelement in dataset") % {"handler": handler.__name__}
102 xs = string.split(x, "/")
103 self.addDirectoryPath(xs,filename)
104
105 def addDirectoryPath(self,split_path, owner):
106 pt = self.pathtree
107 for x in split_path:
108 if x not in pt.keys():
109 pt[x] = {FILES_KEY:{},OWNER_KEY:[]}
110 if owner not in pt[x][OWNER_KEY]:
111 pt[x][OWNER_KEY].append(owner[:])
112 pt = pt[x]
113 return pt
114
115 def addFilePath(self,split_path, owner):
116 pt = self.addDirectoryPath(split_path[:-1], owner)
117 if split_path[-1] not in pt[FILES_KEY]:
118 pt[FILES_KEY][split_path[-1][:]] = []
119 if owner not in pt[FILES_KEY][split_path[-1][:]]:
120 pt[FILES_KEY][split_path[-1][:]].append(owner[:])
121
122 def whoProvides(self,path):
123 if type(path) != types.StringType:
124 raise portage_exception.InvalidData, _("Path passed is not a string: %(path)s") % {"path": path}
125 x = os.path.normpath(path)
126 if x[0:2] == '//':
127 x = x[1:]
128
129 xs = x.split("/")
130 pt = self.pathtree
131 final_dir = xs.pop(-1)
132 for subpath in xs:
133 if subpath in pt.keys():
134 pt = pt[subpath]
135
136 owners = []
137 if final_dir in pt[FILES_KEY]:
138 for x in pt[FILES_KEY][final_dir]:
139 if x not in owners:
140 owners.append(x[:])
141 if final_dir in pt:
142 for x in pt[final_dir][OWNER_KEY]:
143 if x not in owners:
144 owners.append(x[:])
145
146 return owners
147
148
149
150 def test():
151 import os
152 plt = PathLookupTable("spork")
153 for x in os.listdir("/var/db/pkg"):
154 for y in os.listdir("/var/db/pkg/"+x):
155 c_path = "/var/db/pkg/"+x+"/"+y+"/CONTENTS"
156 if os.path.exists(c_path):
157 plt.addFromFile(c_path, ContentsHandler)
158 print "/bin/bash:", plt.whoProvides("/bin/bash")
159 print "/var/spool:", plt.whoProvides("/var/spool")
160 print "/etc/init.d", plt.whoProvides("/etc/init.d")
161 return plt

  ViewVC Help
Powered by ViewVC 1.1.20