/[gli]/trunk/src/GLIUtility.py
Gentoo

Contents of /trunk/src/GLIUtility.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1221 - (show annotations) (download) (as text)
Fri Jan 6 16:39:49 2006 UTC (14 years, 8 months ago) by agaffney
File MIME type: text/x-python
File size: 26979 byte(s)
  src/GLIUtility.py:
  move parse_vdb_contents() to GLIPortage
  src/GLIPortage.py:
  remove all symlink fixups

1 """
2 # Copyright 1999-2005 Gentoo Foundation
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
5 # of which can be found in the main directory of this project.
6 Gentoo Linux Installer
7
8 The GLIUtility module contians all utility functions used throughout GLI.
9 """
10
11 import string, os, re, shutil, sys, random, commands, crypt, pty, select
12 from GLIException import *
13
14 ##
15 # Check to see if a string is actually a string, and if it is not null. Returns bool.
16 # @param string_a string to be checked.
17 def is_realstring(string):
18 # Make sure it is a string
19 if not isinstance(string, (str, unicode)):
20 return False
21
22 return True
23
24 ##
25 # Checks to see if x is a numeral by doing a type conversion.
26 # @param x value to be checked
27 def is_numeric(x):
28 try:
29 float(x)
30 except ValueError:
31 return False
32 else:
33 return True
34
35 ##
36 # Check to see if a string is a valid ip. Returns bool.
37 # @param ip ip to be checked.
38 def is_ip(ip):
39 # Make sure it is a string
40 if not is_realstring(ip):
41 return False
42
43 # Compile the regular expression that validates an IP. It will also check for valid ranges.
44 expr = re.compile('(([0-9]|[01]?[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}([0-9]|[01]?[0-9]{2}|2([0-4][0-9]|5[0-5]))$')
45
46 # Run the test.
47 res = expr.match(ip)
48
49 # Return True only if there are results.
50 return(res != None)
51
52 ##
53 # Check to see if mac is a valid MAC address. Make sure use format_mac
54 # before using this function. Returns bool.
55 # @param mac mac address to be checked.
56 def is_mac(mac):
57 expr = re.compile('([0-9A-F]{2}:){5}[0-9A-F]{2}')
58 res = expr.match(mac)
59 return(res != None)
60
61 ##
62 # Format's a mac address properly. Returns the correctly formatted MAC. (a string)
63 # @param mac mac address to be formatted
64 def format_mac(mac):
65 mac = string.replace(mac, '-', ':')
66 mac = string.upper(mac)
67
68 mac = string.split(mac, ':')
69 for i in range(0, len(mac)):
70 if len(mac[i]) < 2:
71 mac[i] = "0" + mac[i]
72 return string.join(mac, ":")
73
74 ##
75 # Removes leading zero's from an IP address. For example
76 # trim_ip('192.168.01.002') => '192.168.1.2'
77 # @param ip IP address to be trimmed
78 def trim_ip(ip):
79 # Remove leading zero's on the first octet
80 ip = re.sub('^0{1,2}','',ip)
81
82 # Remove leading zero's from the other octets
83 res = re.sub('((?<=\.)(00|0)(?P<num>\d))','\g<num>',ip)
84
85 return(res)
86
87 ##
88 # Check to see if the string passed is a valid device. Returns bool.
89 # @param device device to be checked
90 def is_device(device):
91 # Make sure it is a string
92 if not is_realstring(device):
93 return False
94
95 # Make sure the string starts with /dev/
96 if device[0:5] != '/dev/':
97 return False
98
99 # Check to make sure the device exists
100 return os.access(device, os.F_OK)
101
102 ##
103 # Check to see if the string is a valid hostname. Returns bool.
104 # @param hostname host to be checked
105 def is_hostname(hostname):
106 # Make sure it is a string
107 if not is_realstring(hostname):
108 return False
109
110 expr = re.compile('^([a-zA-Z0-9-_\.])+\.[a-zA-Z]{2,4}$')
111 res = expr.match(hostname)
112
113 return(res != None)
114
115 ##
116 # Check to see if the string is a valid path. Returns bool.
117 # @param path Path to be checked.
118 def is_path(path):
119 # Make sure it is a string
120 if not is_realstring(path):
121 return False
122
123 # Create a regular expression that matches all words and the symbols '-_./' _ is included in the \w
124 expr = re.compile('^[\w\.\-\/~]+$')
125
126 # Run the match
127 res = expr.match(path)
128
129 # Return True only if there are results
130 return(res != None)
131
132 ##
133 # Check to see if the string is a valid file. Returns bool.
134 # @param file file to be checked for validity.
135 def is_file(file):
136 # Make sure it is a string
137 if not is_realstring(file):
138 return False
139
140 # Check to make sure the device exists
141 return os.access(file, os.F_OK)
142
143 ##
144 # Parse a URI. Returns a tuple (protocol, username, password, host, port, path)
145 # Returns None if URI is invalid.
146 # @param uri URI to be parsed
147 def parse_uri(uri):
148 # Compile the regex
149 expr = re.compile('(\w+)://(?:([^:@]+)(?::([^@]+))?@)?(?:([a-zA-Z0-9.-]+)(?::(\d+))?)?(/.*)')
150
151 # Run it against the URI
152 res = expr.match(uri)
153
154 if not res:
155 # URI doesn't match regex and therefore is invalid
156 return None
157
158 # Get tuple of matches
159 # 0 - Protocol
160 # 1 - Username
161 # 2 - Password
162 # 3 - Host
163 # 4 - Port
164 # 5 - Path
165 uriparts = res.groups()
166 return uriparts
167
168 ##
169 # Check to see if the string is a valid URI. Returns bool.
170 # @param uri URI to be validated
171 # @param checklocal=True Whether to look for a local uri.
172 def is_uri(uri, checklocal=True):
173 # Make sure it is a string
174 if not is_realstring(uri):
175 return False
176
177 # Set the valid uri types
178 valid_uri_types = ('ftp', 'rsync', 'http', 'file', 'https', 'scp')
179
180 # Parse the URI
181 uriparts = parse_uri(uri)
182 if not uriparts:
183 # Invalid URI
184 return False
185
186 # Check for valid uri type
187 if not uriparts[0] in valid_uri_types:
188 return False
189
190 # If checklocal and the URI is a local file, check to see if the file exists
191 if uriparts[0] == "file" and checklocal:
192 if not is_file(uriparts[5]):
193 return False
194
195 return True
196
197 ##
198 # Converts a string to a boolean value. anything not "True" is deemed false.
199 # @param input must be a string so it can be converted to boolean.
200 def strtobool(input):
201 if type(input) != str:
202 raise GLIException("GLIUtilityError", 'fatal','strtobool',"The input must be a string!")
203
204 if string.lower(input) == 'true':
205 return True
206 else:
207 return False
208
209 ##
210 # Check to see if device is a valid ethernet device. Returns bool.
211 # @param device device to be checked
212 def is_eth_device(device):
213 # Make sure it is a string
214 if not is_realstring(device):
215 return False
216
217 # Old way w/ reg ex here:
218 # Create a regular expression to test the specified device.
219 #expr = re.compile('^(eth|wlan|ppp)([0-9]{1,2})(:[0-9]{1,2})?$')
220 # Run the match
221 #res = expr.match(device)
222 # Return True only if there are results
223 #return(res != None)
224
225 status, output = spawn("/sbin/ifconfig -a | grep -e '^[A-Za-z]'| cut -d ' ' -f 1 | grep '"+ device + "'", return_output=True)
226 if output:
227 return True
228 return False
229
230 ##
231 # Will return a list of devices found in ifconfig.
232 def get_eth_devices():
233 status, output = spawn("/sbin/ifconfig -a | grep -e '^[A-Za-z]'| cut -d ' ' -f 1", return_output=True)
234 return output.split()
235
236 ##
237 # Checks to see if device is a valid NFS device
238 # @param device device to be checked
239 def is_nfs(device):
240 if not is_realstring(device):
241 return False
242
243 colon_location = device.find(':')
244
245 if colon_location == -1:
246 return False
247
248 host = device[:colon_location]
249 path = device[colon_location+1:]
250
251 return((is_ip(host) or is_hostname(host)) and is_path(path))
252
253 ##
254 # Sets the network ip (used for the livecd environment)
255 # @param dev device to be configured
256 # @param ip ip address of device
257 # @param broadcast broadcast address of device
258 # @param netmask netmask address of device
259 def set_ip(dev, ip, broadcast, netmask):
260 if not is_ip(ip) or not is_ip(netmask) or not is_ip(broadcast):
261 raise GLIException("GLIUtilityError", 'fatal','set_ip', ip + ", " + netmask + "and, " + broadcast + "must be a valid IP's!")
262 if not is_eth_device(dev):
263 raise GLIException("GLIUtilityError", 'fatal','set_ip', dev + "is not a valid ethernet device!")
264
265 options = "%s inet %s broadcast %s netmask %s" % (dev, ip, broadcast, netmask)
266
267 status = spawn("ifconfig " + options)
268
269 if not exitsuccess(status):
270 return False
271
272 return True
273
274 ##
275 # Sets the default route (used for the livecd environment)
276 # @param route ip addresss of gateway.
277 def set_default_route(route):
278 if not is_ip(route):
279 raise GLIException("GLIUtilityError", 'fatal', 'set_default_route', route + " is not an ip address!")
280 status = spawn("route add default gw " + route)
281
282 if not exitsuccess(status):
283 return False
284
285 return True
286
287 ##
288 # Will run a command with various flags for the style of output and logging.
289 #
290 # @param cmd The command to be run
291 # @param quiet=False Whether or not to filter output to /dev/null
292 # @param logfile=None if provied will log output to the given filename
293 # @param display_on_tty8=False will output to tty8 instead of the screen.
294 # @param chroot=None will run the command inside the new chroot env.
295 # @param append_log=False whether to start over on the logfile or append.
296 # @param return_output=False Returns the output along with the exit status
297 def spawn(cmd, quiet=False, logfile=None, display_on_tty8=False, chroot=None, append_log=False, return_output=False, linecount=0, match=None, cc=None, status_message=None):
298 # quiet and return_output really do the same thing. One of them need to be removed.
299 if chroot:
300 wrapper = open(chroot+"/var/tmp/spawn.sh", "w")
301 wrapper.write("#!/bin/bash -l\n" + cmd + "\nexit $?\n")
302 wrapper.close()
303 cmd = "chmod a+x " + chroot + "/var/tmp/spawn.sh && chroot " + chroot + " /var/tmp/spawn.sh 2>&1"
304 else:
305 cmd += " 2>&1 "
306
307 output = ""
308
309 if logfile:
310 if append_log:
311 fd_logfile = open(logfile,'a')
312 else:
313 fd_logfile = open(logfile,'w')
314
315 if display_on_tty8:
316 fd_tty = open('/dev/tty8','w')
317
318 # open a read only pipe
319 ro_pipe = os.popen(cmd, 'r')
320
321 # read a line from the pipe and loop until
322 # pipe is empty
323 # data = ro_pipe.readline()
324 seenlines = 0
325 last_percent = 0
326
327 while 1:
328 data = ro_pipe.read(16384)
329 if not data:
330 if linecount and cc:
331 cc.addNotification("progress", (1, status_message))
332 break
333
334 # print "DEBUG: spawn(): data is " + str(len(data)) + " bytes long"
335
336 if logfile:
337 fd_logfile.write(data)
338 # fd_logfile.flush()
339
340 if display_on_tty8:
341 fd_tty.write(data)
342 fd_tty.flush()
343
344 if return_output:
345 output += data
346
347 if linecount and cc:
348 lastpos = -1
349 while 1:
350 lastpos = data.find("\n", lastpos + 1)
351 if lastpos == -1: break
352 seenlines += 1
353 percent = float(seenlines) / linecount
354 # print "DEBUG: spawn(): seenlines=" + str(seenlines) + ", linecount=" + str(linecount) + ", percent=" + str(percent)
355 if int(percent * 100) >= (last_percent + 5):
356 last_percent = int(percent * 100)
357 cc.addNotification("progress", (percent, status_message))
358 # print "DEBUG: spawn(): send notification " + str((percent, status_message))
359
360 # data = ro_pipe.readline()
361
362 # close the file descriptors
363 if logfile: fd_logfile.close()
364 if display_on_tty8: fd_tty.close()
365
366 # close the pipe and save return value
367 ret = ro_pipe.close() or 0
368
369 if return_output:
370 return ret, output
371 else:
372 return ret
373
374 ##
375 # Will check the status of a spawn result to see if it did indeed return successfully.
376 # @param status Parameter description
377 def exitsuccess(status):
378 if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0:
379 return True
380 return False
381
382 ##
383 # Will produce a bash shell with a special prompt for the installer.
384 def spawn_bash():
385 os.putenv("PROMPT_COMMAND","echo \"Type 'exit' to return to the installer.\"")
386 status = spawn("bash")
387 return status
388
389 ##
390 # Will download or copy a file/uri to a location
391 # @param uri uri to be fetched.
392 # @param path destination for the file.
393 def get_uri(uri, path):
394 uri = uri.strip()
395 status = 0
396
397 if re.match('^(ftp|http(s)?)://',uri):
398 status = spawn("wget --quiet " + uri + " -O " + path)
399
400 elif re.match('^rsync://', uri):
401 status = spawn("rsync --quiet " + uri + " " + path)
402
403 elif uri.startswith("scp://"):
404 # Get tuple of matches
405 # 0 - Protocol
406 # 1 - Username
407 # 2 - Password
408 # 3 - Host
409 # 4 - Port
410 # 5 - Path
411 uriparts = parse_uri(uri)
412 scpcmd = "scp "
413 if uriparts[4]:
414 scpcmd += "-P " + uriparts[4] + " "
415 if uriparts[1]:
416 scpcmd += uriparts[1] + "@"
417 scpcmd += uriparts[3] + ":" + uriparts[5] + " " + path
418 pid, child_fd = pty.fork()
419 if not pid:
420 os.execvp("scp", scpcmd.split())
421 else:
422 while 1:
423 r, w, e = select.select([child_fd], [], [])
424 if child_fd in r:
425 try:
426 data = os.read(child_fd, 1024)
427 except:
428 pid2, status = os.waitpid(pid, 0)
429 break
430 if data.endswith("assword: "):
431 if uriparts[2]:
432 os.write(child_fd, uriparts[2] + "\n")
433 else:
434 os.write(child_fd, "\n")
435 elif data.endswith("Are you sure you want to continue connecting (yes/no)? "):
436 os.write(child_fd, "yes\n")
437 else:
438 pid2, status = os.waitpid(pid, os.WNOHANG)
439 if pid2:
440 break
441
442 elif re.match('^file://', uri):
443 r_file = uri[7:]
444 if os.path.isfile(r_file):
445 shutil.copy(r_file, path)
446 if not os.path.isfile(path):
447 raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "Cannot copy " + r_file + " to " + path)
448 else:
449 # Just in case a person forgets file://
450 if os.path.isfile(uri):
451 shutil.copy(uri, path)
452 if not os.path.isfile(path):
453 raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "Cannot copy " + r_file + " to " + path)
454 else:
455 raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "File does not exist or URI is invalid!")
456
457 if exitsuccess(status) and is_file(path):
458 return True
459
460 return False
461
462 ##
463 # Pings a host. Used to test network connectivity.
464 # @param host host to be pinged.
465 def ping(host):
466 host = str(host)
467 if not (is_hostname(host) or is_ip(host)):
468 return False #invalid IP or hostname
469 status = spawn("ping -n -c 2 " + host)
470 if not exitsuccess(status):
471 return False
472 return True
473
474 ##
475 # Pass in the eth device's number (0, 1, 2, etc).
476 # Returns network information in a tuple.
477 # Order is hw_addr, ip_addr, mask, bcast, route, and
478 # whether it's up (True or False).
479 # @param device device to gather info from.
480 def get_eth_info(device):
481 """Pass in the eth device's number (0, 1, 2, etc).
482 Returns network information in a tuple.
483 Order is hw_addr, ip_addr, mask, bcast, route, and
484 whether it's up (True or False).
485 """
486
487 hw_addr = 'None'
488 ip_addr = 'None'
489 mask = 'None'
490 bcast = 'None'
491 gw = 'None'
492 up = False
493
494 if len(str(device)) == 1:
495 device = "eth" + str(device)
496
497 if not is_eth_device(device):
498 raise GLIException("GLIUtilityError", 'fatal', "get_eth_info", device + " is not a valid ethernet device!")
499
500 status, device_info = spawn("/sbin/ifconfig " + device, return_output=True)
501 if exitsuccess(status):
502 for line in device_info.splitlines():
503 line = line.strip()
504 if 'HWaddr' in line:
505 hw_addr = line.split('HWaddr',1)[1].strip()
506 if 'inet addr' in line:
507 ip_addr = line.split(' ')[0].split(':')[1]
508 if 'Bcast' in line:
509 bcast = line.split(' ')[1].split(':')[1]
510 if 'Mask' in line:
511 mask = line.split(' ')[2].split(':')[1]
512 if line.startswith('UP'):
513 up = True
514 else:
515 raise GLIException("GLIUtilityError", 'fatal', "get_eth_info", device_info)
516 gw = spawn(r"/sbin/route -n | grep -e '^0\.0\.0\.0' | sed -e 's:^0\.0\.0\.0 \+::' -e 's: \+.\+$::'", return_output=True)[1].strip()
517
518 return (hw_addr, ip_addr, mask, bcast, gw, up)
519
520 ##
521 # Will take a uri and get and unpack a tarball into the destination.
522 # @param tarball_uri URI of tarball
523 # @param target_directory destination
524 # @param temp_directory="/tmp" a temporary location (used for dealing with the
525 # ramdisk size limitations of the livecd env.
526 # @param keep_permissions=False Whether or not to keep permissions (-p)
527 def fetch_and_unpack_tarball(tarball_uri, target_directory, temp_directory="/tmp", keep_permissions=False, cc=None):
528 "Fetches a tarball from tarball_uri and extracts it into target_directory"
529
530 # Get tarball info
531 tarball_filename = tarball_uri.split("/")[-1]
532
533 # Get the tarball
534 if not get_uri(tarball_uri, temp_directory + "/" + tarball_filename):
535 raise GLIException("GLIUtilityError", 'fatal', 'fetch_and_unpack_tarball',"Could not fetch " + tarball_uri)
536
537 # Reset tar options
538 tar_options = "xv"
539
540 # If the tarball is bzip'd
541 if tarball_filename.split(".")[-1] == "tbz" or tarball_filename.split(".")[-1] == "bz2":
542 format_option = "j"
543
544 # If the tarball is gzip'd
545 elif tarball_filename.split(".")[-1] == "tgz" or tarball_filename.split(".")[-1] == "gz":
546 format_option = "z"
547
548 tar_options += format_option
549
550 # If we want to keep permissions
551 if keep_permissions:
552 tar_options = tar_options + "p"
553
554 # Get number of files in tarball
555 tarfiles = 0
556 if cc:
557 tarfiles = int(spawn("tar -t" + format_option + "f " + temp_directory + "/" + tarball_filename + " 2>/dev/null | wc -l", return_output=True)[1].strip())
558
559 # Unpack the tarball
560 exitstatus = spawn("tar -" + tar_options + " -f " + temp_directory + "/" + tarball_filename + " -C " + target_directory, display_on_tty8=True, logfile="/tmp/compile_output.log", append_log=True, linecount=tarfiles, cc=cc, status_message="Unpacking " + tarball_filename) # change this to the logfile variable
561
562 if not exitsuccess(exitstatus):
563 raise GLIException("GLIUtilityError", 'fatal', 'fetch_and_unpack_tarball',"Could not unpack " + tarball_uri + " to " + target_directory)
564
565 ##
566 # OLD Will generate a random password. Used when the livecd didn't auto-scramble the root password.
567 # can probably be removed but is good to keep around.
568 def generate_random_password():
569 s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%^&*[]{}-=+_,|'\"<>:/"
570 s = list(s)
571
572 for i in range(0,len(s)/2):
573 x = random.randint(0,len(s)-1)
574 y = random.randint(0,len(s)-1)
575 tmp = s[x]
576 s[x] = s[y]
577 s[y] = tmp
578
579 passwd = ""
580 for i in range(0,random.randint(8,12)):
581 passwd += s[i]
582
583 return passwd
584
585 ##
586 # Will grab a value from a specified file after sourcing it
587 # @param filename file to get the value from
588 # @param value value to look for
589 def get_value_from_config(filename, value):
590 #OLD WAY: return string.strip(commands.getoutput("source " + filename + " && echo $" + value))
591 status, output = spawn("source " + filename + " && echo $" + value, return_output=True)
592 return string.strip(output)
593
594
595 ##
596 # Will take a password and return it hashed in md5 format
597 # @param password the password to be hashed
598 def hash_password(password):
599 salt = "$1$"
600 chars = "./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
601 for i in range(0, 8):
602 salt += chars[random.randint(0, len(chars)-1)]
603 salt += "$"
604 passwd_hash = crypt.crypt(password, salt)
605
606 return passwd_hash
607
608 ##
609 # Returns the real name (manufacturer and model) of a network interface
610 # @param interface Name of interface (like in ifconfig)
611 def get_interface_realname(interface):
612 # TODO: rewrite with 2.4 support
613 if is_file("/sys/class/net/" + interface + "/device"):
614 return spawn("lspci | grep $(basename $(readlink /sys/class/net/" + interface + r"/device)) | sed -e 's|^.\+ Ethernet controller: ||'", return_output=True)[1].strip()
615 else:
616 return "No Information Found"
617
618 def list_stage_tarballs_from_mirror(mirror, arch, subarch):
619 return spawn("wget -O - " + mirror + "/releases/" + arch + "/current/stages/" + subarch + r"/ 2> /dev/null | grep 'bz2\"' | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'", return_output=True)[1].strip().split("\n")
620
621 def list_subarch_from_mirror(mirror, arch):
622 return spawn("wget -O - " + mirror + "/releases/" + arch + r"/current/stages/ 2> /dev/null | grep folder.gif | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'", return_output=True)[1].strip().split("\n")
623
624 def list_mirrors(http=True, ftp=True, rsync=True):
625 mirrors = []
626 mirrortypes = ""
627 if http:
628 mirrortypes += "http"
629 if ftp:
630 if mirrortypes:
631 mirrortypes += '\|'
632 mirrortypes += "ftp"
633 if rsync:
634 if mirrortypes:
635 mirrortypes += '\|'
636 mirrortypes += "rsync"
637 mirrorlist = spawn(r"wget -O - 'http://www.gentoo.org/main/en/mirrors.xml?passthru=1' 2>/dev/null | /bin/sed -ne '/^[[:space:]]\+<uri link=\"\(" + mirrortypes + r"\):\/\/[^\"]\+\">/{s/^[[:space:]]\+<uri link=\"\([^\"]\+\)\">\(.*\)<\/uri>.*$/\1|\2/;p}'", return_output=True)[1].strip().split("\n")
638 for mirror in mirrorlist:
639 mirror = mirror.strip()
640 mirrors.append(mirror.split("|"))
641 return mirrors
642
643 def generate_keymap_list():
644 keymap_list = []
645 path = "/usr/share/keymaps"
646
647 # find /usr/share/keymaps -iname *.map.gz -printf "%f \n"
648 put, get = os.popen4("find "+path+" -iname *.map.gz -printf \"%f \n\"")
649 for keymap in get.readlines():
650 # strip the last 9 chars ( .map.gz\n )
651 keymap.strip()
652 keymap = keymap[:-9]
653 keymap_list.append(keymap)
654
655 # sort the keymap list
656 keymap_list.sort()
657
658 return keymap_list
659
660 def generate_consolefont_list():
661 consolefont_list=[]
662 path = "/usr/share/consolefonts"
663
664 # find /usr/share/consolefonts -iname *.gz -printf "%f \n"
665 put, get = os.popen4("find "+path+" -iname *.gz -printf \"%f \n\"")
666 for consolefont in get.readlines():
667 # strip the last 5 chars ( .gz\n )
668 consolefont.strip()
669 consolefont = consolefont[:-5]
670
671 # test if its psfu or psf or fnt
672 # and remove it if necessary
673 if consolefont[-4:]== "psfu":
674 consolefont = consolefont[:-5]
675 if consolefont[-3:]== "psf":
676 consolefont = consolefont[:-4]
677 if consolefont[-3:]=="fnt":
678 consolefont = consolefont[:-4]
679
680 consolefont_list.append(consolefont)
681
682 # sort the keymap list
683 consolefont_list.sort()
684
685 return consolefont_list
686
687 def generate_consoletranslation_list():
688 consoletranslation_list=[]
689 path = "/usr/share/consoletrans"
690
691 # find /usr/share/keymaps -iname *.trans -printf "%f \n"
692 put, get = os.popen4("find "+path+" -iname *.trans -printf \"%f \n\"")
693 for consoletran in get.readlines():
694 # strip the last 8 chars ( .trans\n )
695 consoletran.strip()
696 consoletran = consoletran[:-8]
697 consoletranslation_list.append(consoletran)
698
699 consoletranslation_list.sort()
700
701 return consoletranslation_list
702
703 def get_global_use_flags():
704 use_desc = {}
705 f = open("/usr/portage/profiles/use.desc", "r")
706 for line in f:
707 line = line.strip()
708 if line == "# The following flags are NOT to be set or unset by users":
709 break
710 if not line or line.startswith("#"): continue
711 dash_pos = line.find(" - ")
712 if dash_pos == -1: continue
713 flagname = line[:dash_pos] or line[dash_pos-1]
714 desc = line[dash_pos+3:]
715 use_desc[flagname] = desc
716 f.close()
717 return use_desc
718
719 def get_local_use_flags():
720 use_local_desc = {}
721 f = open("/usr/portage/profiles/use.local.desc", "r")
722 for line in f:
723 line = line.strip()
724 if not line or line.startswith("#"): continue
725 dash_pos = line.find(" - ")
726 if dash_pos == -1: continue
727 colon_pos = line.find(":", 0, dash_pos)
728 pkg = line[:colon_pos]
729 flagname = line[colon_pos+1:dash_pos] or line[colon_pos+1]
730 desc = "(" + pkg + ") " + line[dash_pos+3:]
731 use_local_desc[flagname] = desc
732 f.close()
733 return use_local_desc
734
735 def get_cd_snapshot_uri():
736 snapshot_loc = spawn("ls /mnt/{cdrom,livecd}/snapshots/portage-* 2>/dev/null | head -n 1", return_output=True)[1].strip()
737 if snapshot_loc:
738 snapshot_loc = "file://" + snapshot_loc
739 return snapshot_loc
740
741 def validate_uri(uri):
742 # Get tuple of matches
743 # 0 - Protocol
744 # 1 - Username
745 # 2 - Password
746 # 3 - Host
747 # 4 - Port
748 # 5 - Path
749 uriparts = parse_uri(uri)
750 if uriparts[0] in ('http', 'https', 'ftp'):
751 ret = spawn("wget --spider " + uri)
752 return exitsuccess(ret)
753 elif uriparts[0] == "file":
754 return is_file(uriparts[5])
755 return True
756
757 def get_directory_listing_from_uri(uri):
758 uriparts = parse_uri(uri)
759 if not uriparts:
760 return []
761 if uriparts[0] == "file":
762 dirlist = os.listdir(uriparts[5])
763 dirlist.sort()
764 dirs = []
765 files = []
766 for entry in dirlist:
767 if os.path.isdir(uriparts[5] + entry):
768 dirs.append(entry + "/")
769 else:
770 files.append(entry)
771 if not uriparts[5] == "/":
772 dirlist = ["../"]
773 else:
774 dirlist = []
775 dirlist += dirs + files
776 elif uriparts[0] == "http":
777 dirlist = spawn("wget -O - http://" + uriparts[3] + uriparts[5] + r" 2> /dev/null | grep -i href | grep -v 'http://' | grep -v 'ftp://' | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'", return_output=True)[1].strip().split("\n")
778 dirs = []
779 files = []
780 for entry in dirlist:
781 if not entry.startswith("/") and entry.find("?") == -1:
782 if entry.endswith("/"):
783 dirs.append(entry)
784 else:
785 files.append(entry)
786 dirs.sort()
787 files.sort()
788 if not uriparts[5] == "/":
789 dirlist = ["../"]
790 else:
791 dirlist = []
792 dirlist += dirs + files
793 elif uriparts[0] == "ftp":
794 dirlist = spawn("wget -O - ftp://" + uriparts[3] + uriparts[5] + r" 2> /dev/null | grep -i href | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i' -e 's|^ftp://[^/]\+/|/|'", return_output=True)[1].strip().split("\n")
795 dirs = []
796 files = []
797 for entry in dirlist:
798 if entry.startswith(uriparts[5]):
799 entry = entry[len(uriparts[5]):]
800 if entry.endswith("/"):
801 dirs.append(entry)
802 else:
803 files.append(entry)
804 dirs.sort()
805 files.sort()
806 if not uriparts[5] == "/":
807 dirlist = ["../"]
808 else:
809 dirlist = []
810 dirlist += dirs + files
811 elif uriparts[0] == "scp":
812 tmpdirlist = ""
813 dirlist = []
814 sshcmd = ["ssh"]
815 if uriparts[4]:
816 sshcmd.append("-p")
817 sshcmd.append(uriparts[4])
818 if uriparts[1]:
819 sshcmd.append(uriparts[1] + "@" + uriparts[3])
820 else:
821 sshcmd.append(uriparts[3])
822 sshcmd.append("ls --color=no -1F " + uriparts[5] + r" 2>/dev/null | sed -e 's:\*$::'")
823 # print str(sshcmd)
824 pid, child_fd = pty.fork()
825 if not pid:
826 os.execvp("ssh", sshcmd)
827 else:
828 got_password_prompt = False
829 while 1:
830 r, w, e = select.select([child_fd], [], [])
831 if child_fd in r:
832 try:
833 data = os.read(child_fd, 1024)
834 except:
835 pid2, status = os.waitpid(pid, 0)
836 break
837 if data.endswith("assword: "):
838 if uriparts[2]:
839 os.write(child_fd, uriparts[2] + "\n")
840 else:
841 os.write(child_fd, "\n")
842 got_password_prompt = True
843 elif data.endswith("Are you sure you want to continue connecting (yes/no)? "):
844 os.write(child_fd, "yes\n")
845 else:
846 if got_password_prompt:
847 if not tmpdirlist and data.endswith("assword: "):
848 raise GLIException("IncorrectPassword", "notice", "get_directory_listing_from_uri", "Your SSH password was incorrect")
849 tmpdirlist += data
850 else:
851 pid2, status = os.waitpid(pid, os.WNOHANG)
852 if pid2:
853 break
854 for tmpentry in tmpdirlist.strip().split("\n"):
855 dirlist.append(tmpentry.strip())
856 dirs = []
857 files = []
858 for entry in dirlist:
859 if entry.endswith("/"):
860 dirs.append(entry)
861 else:
862 files.append(entry)
863 dirs.sort()
864 files.sort()
865 if not uriparts[5] == "/":
866 dirlist = ["../"]
867 else:
868 dirlist = []
869 dirlist += dirs + files
870 else:
871 dirlist = ["this/", "type/", "isn't/", "supported", "yet"]
872 return dirlist
873
874 def cdata(text):
875 if text.startswith("<![CDATA["):
876 return text
877 else:
878 return "<![CDATA[\n" + text + "\n]]>"
879
880 def uncdata(text):
881 if text.startswith("<![CDATA["):
882 return text[9:-5]
883 else:
884 return text
885
886 def get_grp_pkgs_from_cd():
887 if not is_file("/usr/livecd/grppkgs.txt"):
888 return ""
889 #raise GLIException("GLIUtilityError", "fatal", "get_grp_pkgs_from_cd", "Required file /usr/livecd/grppkgs.txt does not exist")
890 status,output = spawn('cat /usr/livecd/grppkgs.txt',return_output=True)
891 output = output.split()
892 #remove the first part before a / for comparision
893 results = []
894 for pkg in output:
895 results.append(pkg[(pkg.find('/')+1):])
896 return results
897
898 def get_keymaps(self):
899 return GLIUtility.spawn(r"find /usr/share/keymaps -iname *.map.gz | sed -e 's:^.\+/::' -e 's:\..\+$::' | sort", return_output=True)[1].strip().split("\n")

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20