import os
import urllib
import subprocess
import sys


def which(bin):
	found = False
	for path in os.environ["PATH"].split(os.pathsep):
		path = path.strip('"')
		if os.path.isfile( os.path.join(path, bin) ):
			found = True
			break
	return found


if os.geteuid() != 0:
	sys.stdout.write("PiRadio setup requires root priveleges.\n")
	sys.exit(1)
	
	
# Import APT
try:
	import apt
except ImportError:
	sys.stdout.write("\nPiRadio setup requires python-apt to be installed.\n")
	sys.exit(1)

# Prep APT cache for usage
sys.stdout.write("Getting APT cache... ")
sys.stdout.flush()
apt_cache = apt.cache.Cache()
apt_cache.update()
sys.stdout.write("done\n\n")
sys.stdout.flush()

# Install missing APT packages
apt_commit = False
for package_name in ["python-dev","vlc"]:
	apt_package = apt_cache[package_name]
	if not apt_package.is_installed:
		sys.stdout.write("Marking "+package_name+" for install\n")
		sys.stdout.flush()
		apt_package.mark_install()
		apt_commit = True
if apt_commit:
	try:
		apt_cache.commit(install_progress=None)
	except Exception, arg:
		sys.stdout.write("APT install fail\n")
	else:
		sys.stdout.write("APT install success\n")
	sys.stdout.write("\n")
	sys.stdout.flush()
	
	
# Install pip if missing
if which("pip") == False:
	sys.stdout.write("Downloading pip installer... ")
	sys.stdout.flush()
	if os.path.isfile("get-pip.py"):
		os.remove("get-pip.py")
	urllib.urlretrieve("https://bootstrap.pypa.io/get-pip.py", "get-pip.py")
	if not os.path.isfile("get-pip.py"):
		sys.stdout.write("fail\n")
	else:
		sys.stdout.write("success\n")
		sys.stdout.write("Installing pip...\n")
		sys.stdout.flush()
		os.system("sudo python get-pip.py")
		if os.path.isfile("get-pip.py"):
			os.remove("get-pip.py")
		if which("pip") == False:
			sys.stdout.write("pip install fail\n")
		else:
			sys.stdout.write("pip install success\n")
	sys.stdout.write("\n")
	sys.stdout.flush()
	
	
# Installing missing pip packages
if which("pip") == True:
	pip_list = subprocess.check_output("pip list", shell=True)
	for package_name in ["cherrypy","formencode","genshi"]:
		found = False
		for line in pip_list.splitlines():
			if line.split(" ")[0].lower() == package_name.lower():
				found = True
				break
		if found == False:
			sys.stdout.write("Installing "+package_name+"...\n")
			sys.stdout.flush()
			os.system("sudo pip install "+package_name)
		else:
			sys.stdout.write("Upgrading "+package_name+"...\n")
			sys.stdout.flush()
			os.system("sudo pip install --upgrade "+package_name)
	sys.stdout.write("\n")
	sys.stdout.flush()
@@ -352,143 +352,818 @@
 %09%0A# 
-Import APT%0Atry:%0A%09import apt%0Aexcept ImportError:%0A%09sys.stdout.write(%22%5CnPiRadio setup requires python-apt to be installed.%5Cn%22)%0A%09sys.exit(1
+Check for i2c_dev module in startup%0Amodules = subprocess.check_output(%22cat /proc/modules%22, shell=True)%0Aif not %22i2c_dev%22 in modules:%0A%09with open('/etc/modules', 'r+') as f:%0A%09%09if not %22i2c-dev%22 in f.read():%0A%09%09%09sys.stdout.write(%22I2C module is not loaded and is not in /etc/modules, adding...%5Cn%22)%0A%09%09%09sys.stdout.flush()%0A%09%09%09f.write(%22i2c-dev%5Cn%22)%0A%09%09%09sys.stdout.write(%22Reboot recommended.%5Cn%22)%0A%09%09%09sys.stdout.flush()%0A%09%0A%09%0A# Import/install+import APT%0Atry:%0A%09import apt%0Aexcept ImportError:%0A%09sys.stdout.write(%22Installing python-apt...%5Cn%22)%0A%09sys.stdout.flush()%0A%09os.system(%22sudo apt-get install python-apt%22)%0A%09try:%0A%09%09import apt%0A%09except ImportError:%0A%09%09sys.stdout.write(%22python-apt install fail%5Cn%22)%0A%09%09sys.stdout.flush()%0A%09%09sys.exit(1)%0A%09else:%0A%09%09sys.stdout.write(%22python-apt install success%5Cn%22)%0A%09sys.stdout.write(%22%5Cn%22)%0A%09sys.stdout.flush(
 )%0A%0A#
@@ -1432,16 +1432,31 @@
 n-dev%22,%22
+python-smbus%22,%22
 vlc%22%5D:%0A%09
@@ -2754,16 +2754,24 @@
 %22genshi%22
+,%22dot3k%22
 %5D:%0A%09%09fou

Check for i2c_dev loaded / in startup, added python-apt install support, added Display-O-Tron 3000 requirements.