/[gli]/branches/overhaul/src/GLIPortage.py
Gentoo

Diff of /branches/overhaul/src/GLIPortage.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/GLIPortage.py Revision 1183 branches/overhaul/src/GLIPortage.py Revision 1700
3# This source code is distributed under the terms of version 2 of the GNU 3# This source code is distributed under the terms of version 2 of the GNU
4# General Public License as published by the Free Software Foundation, a copy 4# General Public License as published by the Free Software Foundation, a copy
5# of which can be found in the main directory of this project. 5# of which can be found in the main directory of this project.
6Gentoo Linux Installer 6Gentoo Linux Installer
7 7
8$Id: GLIPortage.py,v 1.3 2005/12/23 22:39:29 agaffney Exp $ 8$Id: GLIPortage.py,v 1.57 2006/09/13 18:29:13 agaffney Exp $
9""" 9"""
10 10
11import re
11import os 12import os
12import re 13import sys
13import GLIUtility 14import GLIUtility
14 15from GLIException import GLIException
15class MissingPackagesError(Exception):
16 pass
17
18class depgraph:
19
20 def __init__(self):
21 self.graph = {}
22
23 def add(self, node, parent=None):
24 if node not in self.graph:
25 self.graph[node] = [[], []]
26 if parent and parent not in self.graph[node][0]:
27 if parent not in self.graph:
28 self.graph[parent] = [[], []]
29 self.graph[node][0].append(parent)
30 self.graph[parent][1].append(node)
31
32 def remove(self, node):
33 for parent in self.graph[node][0]:
34 self.graph[parent][1].remove(node)
35 for child in self.graph[node][1]:
36 self.graph[child][0].remove(node)
37 return self.graph.pop(node)
38
39 def has_node(self, node):
40 return node in self.graph
41
42 def leaf_nodes(self):
43 return [node for node in self.graph if not self.graph[node][1]]
44
45 def node_count(self):
46 return len(self.graph)
47
48 def important_node(self):
49 important_node = None
50 importance = 0
51 for node in self.graph:
52 if len(self.graph[node][0]) > importance:
53 importance = len(self.graph[node][0])
54 important_node = node
55 return important_node
56 16
57class GLIPortage(object): 17class GLIPortage(object):
58 18
59 def __init__(self, chroot_dir, grp_install, logger, debug): 19 def __init__(self, chroot_dir, logger, debug, cc, compile_logfile):
60 os.environ['ROOT'] = chroot_dir
61 import portage, portage_dep
62 self.vdb = portage.db["/"]["vartree"].dbapi
63 self.tree = portage.db[chroot_dir]["porttree"].dbapi
64 self._chroot_dir = chroot_dir 20 self._chroot_dir = chroot_dir
65 self._logger = logger 21 self._logger = logger
66 self._debug = debug 22 self._debug = debug
67 self._grp_install = grp_install 23 self._cc = cc
24 self._compile_logfile = compile_logfile
68 25
69 def resolve_deps(self, dep_list): 26 def get_deps(self, pkgs, grp_install=False):
70 if dep_list and dep_list[0] == "||":
71 for dep in dep_list[1:]:
72 if isinstance(dep, list):
73 try:
74 atoms = resolve_deps(dep)
75 except MissingPackagesError:
76 continue
77 else:
78 atoms = [dep]
79 all_found = True
80 for atom in atoms:
81 if not atom.startswith("!") and not self.vdb.match(atom):
82 all_found = False
83 break
84 if all_found:
85 return atoms
86 raise MissingPackagesError(dep_list)
87 atoms = []
88 for dep in dep_list:
89 if isinstance(dep, list):
90 atoms.extend(resolve_deps(dep))
91 elif not dep.startswith("!"):
92 if not self.vdb.match(dep):
93 raise MissingPackagesError([dep])
94 atoms.append(dep)
95 return atoms
96
97 def calc_required_pkgs(self, atom, graph, parent=None):
98 pkg = portage.best(self.vdb.match(atom))
99 if not pkg:
100 raise MissingPackagesError([atom])
101 if pkg == parent:
102 return
103 already_processed = graph.has_node(pkg)
104 graph.add(pkg, parent)
105 if already_processed:
106 return
107 useflags = self.vdb.aux_get(pkg, ["USE"])[0].split()
108 rdep_raw = " ".join(self.vdb.aux_get(pkg, ["RDEPEND", "PDEPEND"]))
109 rdep_struct = portage_dep.use_reduce(portage_dep.paren_reduce(rdep_raw), uselist=useflags)
110 rdep_struct = portage.dep_virtual(portage_dep.dep_opconvert(rdep_struct), portage.settings)
111 rdep_atoms = portage.unique_array(resolve_deps(rdep_struct))
112 for atom in rdep_atoms:
113 calc_required_pkgs(atom, graph, pkg)
114
115 def prune_existing(self, graph):
116 db = portage.db[portage.root]["vartree"].dbapi
117 for atom in db.cp_all():
118 for pkg in db.match(atom):
119 if graph.has_node(pkg):
120 graph.remove(pkg)
121
122 def get_deps(self, pkgs):
123 if not self._grp_install:
124 del(os.environ['ROOT'])
125 return GLIUtility.spawn("emerge -p " + pkgs + r" | grep -e '^\[[a-z]' | cut -d ']' -f2 | sed -e 's:^ ::' -e 's: .\+$::'", chroot=self._chroot_dir, return_output=True)[1].split("\n")
126 os.environ['ROOT'] = self._chroot_dir
127 else:
128 pkglist = [] 27 pkglist = []
129 graph = depgraph() 28 if isinstance(pkgs, str):
29 pkgs = pkgs.split()
130 for pkg in pkgs.split(): 30 for pkg in pkgs:
131 if not self.vdb.match(pkg): 31 if not pkg: continue
132 self._logger.log("get_deps(): " + pkg + " is not available via GRP...ignoring") 32 if self._debug: self._logger.log("get_deps(): pkg is " + pkg)
133 continue 33 if not grp_install or not self.get_best_version_vdb(pkg):
134 self.calc_required_pkgs(pkg, graph) 34 if self._debug: self._logger.log("get_deps(): grabbing compile deps")
135 while graph.node_count(): 35 tmppkglist = GLIUtility.spawn("emerge -p " + pkg + r" 2>/dev/null | grep -e '^\[[a-z]' | cut -d ']' -f2 | sed -e 's:^ ::' -e 's: .\+$::'", chroot=self._chroot_dir, return_output=True)[1].strip().split("\n")
136 leaf_nodes = graph.leaf_nodes() 36 else:
137 if not leaf_nodes: 37 if self._debug: self._logger.log("get_deps(): grabbing binary deps")
138 node = graph.important_node() 38 # The runtimedeps.py script generates a package install order that is *very* different from emerge itself
39# tmppkglist = GLIUtility.spawn("python ../../runtimedeps.py " + self._chroot_dir + " " + pkg, return_output=True)[1].strip().split("\n")
40 tmppkglist = []
41 for tmppkg in GLIUtility.spawn("emerge -p " + pkg + r" 2>/dev/null | grep -e '^\[[a-z]' | cut -d ']' -f2 | sed -e 's:^ ::' -e 's: .\+$::'", chroot=self._chroot_dir, return_output=True)[1].strip().split("\n"):
42 if self._debug: self._logger.log("get_deps(): looking at " + tmppkg)
43 if self.get_best_version_vdb("=" + tmppkg):
44 if self._debug: self._logger.log("get_deps(): package " + tmppkg + " in host vdb...adding to tmppkglist")
45 tmppkglist.append(tmppkg)
46 if self._debug: self._logger.log("get_deps(): deplist for " + pkg + ": " + str(tmppkglist))
47 for tmppkg in tmppkglist:
48 if self._debug: self._logger.log("get_deps(): checking to see if " + tmppkg + " is already in pkglist")
49 if not tmppkg in pkglist and not self.get_best_version_vdb_chroot("=" + tmppkg):
50 if self._debug: self._logger.log("get_deps(): adding " + tmppkg + " to pkglist")
139 pkglist.append(node) 51 pkglist.append(tmppkg)
140 graph.remove(node) 52 if self._debug: self._logger.log("get_deps(): pkglist is " + str(pkglist))
141 continue 53 if not pkglist:
142 pkglist.extend(leaf_nodes) 54 raise GLIException("GetDepListError", 'fatal', 'get_deps', "Dep list is empty. This usually means there is no portage tree in the chroot")
143 for node in leaf_nodes:
144 graph.remove(node)
145 return pkglist 55 return pkglist
146 56
147 def copy_pkg_to_chroot(self, package): 57 def parse_vdb_contents(self, file):
58 entries = []
59 try:
60 vdbfile = open(file, "r")
61 except:
62 return entries
63 for line in vdbfile.readlines():
64 parts = line.strip().split(" ")
65 if parts[0] == "obj":
66 entries.append(parts[1])
67# elif parts[0] == "dir":
68# entries.append(parts[1] + "/")
69 elif parts[0] == "sym":
70 entries.append(" ".join(parts[1:4]))
71 entries.sort()
72 return entries
73
74 def copy_pkg_to_chroot(self, package, use_root=False, ignore_missing=False):
148 symlinks = { '/bin/': '/mnt/livecd/bin/', '/boot/': '/mnt/livecd/boot/', '/lib/': '/mnt/livecd/lib/', 75 symlinks = { '/bin': '/mnt/livecd/bin/', '/boot': '/mnt/livecd/boot/', '/lib': '/mnt/livecd/lib/',
149 '/opt/': '/mnt/livecd/opt/', '/sbin/': '/mnt/livecd/sbin/', '/usr/': '/mnt/livecd/usr/', 76 '/opt': '/mnt/livecd/opt/', '/sbin': '/mnt/livecd/sbin/', '/usr': '/mnt/livecd/usr/',
150 '/etc/gconf/': '/usr/livecd/gconf/' } 77 '/etc/gconf': '/usr/livecd/gconf/' }
78
79 tmpdir = "/var/tmp"
80 image_dir = tmpdir + "/portage/" + package.split("/")[1] + "/image"
81 root_cmd = ""
82 tmp_chroot_dir = self._chroot_dir
83 portage_tmpdir = "/var/tmp"
84 vdb_dir = "/var/db/pkg/"
85 if use_root:
86 root_cmd = "ROOT=" + self._chroot_dir
87 tmp_chroot_dir = ""
88 portage_tmpdir = self._chroot_dir + "/var/tmp"
89 vdb_dir = self._chroot_dir + "/var/db/pkg/"
90
91 # Create /tmp, /var/tmp, and /var/lib/portage with proper permissions
92 oldumask = os.umask(0)
93 if not os.path.exists(self._chroot_dir + "/tmp"):
94 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): /tmp doesn't exist in chroot...creating with proper permissions")
95 try:
96 os.mkdir(self._chroot_dir + "/tmp", 01777)
97 except:
98 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Failed to create /tmp in chroot")
99 if not os.path.exists(self._chroot_dir + "/var/tmp"):
100 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): /var/tmp doesn't exist in chroot...creating with proper permissions")
101 try:
102 os.mkdir(self._chroot_dir + "/var", 0755)
103 except:
104 pass
105 try:
106 os.mkdir(self._chroot_dir + "/var/tmp", 01777)
107 except:
108 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Failed to create /var/tmp in chroot")
109 if not os.path.exists(self._chroot_dir + "/var/lib/portage"):
110 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): /var/lib/portage doesn't exist in chroot...creating with proper permissions")
111 try:
112 os.mkdir(self._chroot_dir + "/var/lib", 0755)
113 except:
114 pass
115 try:
116 os.mkdir(self._chroot_dir + "/var/lib/portage", 02750)
117 except:
118 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Failed to create /var/lib/portage in chroot")
119 os.umask(oldumask)
120
121 # Check to see if package is actually in vdb
122 if not GLIUtility.is_file("/var/db/pkg/" + package):
123 if ignore_missing:
124 if self._debug:
125 self._logger.log("DEBUG: copy_pkg_to_chroot(): package " + package + " does not have a vdb entry but ignore_missing=True...ignoring error")
126 return
127 else:
128 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "There is no vdb entry for " + package)
151 129
152 # Copy the vdb entry for the package from the LiveCD to the chroot 130 # Copy the vdb entry for the package from the LiveCD to the chroot
153 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): copying vdb entry for " + package) 131 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): copying vdb entry for " + package)
154 if not GLIUtility.exitsuccess(GLIUtility.spawn("mkdir -p " + self._chroot_dir + "/var/db/pkg/" + package + " && cp -a /var/db/pkg/" + package + "/* " + self._chroot_dir + "/var/db/pkg/" + package)): 132 if not GLIUtility.exitsuccess(GLIUtility.spawn("mkdir -p " + self._chroot_dir + "/var/db/pkg/" + package + " && cp -a /var/db/pkg/" + package + "/* " + self._chroot_dir + "/var/db/pkg/" + package, logfile=self._compile_logfile, append_log=True)):
155 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not copy vdb entry for " + package) 133 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not copy vdb entry for " + package)
156 134
135 # Create the image dir in the chroot
136 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running 'mkdir -p " + self._chroot_dir + image_dir + "'")
137 if not GLIUtility.exitsuccess(GLIUtility.spawn("mkdir -p " + self._chroot_dir + image_dir, logfile=self._compile_logfile, append_log=True)):
138 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not create image dir for " + package)
139
157 # Create list of files for tar to work with from CONTENTS file in vdb entry 140 # Create list of files for tar to work with from CONTENTS file in vdb entry
158 entries = GLIUtility.parse_vdb_contents("/var/db/pkg/" + package + "/CONTENTS") 141 entries = self.parse_vdb_contents("/var/db/pkg/" + package + "/CONTENTS")
142 if not entries:
143 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): no files for " + package + "...skipping tar and symlink fixup")
144 else:
159 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot: files for " + package + ": " + str(entries)) 145# if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot: files for " + package + ": " + str(entries))
160 try: 146 try:
161 tarfiles = open("/tmp/tarfilelist", "w") 147 tarfiles = open("/tmp/tarfilelist", "w")
162 for entry in entries: 148 for entry in entries:
163 parts = entry.split(" ") 149 parts = entry.split(" ")
164 # Hack for symlink crappiness 150# # Hack for symlink crappiness
165 for symlink in symlinks: 151# for symlink in symlinks:
166 if parts[0].startswith(symlink): 152# if parts[0].startswith(symlink):
167 parts[0] = symlinks[symlink] + parts[0][len(symlink):] 153# parts[0] = symlinks[symlink] + parts[0][len(symlink):]
168 tarfiles.write(parts[0] + "\n") 154 tarfiles.write(parts[0] + "\n")
169 tarfiles.close() 155 tarfiles.close()
170 except: 156 except:
171 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not create filelist for " + package) 157 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not create filelist for " + package)
172 158
173 # Use tar to transfer files into IMAGE directory 159 # Use tar to transfer files into IMAGE directory
174 tmpdir = "/var/tmp/portage"
175 image_dir = tmpdir + "/" + package.split("/")[1] + "/image"
176 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running 'mkdir -p " + self._chroot_dir + image_dir + " && tar -c --files-from=/tmp/tarfilelist --no-recursion 2>/dev/null | tar -C " + self._chroot_dir + image_dir + " -x'") 160 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running 'tar -cp --files-from=/tmp/tarfilelist --no-recursion 2>/dev/null | tar -C " + self._chroot_dir + image_dir + " -xp'")
177 if not GLIUtility.exitsuccess(GLIUtility.spawn("mkdir -p " + self._chroot_dir + image_dir + " && tar -c --files-from=/tmp/tarfilelist --no-recursion 2>/dev/null | tar -C " + self._chroot_dir + image_dir + " -x")): 161 if not GLIUtility.exitsuccess(GLIUtility.spawn("tar -cp --files-from=/tmp/tarfilelist --no-recursion 2>/dev/null | tar -C " + self._chroot_dir + image_dir + " -xp", logfile=self._compile_logfile, append_log=True)):
178 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute tar for " + package) 162 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute tar for " + package)
179 163
164 # Fix mode, uid, and gid of directories
165 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running find " + self._chroot_dir + image_dir + " -mindepth 1 -type d 2>/dev/null | sed -e 's:^" + self._chroot_dir + image_dir + "::' | grep -v '^$'")
166 dirlist = GLIUtility.spawn("find " + self._chroot_dir + image_dir + " -mindepth 1 -type d 2>/dev/null | sed -e 's:^" + self._chroot_dir + image_dir + "::' | grep -v '^$'", return_output=True)[1].strip().split("\n")
167 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): found the following directories: " + str(dirlist))
168 if not dirlist or dirlist[0] == "":
169 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "directory list entry for " + package + "...this shouldn't happen!")
170 for dir in dirlist:
171 dirstat = os.stat(dir)
172 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): setting mode " + str(dirstat[0]) + " and uid/gid " + str(dirstat[4]) + "/" + str(dirstat[5]) + " for directory " + self._chroot_dir + image_dir + dir)
173 os.chown(self._chroot_dir + image_dir + dir, dirstat[4], dirstat[5])
174 os.chmod(self._chroot_dir + image_dir + dir, dirstat[0])
175
180 # More symlink crappiness hacks 176# # More symlink crappiness hacks
181 for symlink in symlinks: 177# for symlink in symlinks:
182 if GLIUtility.is_file(self._chroot_dir + image_dir + symlinks[symlink]): 178## if GLIUtility.is_file(self._chroot_dir + image_dir + symlinks[symlink]):
183# parts[0] = symlinks[symlink] + parts[len(symlink):] 179# if os.path.islink(self._chroot_dir + image_dir + symlink):
184 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): fixing /usr/livecd/gconf/ stuff in " + image_dir + " for " + package) 180# if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): fixing " + symlink + " symlink ickiness stuff in " + image_dir + " for " + package)
181# GLIUtility.spawn("rm " + self._chroot_dir + image_dir + symlink)
185 if not GLIUtility.exitsuccess(GLIUtility.spawn("mv " + self._chroot_dir + image_dir + symlinks[symlink] + " " + self._chroot_dir + image_dir + symlink)): 182# if not GLIUtility.exitsuccess(GLIUtility.spawn("mv " + self._chroot_dir + image_dir + symlinks[symlink] + " " + self._chroot_dir + image_dir + symlink)):
186 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not fix /usr/livecd/gconf/ stuff for " + package) 183# raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not fix " + symlink + " symlink ickiness for " + package)
187 184
188 # Run pkg_setup 185 # Run pkg_setup
189 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running pkg_setup for " + package) 186 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running pkg_setup for " + package)
190 if not GLIUtility.exitsuccess(GLIUtility.spawn("env ROOT=" + self._chroot_dir + " PORTAGE_TMPDIR=" + self._chroot_dir + tmpdir + " ebuild " + self._chroot_dir + "/var/db/pkg/" + package + "/*.ebuild setup")): 187 if not GLIUtility.exitsuccess(GLIUtility.spawn("env " + root_cmd + " PORTAGE_TMPDIR=" + portage_tmpdir + " FEATURES=noauto ebuild " + vdb_dir + package + "/*.ebuild setup", chroot=tmp_chroot_dir, logfile=self._compile_logfile, append_log=True)):
191 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute pkg_setup for " + package) 188 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute pkg_setup for " + package)
192
193 # Run qmerge
194# if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running qmerge for " + package)
195# if not GLIUtility.exitsuccess(GLIUtility.spawn("env ROOT=" + self._chroot_dir + " PORTAGE_TMPDIR=" + self._chroot_dir + tmpdir + " ebuild " + self._chroot_dir + "/var/db/pkg/" + package + "/*.ebuild qmerge")):
196# raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute qmerge for " + package)
197 189
198 # Run pkg_preinst 190 # Run pkg_preinst
199 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running preinst for " + package) 191 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running preinst for " + package)
200 if not GLIUtility.exitsuccess(GLIUtility.spawn("env ROOT=" + self._chroot_dir + " PORTAGE_TMPDIR=" + self._chroot_dir + tmpdir + " ebuild " + self._chroot_dir + "/var/db/pkg/" + package + "/*.ebuild preinst")): 192 if not GLIUtility.exitsuccess(GLIUtility.spawn("env " + root_cmd + " PORTAGE_TMPDIR=" + portage_tmpdir + " FEATURES=noauto ebuild " + vdb_dir + package + "/*.ebuild preinst", chroot=tmp_chroot_dir, logfile=self._compile_logfile, append_log=True)):
201 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute preinst for " + package) 193 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute preinst for " + package)
202 194
203 # Copy files from image_dir to chroot 195 # Copy files from image_dir to chroot
196 if not entries:
197 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): no files for " + package + "...skipping copy from image dir to /")
198 else:
204 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): copying files from " + image_dir + " to / for " + package) 199 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): copying files from " + image_dir + " to / for " + package)
205 if not GLIUtility.exitsuccess(GLIUtility.spawn("cp -a " + self._chroot_dir + image_dir + "/* " + self._chroot_dir)): 200# if not GLIUtility.exitsuccess(GLIUtility.spawn("cp -a " + self._chroot_dir + image_dir + "/* " + self._chroot_dir)):
201 if not GLIUtility.exitsuccess(GLIUtility.spawn("tar -C " + self._chroot_dir + image_dir + "/ -cp . | tar -C " + self._chroot_dir + "/ -xp", logfile=self._compile_logfile, append_log=True)):
206 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not copy files from " + image_dir + " to / for " + package) 202 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not copy files from " + image_dir + " to / for " + package)
207 203
208 # Run pkg_postinst 204 # Run pkg_postinst
209 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running postinst for " + package) 205 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running postinst for " + package)
210 if not GLIUtility.exitsuccess(GLIUtility.spawn("env ROOT=" + self._chroot_dir + " PORTAGE_TMPDIR=" + self._chroot_dir + tmpdir + " ebuild " + "/var/db/pkg/" + package + "/*.ebuild postinst")): 206 if not GLIUtility.exitsuccess(GLIUtility.spawn("env " + root_cmd + " PORTAGE_TMPDIR=" + portage_tmpdir + " FEATURES=noauto ebuild " + vdb_dir + package + "/*.ebuild postinst", chroot=tmp_chroot_dir, logfile=self._compile_logfile, append_log=True)):
211 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute postinst for " + package) 207 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not execute postinst for " + package)
212 208
213 # Remove image_dir 209 # Remove image_dir
214 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): removing + " + image_dir + " for " + package) 210 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): removing " + image_dir + " for " + package)
215 if not GLIUtility.exitsuccess(GLIUtility.spawn("rm -rf " + self._chroot_dir + image_dir)): 211 if not GLIUtility.exitsuccess(GLIUtility.spawn("rm -rf " + self._chroot_dir + image_dir, logfile=self._compile_logfile, append_log=True)):
216 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not remove + " + image_dir + " for " + package) 212 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not remove + " + image_dir + " for " + package)
213
214 # Run env-update
215 if not use_root:
216 if self._debug: self._logger.log("DEBUG: copy_pkg_to_chroot(): running env-update inside chroot")
217 if not GLIUtility.exitsuccess(GLIUtility.spawn("env-update", chroot=self._chroot_dir, logfile=self._compile_logfile, append_log=True)):
218 raise GLIException("CopyPackageToChrootError", 'fatal', 'copy_pkg_to_chroot', "Could not run env-update for " + package)
217 219
218 def add_pkg_to_world(self, package): 220 def add_pkg_to_world(self, package):
219 if package.find("/") == -1: 221 if package.find("/") == -1:
220 package = GLIUtility.spawn("portageq best_version / " + package, chroot=self._chroot_dir, return_output=True)[1].strip() 222 package = self.get_best_version_vdb_chroot(package)
221 if not package: return False 223 if not package: return False
222 expr = re.compile('^(.+?)(-\d.+)?$') 224 expr = re.compile('^=?(.+?/.+?)(-\d.+)?$')
223 res = expr.match(package) 225 res = expr.match(package)
224 if res: 226 if res:
225 GLIUtility.spawn("echo " + res.group(1) + " >> " + self._chroot_dir + "/var/lib/portage/world") 227 GLIUtility.spawn("echo " + res.group(1) + " >> " + self._chroot_dir + "/var/lib/portage/world")
226 228
227 def get_best_version_vdb(self, package): 229 def get_best_version_vdb(self, package):
228 return portage.best(vdb.match(package)) 230 if package.startswith('='):
231 package = package[1:]
232 if GLIUtility.is_file("/var/db/pkg/" + package):
233 return package
234 else:
235 return ""
236 else:
237 return GLIUtility.spawn("portageq best_version / " + package, return_output=True)[1].strip()
229 238
239 def get_best_version_vdb_chroot(self, package):
240 if package.startswith('='):
241 package = package[1:]
242 if GLIUtility.is_file(self._chroot_dir + "/var/db/pkg/" + package):
243 return package
244 else:
245 return ""
246 else:
247 return GLIUtility.spawn("portageq best_version / " + package, chroot=self._chroot_dir, return_output=True)[1].strip()
248
230 def get_best_version_tree(self, package): 249# def get_best_version_tree(self, package):
231 return portage.best(tree.match(package)) 250# return portage.best(tree.match(package))
251
252 def emerge(self, packages, grp_install=False, add_to_world=True):
253 if isinstance(packages, str):
254 packages = packages.split()
255 self._cc.addNotification("progress", (0, "Calculating dependencies for " + " ".join(packages)))
256 pkglist = self.get_deps(packages, grp_install)
257 if self._debug: self._logger.log("install_packages(): pkglist is " + str(pkglist))
258 for i, pkg in enumerate(pkglist):
259 if not pkg: continue
260 if self._debug: self._logger.log("install_packages(): processing package " + pkg)
261 self._cc.addNotification("progress", (float(i) / len(pkglist), "Emerging " + pkg + " (" + str(i+1) + "/" + str(len(pkglist)) + ")"))
262 if not grp_install or not self.get_best_version_vdb("=" + pkg):
263 status = GLIUtility.spawn("emerge -1 =" + pkg, display_on_tty8=True, chroot=self._chroot_dir, logfile=self._compile_logfile, append_log=True)
264# status = self._emerge("=" + pkg)
265 if not GLIUtility.exitsuccess(status):
266 raise GLIException("EmergePackageError", "fatal", "emerge", "Could not emerge " + pkg + "!")
267 else:
268# try:
269 self.copy_pkg_to_chroot(pkg)
270# except:
271# raise GLIException("EmergePackageError", "fatal", "emerge", "Could not emerge " + pkg + "!")
272 self._cc.addNotification("progress", (float(i+1) / len(pkglist), "Done emerging " + pkg + " (" + str(i+1) + "/" + str(len(pkglist)) + ")"))
273 if add_to_world:
274 for package in packages:
275 self.add_pkg_to_world(package)
276
277
278def usage(progname):
279 print """
280Usage: %s [-c|--chroot-dir <chroot directory>] [-g|--grp] [-s|--stage3] [-h|--help]
281
282Options:
283
284 -c|--chroot-dir Specifies the directory where your chroot is. This is
285 "/mnt/gentoo" by default.
286
287 -g|--grp Install specified packages and dependencies into chroot
288 by using files from the LiveCD.
289
290 -s|--stage3 Create a stage3 equivelant in the chroot directory by using
291 files from the LiveCD.
292
293 -k|--kernel Install kernel and modules from LiveCD into the chroot
294
295 -h|--help Display this help
296""" % (progname)
297
298if __name__ == "__main__":
299 chroot_dir = "/mnt/gentoo"
300 mode = None
301 grp_packages = []
302 progname = sys.argv.pop(0)
303 while len(sys.argv):
304 arg = sys.argv.pop(0)
305 if arg == "-c" or arg == "--chroot-dir":
306 chroot_dir = sys.argv.pop(0)
307 elif arg == "-g" or arg == "--grp":
308 mode = "grp"
309 elif arg == "-s" or arg == "--stage3":
310 mode = "stage3"
311 elif arg == "-k" or arg == "--kernel":
312 mode = "kernel"
313 elif arg == "-h" or arg == "--help":
314 usage(progname)
315 sys.exit(0)
316 elif arg[0] == "-":
317 usage(progname)
318 sys.exit(1)
319 else:
320 grp_packages.append(arg)
321
322 gliportage = GLIPortage(chroot_dir, True, None, False, None, None)
323 if mode == "stage3":
324 if not GLIUtility.is_file("/usr/livecd/systempkgs.txt"):
325 print "Required file /usr/livecd/systempkgs.txt does not exist!"
326 sys.exit(1)
327 try:
328 syspkgs = open("/usr/livecd/systempkgs.txt", "r")
329 systempkgs = syspkgs.readlines()
330 syspkgs.close()
331 except:
332 print "Could not open /usr/livecd/systempkgs.txt!"
333 sys.exit(1)
334
335 # Pre-create /lib (and possible /lib32 and /lib64)
336 if os.path.islink("/lib") and os.readlink("/lib") == "lib64":
337 if not GLIUtility.exitsuccess(GLIUtility.spawn("mkdir " + chroot_dir + "/lib64 && ln -s lib64 " + chroot_dir + "/lib")):
338 print "Could not precreate /lib64 dir and /lib -> /lib64 symlink"
339 sys.exit(1)
340
341 syspkglen = len(systempkgs)
342 for i, pkg in enumerate(systempkgs):
343 pkg = pkg.strip()
344 print "Copying " + pkg + " (" + str(i+1) + "/" + str(syspkglen) + ")"
345 gliportage.copy_pkg_to_chroot(pkg, True, ignore_missing=True)
346 GLIUtility.spawn("cp /etc/make.conf " + chroot_dir + "/etc/make.conf")
347 GLIUtility.spawn("ln -s `readlink /etc/make.profile` " + chroot_dir + "/etc/make.profile")
348 GLIUtility.spawn("cp -f /etc/inittab.old " + chroot_dir + "/etc/inittab")
349
350 # Nasty, nasty, nasty hack because vapier is a tool
351 for tmpfile in ("/etc/passwd", "/etc/group", "/etc/shadow"):
352 GLIUtility.spawn("grep -ve '^gentoo' " + tmpfile + " > " + chroot_dir + tmpfile)
353
354 chrootscript = r"""
355 #!/bin/bash
356
357 source /etc/make.conf
358 export LDPATH="/usr/lib/gcc-lib/${CHOST}/$(cd /usr/lib/gcc-lib/${CHOST} && ls -1 | head -n 1)"
359
360 ldconfig $LDPATH
361 gcc-config 1
362 env-update
363 source /etc/profile
364 modules-update
365 [ -f /usr/bin/binutils-config ] && binutils-config 1
366 source /etc/profile
367 #mount -t proc none /proc
368 #cd /dev
369 #/sbin/MAKEDEV generic-i386
370 #umount /proc
371 [ -f /lib/udev-state/devices.tar.bz2 ] && tar -C /dev -xjf /lib/udev-state/devices.tar.bz2
372 """
373 script = open(chroot_dir + "/tmp/extrastuff.sh", "w")
374 script.write(chrootscript)
375 script.close()
376 GLIUtility.spawn("chmod 755 /tmp/extrastuff.sh && /tmp/extrastuff.sh", chroot=chroot_dir)
377 GLIUtility.spawn("rm -rf /var/tmp/portage/* /usr/portage /tmp/*", chroot=chroot_dir)
378 print "Stage3 equivelant generation complete!"
379 elif mode == "grp":
380 for pkg in grp_packages:
381 if not gliportage.get_best_version_vdb(pkg):
382 print "Package " + pkg + " is not available for install from the LiveCD"
383 continue
384 pkglist = gliportage.get_deps(pkg, True)
385 for i, tmppkg in enumerate(pkglist):
386 print "Copying " + tmppkg + " (" + str(i+1) + "/" + str(len(pkglist)) + ")"
387 gliportage.copy_pkg_to_chroot(tmppkg)
388 gliportage.add_pkg_to_world(pkg)
389 print "GRP install complete!"
390 elif mode == "kernel":
391 kernelpkg = gliportage.get_best_version_vdb("sys-kernel/livecd-kernel")
392 gliportage.copy_pkg_to_chroot(kernelpkg)
393 print "LiveCD kernel installed!"
394 else:
395 print "You must specify an operating mode (-g or -s)!"
396 usage(progname)
397 sys.exit(1)

Legend:
Removed from v.1183  
changed lines
  Added in v.1700

  ViewVC Help
Powered by ViewVC 1.1.20