# low_backup module # Authors: Nicholas Alexander && Otacilio Lacerda import commands import os BATTERY = os.environ['BATTERY_PATH'] + 'battery.py' EXECUTE = "./" USER_HOST = "root" def queryProductInformation(deviceIp): """ Update device name by getting device product name and os version informations. Use osso-product-info command to get the device and device OS short names and set each to it correspondent attribute. """ info = commands.getoutput("ssh -l %s %s osso-product-info" % (USER_HOST, deviceIp)) deviceName = _extractOssoInfo(info, "shortName") deviceOs = _extractOssoInfo(info, "shortOS") ossoVersion = _extractOssoInfo(info, "ossoVersion") if deviceName != -1 and deviceOs != -1: deviceName = deviceName.strip("'") deviceOs = deviceOs.strip("'") else: deviceName = "NO INFORMATION" deviceOs = "NO INFORMATION" return (deviceName, deviceOs, ossoVersion) def queryDeviceStorage(deviceIp): """Returns a list of tuples, each tuple representing a memory status. Tuples are in this format: (total, used) Returns: mem_infos -- List with all tuples holding memory info """ info = commands.getoutput("ssh -l root %s df" % deviceIp).splitlines() mem_infos = [-1, -1, -1] for line in info: if line.find("/dev/mtdblock4") != -1: if line[-1] == "/": total_used = _get_memory(line, "/dev/mtdblock4") mem_infos.pop(0) mem_infos.insert(0, total_used) elif line.find("/media/mmc1") != -1: total_used = _get_memory(line, "/dev/mmcblk0p1") mem_infos.pop(1) mem_infos.insert(1, total_used) elif line.find("/media/mmc2") != -1: total_used = _get_memory(line, "/dev/mmcblk1p1") mem_infos.pop(2) mem_infos.insert(2, total_used) return mem_infos def queryDeviceBattery(deviceIp): """Return device current battery status in a string. This method runs a python script in the device that returns the battery status, this status is represented by one string with the percentage of battery current charge or the number -1 case battery is charging. Returns: text -- Text with the battery status """ # Calls script that returns device battery status os.system("scp %s %s@%s:/tmp" % (BATTERY, USER_HOST, deviceIp)) battery_status = commands.getoutput("ssh -l %s %s /usr/bin/python \ /tmp/battery.py" % (USER_HOST, deviceIp)) return battery_status def _get_memory(line, path): """Retrieve and return total and used memory information from the given line using the memory path to retrieve the right information. This function is to be used with a line of the return of a df command. Arguments: line -- The line where the memory information is path -- The path in the begining of the line Returns: total -- Total memory used -- Amount of used memory """ number_of_infos = 0 i = len(path) + 1 while number_of_infos < 2: char = line[i] if char != " ": start = i end = line.find(" ", start + 1) if number_of_infos == 0: total = line[start: end] elif number_of_infos == 1: used = line[start: end] i = end number_of_infos += 1 i += 1 return total, used def _extractOssoInfo(osso_string, info_type="name"): """Read the osso-product-info command return string and extract the needed info depeding on info_type argument. Arguments: osso_string -- The string returned by osso-product-info command info_type -- the kind of information to b extracted, can be: name - returns device full name (default) OS - returns device OS full name shortName - returns device short name shortOS - returns device short OS name Returns: info -- String with the needed information -1 -- Case the information couldn't be found in the given string """ detailed_type = "" if info_type == "shortName": detailed_type = "OSSO_PRODUCT_NAME" elif info_type == "shortOS": detailed_type = "OSSO_PRODUCT_RELEASE_NAME" elif info_type == "name": detailed_type = "OSSO_PRODUCT_FULL_NAME" elif info_type == "OS": detailed_type = "OSSO_PRODUCT_RELEASE_FULL_NAME" elif info_type == "ossoVersion": detailed_type = "OSSO_VERSION" else: detailed_type = "OSSO_PRODUCT_FULL_NAME" types_list = osso_string.splitlines() info = -1 for type_line in types_list: if type_line.startswith(detailed_type): # The second argument is the information itself since informations # are displayed like: OSSO_PRODUCT_RELEASE_NAME='OS 2008' info = type_line.split("=")[1] return info