All pastes #42867 Raw Edit

GLIUtility.py.diff

public text v1 · immutable
#42867 ·published 2006-02-23 06:29 UTC
rendered paste body
--- GLIUtility.orig.py	2006-02-14 11:40:30.000000000 -0500
+++ GLIUtility.py	2006-02-23 01:24:07.000000000 -0500
@@ -726,41 +726,38 @@
 
 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
+	import glob
+	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 +797,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().split("\n")
 		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 +820,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().split("\n")
 		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 +850,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 +878,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().split("\n")]
 		dirs = []
 		files = []
 		for entry in dirlist:
@@ -888,12 +896,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 +914,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().split("\n")
+	result = [remove_suffix(os.path.basename(keymap), ".map.gz") for keymap in result]
+	result.sort()
+	return result
+
+_chosts = {
+	"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 _chosts.get(arch, ())
+
+def remove_prefix(s, prefix):
+	return s.split(prefix, 1)[1]
+
+def remove_suffix(s, suffix):
+	return s.rsplit(suffix, 1)[0]