LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 02-04-2005, 09:56 PM   #1
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Rep: Reputation: 30
Python package fetching program


Hey everybody. I just finished my first parctical program in python. It's similar to swaret or slapt-get, but it only downloads packages, I haven't written the install section yet.

Anyways try it out and let me know what you think.

# getpkg.py
Code:
#! /usr/bin/python

import urllib,HTMLParser
import os,os.path

homedir = os.path.expanduser('~')
config_folder = homedir + "/.getpkg"
download_folder = config_folder + "/downloads"
installed_file = "installed"
available_file = "available"
country_file = "country"
version_file = "version"

try: fp = os.path.exists(config_folder)
except: fp =0
if not fp:
	os.mkdir(config_folder)

try: fp = os.path.exists(download_folder)
except: fp =0
if not fp:
	os.mkdir(download_folder)

packages_dir="/var/log/packages"
country_list = ["Australia","Austria","Belgium","Brasil","Bulgaria","Canada","Costa Rica","Czech Republic","Finland",\
"France","Germany","Greece","Hong Kong","Hungary","Ireland","Italy","Japan","Korea","Netherlands","Norway","Poland",\
"Portugal","Republic of Moldova","Romania","Russia","Serbia","South Africa","Spain","Sweden","Switzerland","Taiwan",\
"Thailand","UK","USA" ]
discsets = ["a","ap","d","e","f","gnome","k","kde","kdei","l","n","t","tcl","x","xap","extra","pasture","patches"]
slack_versions = ["8.1","9.0","9.1","10.0","current"]
menu = ["Choose Country","Choose Version","Update Installed List","Update Available List","Package Search","Exit"]
mirror_list_url="http://www.slackware.com/getslack/list.php?country="
mirrors = []
available = []

def get_installed_pkgs():
	installed = []
	packages = os.listdir(packages_dir)
	num_packages = len(packages)
	for package in packages:
		parts = package.split("-")
		revision = parts.pop()
		arch = parts.pop()
		version = parts.pop()
		pkgname = '-'.join(parts)
		this_package = {'package' : package,'name' : pkgname,'version' : version,'arch' : arch,'revision' : revision}
		installed.append(this_package)
	return installed
	
def choose_by_index(arr,heading):
	print ""
	print "Choose " + heading
	print "Please enter the number of your choice"
	for i in range(len(arr)):
		print str(i) + " : " + arr[i]
	print ""
	while 1:
		try:
			returnval = raw_input("Enter Index: ")
			intvalue = int(returnval)
			if intvalue >= 0 and intvalue < len(arr): break
			else:
				print "Invalid input, please try again"
				print "Please enter a value between 0 and " + str(len(arr) -1)
		except ValueError:
			print "Invalid input, please try again"
			print "Please enter a value between 0 and " + str(len(arr) -1)
	return returnval

class MyHTMLParser(HTMLParser.HTMLParser):
	
	global mirrors
	
	def handle_starttag(self, tag, attrs):
		if tag == "a" or tag == "A":
			for url in attrs:
			 theHREF = url[1]
			 if theHREF[0:4] == "http" or theHREF[0:4] == "ftp:":
			 	if theHREF != "http://store.slackware.com" and theHREF != "http://store.slackware.com/" and theHREF != "http://slackware.com/trademark/trademark.php":
			 		# make sure the mirro has trailing /
			 		if theHREF[-1] != "/": theHREF += "/"
			 		mirrors.append(theHREF)

class ParseDataForPackages(HTMLParser.HTMLParser):
	
	packages = []
	
	def handle_starttag(self, tag, attrs):
		if tag == "a" or tag == "A":
			for url in attrs:
			 theHREF = url[1]
			 if theHREF[-4:] == ".tgz" or theHREF[-4:] == ".TGZ":
			 	parts = theHREF.split("-")
			 	revision = parts.pop()
			 	arch = parts.pop()
			 	version = parts.pop()
			 	pkgname = '-'.join(parts)
			 	this_package = {'package' : package,'name' : pkgname,'version' : version,'arch' : arch,'revision' : revision}
			 	packages.append(this_package)

def read_filelist(filelist):
	pkgs = []
	while 1:
		line = filelist.readline()
		if line[-4:-1] == "tgz":
			this_pkg = {}
			words = line[:-1].split(" ")
			this_pkg["filesize"] = words[len(words)-4]
			package = words[len(words)-1]
			path = package[2:]
			this_pkg["path"] = path
			parts = path.split("/")
			if parts[0] == "slackware": disc = parts[1]
			elif parts[0] == "source": break
			else: disc = parts[0]
			pkg = parts[len(parts)-1]
			this_pkg["discset"] = disc
			this_pkg["package"] = pkg
			pkg_parts = pkg[:-4].split("-")
			if len(pkg_parts) >= 4:
				this_pkg['revision'] = pkg_parts.pop()
				this_pkg['arch'] = pkg_parts.pop()
				this_pkg['version'] = pkg_parts.pop()
				this_pkg['name'] = '-'.join(pkg_parts)
			else:
				this_pkg['revision'] = ""
				this_pkg['arch'] = ""
				this_pkg['version'] = ""
				this_pkg['name'] = ""
			pkgs.append(this_pkg)
		if not line: break
	return pkgs
	
inst_path = config_folder + "/" + installed_file
try: fp = os.path.exists(inst_path)
except: fp = 0
if not fp:
	inst_pkglist = get_installed_pkgs()
	output = open(inst_path,'w')
	for pkg in inst_pkglist:
		output.write(str(pkg) + "\n")
	output.close()
else:
	input = open(inst_path,'r')
	inst_pkglist = []
	EOF = False
	while not EOF:
		line = input.readline()
		if line:
			if line[-1] == "\n": line = line[:-1]
			inst_pkglist.append(eval(line))
		else: EOF = True
print str(len(inst_pkglist)) + " packages currently installed"

def choose_country():
	global mirrors
	global country_list
	mirrors = []
	
	while len(mirrors) == 0:
		country_choice = choose_by_index(country_list,"country")
		URL = mirror_list_url + country_list[int(country_choice)]
		        
		f = urllib.urlopen(URL)
		page = f.read()
		p = MyHTMLParser()
		p.feed(page)
		if len(mirrors) == 0: print "No mirrors found in " + country_list[int(country_choice)]
		else:
			print "Available Mirrors in " + country_list[int(country_choice)] + ":"
			for m in mirrors:
				print m	
	return country_list[int(country_choice)]

country_path = config_folder + "/" + country_file
try: fp = os.path.exists(country_path)
except: fp = 0
if fp:
	input = open(country_path,'r')
	country = input.readline()
	#country = country[:-1]
	input.close()
	URL = mirror_list_url + country
	while len(mirrors) == 0:
		f = urllib.urlopen(URL)
		page = f.read()
		p = MyHTMLParser()
		p.feed(page)
		if len(mirrors) == 0: print "No mirrors found in " + country
	print "Available Mirrors in " + country + ":"
	for m in mirrors:
		print m
else:
	country = choose_country()
	output = open(country_path,'w')
	output.write(country)
	output.close()
	

def choose_version():
	global slack_versions
	
	version_choice = choose_by_index(slack_versions,"Slackware Version")
	v = "slackware-" + slack_versions[int(version_choice)] + "/"
	return v

version_path = config_folder + "/" + version_file
try: fp = os.path.exists(version_path)
except: fp = 0
if fp:
	input = open(version_path,'r')
	version = input.readline()
	#version = version[:-1]
	input.close()
else:
	version = choose_version()
	output = open(version_path,'w')
	output.write(version)
	output.close()

def find_avail_packages():	
	global mirrors
	global version
	global available
	available = []
	max = len(mirrors)
	target = version + "FILELIST.TXT"
	ind = 0
	data = 0;
	while not data:
		URL = mirrors[ind] + target
		print "opening " + URL
		try:
			file = urllib.urlopen(URL)
			available = read_filelist(filelist=file)
			data = 1
		except:
			ind += 1
			if ind == max: ind = 0
			data = 0


available_path = config_folder + "/" + available_file
try: fp = os.path.exists(available_path)
except: fp = 0
if fp:
	input = open(available_path,'r')
	EOF = False
	while not EOF:
		line = input.readline()
		if line:
			if line[-1] == "\n": line = line[:-1]
			available.append(eval(line))
		else: EOF = True
else:
	foo = find_avail_packages()
	output = open(available_path,'w')
	for pkg in available:
		output.write(str(pkg) + "\n")
	output.close()

print str(len(available)) + " packages available"

def package_search():
	global available
	global inst_pkglist
	global version
	global mirrors
	global discsets
	global download_folder
	search = []
	search_options = ["Search by Name","Search by Disk Set","Upgrade Installed Packages","Return to Main Menu"]
	YN = ["No","Yes","No to All","Yes to All"]
	search_choice = int(choose_by_index(search_options,"Search Method"))
	if search_choice == 0:
		term = raw_input("Enter Search Term: ")
		for a_pkg in available:
			found_term = a_pkg['name'].find(term,0,len(a_pkg['name']))
			if not found_term:
				a_vers = a_pkg['version']
				a_disc = a_pkg['discset']
				a_package = a_pkg['package']
				a_rev = a_pkg['revision']
				a_arch = a_pkg['arch']
				i_vers = ''
				i_rev = ''
				i_package = ''
				i_arch = ''
				for i_pkg in inst_pkglist:
					if i_pkg['name'] == a_pkg['name']:
						i_vers = i_pkg['version']
						i_rev = i_pkg['revision']
						i_package = i_pkg['package']
						i_arch = i_pkg['arch']
				this_item = {}
				this_item['name'] = a_pkg['name']
				this_item['avail-vers'] = a_vers
				this_item['avail-arch'] = a_arch
				this_item['avail-rev'] = a_rev
				this_item['avail-pkg'] = a_package
				this_item['avail-disc'] = a_disc
				this_item['avail-path'] = a_pkg['path']
				this_item['inst-vers'] = i_vers
				this_item['inst-arch'] = i_arch
				this_item['inst-rev'] = i_rev
				this_item['inst-pkg'] = i_package
				search.append(this_item)
		if len(search) == 0:
			print "No results found for '" + term + "'"
				
	elif search_choice == 1:
		disc_choice = int(choose_by_index(discsets,"Disk Set"))
		theDiskSet = discsets[disc_choice]
		for a_pkg in available:
			if a_pkg['discset'] == theDiskSet:
				a_vers = a_pkg['version']
				a_disc = a_pkg['discset']
				a_package = a_pkg['package']
				a_rev = a_pkg['revision']
				a_arch = a_pkg['arch']
				i_vers = ''
				i_rev = ''
				i_package = ''
				i_arch = ''
				for i_pkg in inst_pkglist:
					if i_pkg['name'] == a_pkg['name']:
						i_vers = i_pkg['version']
						i_rev = i_pkg['revision']
						i_package = i_pkg['package']
						i_arch = i_pkg['arch']
				this_item = {}
				this_item['name'] = a_pkg['name']
				this_item['avail-vers'] = a_vers
				this_item['avail-arch'] = a_arch
				this_item['avail-rev'] = a_rev
				this_item['avail-pkg'] = a_package
				this_item['avail-disc'] = a_disc
				this_item['avail-path'] = a_pkg['path']
				this_item['inst-vers'] = i_vers
				this_item['inst-arch'] = i_arch
				this_item['inst-rev'] = i_rev
				this_item['inst-pkg'] = i_package
				search.append(this_item)
		if len(search) == 0:
			print "No results found for '" + theDiskSet + "'"
	elif search_choice == 2:
		for i_pkg in inst_pkglist:
			for a_pkg in available:
				if i_pkg['name'] == a_pkg['name']:
					if not (i_pkg['version'] == a_pkg['version'] and i_pkg['revision'] == a_pkg['revision']):
						this_item = {}
						this_item['name'] = a_pkg['name']
						this_item['avail-vers'] = a_pkg['version']
						this_item['avail-arch'] = a_pkg['arch']
						this_item['avail-rev'] = a_pkg['revision']
						this_item['avail-pkg'] = a_pkg['package']
						this_item['avail-disc'] = a_pkg['discset']
						this_item['avail-path'] = a_pkg['path']
						this_item['inst-vers'] = i_pkg['version']
						this_item['inst-arch'] = i_pkg['arch']
						this_item['inst-rev'] = i_pkg['revision']
						this_item['inst-pkg'] = i_pkg['package']
						search.append(this_item)
		if len(search) == 0:
			print "No packages available for upgrade"		
	elif search_choice == 3:
		main_menu()

	if len(search) != 0:
			download = []
			skip_all = 0
			dl_all = 0
			print str(len(search)) + " package(s) found"
			for found in search:
				have_it = 0
				print ""
				if found['avail-vers'] == found['inst-vers'] and found['avail-rev'] == found['inst-rev']:
					print "latest version of " + found['name'] + " is installed"
					have_it = 1
				elif found['inst-pkg'] == '':
					print found['name'] + " is not installed"
					if skip_all == 0 and dl_all == 0: dl = int(choose_by_index(YN,"to Download?"))
					else: dl = 0
					if dl == 1: download.append(found)
					if dl == 2: skip_all = 1
					if dl == 3: dl_all = 1
				else:
					print "a different version of " + found['name'] + " is installed"
					print "Installed : " + found['inst-pkg']
					print "Available : " + found['avail-pkg']
					if skip_all == 0 and dl_all == 0: dl = int(choose_by_index(YN,"to Download?"))
					else: dl = 0
					if dl == 1: download.append(found)
					if dl == 2: skip_all = 1
					if dl == 3: dl_all = 1
				if dl_all == 1 and have_it == 0: download.append(found)
			if len(download) != 0:
				print ""
				print str(len(download)) + " package(s) to download"
				for pkg in download:
					ind = 0
					max = len(mirrors)
					data = 0
					while not data:
						try:
							URL = mirrors[ind] + version + pkg['avail-path']
							print "downloading " + pkg['avail-pkg'] + " from " + mirrors[ind]
							wget_cmd = "wget -c " + URL
							os.system(wget_cmd)
							fp = os.path.exists(pkg['avail-pkg'])
							if fp:
								#file was downloaded
								data = 1	
								# move file to downloads folder & delete wget_log
								move_cmd = "mv " + pkg['avail-pkg'] + " " + download_folder
								os.system(move_cmd)
								fp2 = os.path.exists('wget-log')
								if fp2: os.system('rm wget-log')
							else:
								#file was not downloaded
								data = 0
								ind += 1
								if ind == max: ind = 0
								
						except:
							ind += 1
							if ind == max: ind = 0
							data = ""

def main_menu():
	global menu
	global inst_pkglist
	global inst_path
	global country_list
	global country_path
	global version
	global version_path
	
	menu_choice = int(choose_by_index(menu,"Options"))
	
	if menu_choice == 0:
		country = ""
		country = choose_country()
		os.unlink(country_path)
		output = open(country_path,'w')
		output.write(country)
		output.close()
	
	elif menu_choice == 1:
		version = choose_version()
		os.unlink(version_path)
		output = open(version_path,'w')
		output.write(version)
		output.close()
	
	elif menu_choice == 2:
		inst_pkglist = get_installed_pkgs()
		os.unlink(inst_path)
		output = open(inst_path,'w')
		for pkg in inst_pkglist:
			output.write(str(pkg) + "\n")
		output.close()
		print str(len(inst_pkglist)) + " packages currently installed"
		
	elif menu_choice == 3:
		foo = find_avail_packages()
		os.unlink(available_path)
		output = open(available_path,'w')
		for pkg in available:
			output.write(str(pkg) + "\n")
		output.close()
		print str(len(available)) + " packages available"
	
	elif menu_choice == 4:
		package_search()
	
	elif menu_choice == 5:
		return
	
	main_menu()

main_menu()
 
Old 02-05-2005, 03:12 AM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Rather than having us read the code, would you mind explaining where you are headed to?
 
Old 02-06-2005, 06:34 AM   #3
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Original Poster
Rep: Reputation: 30
I did.

Quote:
It's similar to swaret or slapt-get, but it only downloads packages,
Just save the code and run it. It's menu driven, and saves configurations in your home directory.

e.g.
paste code into kwrite
save as "getpkg"
type "./getpkg" in console window.
 
Old 02-06-2005, 06:37 AM   #4
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Still, what's the point? Why should I use that instead of slackpkg for example, which is officially supported and can also install if requested?
 
Old 02-06-2005, 06:44 AM   #5
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Original Poster
Rep: Reputation: 30
The same points as ALL of open source software. If you write something good you share with the community. Maybe if you tried it instead of just dumping on it you might even like it.
 
Old 02-06-2005, 06:49 AM   #6
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Given the large amount of open source software, I do not have the time to try them all unless I see a point. I can't see it here, and more importantly you are not willing to provide one.
Sorry, that's not going to work.
 
Old 02-06-2005, 08:35 AM   #7
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Original Poster
Rep: Reputation: 30
Fine, don't try it, but criticising someone for writing software isn't what this forum is all about.

The points.
1. because i had an itch to scratch
2. because I can't imagine a reason why you'd need to be root to download packages
3. because choosing from a menu is easier than editing config files

This revised version will install downloaded packages.

Code:
#! /usr/bin/python

import urllib,HTMLParser
import os,os.path

homedir = os.path.expanduser('~')
config_folder = homedir + "/.getpkg"
download_folder = config_folder + "/downloads"
installed_file = "installed"
available_file = "available"
country_file = "country"
version_file = "version"

try: fp = os.path.exists(config_folder)
except: fp =0
if not fp:
	os.mkdir(config_folder)

try: fp = os.path.exists(download_folder)
except: fp =0
if not fp:
	os.mkdir(download_folder)

packages_dir="/var/log/packages"
country_list = ["Australia","Austria","Belgium","Brasil","Bulgaria","Canada","Costa Rica","Czech Republic","Finland",\
"France","Germany","Greece","Hong Kong","Hungary","Ireland","Italy","Japan","Korea","Netherlands","Norway","Poland",\
"Portugal","Republic of Moldova","Romania","Russia","Serbia","South Africa","Spain","Sweden","Switzerland","Taiwan",\
"Thailand","UK","USA" ]
discsets = ["a","ap","d","e","f","gnome","k","kde","kdei","l","n","t","tcl","x","xap","extra","pasture","patches"]
slack_versions = ["8.1","9.0","9.1","10.0","current"]
menu = ["Choose Country","Choose Version","Update Installed List","Update Available List","Package Search",\
"List Downloaded Packages","Install Downloaded Packages","Empty Download Directory","Exit"]
mirror_list_url="http://www.slackware.com/getslack/list.php?country="
mirrors = []
available = []

def get_installed_pkgs():
	installed = []
	packages = os.listdir(packages_dir)
	num_packages = len(packages)
	for package in packages:
		parts = package.split("-")
		revision = parts.pop()
		arch = parts.pop()
		version = parts.pop()
		pkgname = '-'.join(parts)
		this_package = {'package' : package,'name' : pkgname,'version' : version,'arch' : arch,'revision' : revision}
		installed.append(this_package)
	return installed
	
def choose_by_index(arr,heading):
	print ""
	print "Choose " + heading
	print "Please enter the number of your choice"
	for i in range(len(arr)):
		print str(i) + " : " + arr[i]
	print ""
	while 1:
		try:
			returnval = raw_input("Enter Index: ")
			intvalue = int(returnval)
			if intvalue >= 0 and intvalue < len(arr): break
			else:
				print "Invalid input, please try again"
				print "Please enter a value between 0 and " + str(len(arr) -1)
		except ValueError:
			print "Invalid input, please try again"
			print "Please enter a value between 0 and " + str(len(arr) -1)
	return returnval

class MyHTMLParser(HTMLParser.HTMLParser):
	
	global mirrors
	
	def handle_starttag(self, tag, attrs):
		if tag == "a" or tag == "A":
			for url in attrs:
			 theHREF = url[1]
			 if theHREF[0:4] == "http" or theHREF[0:4] == "ftp:":
			 	if theHREF != "http://store.slackware.com" and \
			 	theHREF != "http://store.slackware.com/" and \
			 	theHREF != "http://slackware.com/trademark/trademark.php":
			 		# make sure the mirro has trailing /
			 		if theHREF[-1] != "/": theHREF += "/"
			 		mirrors.append(theHREF)

def read_filelist(filelist):
	pkgs = []
	while 1:
		line = filelist.readline()
		if line[-4:-1] == "tgz":
			this_pkg = {}
			words = line[:-1].split(" ")
			this_pkg["filesize"] = words[len(words)-4]
			package = words[len(words)-1]
			path = package[2:]
			this_pkg["path"] = path
			parts = path.split("/")
			if parts[0] == "slackware": disc = parts[1]
			elif parts[0] == "source": break
			else: disc = parts[0]
			pkg = parts[len(parts)-1]
			this_pkg["discset"] = disc
			this_pkg["package"] = pkg
			pkg_parts = pkg[:-4].split("-")
			if len(pkg_parts) >= 4:
				this_pkg['revision'] = pkg_parts.pop()
				this_pkg['arch'] = pkg_parts.pop()
				this_pkg['version'] = pkg_parts.pop()
				this_pkg['name'] = '-'.join(pkg_parts)
			else:
				this_pkg['revision'] = ""
				this_pkg['arch'] = ""
				this_pkg['version'] = ""
				this_pkg['name'] = ""
			pkgs.append(this_pkg)
		if not line: break
	return pkgs
	
inst_path = config_folder + "/" + installed_file
try: fp = os.path.exists(inst_path)
except: fp = 0
if not fp:
	inst_pkglist = get_installed_pkgs()
	output = open(inst_path,'w')
	for pkg in inst_pkglist:
		output.write(str(pkg) + "\n")
	output.close()
else:
	input = open(inst_path,'r')
	inst_pkglist = []
	EOF = False
	while not EOF:
		line = input.readline()
		if line:
			if line[-1] == "\n": line = line[:-1]
			inst_pkglist.append(eval(line))
		else: EOF = True
print str(len(inst_pkglist)) + " packages currently installed"

def choose_country():
	global mirrors
	global country_list
	mirrors = []
	
	while len(mirrors) == 0:
		country_choice = choose_by_index(country_list,"country")
		URL = mirror_list_url + country_list[int(country_choice)]
		        
		f = urllib.urlopen(URL)
		page = f.read()
		p = MyHTMLParser()
		p.feed(page)
		if len(mirrors) == 0: print "No mirrors found in " + country_list[int(country_choice)]
		else:
			print "Available Mirrors in " + country_list[int(country_choice)] + ":"
			for m in mirrors:
				print m	
	return country_list[int(country_choice)]

country_path = config_folder + "/" + country_file
try: fp = os.path.exists(country_path)
except: fp = 0
if fp:
	input = open(country_path,'r')
	country = input.readline()
	#country = country[:-1]
	input.close()
	URL = mirror_list_url + country
	while len(mirrors) == 0:
		f = urllib.urlopen(URL)
		page = f.read()
		p = MyHTMLParser()
		p.feed(page)
		if len(mirrors) == 0: print "No mirrors found in " + country
	print "Available Mirrors in " + country + ":"
	for m in mirrors:
		print m
else:
	country = choose_country()
	output = open(country_path,'w')
	output.write(country)
	output.close()
	

def choose_version():
	global slack_versions
	
	version_choice = choose_by_index(slack_versions,"Slackware Version")
	v = "slackware-" + slack_versions[int(version_choice)] + "/"
	return v

version_path = config_folder + "/" + version_file
try: fp = os.path.exists(version_path)
except: fp = 0
if fp:
	input = open(version_path,'r')
	version = input.readline()
	#version = version[:-1]
	input.close()
else:
	version = choose_version()
	output = open(version_path,'w')
	output.write(version)
	output.close()

def find_avail_packages():	
	global mirrors
	global version
	global available
	available = []
	max = len(mirrors)
	target = version + "FILELIST.TXT"
	ind = 0
	data = 0;
	while not data:
		URL = mirrors[ind] + target
		print "opening " + URL
		try:
			file = urllib.urlopen(URL)
			available = read_filelist(filelist=file)
			data = 1
		except:
			ind += 1
			if ind == max: ind = 0
			data = 0


available_path = config_folder + "/" + available_file
try: fp = os.path.exists(available_path)
except: fp = 0
if fp:
	input = open(available_path,'r')
	EOF = False
	while not EOF:
		line = input.readline()
		if line:
			if line[-1] == "\n": line = line[:-1]
			available.append(eval(line))
		else: EOF = True
else:
	foo = find_avail_packages()
	output = open(available_path,'w')
	for pkg in available:
		output.write(str(pkg) + "\n")
	output.close()

print str(len(available)) + " packages available"

def package_search():
	global available
	global inst_pkglist
	global version
	global mirrors
	global discsets
	global download_folder
	search = []
	search_options = ["Search by Name","Search by Disk Set","Upgrade Installed Packages","Return to Main Menu"]
	YN = ["No","Yes","No to All","Yes to All"]
	search_choice = int(choose_by_index(search_options,"Search Method"))
	if search_choice == 0:
		term = raw_input("Enter Search Term: ")
		for a_pkg in available:
			found_term = a_pkg['name'].find(term,0,len(a_pkg['name']))
			if not found_term:
				a_vers = a_pkg['version']
				a_disc = a_pkg['discset']
				a_package = a_pkg['package']
				a_rev = a_pkg['revision']
				a_arch = a_pkg['arch']
				i_vers = ''
				i_rev = ''
				i_package = ''
				i_arch = ''
				for i_pkg in inst_pkglist:
					if i_pkg['name'] == a_pkg['name']:
						i_vers = i_pkg['version']
						i_rev = i_pkg['revision']
						i_package = i_pkg['package']
						i_arch = i_pkg['arch']
				this_item = {}
				this_item['name'] = a_pkg['name']
				this_item['avail-vers'] = a_vers
				this_item['avail-arch'] = a_arch
				this_item['avail-rev'] = a_rev
				this_item['avail-pkg'] = a_package
				this_item['avail-disc'] = a_disc
				this_item['avail-path'] = a_pkg['path']
				this_item['inst-vers'] = i_vers
				this_item['inst-arch'] = i_arch
				this_item['inst-rev'] = i_rev
				this_item['inst-pkg'] = i_package
				search.append(this_item)
		if len(search) == 0:
			print "No results found for '" + term + "'"
				
	elif search_choice == 1:
		disc_choice = int(choose_by_index(discsets,"Disk Set"))
		theDiskSet = discsets[disc_choice]
		for a_pkg in available:
			if a_pkg['discset'] == theDiskSet:
				a_vers = a_pkg['version']
				a_disc = a_pkg['discset']
				a_package = a_pkg['package']
				a_rev = a_pkg['revision']
				a_arch = a_pkg['arch']
				i_vers = ''
				i_rev = ''
				i_package = ''
				i_arch = ''
				for i_pkg in inst_pkglist:
					if i_pkg['name'] == a_pkg['name']:
						i_vers = i_pkg['version']
						i_rev = i_pkg['revision']
						i_package = i_pkg['package']
						i_arch = i_pkg['arch']
				this_item = {}
				this_item['name'] = a_pkg['name']
				this_item['avail-vers'] = a_vers
				this_item['avail-arch'] = a_arch
				this_item['avail-rev'] = a_rev
				this_item['avail-pkg'] = a_package
				this_item['avail-disc'] = a_disc
				this_item['avail-path'] = a_pkg['path']
				this_item['inst-vers'] = i_vers
				this_item['inst-arch'] = i_arch
				this_item['inst-rev'] = i_rev
				this_item['inst-pkg'] = i_package
				search.append(this_item)
		if len(search) == 0:
			print "No results found for '" + theDiskSet + "'"
	elif search_choice == 2:
		for i_pkg in inst_pkglist:
			for a_pkg in available:
				if i_pkg['name'] == a_pkg['name']:
					if not (i_pkg['version'] == a_pkg['version'] and i_pkg['revision'] == a_pkg['revision']):
						this_item = {}
						this_item['name'] = a_pkg['name']
						this_item['avail-vers'] = a_pkg['version']
						this_item['avail-arch'] = a_pkg['arch']
						this_item['avail-rev'] = a_pkg['revision']
						this_item['avail-pkg'] = a_pkg['package']
						this_item['avail-disc'] = a_pkg['discset']
						this_item['avail-path'] = a_pkg['path']
						this_item['inst-vers'] = i_pkg['version']
						this_item['inst-arch'] = i_pkg['arch']
						this_item['inst-rev'] = i_pkg['revision']
						this_item['inst-pkg'] = i_pkg['package']
						search.append(this_item)
		if len(search) == 0:
			print "No packages available for upgrade"		
	elif search_choice == 3:
		main_menu()

	if len(search) != 0:
			download = []
			skip_all = 0
			dl_all = 0
			print str(len(search)) + " package(s) found"
			for found in search:
				have_it = 0
				print ""
				if found['avail-vers'] == found['inst-vers'] and found['avail-rev'] == found['inst-rev']:
					print "latest version of " + found['name'] + " is installed"
					have_it = 1
				elif found['inst-pkg'] == '':
					print found['name'] + " is not installed"
					if skip_all == 0 and dl_all == 0: dl = int(choose_by_index(YN,"to Download?"))
					else: dl = 0
					if dl == 1: download.append(found)
					if dl == 2: skip_all = 1
					if dl == 3: dl_all = 1
				else:
					print "a different version of " + found['name'] + " is installed"
					print "Installed : " + found['inst-pkg']
					print "Available : " + found['avail-pkg']
					if skip_all == 0 and dl_all == 0: dl = int(choose_by_index(YN,"to Download?"))
					else: dl = 0
					if dl == 1: download.append(found)
					if dl == 2: skip_all = 1
					if dl == 3: dl_all = 1
				if dl_all == 1 and have_it == 0: download.append(found)
			if len(download) != 0:
				print ""
				print str(len(download)) + " package(s) to download"
				for pkg in download:
					ind = 0
					max = len(mirrors)
					data = 0
					while not data:
						try:
							URL = mirrors[ind] + version + pkg['avail-path']
							print "downloading " + pkg['avail-pkg'] + " from " + mirrors[ind]
							wget_cmd = "wget -c " + URL
							os.system(wget_cmd)
							fp = os.path.exists(pkg['avail-pkg'])
							if fp:
								#file was downloaded
								data = 1	
								# move file to downloads folder & delete wget_log
								move_cmd = "mv " + pkg['avail-pkg'] + " " + download_folder
								os.system(move_cmd)
								fp2 = os.path.exists('wget-log')
								if fp2: os.system('rm wget-log')
							else:
								#file was not downloaded
								data = 0
								ind += 1
								if ind == max: ind = 0
								
						except:
							ind += 1
							if ind == max: ind = 0
							data = ""

def list_downloaded_pkgs():
	downloaded = os.listdir(download_folder)
	if len(downloaded) == 0:
		print "Download folder is empty"
		return
	for pkg in downloaded: print pkg

def install_downloaded_pkgs():
	downloaded = os.listdir(download_folder)
	if len(downloaded) == 0:
		print "Download folder is empty"
		return
	for pkg in downloaded:
		inst_cmd = "su -c 'upgradepkg --install-new " + download_folder + "/" + pkg + "'"
		os.system(inst_cmd)

def rm_downloaded_pkgs():
	downloaded = os.listdir(download_folder)
	if len(downloaded) == 0:
		print "Download folder is empty"
		return
	for pkg in downloaded:
		rm_cmd = "rm " + download_folder + "/" + pkg
		os.system(rm_cmd)
	print str(len(downloaded)) + " packages removed"
	
def main_menu():
	global menu
	global inst_pkglist
	global inst_path
	global country_list
	global country_path
	global version
	global version_path
	
	menu_choice = int(choose_by_index(menu,"Options"))
	
	if menu_choice == 0:
		country = ""
		country = choose_country()
		os.unlink(country_path)
		output = open(country_path,'w')
		output.write(country)
		output.close()
	
	elif menu_choice == 1:
		version = choose_version()
		os.unlink(version_path)
		output = open(version_path,'w')
		output.write(version)
		output.close()
	
	elif menu_choice == 2:
		inst_pkglist = get_installed_pkgs()
		os.unlink(inst_path)
		output = open(inst_path,'w')
		for pkg in inst_pkglist:
			output.write(str(pkg) + "\n")
		output.close()
		print str(len(inst_pkglist)) + " packages currently installed"
		
	elif menu_choice == 3:
		foo = find_avail_packages()
		os.unlink(available_path)
		output = open(available_path,'w')
		for pkg in available:
			output.write(str(pkg) + "\n")
		output.close()
		print str(len(available)) + " packages available"
	
	elif menu_choice == 4:
		package_search()
	
	elif menu_choice == 5:
		list_downloaded_pkgs()
	
	elif menu_choice == 6:
		install_downloaded_pkgs()
	
	elif menu_choice == 7:
		rm_downloaded_pkgs()
		
	elif menu_choice == 8:
		return
	
	main_menu()

main_menu()
 
Old 02-06-2005, 09:05 AM   #8
cccc828
Member
 
Registered: Feb 2004
Location: Austria
Distribution: Slackware
Posts: 95

Rep: Reputation: 15
I do not know LQ's rules on posting masses of useless text, it is their traffic, but I know that I do not want to download hundreds of useless bytes just to follow this discussion. Could you maybe put the script somewhere on the internet and simply post the link?
 
Old 02-07-2005, 06:58 PM   #9
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Original Poster
Rep: Reputation: 30
I've seen other scripts posted here, but I'm not one to argue. I put it on sourceforge.

http://sourceforge.net/projects/getpkg/
 
Old 02-07-2005, 08:17 PM   #10
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally posted by uselpa
Still, what's the point? Why should I use that instead of slackpkg for example, which is officially supported and can also install if requested?
that's totally beside the point... this was datadriven's first python program and he was just looking for people to give it a shot and perhaps even share some CONSTRUCTIVE feedback... this holier-than-though thing you have going on here is totally uncalled for... geez man, how can you be so insensitive???
Quote:
Given the large amount of open source software, I do not have the time to try them all unless I see a point. I can't see it here, and more importantly you are not willing to provide one.
Sorry, that's not going to work.
there you go again... dude, it's fairly OBVIOUS that the main point was the getting constructive feedback... and if you don't have the time or the desire to try his script then don't, but there's no need to be so anal-retentive about it by posting useless and unfounded critisism... don't you realize how ridiculous it is to ask for "reasons" to try someone's first python script???

don't worry datadriven, we're not all like this here at LQ...

anyways, it's true that posts as huge as your code can sometimes be inconvenient, so it's nice that you set-up an account at sf.net for your project and linked it here... i hope you keep hacking your script and continue on your python journey... don't let the negativity emanated by some folks bring you down...

i have no python knowledge, hence i can't contribute any actual code, but i'll try to give you some end-user feedback if i can, once i get around to trying your script... keep up the good work man!!! and keep coding!!!

=)

Last edited by win32sux; 02-07-2005 at 08:32 PM.
 
Old 02-07-2005, 08:29 PM   #11
mdarby
Member
 
Registered: Nov 2004
Location: Columbus, Ohio
Distribution: Slackware-Current / Debian
Posts: 795

Rep: Reputation: 30
This thread showcases the difference between programmers and non-programmers.

I, for one, am a programmer. Thank you for sharing your code with us; I hope that one day it will replace swaret or slapt-get.

Programming isn't just about filling a need, it's about quenching curiousity.
 
Old 02-07-2005, 08:34 PM   #12
mdarby
Member
 
Registered: Nov 2004
Location: Columbus, Ohio
Distribution: Slackware-Current / Debian
Posts: 795

Rep: Reputation: 30
BTW, I get this when I attempt to run your script (downloaded form SF)

Code:
Traceback (most recent call last):
  File "./getpkg", line 124, in ?
    inst_pkglist = get_installed_pkgs()
  File "./getpkg", line 45, in get_installed_pkgs
    version = parts.pop()
IndexError: pop from empty list
 
Old 02-07-2005, 10:59 PM   #13
datadriven
Member
 
Registered: Jun 2003
Location: Holly Hill, Florida
Distribution: Slackware 10.1
Posts: 317

Original Poster
Rep: Reputation: 30
The script determines what packages you have installed by reading /var/log/packages. Have you changed the permissions on that folder?
 
Old 02-08-2005, 05:38 AM   #14
mdarby
Member
 
Registered: Nov 2004
Location: Columbus, Ohio
Distribution: Slackware-Current / Debian
Posts: 795

Rep: Reputation: 30
Code:
drwxr-xr-x  2 root root    18K 2005-01-31 09:18 packages/
 
Old 02-08-2005, 08:31 AM   #15
mdarby
Member
 
Registered: Nov 2004
Location: Columbus, Ohio
Distribution: Slackware-Current / Debian
Posts: 795

Rep: Reputation: 30
Update: An updated version will be available at the above SF link that works flawlessly for my setup (The earlier bug listed above has been squashed).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Uninstall a python package danesp Linux - Newbie 8 06-01-2005 11:25 PM
installing Python 2.4 package on Suse 9.1 asilentmurmur SUSE / openSUSE 7 03-06-2005 11:35 AM
where can i find the python-devel package ? brokenflea Slackware 10 02-13-2005 08:47 AM
Making a Python program gamehack Programming 0 04-11-2004 05:12 AM
noddy program suggestions for python acid_kewpie Programming 0 07-17-2002 07:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration