rendered paste body--- GLIUtility.orig.py 2006-02-14 11:40:30.000000000 -0500
+++ GLIUtility.py 2006-02-23 14:22:40.000000000 -0500
@@ -8,18 +8,14 @@
The GLIUtility module contians all utility functions used throughout GLI.
"""
-import string, os, re, shutil, sys, random, commands, crypt, pty, select
+import commands, crypt, glob, os, pty, random, re, select, shutil, string, sys
from GLIException import *
##
# Check to see if a string is actually a string, and if it is not null. Returns bool.
# @param string_a string to be checked.
def is_realstring(string):
- # Make sure it is a string
- if not isinstance(string, (str, unicode)):
- return False
-
- return True
+ return isinstance(string, (str, unicode)):
##
# Checks to see if x is a numeral by doing a type conversion.
@@ -32,6 +28,9 @@
else:
return True
+# Compile the regular expression that validates an IP. It will also check for valid ranges.
+_is_ip_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]))$')
+
##
# Check to see if a string is a valid ip. Returns bool.
# @param ip ip to be checked.
@@ -40,35 +39,33 @@
if not is_realstring(ip):
return False
- # Compile the regular expression that validates an IP. It will also check for valid ranges.
- 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]))$')
-
# Run the test.
- res = expr.match(ip)
+ res = _is_ip_expr.match(ip)
# Return True only if there are results.
- return(res != None)
+ return (res is not None)
+
+_is_mac_expr = re.compile('([0-9A-F]{2}:){5}[0-9A-F]{2}')
##
# Check to see if mac is a valid MAC address. Make sure use format_mac
# before using this function. Returns bool.
# @param mac mac address to be checked.
def is_mac(mac):
- expr = re.compile('([0-9A-F]{2}:){5}[0-9A-F]{2}')
- res = expr.match(mac)
- return(res != None)
+ res = is_mac_expr.match(mac)
+ return (res is not None)
##
# Format's a mac address properly. Returns the correctly formatted MAC. (a string)
# @param mac mac address to be formatted
def format_mac(mac):
- mac = string.replace(mac, '-', ':')
+ mac = string.replace(mac, "-", ":")
mac = string.upper(mac)
- mac = string.split(mac, ':')
- for i in range(0, len(mac)):
- if len(mac[i]) < 2:
- mac[i] = "0" + mac[i]
+ mac = string.split(mac, ":")
+ for i, part in enumerate(mac):
+ if len(part) < 2:
+ mac[i] = "0" + part
return string.join(mac, ":")
##
@@ -76,13 +73,18 @@
# trim_ip('192.168.01.002') => '192.168.1.2'
# @param ip IP address to be trimmed
def trim_ip(ip):
- # Remove leading zero's on the first octet
- ip = re.sub('^0{1,2}','',ip)
+ ip = string.split(ip, ".")
+ ip = [str(int(part)) for part in ip]
+ return string.join(ip, ".")
+ # Old way w/regex here:
+ # Remove leading zero's on the first octet
+ #ip = re.sub(r'^0{1,2}\d.','',ip)
+ #
# Remove leading zero's from the other octets
- res = re.sub('((?<=\.)(00|0)(?P<num>\d))','\g<num>',ip)
-
- return(res)
+ #res = re.sub(r'((?<=\.)(00|0)(?P<num>\d))','\g<num>',ip)
+ #
+ #return(res)
##
# Check to see if the string passed is a valid device. Returns bool.
@@ -93,12 +95,14 @@
return False
# Make sure the string starts with /dev/
- if device[0:5] != '/dev/':
+ if not device.startswith('/dev/'):
return False
# Check to make sure the device exists
return os.access(device, os.F_OK)
+_is_hostname_expr = re.compile('^([a-zA-Z0-9-_\.])+\.[a-zA-Z]{2,4}$')
+
##
# Check to see if the string is a valid hostname. Returns bool.
# @param hostname host to be checked
@@ -107,11 +111,13 @@
if not is_realstring(hostname):
return False
- expr = re.compile('^([a-zA-Z0-9-_\.])+\.[a-zA-Z]{2,4}$')
- res = expr.match(hostname)
+ res = _is_hostname_expr.match(hostname)
+
+ return (res is not None)
+
+# Create a regular expression that matches all words and the symbols '-_./' _ is included in the \w
+_is_path_expr = re.compile('^[\w\.\-\/~]+$')
- return(res != None)
-
##
# Check to see if the string is a valid path. Returns bool.
# @param path Path to be checked.
@@ -120,14 +126,11 @@
if not is_realstring(path):
return False
- # Create a regular expression that matches all words and the symbols '-_./' _ is included in the \w
- expr = re.compile('^[\w\.\-\/~]+$')
-
# Run the match
- res = expr.match(path)
+ res = _is_path_expr.match(path)
# Return True only if there are results
- return(res != None)
+ return (res is not None)
##
# Check to see if the string is a valid file. Returns bool.
@@ -140,16 +143,15 @@
# Check to make sure the device exists
return os.access(file, os.F_OK)
+_parse_uri_expr = re.compile('(\w+)://(?:([^:@]+)(?::([^@]+))?@)?(?:([a-zA-Z0-9.-]+)(?::(\d+))?)?(/.*)')
+
##
# Parse a URI. Returns a tuple (protocol, username, password, host, port, path)
# Returns None if URI is invalid.
# @param uri URI to be parsed
def parse_uri(uri):
- # Compile the regex
- expr = re.compile('(\w+)://(?:([^:@]+)(?::([^@]+))?@)?(?:([a-zA-Z0-9.-]+)(?::(\d+))?)?(/.*)')
-
- # Run it against the URI
- res = expr.match(uri)
+ # Run the regex against the URI
+ res = _parse_uri_expr.match(uri)
if not res:
# URI doesn't match regex and therefore is invalid
@@ -162,8 +164,7 @@
# 3 - Host
# 4 - Port
# 5 - Path
- uriparts = res.groups()
- return uriparts
+ return res.groups()
##
# Check to see if the string is a valid URI. Returns bool.
@@ -198,7 +199,7 @@
# Converts a string to a boolean value. anything not "True" is deemed false.
# @param input must be a string so it can be converted to boolean.
def strtobool(input):
- if type(input) != str:
+ if not isinstance(input, (str, unicode)):
raise GLIException("GLIUtilityError", 'fatal','strtobool',"The input must be a string!")
if string.lower(input) == 'true':
@@ -222,16 +223,14 @@
# Return True only if there are results
#return(res != None)
- status, output = spawn("/sbin/ifconfig -a | grep -e '^[A-Za-z]'| cut -d ' ' -f 1 | grep '"+ device + "'", return_output=True)
- if output:
- return True
- return False
+ return (device in get_eth_devices())
##
# Will return a list of devices found in ifconfig.
def get_eth_devices():
- status, output = spawn("/sbin/ifconfig -a | grep -e '^[A-Za-z]'| cut -d ' ' -f 1", return_output=True)
- return output.split()
+ output = spawn("/sbin/ifconfig -a", return_output=True).splitlines()
+ result = [line.split(" ", 1)[0] for line in output if not line.startswith(" ")]
+ return result
##
# Checks to see if device is a valid NFS device
@@ -240,15 +239,12 @@
if not is_realstring(device):
return False
- colon_location = device.find(':')
-
- if colon_location == -1:
+ try:
+ host, path = device.split(":", 1)
+ except ValueError: # split string not found
return False
- host = device[:colon_location]
- path = device[colon_location+1:]
-
- return((is_ip(host) or is_hostname(host)) and is_path(path))
+ return ((is_ip(host) or is_hostname(host)) and is_path(path))
##
# Sets the network ip (used for the livecd environment)
@@ -266,10 +262,7 @@
status = spawn("ifconfig " + options)
- if not exitsuccess(status):
- return False
-
- return True
+ return exitsuccess(status)
##
# Sets the default route (used for the livecd environment)
@@ -279,10 +272,7 @@
raise GLIException("GLIUtilityError", 'fatal', 'set_default_route', route + " is not an ip address!")
status = spawn("route add default gw " + route)
- if not exitsuccess(status):
- return False
-
- return True
+ return exitsuccess(status)
##
# Will run a command with various flags for the style of output and logging.
@@ -300,9 +290,11 @@
if chroot:
wrapper = open(chroot+"/var/tmp/spawn.sh", "w")
- wrapper.write("#!/bin/bash -l\n" + cmd + "\nexit $?\n")
+ print >> wrapper, "#!/bin/bash -l"
+ print >> wrapper, cmd
+ print >> wrapper, "exit $?"
wrapper.close()
- cmd = "chmod a+x " + chroot + "/var/tmp/spawn.sh && chroot " + chroot + " /var/tmp/spawn.sh 2>&1"
+ cmd = "chmod o+x " + chroot + "/var/tmp/spawn.sh && chroot " + chroot + " /var/tmp/spawn.sh 2>&1"
else:
cmd += " 2>&1 "
if debug:
@@ -399,7 +391,8 @@
def exitsuccess(status):
if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0:
return True
- return False
+ else:
+ return False
##
# Will produce a bash shell with a special prompt for the installer.
@@ -488,11 +481,9 @@
def ping(host):
host = str(host)
if not (is_hostname(host) or is_ip(host)):
- return False #invalid IP or hostname
- status = spawn("ping -n -c 2 " + host)
- if not exitsuccess(status):
- return False
- return True
+ return False # invalid IP or hostname
+ status = spawn("ping -n -c 2 '" + host + "'")
+ return exitsuccess(status)
##
# Pass in the eth device's number (0, 1, 2, etc).
@@ -523,20 +514,24 @@
status, device_info = spawn("/sbin/ifconfig " + device, return_output=True)
if exitsuccess(status):
for line in device_info.splitlines():
- line = line.strip()
- if 'HWaddr' in line:
- hw_addr = line.split('HWaddr',1)[1].strip()
- if 'inet addr' in line:
- ip_addr = line.split(' ')[0].split(':')[1]
- if 'Bcast' in line:
- bcast = line.split(' ')[1].split(':')[1]
- if 'Mask' in line:
- mask = line.split(' ')[2].split(':')[1]
- if line.startswith('UP'):
- up = True
+ for prop in line.strip().split(" "):
+ if prop.startswith("HWaddr "):
+ hw_addr = remove_prefix(prop, "HWaddr ")
+ elif prop.startswith("inet addr:"):
+ ip_addr = remove_prefix(prop, "inet addr:")
+ elif prop.startswith("Bcast:"):
+ bcast = remove_prefix(prop, "Bcast:")
+ elif prop.startswith("Mask:"):
+ ip_addr = remove_prefix(prop, "Mask:")
+ elif prop.startswith("UP"):
+ up = True
else:
raise GLIException("GLIUtilityError", 'fatal', "get_eth_info", device_info)
- 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()
+
+ output = spawn("/sbin/route -n", return_output=True)[1].splitlines() # get the routing table
+ output = [line for line in output if line.startswith("0.0.0.0")][0] # get the first line that starts with 0.0.0.0
+ output = remove_prefix(output, "0.0.0.0").strip() # remove the 0.0.0.0 and strip the leaving spaces
+ gw = output.split()[0] # get the first "word"
return (hw_addr, ip_addr, mask, bcast, gw, up)
@@ -545,43 +540,54 @@
# @param tarball_uri URI of tarball
# @param target_directory destination
# @param temp_directory="/tmp" a temporary location (used for dealing with the
-# ramdisk size limitations of the livecd env.
+# ramdisk size limitations of the livecd env.)
# @param keep_permissions=False Whether or not to keep permissions (-p)
def fetch_and_unpack_tarball(tarball_uri, target_directory, temp_directory="/tmp", keep_permissions=False, cc=None):
"Fetches a tarball from tarball_uri and extracts it into target_directory"
# Get tarball info
- tarball_filename = tarball_uri.split("/")[-1]
+ tarball_filename = os.path.basename(tarball_uri)
+ local_tarball = os.path.join(temp_directory, tarball_filename)
# Get the tarball
- if not get_uri(tarball_uri, temp_directory + "/" + tarball_filename, cc):
+ if not get_uri(tarball_uri, local_tarball, cc):
raise GLIException("GLIUtilityError", 'fatal', 'fetch_and_unpack_tarball',"Could not fetch " + tarball_uri)
# Reset tar options
- tar_options = "xv"
-
- # If the tarball is bzip'd
- if tarball_filename.split(".")[-1] == "tbz" or tarball_filename.split(".")[-1] == "bz2":
- format_option = "j"
-
- # If the tarball is gzip'd
- elif tarball_filename.split(".")[-1] == "tgz" or tarball_filename.split(".")[-1] == "gz":
- format_option = "z"
-
- tar_options += format_option
+ tar_options = ["xv"]
+
+ # Compression
+ compression = ""
+ ext = os.path.splitext(tarball_filename)[1]
+
+ if ext == ".tbz" or ext == ".bz2":
+ compression = "j"
+ elif ext == ".tgz" or ext == ".gz":
+ compression = "z"
+ elif ext == ".Z": # For completeness
+ compression = "Z"
+ tar_options.append(compression)
# If we want to keep permissions
if keep_permissions:
- tar_options = tar_options + "p"
+ tar_options.append("p")
# Get number of files in tarball
tarfiles = 0
if cc:
cc.addNotification("progress", (0, "Determining the number of files in " + tarball_filename))
- tarfiles = int(spawn("tar -t" + format_option + "f " + temp_directory + "/" + tarball_filename + " 2>/dev/null | wc -l", return_output=True)[1].strip())
+ tarfiles = len(spawn("tar -t" + compression + "f " + local_tarball + " 2>/dev/null", return_output=True)[1].strip().splitlines())
# Unpack the tarball
- 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
+ exitstatus = spawn(
+ "tar -" + string.join(tar_options, "") + " -f " + local_tarball + " -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
if not exitsuccess(exitstatus):
raise GLIException("GLIUtilityError", 'fatal', 'fetch_and_unpack_tarball',"Could not unpack " + tarball_uri + " to " + target_directory)
@@ -592,19 +598,9 @@
def generate_random_password():
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%^&*[]{}-=+_,|'\"<>:/"
s = list(s)
-
- for i in range(0,len(s)/2):
- x = random.randint(0,len(s)-1)
- y = random.randint(0,len(s)-1)
- tmp = s[x]
- s[x] = s[y]
- s[y] = tmp
-
- passwd = ""
- for i in range(0,random.randint(8,12)):
- passwd += s[i]
-
- return passwd
+ random.shuffle(s)
+ passwd = [random.choice(s) for i in range(random.randint(8,12))]
+ return string.join(passwd, "")
##
# Will grab a value from a specified file after sourcing it
@@ -615,19 +611,14 @@
status, output = spawn("source " + filename + " && echo $" + value, return_output=True)
return string.strip(output)
+_hash_chars = "./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
##
# Will take a password and return it hashed in md5 format
# @param password the password to be hashed
def hash_password(password):
- salt = "$1$"
- chars = "./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- for i in range(0, 8):
- salt += chars[random.randint(0, len(chars)-1)]
- salt += "$"
- passwd_hash = crypt.crypt(password, salt)
-
- return passwd_hash
+ salt = ["$1$"] + [random.choice(_hash_chars) for i in range(8)] + ["$"]
+ return crypt.crypt(password, string.join(salt, ""))
##
# Returns the real name (manufacturer and model) of a network interface
@@ -635,30 +626,42 @@
def get_interface_realname(interface):
# TODO: rewrite with 2.4 support
if is_file("/sys/class/net/" + interface + "/device"):
- return spawn("lspci | grep $(basename $(readlink /sys/class/net/" + interface + r"/device)) | sed -e 's|^.\+ Ethernet controller: ||'", return_output=True)[1].strip()
+ return spawn(
+ "lspci" +
+ " | grep $(basename $(readlink /sys/class/net/" + interface + "/device))" +
+ r" | sed -e 's|^.\+ Ethernet controller: ||'",
+ return_output=True)[1].strip()
else:
return "No Information Found"
def list_stage_tarballs_from_mirror(mirror, arch, subarch):
- 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")
+ return spawn(
+ "wget -O - " + mirror + "/releases/" + arch + "/current/stages/" + subarch + "/ 2> /dev/null" +
+ r" | grep 'bz2\"'" +
+ r" | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'",
+ return_output=True)[1].strip().splitlines()
def list_subarch_from_mirror(mirror, arch):
- 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")
+ return spawn(
+ "wget -O - " + mirror + "/releases/" + arch + "/current/stages/ 2> /dev/null" +
+ r" | grep 'folder\.gif'" +
+ r" | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'",
+ return_output=True)[1].strip().splitlines()
def list_mirrors(http=True, ftp=True, rsync=True):
mirrors = []
- mirrortypes = ""
+ mirrortypes = []
if http:
- mirrortypes += "http"
+ mirrortypes.append("http")
if ftp:
- if mirrortypes:
- mirrortypes += '\|'
- mirrortypes += "ftp"
+ mirrortypes.append("ftp")
if rsync:
- if mirrortypes:
- mirrortypes += '\|'
- mirrortypes += "rsync"
- 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")
+ mirrortypes.append("rsync")
+ mirrortypes = string.join(mirrortypes, r"\|")
+ mirrorlist = spawn(
+ "wget -O - 'http://www.gentoo.org/main/en/mirrors.xml?passthru=1' 2>/dev/null" +
+ r" | /bin/sed -ne '/^[[:space:]]\+<uri link=\"\(" + mirrortypes + r"\):\/\/[^\"]\+\">/{s/^[[:space:]]\+<uri link=\"\([^\"]\+\)\">\(.*\)<\/uri>.*$/\1|\2/;p}'",
+ return_output=True)[1].strip().splitlines()
for mirror in mirrorlist:
mirror = mirror.strip()
mirrors.append(mirror.split("|"))
@@ -668,13 +671,9 @@
keymap_list = []
path = "/usr/share/keymaps"
- # find /usr/share/keymaps -iname *.map.gz -printf "%f \n"
- put, get = os.popen4("find "+path+" -iname *.map.gz -printf \"%f \n\"")
- for keymap in get.readlines():
- # strip the last 9 chars ( .map.gz\n )
- keymap.strip()
- keymap = keymap[:-9]
- keymap_list.append(keymap)
+ # find /usr/share/keymaps -name '*.map.gz' -printf "%f\n"
+ put, get = os.popen4("find "+path+r" -name '*.map.gz' -printf \"%f\n\"")
+ keymap_list = [remove_suffix(line, ".map.gz\n") for line in get]
# sort the keymap list
keymap_list.sort()
@@ -685,21 +684,19 @@
consolefont_list=[]
path = "/usr/share/consolefonts"
- # find /usr/share/consolefonts -iname *.gz -printf "%f \n"
- put, get = os.popen4("find "+path+" -iname *.gz -printf \"%f \n\"")
- for consolefont in get.readlines():
- # strip the last 5 chars ( .gz\n )
- consolefont.strip()
- consolefont = consolefont[:-5]
+ # find /usr/share/consolefonts -name '*.gz' -printf "%f\n"
+ put, get = os.popen4("find "+path+" -name '*.gz' -printf \"%f\n\"")
+ for consolefont in get:
+ consolefont = remove_suffix(consolefont, ".gz\n")
# test if its psfu or psf or fnt
# and remove it if necessary
- if consolefont[-4:]== "psfu":
- consolefont = consolefont[:-5]
- if consolefont[-3:]== "psf":
- consolefont = consolefont[:-4]
- if consolefont[-3:]=="fnt":
- consolefont = consolefont[:-4]
+ if consolefont.endswith(".psfu"):
+ consolefont = remove_suffix(consolefont, ".psfu")
+ if consolefont.endswith(".psf"):
+ consolefont = remove_suffix(consolefont, ".psf")
+ if consolefont.endswith(".fnt"):
+ consolefont = remove_suffix(consolefont, ".fnt")
consolefont_list.append(consolefont)
@@ -709,58 +706,48 @@
return consolefont_list
def generate_consoletranslation_list():
- consoletranslation_list=[]
path = "/usr/share/consoletrans"
- # find /usr/share/keymaps -iname *.trans -printf "%f \n"
- put, get = os.popen4("find "+path+" -iname *.trans -printf \"%f \n\"")
- for consoletran in get.readlines():
- # strip the last 8 chars ( .trans\n )
- consoletran.strip()
- consoletran = consoletran[:-8]
- consoletranslation_list.append(consoletran)
-
+ # find /usr/share/consoletrans -name '*.trans' -printf "%f\n"
+ put, get = os.popen4("find "+path+" -name '*.trans' -printf \"%f\n\"")
+ consoletranslation_list = [remove_suffix(line, ".trans\n") for line in get]
consoletranslation_list.sort()
return consoletranslation_list
def get_global_use_flags():
use_desc = {}
- f = open("/usr/portage/profiles/use.desc", "r")
- for line in f:
+ for line in open("/usr/portage/profiles/use.desc", "r"):
line = line.strip()
if line == "# The following flags are NOT to be set or unset by users":
break
if not line or line.startswith("#"): continue
- dash_pos = line.find(" - ")
- if dash_pos == -1: continue
- flagname = line[:dash_pos] or line[dash_pos-1]
- desc = line[dash_pos+3:]
- use_desc[flagname] = desc
- f.close()
+ try:
+ flagname, desc = line.split(" - ", 1)
+ use_desc[flagname] = desc
+ except ValueError: # split string not found
+ continue
return use_desc
def get_local_use_flags():
use_local_desc = {}
- f = open("/usr/portage/profiles/use.local.desc", "r")
- for line in f:
+ for line in open("/usr/portage/profiles/use.local.desc", "r"):
line = line.strip()
if not line or line.startswith("#"): continue
- dash_pos = line.find(" - ")
- if dash_pos == -1: continue
- colon_pos = line.find(":", 0, dash_pos)
- pkg = line[:colon_pos]
- flagname = line[colon_pos+1:dash_pos] or line[colon_pos+1]
- desc = "(" + pkg + ") " + line[dash_pos+3:]
- use_local_desc[flagname] = desc
- f.close()
+ try:
+ line, desc = line.split(" - ", 1)
+ pkg, flagname = line.split(":", 1)
+ use_local_desc[flagname] = "(" + pkg + ")" + desc
+ except ValueError: # split string not found
+ continue
return use_local_desc
def get_cd_snapshot_uri():
- snapshot_loc = spawn("ls /mnt/{cdrom,livecd}/snapshots/portage-* 2>/dev/null | head -n 1", return_output=True)[1].strip()
- if snapshot_loc:
- snapshot_loc = "file://" + snapshot_loc
- return snapshot_loc
+ snapshot_loc = glob.glob("/mnt/{cdrom,livecd}/snapshots/portage-*")
+ try:
+ return "file://" + snapshot_loc[0]
+ except IndexError:
+ return ""
def validate_uri(uri):
# Get tuple of matches
@@ -800,11 +787,17 @@
dirlist = []
dirlist += dirs + files
elif uriparts[0] == "http":
- 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")
+ command = \
+ "wget -O - " + uriparts[0] + "://" + uriparts[3] + uriparts[5] + " 2> /dev/null" + \
+ " | grep -i href" + \
+ " | grep -v 'http://'" + \
+ " | grep -v 'ftp://'" + \
+ r" | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'"
+ dirlist = spawn(command, return_output=True)[1].strip().splitlines()
dirs = []
files = []
for entry in dirlist:
- if not entry.startswith("/") and entry.find("?") == -1:
+ if not entry.startswith("/") and ("?" in entry):
if entry.endswith("/"):
dirs.append(entry)
else:
@@ -817,26 +810,28 @@
dirlist = []
dirlist += dirs + files
elif uriparts[0] == "ftp":
- 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")
+ command = "wget -O - " + uriparts[0] + "://" + uriparts[3] + uriparts[5] + " 2> /dev/null" + \
+ " | grep -i href" + \
+ r" | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i' -e 's|^ftp://[^/]\+/|/|'"
+ dirlist = spawn(command, return_output=True)[1].strip().splitlines()
dirs = []
files = []
for entry in dirlist:
if entry.startswith(uriparts[5]):
- entry = entry[len(uriparts[5]):]
+ entry = remove_prefix(entry, uriparts[5])
if entry.endswith("/"):
dirs.append(entry)
else:
files.append(entry)
dirs.sort()
files.sort()
- if not uriparts[5] == "/":
+ if uriparts[5] != "/":
dirlist = ["../"]
else:
dirlist = []
dirlist += dirs + files
elif uriparts[0] == "scp":
- tmpdirlist = ""
- dirlist = []
+ dirlist = ""
sshcmd = ["ssh"]
if uriparts[4]:
sshcmd.append("-p")
@@ -845,7 +840,10 @@
sshcmd.append(uriparts[1] + "@" + uriparts[3])
else:
sshcmd.append(uriparts[3])
- sshcmd.append("ls --color=no -1F " + uriparts[5] + r" 2>/dev/null | sed -e 's:\*$::'")
+ sshcmd.append(
+ "ls --color=no -1F " + uriparts[5] + " 2>/dev/null" +
+ r" | sed -e 's:\*$::'"
+ )
# print str(sshcmd)
pid, child_fd = pty.fork()
if not pid:
@@ -870,15 +868,15 @@
os.write(child_fd, "yes\n")
else:
if got_password_prompt:
- if not tmpdirlist and data.endswith("assword: "):
+ if dirlist or not data.endswith("assword: "):
+ dirlist += data
+ else:
raise GLIException("IncorrectPassword", "notice", "get_directory_listing_from_uri", "Your SSH password was incorrect")
- tmpdirlist += data
else:
pid2, status = os.waitpid(pid, os.WNOHANG)
if pid2:
break
- for tmpentry in tmpdirlist.strip().split("\n"):
- dirlist.append(tmpentry.strip())
+ dirlist = [entry.strip() for entry in dirlist.strip().splitlines()]
dirs = []
files = []
for entry in dirlist:
@@ -888,12 +886,13 @@
files.append(entry)
dirs.sort()
files.sort()
- if not uriparts[5] == "/":
+ if uriparts[5] != "/":
dirlist = ["../"]
else:
dirlist = []
dirlist += dirs + files
else:
+ #raise NotImplementedError, uriparts[0]
dirlist = ["this/", "type/", "isn't/", "supported", "yet"]
return dirlist
@@ -905,41 +904,43 @@
def uncdata(text):
if text.startswith("<![CDATA["):
- return text[9:-5]
+ return remove_suffix(remove_prefix(text, "<!CDATA[\n"), "\n]]>")
else:
return text
def get_grp_pkgs_from_cd():
- if not is_file("/usr/livecd/grppkgs.txt"):
+ try:
+ grp_pkgs = open("/usr/livecd/grppkgs.txt", "r").read().split()
+ except IOError: # File not found or not readable
return ""
#raise GLIException("GLIUtilityError", "fatal", "get_grp_pkgs_from_cd", "Required file /usr/livecd/grppkgs.txt does not exist")
- status,output = spawn('cat /usr/livecd/grppkgs.txt',return_output=True)
- output = output.split()
- #remove the first part before a / for comparision
- results = []
- for pkg in output:
- results.append(pkg[(pkg.find('/')+1):])
- return results
+ else:
+ #remove the first part before a / for comparision
+ return [remove_prefix(pkg, "/") for pkg in grp_pkgs]
def get_keymaps(self):
- return GLIUtility.spawn(r"find /usr/share/keymaps -iname *.map.gz | sed -e 's:^.\+/::' -e 's:\..\+$::' | sort", return_output=True)[1].strip().split("\n")
+ result = GLIUtility.spawn("find /usr/share/keymaps -name *.map.gz", return_output=True)[1].strip().splitlines()
+ result = [remove_suffix(os.path.basename(keymap), ".map.gz") for keymap in result]
+ result.sort()
+ return result
+
+_get_chosts_dict = {
+ "x86": ("i386-pc-linux-gnu", "i486-pc-linux-gnu", "i586-pc-linux-gnu", "i686-pc-linux-gnu", ),
+ "amd64": ("x86_64-pc-linux-gnu", ),
+ "alpha": ("alpha-unknown-linux-gnu", ),
+ "ppc": ("powerpc-unknown-linux-gnu", ),
+ "ppc64": ("powerpc64-unknown-linux-gnu", ),
+ "sparc": ("sparc-unknown-linux-gnu", ),
+ "sparc64": ("sparc-unknown-linux-gnu", ),
+ "hppa": ("hppa-unknown-linux-gnu", "hppa1.1-unknown-linux-gnu", "hppa2.0-unknown-linux-gnu", ),
+ "mips": ("mips-unknown-linux-gnu", ),
+}
def get_chosts(arch):
- chosts = []
- if arch == "x86":
- chosts = ["i386-pc-linux-gnu", "i486-pc-linux-gnu", "i586-pc-linux-gnu", "i686-pc-linux-gnu"]
- if arch == "amd64":
- chosts = ["x86_64-pc-linux-gnu"]
- if arch == "alpha":
- chosts = ["alpha-unknown-linux-gnu"]
- if arch == "ppc":
- chosts = ["powerpc-unknown-linux-gnu"]
- if arch == "ppc64":
- chosts = ["powerpc64-unknown-linux-gnu"]
- if arch in ["sparc", "sparc64"]:
- chosts = ["sparc-unknown-linux-gnu"]
- if arch == "hppa":
- chosts = ["hppa-unknown-linux-gnu", "hppa1.1-unknown-linux-gnu", "hppa2.0-unknown-linux-gnu"]
- if arch == "mips":
- chosts = ["mips-unknown-linux-gnu"]
- return chosts
+ return _get_chosts_dict.get(arch, ())
+
+def remove_prefix(s, prefix):
+ return s.split(prefix, 1)[1]
+
+def remove_suffix(s, suffix):
+ return s.rsplit(suffix, 1)[0]