author | Stephen Hahn <sch@sun.com> |
Thu, 21 Jun 2007 23:43:12 -0700 | |
changeset 50 | bfcb1661f019 |
parent 45 | 8a3f00dea14f |
child 51 | d04d55e16d0b |
permissions | -rw-r--r-- |
19 | 1 |
#!/usr/bin/python |
2 |
# |
|
3 |
# CDDL HEADER START |
|
4 |
# |
|
5 |
# The contents of this file are subject to the terms of the |
|
6 |
# Common Development and Distribution License (the "License"). |
|
7 |
# You may not use this file except in compliance with the License. |
|
8 |
# |
|
9 |
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
10 |
# or http://www.opensolaris.org/os/licensing. |
|
11 |
# See the License for the specific language governing permissions |
|
12 |
# and limitations under the License. |
|
13 |
# |
|
14 |
# When distributing Covered Code, include this CDDL HEADER in each |
|
15 |
# file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
16 |
# If applicable, add the following below this CDDL HEADER, with the |
|
17 |
# fields enclosed by brackets "[]" replaced with your own identifying |
|
18 |
# information: Portions Copyright [yyyy] [name of copyright owner] |
|
19 |
# |
|
20 |
# CDDL HEADER END |
|
21 |
# |
|
22 |
||
23 |
# Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
|
24 |
# Use is subject to license terms. |
|
25 |
||
26 | 26 |
import os |
34
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
27 |
import re |
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
28 |
import urllib |
34
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
29 |
|
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
30 |
import pkg.catalog as catalog |
50 | 31 |
import pkg.manifest as manifest |
26 | 32 |
|
19 | 33 |
IMG_ENTIRE = 0 |
34 |
IMG_PARTIAL = 1 |
|
35 |
IMG_USER = 2 |
|
36 |
||
26 | 37 |
img_user_prefix = ".org.opensolaris,pkg" |
30 | 38 |
img_root_prefix = "var/pkg" |
26 | 39 |
|
19 | 40 |
class Image(object): |
41 |
"""An Image object is a directory tree containing the laid-down contents |
|
42 |
of a self-consistent graph of Packages. |
|
43 |
||
44 |
An Image has a root path. |
|
45 |
||
46 |
An Image of type IMG_ENTIRE does not have a parent Image. Other Image |
|
47 |
types must have a parent Image. The external state of the parent Image |
|
48 |
must be accessible from the Image's context, or duplicated within the |
|
49 |
Image (IMG_PARTIAL for zones, for instance). |
|
50 |
||
51 |
The parent of a user Image can be a partial Image. The parent of a |
|
52 |
partial Image must be an entire Image. |
|
53 |
||
54 |
An Image of type IMG_USER stores its external state at self.root + |
|
26 | 55 |
".org.opensolaris,pkg". |
19 | 56 |
|
57 |
An Image of type IMG_ENTIRE or IMG_PARTIAL stores its external state at |
|
58 |
self.root + "/var/pkg". |
|
59 |
||
26 | 60 |
An Image needs to be able to have a different repository set than the |
61 |
system's root Image. |
|
62 |
||
29
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
63 |
Directory layout |
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
64 |
|
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
65 |
$IROOT/catalog |
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
66 |
Directory containing catalogs for URIs of interest. Filename is |
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
67 |
the escaped URI of the catalog. |
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
68 |
|
30 | 69 |
$IROOT/file |
70 |
Directory containing file hashes of installed packages. |
|
71 |
||
72 |
$IROOT/pkg |
|
73 |
Directory containing manifests and states of installed packages. |
|
74 |
||
19 | 75 |
XXX Root path probably can't be absolute, so that we can combine or |
30 | 76 |
reuse Image contents. |
26 | 77 |
|
78 |
XXX Image file format? Image file manipulation API?""" |
|
79 |
||
80 |
def __init__(self): |
|
81 |
self.type = None |
|
82 |
self.root = None |
|
30 | 83 |
self.imgdir = None |
26 | 84 |
self.repo_uris = [] |
85 |
self.filter_tags = {} |
|
86 |
||
32
c26788b7217c
begin client install; sequences are now timestamps; skeleton ELF module
Stephen Hahn <sch@sun.com>
parents:
30
diff
changeset
|
87 |
self.catalogs = {} |
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
88 |
self.authorities = {} |
32
c26788b7217c
begin client install; sequences are now timestamps; skeleton ELF module
Stephen Hahn <sch@sun.com>
parents:
30
diff
changeset
|
89 |
|
50 | 90 |
self.attrs = {} |
91 |
||
92 |
self.attrs["Policy-Require-Optional"] = False |
|
93 |
self.attrs["Policy-Pursue-Latest"] = True |
|
94 |
||
26 | 95 |
def find_parent(self): |
96 |
# Ascend from current working directory to find first |
|
97 |
# encountered image. |
|
98 |
while True: |
|
99 |
d = os.getcwd() |
|
19 | 100 |
|
26 | 101 |
if os.path.isdir("%s/%s" % (d, img_user_prefix)): |
102 |
# XXX Look at image file to determine filter |
|
103 |
# tags and repo URIs. |
|
104 |
self.type = IMG_USER |
|
105 |
self.root = d |
|
30 | 106 |
self.imgdir = "%s/%s" % (d, img_user_prefix) |
26 | 107 |
return |
108 |
elif os.path.isdir("%s/%s" % (d, img_root_prefix)): |
|
109 |
# XXX Look at image file to determine filter |
|
110 |
# tags and repo URIs. |
|
111 |
# XXX Look at image file to determine if this |
|
112 |
# image is a partial image. |
|
113 |
self.type = IMG_ENTIRE |
|
114 |
self.root = d |
|
30 | 115 |
self.imgdir = "%s/%s" % (d, img_root_prefix) |
26 | 116 |
return |
117 |
||
118 |
assert d != "/" |
|
119 |
||
120 |
os.chdir("..") |
|
121 |
||
29
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
122 |
def mkdirs(self): |
30 | 123 |
if not os.path.isdir(self.imgdir + "/catalog"): |
124 |
os.makedirs(self.imgdir + "/catalog") |
|
125 |
if not os.path.isdir(self.imgdir + "/file"): |
|
126 |
os.makedirs(self.imgdir + "/file") |
|
127 |
if not os.path.isdir(self.imgdir + "/pkg"): |
|
128 |
os.makedirs(self.imgdir + "/pkg") |
|
29
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
129 |
|
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
130 |
def set_attrs(self, type, root): |
19 | 131 |
self.type = type |
22
379f0f6809ff
pkgsend open, pkgsend add file, pkgsend close; Transaction object
"Stephen Hahn <sch@sun.com>"
parents:
19
diff
changeset
|
132 |
self.root = root |
379f0f6809ff
pkgsend open, pkgsend add file, pkgsend close; Transaction object
"Stephen Hahn <sch@sun.com>"
parents:
19
diff
changeset
|
133 |
if self.type == IMG_USER: |
30 | 134 |
self.imgdir = self.root + "/" + img_user_prefix |
22
379f0f6809ff
pkgsend open, pkgsend add file, pkgsend close; Transaction object
"Stephen Hahn <sch@sun.com>"
parents:
19
diff
changeset
|
135 |
else: |
30 | 136 |
self.imgdir = self.root + "/" + img_root_prefix |
19 | 137 |
|
29
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
138 |
def get_root(self): |
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
139 |
return self.root |
f621f1df0849
fix install target; correct imports for module renames
"Stephen Hahn <sch@sun.com>"
parents:
26
diff
changeset
|
140 |
|
19 | 141 |
def set_resource(self, resource): |
142 |
return |
|
143 |
||
32
c26788b7217c
begin client install; sequences are now timestamps; skeleton ELF module
Stephen Hahn <sch@sun.com>
parents:
30
diff
changeset
|
144 |
def reload_catalogs(self): |
c26788b7217c
begin client install; sequences are now timestamps; skeleton ELF module
Stephen Hahn <sch@sun.com>
parents:
30
diff
changeset
|
145 |
cdir = "%s/%s" % (self.imgdir, "catalog") |
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
146 |
for cf in os.listdir(cdir): |
36
b31b3587fb1c
begin client catalog evaluation; fix bugs from refactoring
"Stephen Hahn <sch@sun.com>"
parents:
34
diff
changeset
|
147 |
c = catalog.Catalog() |
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
148 |
c.load("%s/%s" % (cdir, cf)) |
34
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
149 |
|
36
b31b3587fb1c
begin client catalog evaluation; fix bugs from refactoring
"Stephen Hahn <sch@sun.com>"
parents:
34
diff
changeset
|
150 |
self.catalogs[cf] = c |
32
c26788b7217c
begin client install; sequences are now timestamps; skeleton ELF module
Stephen Hahn <sch@sun.com>
parents:
30
diff
changeset
|
151 |
|
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
152 |
# XXX XXX |
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
153 |
# build up authorities |
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
154 |
|
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
155 |
def display_catalogs(self): |
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
156 |
for c in self.catalogs.values(): |
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
157 |
c.display() |
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
158 |
|
50 | 159 |
def get_matching_pkgs(self, pfmri): |
160 |
"""Exact matches to the given FMRI. Returns a list of (catalog, |
|
161 |
pkg) pairs.""" |
|
34
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
162 |
|
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
163 |
m = [] |
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
164 |
for c in self.catalogs.values(): |
50 | 165 |
m.extend([(c, p) \ |
166 |
for p in c.get_matching_pkgs(pfmri, None)]) |
|
34
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
167 |
|
8b9613402116
refactor catalog and package to be common; improve bump-server
Stephen Hahn <sch@sun.com>
parents:
33
diff
changeset
|
168 |
return m |
32
c26788b7217c
begin client install; sequences are now timestamps; skeleton ELF module
Stephen Hahn <sch@sun.com>
parents:
30
diff
changeset
|
169 |
|
50 | 170 |
def get_regex_matching_fmris(self, regex): |
171 |
"""FMRIs matching the given regular expression. Returns of a |
|
172 |
list of (catalog, PkgFmri) pairs.""" |
|
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
173 |
|
50 | 174 |
m = [] |
175 |
for c in self.catalogs.values(): |
|
176 |
m.extend([(c, p) \ |
|
177 |
for p in c.get_regex_matching_fmris(regex)]) |
|
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
178 |
|
50 | 179 |
return m |
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
180 |
|
50 | 181 |
def has_manifest(self, fmri): |
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
182 |
mpath = fmri.get_dir_path() |
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
183 |
|
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
184 |
local_mpath = "%s/pkg/%s/manifest" % (self.imgdir, mpath) |
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
185 |
|
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
186 |
if (os.path.exists(local_mpath)): |
50 | 187 |
return True |
188 |
||
189 |
return False |
|
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
190 |
|
50 | 191 |
def get_manifest(self, fmri): |
192 |
m = manifest.Manifest() |
|
193 |
mcontent = file("%s/pkg/%s/manifest" % |
|
194 |
(self.imgdir, fmri.get_dir_path())).read() |
|
195 |
m.set_content(mcontent) |
|
196 |
return m |
|
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
197 |
|
50 | 198 |
def get_version_installed(self, fmri): |
199 |
istate = "%s/pkg/%s/installed" % (self.imgdir, fmri.get_dir_path(True)) |
|
200 |
if not os.path.exists(istate): |
|
201 |
raise LookupError, "no installed version of '%s'" % fmri |
|
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
202 |
|
50 | 203 |
vs = os.readlink(istate) |
204 |
||
205 |
return fmri.PkgFmri("%s/%s" % (fmri, vs)) |
|
45
8a3f00dea14f
catalog refresh; basic catalog listing; precise manifest pulls
Stephen Hahn <sch@sun.com>
parents:
39
diff
changeset
|
206 |
|
50 | 207 |
if __name__ == "__main__": |
208 |
# XXX Need to construct a trivial image and catalog. |
|
209 |
pass |