def __init__(self, parent, device_path, model, connection): """ Crea la estructura de directorios Crea el fichero de configuración de gnokii para el dispositivo pasado Inicia el proceso de smsd Pueblo y vigilo la cola interna Notifico errores al metaserver """ self._smsd = None self.parent = parent self.device_path = device_path device_name = self.device_path.lstrip("/dev/") self.config_section = "%s.%s" % (device_name, model) self.model = model self.connection = connection or "serial" self.config_file = "gnokii" + device_path.replace("/", ".") self.make_config_file() self.description = None debug(self.get_description()) self.configure_dirs() self._keep_running = True self.monitor(self)
def get_description(self): if not self.description: proc = self.gnokii("--identify") self.description = {} for line in proc.stdout.readlines(): key, value = line.split(":") self.description[key.strip()] = value.strip() if self.description["IMEI"].isdigit(): return self.description else: debug("%s has no IMEI?" % self.device_path) return
def send(self, command, *args): """ Sends string to the server. This is a low level tool, try yo use the specific method insteat. """ if self.is_alive(): command = " ".join([command] + list(args)) debug(command) self._proc.stdin.write(command) return self.get_result() else: raise IOError("Server is not alive")
def getmms(self, memory_type, start, end='', format="human"): """ Gets MMS messages from specified memory type starting at entry start and ending at end. :format: output format, could be "human" (human redeable), "pdu" (binary as received by the phone or "raw" (as read from the phone). """ file = mktemp(".smsd") if format != "human": assert format in ("pdu", "raw") format = "--%s" % format result = self.send('--getmms', memory_type, start, end, format, file, '--overwrite', EOL) debug(result) return open(file).read()
def close(self): """ Espera a que el servidor finalice sus tareas Mata al servidor Limpia el entorno """ self.wait() for pause in xrange(15): if self._smsd is None: debug("smsd was no active") return elif self._smsd.returncode: break else: time.sleep(1) self._smsd.kill() return self.is_alive()
def get_result(self): """ Read and parse the server output. """ lasttime = time.time() result = None output = "" while not result and self.is_alive(): if READ_TIMEOUT < (time.time() - lasttime): debug("TIMEOUT") break try: new = self._proc.stdout.read() except IOError, e: debug("Waiting stdout") if e.errno != 11: raise else: time.sleep(READ_PAUSE) else: debug("Added to output: %s" % new) output += new lasttime = time.time() result = re.match(RESULT_RE, output)
def monitor(self): """ WTF, smsd doesnt accept the --config option, we must improvise. A global config file has a section by /dev/*-model device. e.g.: [phone_ttyUSB0-AT-HW] model = AT-HW We run smsd with the -t "devicename-model". """ #TODO: Report this ^^^ bug while True: if self._keep_running and not self.is_alive(): environ = {} environ["PWD"] = self.pathbase pprint(environ) command = ["smsd", "--logfile", self.log_path, "--phone", self.config_section, "-c", self.outbox_path, "-m", "file", "-u", os.path.join(self.parent.pathbase, "bin/handler.sh")] debug(" ".join(command)) stdout = open(os.path.join(self.pathbase, "stdout"), "a") stdout.write(" ".join(command) + "\n") stderr = open(os.path.join(self.pathbase, "stderr"), "w") self._smsd = Popen(command, stdout=stdout, stderr=stderr, shell=True) self._smsd.wait() else: debug("Server:Monitor: %s, %s" % (self._keep_running, self.is_alive())) time.sleep(5)
def main(options, args): debug(options, args) return 0
return optparser.parse_args() def main(options, args): debug(options, args) return 0 if __name__ == "__main__": # == Reading the options of the execution == options, args = get_options() error = Verbose(options.verbose - options.quiet + 2, "E: ") warning = Verbose(options.verbose - options.quiet + 1, "W: ") info = Verbose(options.verbose - options.quiet + 0) moreinfo = Verbose(options.verbose - options.quiet -1) debug = Verbose(options.verbose - options.quiet - 2, "D: ") debug("""Options: '%s', args: '%s'""" % (options, args)) exit(main(options, args)) else: error = Verbose(2 - DEBUG, "E: ") warning = Verbose(1 - DEBUG, "W: ") info = Verbose(0 - DEBUG) moreinfo = Verbose(1 - DEBUG) debug = Verbose(2 - DEBUG, "D: ")