Last update of gforge repository
[tablet-suite] / src / pcsdeviceutils.py
1 # low_backup module
2 # Authors: Nicholas Alexander && Otacilio Lacerda
3
4 import commands
5 import os
6
7 BATTERY = os.environ['BATTERY_PATH'] + 'battery.py'
8 EXECUTE = "./"
9 USER_HOST = "root"
10
11 def queryProductInformation(deviceIp):
12     """ Update device name by getting device product name and os version
13     informations.
14
15     Use osso-product-info command to get the device and device OS short
16     names and set each to it correspondent attribute.
17
18     """
19
20     info = commands.getoutput("ssh -l %s %s osso-product-info" %
21                                         (USER_HOST, deviceIp))
22
23     deviceName = _extractOssoInfo(info, "shortName")
24     deviceOs = _extractOssoInfo(info, "shortOS")
25     ossoVersion = _extractOssoInfo(info, "ossoVersion")
26     if deviceName != -1 and deviceOs != -1:
27         deviceName = deviceName.strip("'")
28         deviceOs = deviceOs.strip("'")
29     else:
30         deviceName = "NO INFORMATION"
31         deviceOs = "NO INFORMATION"
32
33     return (deviceName, deviceOs, ossoVersion)
34
35 def queryDeviceStorage(deviceIp):
36     """Returns a list of tuples, each tuple representing a memory status.
37
38     Tuples are in this format: (total, used)
39
40     Returns:
41         mem_infos -- List with all tuples holding memory info
42
43     """
44     info = commands.getoutput("ssh -l root %s df" %
45                               deviceIp).splitlines()
46     mem_infos = [-1, -1, -1]
47     for line in info:
48         if line.find("/dev/mtdblock4") != -1:
49             if line[-1] == "/":
50                 total_used = _get_memory(line, "/dev/mtdblock4")
51                 mem_infos.pop(0)
52                 mem_infos.insert(0, total_used)
53         
54         elif line.find("/media/mmc1") != -1:
55             total_used = _get_memory(line, "/dev/mmcblk0p1")
56             mem_infos.pop(1)
57             mem_infos.insert(1, total_used)
58         
59         elif line.find("/media/mmc2") != -1:
60             total_used = _get_memory(line, "/dev/mmcblk1p1")
61             mem_infos.pop(2)
62             mem_infos.insert(2, total_used)
63  
64     return mem_infos
65
66 def queryDeviceBattery(deviceIp):
67     """Return device current battery status in a string.
68
69     This method runs a python script in the device that returns the battery
70     status, this status is represented by one string with the percentage of
71     battery current charge or the number -1 case battery is charging.
72
73     Returns:
74     text -- Text with the battery status
75
76     """
77
78     # Calls script that returns device battery status    
79     os.system("scp %s %s@%s:/tmp" % (BATTERY, USER_HOST, deviceIp))
80     battery_status = commands.getoutput("ssh -l %s %s /usr/bin/python \
81                                         /tmp/battery.py" % (USER_HOST, 
82                                                             deviceIp))
83     return battery_status
84
85 def _get_memory(line, path):
86     """Retrieve and return total and used memory information from the given 
87     line using the memory path to retrieve the right information.
88
89     This function is to be used with a line of the return of a df command.
90
91     Arguments:
92     line -- The line where the memory information is
93     path -- The path in the begining of the line
94
95     Returns:
96     total -- Total memory
97     used -- Amount of used memory
98
99     """
100     number_of_infos = 0
101     i = len(path) + 1
102     while number_of_infos < 2:
103         char = line[i]
104         if char != " ":
105             start = i
106             end = line.find(" ", start + 1)
107             if number_of_infos == 0:
108                 total = line[start: end]
109             elif number_of_infos == 1:
110                 used = line[start: end]
111             i = end
112             number_of_infos += 1
113         i += 1
114     return total, used
115
116 def _extractOssoInfo(osso_string, info_type="name"):
117     """Read the osso-product-info command return string and extract the
118     needed info depeding on info_type argument.
119
120     Arguments:
121     osso_string -- The string returned by osso-product-info command
122     info_type -- the kind of information to b extracted, can be:
123         name - returns device full name (default)
124         OS - returns device OS full name
125         shortName - returns device short name
126         shortOS - returns device short OS name
127
128     Returns:
129     info -- String with the needed information
130     -1 -- Case the information couldn't be found in the given string
131
132     """
133     detailed_type = ""
134     if info_type == "shortName":
135         detailed_type = "OSSO_PRODUCT_NAME"
136     elif info_type == "shortOS":
137         detailed_type = "OSSO_PRODUCT_RELEASE_NAME"
138     elif info_type == "name":
139         detailed_type = "OSSO_PRODUCT_FULL_NAME"
140     elif info_type == "OS":
141         detailed_type = "OSSO_PRODUCT_RELEASE_FULL_NAME"
142     elif info_type == "ossoVersion":
143         detailed_type = "OSSO_VERSION"
144     else:
145         detailed_type = "OSSO_PRODUCT_FULL_NAME"
146
147     types_list = osso_string.splitlines()
148     info = -1
149     for type_line in types_list:
150         if type_line.startswith(detailed_type):
151             # The second argument is the information itself since informations
152             # are displayed like: OSSO_PRODUCT_RELEASE_NAME='OS 2008'
153             info = type_line.split("=")[1]
154
155     return info