Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created January 5, 2011 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btbytes/766469 to your computer and use it in GitHub Desktop.
Save btbytes/766469 to your computer and use it in GitHub Desktop.
python subprocess example
#!/usr/bin/env python
# example table.py
import pygtk
pygtk.require('2.0')
import gtk
import subprocess
import time
import gobject
gup=1
class Table:
# Our callback.
# The data passed to this method is printed to stdout
def updategprs(self,label):
global gup
print "update grps label"
output=subprocess.Popen(["gprsstats"], stdout=subprocess.PIPE).communicate()[0]
label.set_text(output)
if gup == 1:
return True
else:
label.set_text("")
return False
def callback(self, widget, data=None):
print "Hello again - %s was pressed" % data
def gps_switch(self,button,data):
# TODO
#if (os.path.isfile("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-gps.0/pwron")):
# output=subprocess.Popen(["cat", "/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-gps.0/pwron"], stdout=subprocess.PIPE).communicate()[0]
#else:
# output=subprocess.Popen(["cat", "/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-gps.0/power_on"], stdout=subprocess.PIPE).communicate()[0]
output=subprocess.Popen(["cat", "/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-gps.0/pwron"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
output=int(output)
if output==1:
button.set_label("GPS is off")
ofile = open("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-gps.0/pwron", "wb")
change=subprocess.Popen(["echo","0"], stdout=ofile)
ofile.close()
else:
button.set_label("GPS is on")
ofile = open("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-gps.0/pwron", "wb")
change=subprocess.Popen(["echo","1"], stdout=ofile)
ofile.close()
time.sleep(1)
subprocess.Popen(["agps"])
def bluetooth_switch(self,button,data):
output=subprocess.Popen(["cat", "/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/voltage_ldo4"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
output=int(output)
ofile = open("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-bt.0/power_on", "wb")
ofile2 = open("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-bt.0/reset", "wb")
if output>0:
button.set_label("BlueTooth is off")
change=subprocess.Popen(["echo","0"], stdout=ofile)
change=subprocess.Popen(["echo","1"], stdout=ofile2)
change=subprocess.Popen(["echo","0"], stdout=ofile2)
subprocess.Popen(["sh","-c","/etc/init.d/bluetooth stop"])
else:
subprocess.Popen(["sh","-c","/etc/init.d/bluetooth start"])
button.set_label("BlueTooth is on")
change=subprocess.Popen(["echo","1"], stdout=ofile)
change=subprocess.Popen(["echo","1"], stdout=ofile2)
change=subprocess.Popen(["echo","0"], stdout=ofile2)
ofile.close()
ofile2.close()
def gsm_switch(self,button,data):
output=subprocess.Popen(["cat", "/sys/devices/platform/neo1973-pm-gsm.0/power_on"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
output=int(output)
ofile = open("/sys/devices/platform/neo1973-pm-gsm.0/power_on", "wb")
if output>0:
button.set_label("GSM is off")
change=subprocess.Popen(["echo","0"], stdout=ofile)
else:
button.set_label("GSM is on")
change=subprocess.Popen(["echo","1"], stdout=ofile)
ofile.close()
def wifi_switch(self,button,data):
output=subprocess.Popen(["wifistatus"], stdout=subprocess.PIPE).communicate()[0]
print output
print output.find("off")
if output.find("off") == -1:
button.set_label("Wifi is off")
change=subprocess.Popen(["iwconfig", "eth0","txpower","off"], stdout=subprocess.PIPE)
else:
button.set_label("Wifi is on")
change=subprocess.Popen(["iwconfig", "eth0","txpower","auto"], stdout=subprocess.PIPE)
def gprs_switch(self,button,label):
global gup
output=subprocess.Popen(["grep", "ppp", "/proc/net/dev"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
print "Requested GPRS off"
subprocess.Popen(["gprsoff"])
time.sleep(5)
output=subprocess.Popen(["grep", "ppp", "/proc/net/dev"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
label.set_text("FAILED")
gup=0
else:
button.set_label("GPRS is off")
gup=0
label.set_text("")
else:
print "Requested GPRS on"
subprocess.Popen(["gprson"])
mytimer=time.time()
while time.time() < mytimer + 30:
time.sleep(1)
print "Waiting for pppd"
output=subprocess.Popen(["grep", "ppp", "/proc/net/dev"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
output=subprocess.Popen(["gprsqison"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
break
if output == "":
label.set_text("FAILED")
else:
button.set_label("GPRS is on")
label.set_text("Initializing...")
gobject.timeout_add(5000,self.updategprs,label)
# This callback quits the program
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def resetusb0(self,widget):
subprocess.Popen(["sh","-c","ifdown usb0 && ifup usb0"])
def restartaction(self,widget):
subprocess.Popen(["reboot"])
def __init__(self):
# Create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
# Set the window title
self.window.set_title("Services")
self.window.set_default_size(480,640)
# Set a handler for delete_event that immediately
# exits GTK.
self.window.connect("delete_event", self.delete_event)
# Sets the border width of the window.
self.window.set_border_width(20)
# Create a 2x2 table
table = gtk.Table(4, 3, True)
table2 = gtk.Table(2, 1, True)
table3 = gtk.Table(2, 1, True)
table.attach (table2,2,3,0,1,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
table.attach (table3,0,1,0,1,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
# Put the table in the main windowgtk theme modify font size
self.window.add(table)
# Create GPS button
button = gtk.Button("GPS")
# When the button is clicked, we call the "callback" method
# with a pointer to "button 1" as its argument
output=subprocess.Popen(["cat", "/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/neo1973-pm-gps.0/pwron"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
output=int(output)
if output==1:
button.set_label("GPS is on")
else:
button.set_label("GPS is off")
button.connect("clicked", self.gps_switch, "gps")
# Insert button 1 into the upper left quadrant of the table
table.attach(button, 0, 1, 1, 2,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
# Create BlueTooth button
button = gtk.Button("BlueTooth")
# When the button is clicked, we call the "callback" method
# with a pointer to "button 2" as its argument
output=subprocess.Popen(["cat", "/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/voltage_ldo4"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
output=int(output)
if output>0:
button.set_label("BlueTooth is on")
else:
button.set_label("BlueTooth is off")
button.connect("clicked", self.bluetooth_switch,"bluetooth")
# Insert button 2 into the upper right quadrant of the table
table.attach(button, 1, 2, 2, 3,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
# Create Wifi button
button = gtk.Button("Wifi")
# When the button is clicked, we call the "callback" method
# with a pointer to "button 2" as its argument
output=subprocess.Popen(["wifistatus"], stdout=subprocess.PIPE).communicate()[0]
if output.find("off") == -1:
button.set_label("Wifi is on")
else:
button.set_label("Wifi is off")
button.connect("clicked", self.wifi_switch,"Wifi")
# Insert button 2 into the upper right quadrant of the table
table.attach(button, 2, 3, 1, 2,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
# Create GSM button
button = gtk.Button("GSM")
# When the button is clicked, we call the "callback" method
# with a pointer to "button 2" as its argument
output=subprocess.Popen(["cat", "/sys/devices/platform/neo1973-pm-gsm.0/power_on"], stdout=subprocess.PIPE).communicate()[0]
if output != "":
output=int(output)
if output>0:
button.set_label("GSM is on")
else:
button.set_label("GSM is off")
button.connect("clicked", self.gsm_switch,"GSM")
# Insert button 3 into the upper right quadrant of the table
table3.attach(button, 0, 1, 0, 1,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
# Create GPRS button
button = gtk.Button("GPRS")
label = gtk.Label("")
table2.attach(label, 0, 1, 1, 2,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
label.show()
button.connect("clicked", self.gprs_switch,label)
table2.attach(button, 0, 1, 0, 1,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
output=subprocess.Popen(["grep", "ppp", "/proc/net/dev"], stdout=subprocess.PIPE).communicate()[0]
if output!="":
button.set_label("GPRS is on")
gobject.timeout_add(5000,self.updategprs,label)
else:
button.set_label("GPRS is off")
# ptt create reset usb0 button
button = gtk.Button("Reset usb0")
button.connect("clicked", self.resetusb0)
table.attach(button, 0, 1, 3, 4,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
# Create "Quit" button
button = gtk.Button("Close")
# When the button is clicked, we call the main_quit function
# and the program exits
button.connect("clicked", lambda w: gtk.main_quit())
# Insert the quit button into the both lower quadrants of the table
table.attach(button, 0, 3, 4, 5,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
button = gtk.Button("Reboot FR")
# Create "Reboot" button
button = gtk.Button("Reboot FR")
# When the button is clicked, we call the main_quit function
# and the program exits
button.connect("clicked", self.restartaction)
# Insert the quit button into the both lower quadrants of the table
table.attach(button, 2, 3, 3, 4,xoptions=gtk.EXPAND, yoptions=gtk.EXPAND)
button.show()
table.show()
table2.show()
table3.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
Table()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment