Ejemplo n.º 1
0
    def SetTextureHandler(config, logger, uriHandler=None):
        """ Fetches a TextureManager for specific mode and channel.

        @param config:              The Retrospect Config object
        @param logger:              An Logger
        @param uriHandler:          The UriHandler

        @return: A TextureHandler object for the requested mode

        """

        mode = config.TextureMode.lower()
        if logger is not None:
            logger.Trace("Creating '%s' Texture Mananger", mode)

        if mode == Local:
            import local
            TextureHandler.__TextureHandler = local.Local(logger)
        elif mode == Remote:
            import remote
            TextureHandler.__TextureHandler = remote.Remote(
                config.TextureUrl, logger)
        elif mode == Cached:
            import cached
            TextureHandler.__TextureHandler = cached.Cached(
                config.TextureUrl, config.profileDir, logger, uriHandler)
        else:
            raise Exception("Invalide mode: %s" % (mode, ))

        return TextureHandler.__TextureHandler
Ejemplo n.º 2
0
    def leeYSepara(self, cad):
        """
		Función separa las cadenas de etiquetas y las transforma en la información pertinente para trabajar con ellas
        en modo local y de Dropbox.

		Parámetros:
		cad -- cadena de Dropbox.

		"""
        x = local.Local()
        y = x.leerFicheroL("/notas.txt")
        for n in y:
            t = infoEt.InfoEtiquetas(n)
            self.__listaL.append(t)
            print(t.getHijo())
            print(t.getPadre())
        z = cad
        print(cad)
        y = cad.split("\n")
        if (len(y) > 0):
            print(y)
            for z in y:
                t = infoEt.InfoEtiquetas(z)
                self.__listaD.append(t)
                print(t.getHijo())
                print(t.getPadre())
Ejemplo n.º 3
0
 def __init__(self, dx):
     #Iniciar	el	objeto	QMainWindow
     QMainWindow.__init__(self)
     #Cargar	la	configuración	del	archivo	.ui	en	el	objeto
     self.ruta = os.getcwd() + "/icons/"
     uic.loadUi("mainwindow2.ui", self)
     self.carpetaActual = ""
     self.drop = dx
     self.tlocal = local.Local()
     self.setWindowTitle("Droppy")
     self.localM = False
     iconCar = QIcon(self.ruta + 'New-Folder-icon.png')
     iconFil = QIcon(self.ruta + 'nfile.png')
     iconBor = QIcon(self.ruta + 'papelera.png')
     self.iconDrop = QIcon(self.ruta + 'apps.png')
     iconApp = QIcon("app.png")
     self.control = controlInfo.ControlInfo()
     self.directorioP = []
     #########################Barra de menú##########################
     self.systray = QSystemTrayIcon(iconApp, self)
     show_action = QAction("Show", self)
     quit_action = QAction("Exit", self)
     hide_action = QAction("Hide", self)
     show_action.triggered.connect(self.show)
     hide_action.triggered.connect(self.hide)
     quit_action.triggered.connect(qApp.quit)
     tray_menu = QMenu()
     tray_menu.addAction(show_action)
     tray_menu.addAction(hide_action)
     tray_menu.addAction(quit_action)
     self.systray.setContextMenu(tray_menu)
     self.systray.show()
     ######################Icono visible dock########################
     self.setWindowIcon(iconApp)
     ################################################################
     self.bCarpeta.setIcon(iconCar)
     self.bFichero.setIcon(iconFil)
     self.bDropb.setIcon(self.iconDrop)
     self.bBorrar.setIcon(iconBor)
     self.carpetas.itemClicked.connect(self.hijos)
     self.ficheros.itemClicked.connect(self.borrarfich)
     self.ficheros.itemDoubleClicked.connect(self.abrir)
     self.prim = 0
     self.forma()
     #self.formaLocal()
     self.boCarpeta = ""
     self.boFichero = ""
     self.drop.buscar()
     #Asociar botones a funciones
     self.bCarpeta.clicked.connect(self.crearCarpeta)
     self.bDropb.clicked.connect(self.cambio)
     self.bFichero.clicked.connect(self.crearFich)
     self.bBorrar.clicked.connect(self.borrar)
     self.lineEdit.returnPressed.connect(self.filtrar)
     lectura = str(self.drop.abrirFichero("etiquetas.txt"), 'cp1252')
     self.control.leeYSepara(lectura)
Ejemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'seq_a',
        help='Path to first FASTA file (e.g. fastas/HomoSapiens-SHH.fasta)')
    parser.add_argument('seq_b', help='Path to second FASTA file')
    parser.add_argument('--align_type',
                        help='Alignment type (e.g. local)',
                        required=True)
    parser.add_argument(
        '--score',
        help='Score matrix in.tsv format (default is score_matrix.tsv) ',
        default='score_matrix.tsv')
    command_args = parser.parse_args()

    parsed_a = fastaread(command_args.seq_a).__next__()
    a_name, a_seq = parsed_a[NAME], parsed_a[SEQ]
    parsed_b = fastaread(command_args.seq_b).__next__()
    b_name, b_seq = parsed_b[NAME], parsed_b[SEQ]
    score_matrix = np.genfromtxt(fname=command_args.score,
                                 delimiter="\t",
                                 skip_header=1,
                                 filling_values=1)[:, 1:]

    score = 0
    alignment = []
    if command_args.align_type == 'global':
        final_align = align_global.GlobalAlign(score_matrix, a_seq, b_seq)
        alignment = final_align.get_alignment()
        score = final_align.get_align_score()
    elif command_args.align_type == 'local':
        final_align = local.Local(score_matrix, a_seq, b_seq)
        alignment = final_align.get_alignment()
        score = final_align.get_align_score()
    elif command_args.align_type == 'overlap':
        final_align = overlap.Overlap(score_matrix, a_seq, b_seq)
        alignment = final_align.get_alignment()
        score = final_align.get_align_score()

    # print the best alignment and score

    split_size = 50
    length = len(alignment[TOP])
    top = [
        alignment[TOP][i:i + split_size] for i in range(0, length, split_size)
    ]
    bottom = [
        alignment[BOTTOM][i:i + split_size]
        for i in range(0, length, split_size)
    ]
    for i in range(len(top)):
        print(top[i] + "\n" + bottom[i] + "\n")
    print(command_args.align_type + ": " + str(score))
Ejemplo n.º 5
0
	def __init__(self, fich,pad):
		#Iniciar	el	objeto	QMainWindow
		QMainWindow.__init__(self)
		#Cargar	la	configuración	del	archivo	.ui	en	el	objeto
		self.ruta=os.getcwd()+"/icons/"
		self.padre=pad
		uic.loadUi("mainwindow3.ui",self)
		self.clave=AESCipher.AESCipher()
		self.loc=local.Local()
		self.setWindowTitle(fich)
		self.fichero=fich

		self.abrir(self.fichero)
		iconSa=QIcon(self.ruta+'save-icon.png')
		iconL=QIcon(self.ruta+'lista-icon.png')
		iconN=QIcon(self.ruta+'bold.png')
		iconSub=QIcon(self.ruta+'underline.png')
		self.abierto=QIcon(self.ruta+'abierto.png')
		self.cerrado=QIcon(self.ruta+'cerrado.png')
		self.bbusqueda=QIcon(self.ruta+'lupa.png')
		self.bimpri=QIcon(self.ruta+'print1600.png')
		self.encrip=False

		self.saves.setIcon(iconSa)
		self.negrita.setIcon(iconN)
		self.listaB.setIcon(iconL)
		self.subButton.setIcon(iconSub)
		self.bEncrip.setIcon(self.abierto)
		self.bBuscar.setIcon(self.bbusqueda)
		self.bImprimir.setIcon(self.bimpri)
		self.saves.clicked.connect(self.save)
		self.negrita.clicked.connect(self.bold)
		self.listaB.clicked.connect(self.lista)
		self.etiquet.clicked.connect(self.nuevaE)
		self.subButton.clicked.connect(self.subra)
		self.bEncrip.clicked.connect(self.cambiarEncriptador)
		self.bImprimir.clicked.connect(self.imprimir)
		self.bBuscar.clicked.connect(self.busqueda)
		QShortcut(QtGui.QKeySequence("Ctrl+B"), self, self.bold)
		QShortcut(QtGui.QKeySequence("Ctrl+L"), self, self.lista)
		QShortcut(QtGui.QKeySequence("Ctrl+U"), self, self.subra)
		QShortcut(QtGui.QKeySequence("Ctrl+S"), self, self.save)
		QShortcut(QtGui.QKeySequence("Ctrl+F"), self, self.busqueda)
		QShortcut(QtGui.QKeySequence("Ctrl+P"), self, self.imprimir)
Ejemplo n.º 6
0
def popInicial(numIndividuos):
    populacao = []
    for i in range(numIndividuos):
        s = sol.Solucao()
        for i in range(len(capacidade[0])): # Adiciona os locais de votacao ao array locais
            localVotacao = local.Local(i, capacidade[0][i])
            s.addLocal(localVotacao)

        listaSetores = random.sample(range(len(demandas[0])), len(demandas[0])) # sequencia aleatoria de setores

        while(len(listaSetores) > 0):# Atribui as demandas das regioes da cidade aos locais de votacao
        	cont = random.randint(0, 165)
            # Verifica a capacidade maxima do local de votaçao
        	if(s.getLocal(cont).getOcupacao() + demandas[0][listaSetores[0]] <= s.getLocal(cont).getCapacidade() and
            distancias[cont][listaSetores[0]] <= maxDist):
        		s.getLocal(cont).addRegiao(listaSetores[0])
        		s.getLocal(cont).addOcupacao(demandas[0][listaSetores[0]])
        		listaSetores.pop(0)
        calculaFitness(s)
        populacao.append(s)
    return populacao
Ejemplo n.º 7
0
    def generate_revision_key(self, url, ud, d):
        key = self._revision_key(url, ud, d)
        return "%s-%s" % (key, bb.data.getVar("PN", d, True) or "")


import cvs
import git
import local
import svn
import wget
import svk
import ssh
import perforce
import bzr
import hg
import osc
import repo

methods.append(local.Local())
methods.append(wget.Wget())
methods.append(svn.Svn())
methods.append(git.Git())
methods.append(cvs.Cvs())
methods.append(svk.Svk())
methods.append(ssh.SSH())
methods.append(perforce.Perforce())
methods.append(bzr.Bzr())
methods.append(hg.Hg())
methods.append(osc.Osc())
methods.append(repo.Repo())
Ejemplo n.º 8
0
    def cambio(self):
        print(type(self.recipiente.text()))
        self.drop.nuevoToken(str(self.recipiente.text()))
        self.ventana2 = arbol.Arbol(self.drop)
        self.ventana2.show()
        self.close()


#Instancia para iniciar una aplicación
app = QApplication(sys.argv)
#Crear un objeto de la clase
if os.path.exists(".token.txt"):
    drop = completo.DropObj("colores.py")
    drop.autoiden()
    ventana2 = arbol.Arbol(drop)
    ventana2.show()
    ventana2.activateWindow()
else:
    loc = local.Local()
    loc.crearCarpetaInicio()
    if not os.path.exists(".token.txt"):
        loc.crearFicheroInial()
    _ventana = Ventana()

    #Mostra la ventana
    _ventana.show()

#Ejecutar la aplicación
app.exec_()
Ejemplo n.º 9
0
            str(k),
            str(time),
            str(result),
            str(rendement),
            str(maxQ)
        ])
        print(
            "fait pour algo: {} taille {} serie {} exemple {} en {} s".format(
                obj.name, str(i), str(j), str(k), str(time)))


if __name__ == '__main__':

    gloutonObj = glouton.Glouton()
    progdynObj = progdyn.Progdyn()
    localObj = local.Local()

    path = "/home/gregoire/Documents/INF8775/TP/tp1-H19/exemplaires"
    arrayObj = [localObj]
    with open("data.csv", mode='a') as csvfile:
        c = csv.writer(csvfile)
        c.writerow([
            'algorithme', 'taille', 'serie', 'exemplaire',
            'temps d\'exécution', 'revenu', 'rendement', 'maxQ'
        ])

    for obj in arrayObj:
        print(obj.name)
        tailles = [100, 1000, 10000]
        series = [10, 100, 1000]
        exemples = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Ejemplo n.º 10
0
class Adb(connection.Connection):
    """
    Singleton object to facilitate adb connection with the device
    Only one device per object

    serial  -- device serial
    port    -- the port for adb server running on the host

    verbose -- if True print some extra messages to STDOUT
    """

    __metaclass__ = base_utils.SingletonType
    serial = None
    port = None
    adb = "adb"
    cmd_prefix = []
    verbose = False
    local_conn = local.Local()

    def __init__(self, **kwargs):
        super(Adb, self).__init__()
        if kwargs.has_key('port'):
            self.port = kwargs['port']
            if self.port:
                self.adb = "{0} -P {1}".format(self.adb, self.port)
                os.environ['ANDROID_ADB_SERVER_PORT'] = self.port
        if kwargs.has_key('serial'):
            self.serial = kwargs['serial']
        if kwargs.has_key('verbose'):
            self.verbose = kwargs['verbose']
        self.cmd_prefix = self.adb.split()
        self.cmd_prefix.extend(["-s", self.serial])

    def run_cmd(self,
                command,
                mode="sync",
                soutfile=None,
                dont_split=False,
                timeout=10,
                env={},
                liveprint=True,
                ignore_error=False,
                cmd_type=None):
        """run adb shell command"""
        cmd = []
        cmd.extend(self.cmd_prefix)
        if cmd_type != "reboot":
            cmd.append('shell')
        if dont_split:
            cmd.append(command)
        else:
            cmd.extend(command.split())
        return self.run_cmd_linux(cmd,
                                  mode=mode,
                                  soutfile=soutfile,
                                  timeout=timeout,
                                  env=env,
                                  liveprint=liveprint,
                                  ignore_error=ignore_error)

    def run_cmd_linux(self,
                      command,
                      mode="sync",
                      soutfile=None,
                      timeout=10,
                      env={},
                      liveprint=True,
                      ignore_error=False,
                      with_accept=False):
        """run linux bash command using Popen"""
        if self.verbose:
            print "Executing {0}".format(" ".join(command))
        __env = os.environ
        __env.update(env)
        p = None
        __err = 'Timeout {0} second(s) reached while executing "{1}"'.format(
            timeout, " ".join(command))
        if soutfile == None:
            p = subprocess.Popen(command, stdout = subprocess.PIPE, \
                stderr = subprocess.PIPE, env = __env)
        else:
            p = subprocess.Popen(command, stdout = open(soutfile, "wr"), \
                stderr = subprocess.PIPE, env = __env)
        if mode.lower() == "sync":

            def handler(signum, frame):
                raise base_utils.TimeoutError(__err)

            signal.signal(signal.SIGALRM, handler)
            signal.alarm(timeout)

            while True:
                if self.verbose and soutfile == None and liveprint:
                    print "STDOUT", p.stdout.read()
                    print "STDERR", p.stderr.read()
                if p.poll() != None:
                    if not ignore_error:
                        __error = p.stderr.read().strip()
                        if __error != '' and "Warning" not in __error:
                            signal.alarm(0)
                            raise AssertionError(
                                "Error encountered:\n{0}".format(__error))
                    signal.alarm(0)
                    return p

        elif mode.lower() == "async":
            # Add below lines to fail in case run_cmd returns with failure
            # while starting to execute the command
            time.sleep(0.5)
            if p.poll() not in [None, 0]:
                if not ignore_error:
                    __error = p.stderr.read().strip()
                    # print __error
                    if __error != '' and "Warning" not in __error:
                        raise AssertionError(
                            "Error encountered:\n{0}".format(__error))
            return p
        else:
            raise AdbError("Mode '{0}' not supported. \
                            Use only 'sync' or 'async'.".format(mode))

    def open_connection(self):
        """connect to device if not already connected"""
        if not self.check_connected():
            cmd_string = "{0} connect {1}".format(self.adb, self.serial)
            self.run_cmd_linux(cmd_string.split(), timeout=10)
            time.sleep(1)
        return self.check_connected()

    def adb_root(self):
        """get adb root session"""
        cmd_string = "{0} -s {1} root".format(self.adb, self.serial)
        p = self.run_cmd_linux(cmd_string.split(), timeout=5)
        if "adbd is already running as root" not in p.stdout.read():
            time.sleep(5)
        return self.open_connection()

    def adb_remount(self):
        """remount /system and /vendor"""
        cmd_string = "{0} -s {1} remount".format(self.adb, self.serial)
        return self.run_cmd_linux(cmd_string.split(), timeout=10)

    def adb_disable_verity(self):
        """Disable verity in order to write in /system partition"""
        cmd_string = "{0} -s {1} disable-verity".format(self.adb, self.serial)
        return self.run_cmd_linux(cmd_string.split(), timeout=20)

    def kill_server(self):
        """kill adb server"""
        cmd_string = "{0} kill-server".format(self.adb)
        self.run_cmd_linux(cmd_string.split())
        time.sleep(1)

    def reboot_device(self,
                      reboot_params="",
                      ip_enabled=False,
                      reboot_timeout=60):
        """reboot the device and check it is connected again"""
        if self.verbose:
            print "Rebooting .."
        ip = self.serial.split(":")[0]
        cmd = "reboot {0}".format(reboot_params)
        reboot_proc = self.run_cmd(cmd, mode="sync", cmd_type="reboot")
        if reboot_proc.poll() == 0:
            if ip_enabled:
                try:
                    self.local_conn.wait_for_no_ping(ip,
                                                     timeout=reboot_timeout /
                                                     2)
                except base_utils.TimeoutError:
                    return False
                self.kill_server()
                time.sleep(1)
                try:
                    self.local_conn.wait_for_ping(ip, timeout=reboot_timeout)
                except base_utils.TimeoutError:
                    return False
                if reboot_params == "":
                    self.open_connection()
                return True
            else:
                waiting = 0
                while waiting < reboot_timeout:
                    time.sleep(2)
                    check = self.check_connected(device_state=reboot_params)
                    if check == None:
                        # timeout
                        return False
                    if check:
                        break
                    waiting += 2
                return waiting < reboot_timeout
        else:
            return False

    def check_connected(self, device_state=None):
        """check adb connection with the device"""
        if device_state == "recovery":
            return self.local_conn.check_adb(serial=self.serial,
                                             device_state=device_state)

        if device_state == "fastboot" or device_state == "bootloader":
            return self.local_conn.check_fastboot(serial=self.serial)

        try:
            self.run_cmd("ls sdcard", timeout=20)
        except base_utils.TimeoutError:
            return None
        except Exception, e:
            return False
        return True
class Adb(connection.Connection):
    """
    Singleton object to facilitate adb connection with the device
    Only one device per object

    serial  -- device serial
    port    -- the port for adb server running on the host

    verbose -- if True print some extra messages to STDOUT
    """

    __metaclass__ = base_utils.SingletonType
    serial = None
    port = None
    adb = "adb"
    cmd_prefix = []
    verbose = False
    local_conn = local.Local()

    def __init__(self, **kwargs):
        super(Adb, self).__init__()
        if "port" in kwargs:
            self.port = kwargs['port']
            if self.port:
                self.adb = "{0} -P {1}".format(self.adb, self.port)
                os.environ['ANDROID_ADB_SERVER_PORT'] = self.port
        if "serial" in kwargs:
            self.serial = kwargs['serial']
        if "verbose" in kwargs:
            self.verbose = kwargs['verbose']
        self.cmd_prefix = self.adb.split()
        self.cmd_prefix.extend(["-s", self.serial])

    def run_cmd(self,
                command,
                mode="sync",
                soutfile=None,
                dont_split=False,
                timeout=10,
                env={},
                liveprint=True,
                ignore_error=False,
                cmd_type=None):
        """run adb shell command"""
        cmd = []
        cmd.extend(self.cmd_prefix)
        if cmd_type != "reboot":
            cmd.append('shell')
        if dont_split:
            cmd.append(command)
        else:
            cmd.extend(command.split())
        return self.run_cmd_linux(cmd,
                                  mode=mode,
                                  soutfile=soutfile,
                                  timeout=timeout,
                                  env=env,
                                  liveprint=liveprint,
                                  ignore_error=ignore_error)

    def run_cmd_linux(self,
                      command,
                      mode="sync",
                      soutfile=None,
                      timeout=10,
                      env={},
                      liveprint=True,
                      ignore_error=False):
        """run linux bash command using Popen"""
        if self.verbose:
            print "Executing {0}".format(" ".join(command))
        __env = os.environ
        __env.update(env)
        p = None
        __err = 'Timeout {0} second(s) reached while executing "{1}"'.format(
            timeout, " ".join(command))
        if soutfile is None:
            p = subprocess.Popen(command,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 env=__env)
        else:
            p = subprocess.Popen(command,
                                 stdout=open(soutfile, "wr"),
                                 stderr=subprocess.PIPE,
                                 env=__env)
        if mode.lower() == "sync":

            def handler(signum, frame):
                raise base_utils.TimeoutError(__err)

            signal.signal(signal.SIGALRM, handler)
            signal.alarm(timeout)
            while True:
                if self.verbose and soutfile is None and liveprint:
                    print "STDOUT", p.stdout.read()
                    print "STDERR", p.stderr.read()
                if p.poll() is not None:
                    if not ignore_error:
                        __error = p.stderr.read().strip()
                        if __error != '' and "Warning" not in __error:
                            signal.alarm(0)
                            raise AssertionError(
                                "Error encountered:\n{0}".format(__error))
                    signal.alarm(0)
                    return p

        elif mode.lower() == "async":
            # Add below lines to fail in case run_cmd returns with failure
            # while starting to execute the command
            time.sleep(0.5)
            if p.poll() not in [None, 0]:
                if not ignore_error:
                    __error = p.stderr.read().strip()
                    # print __error
                    if __error != '' and "Warning" not in __error:
                        raise AssertionError(
                            "Error encountered:\n{0}".format(__error))
            return p
        else:
            raise AdbError("Mode '{0}' not supported. \
                            Use only 'sync' or 'async'.".format(mode))

    def open_connection(self):
        """connect to device if not already connected"""
        if not self.check_connected():
            cmd_string = "{0} connect {1}".format(self.adb, self.serial)
            self.run_cmd_linux(cmd_string.split(), timeout=10)
            time.sleep(1)
        return self.check_connected()

    def adb_root(self):
        """get adb root session"""
        cmd_string = "{0} -s {1} root".format(self.adb, self.serial)
        p = self.run_cmd_linux(cmd_string.split(), timeout=5)
        if "adbd is already running as root" not in p.stdout.read():
            time.sleep(5)
        return self.open_connection()

    def adb_remount(self):
        """remount /system and /vendor"""
        cmd_string = "{0} -s {1} remount".format(self.adb, self.serial)
        return self.run_cmd_linux(cmd_string.split(), timeout=10)

    def adb_disable_verity(self):
        """Disable verity in order to write in /system partition"""
        cmd_string = "{0} -s {1} disable-verity".format(self.adb, self.serial)
        return self.run_cmd_linux(cmd_string.split(), timeout=20)

    def kill_server(self):
        """kill adb server"""
        cmd_string = "{0} kill-server".format(self.adb)
        self.run_cmd_linux(cmd_string.split())
        time.sleep(1)

    def reboot_device(self,
                      reboot_params="",
                      ip_enabled=False,
                      reboot_timeout=60):
        """reboot the device and check it is connected again"""
        if self.verbose:
            print "Rebooting .."
        ip = self.serial.split(":")[0]
        cmd = "reboot {0}".format(reboot_params)
        reboot_proc = self.run_cmd(cmd, mode="sync", cmd_type="reboot")
        if reboot_proc.poll() == 0:
            if ip_enabled:
                try:
                    self.local_conn.wait_for_no_ping(ip,
                                                     timeout=reboot_timeout /
                                                     2)
                except base_utils.TimeoutError:
                    return False
                self.kill_server()
                time.sleep(1)
                try:
                    self.local_conn.wait_for_ping(ip, timeout=reboot_timeout)
                except base_utils.TimeoutError:
                    return False
                if reboot_params == "":
                    self.open_connection()
                return True
            else:
                waiting = 0
                while waiting < reboot_timeout:
                    time.sleep(2)
                    check = self.check_connected(device_state=reboot_params)
                    if check is None:
                        # timeout
                        return False
                    if check:
                        break
                    waiting += 2
                return waiting < reboot_timeout
        else:
            return False

    def check_connected(self, device_state=None):
        """check adb connection with the device"""
        if device_state == "recovery":
            return self.local_conn.check_adb(serial=self.serial,
                                             device_state=device_state)

        if device_state == "fastboot" or device_state == "bootloader":
            return self.local_conn.check_fastboot(serial=self.serial)

        try:
            self.run_cmd("ls sdcard", timeout=20)
        except base_utils.TimeoutError:
            return None
        except Exception:
            return False
        return True

    def close_connection(self):
        """discovnnect from the device"""
        cmd_string = "{0} disconnect {1}".format(self.adb, self.serial)
        self.run_cmd_linux(cmd_string.split(), timeout=1)

    def get_file(self, remote, local, timeout=60):
        """get file from the device"""
        cmd = []
        cmd.extend(self.cmd_prefix)
        cmd_string = "pull {0} {1}".format(remote, local)
        cmd.extend(cmd_string.split())
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)
        err = p.stderr.read()
        out = p.stdout.read()
        assert "KB/s" in err or "100%" in out or (
            not err and not out), "Could not get file\n{0}".format(err)

    def put_file(self, local, remote, timeout=60):
        """push file to device"""
        cmd = []
        cmd.extend(self.cmd_prefix)
        cmd_string = "push {0} {1}".format(local, remote)
        cmd.extend(cmd_string.split())
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)
        err = p.stderr.read()
        out = p.stdout.read()
        assert "KB/s" in err or "100%" in out or (
            not err and not out), "Could not push file\n{0}".format(err)

    def install_apk(self, apk, timeout=60):
        """install apk"""
        cmd = ["shell", "settings", "get", "secure", "install_non_market_apps"]
        cmd = self.cmd_prefix + cmd
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)
        (unknown_apps_state, err) = p.communicate()
        unknown_apps_state = unknown_apps_state.strip()
        cmd = ["shell", "settings", "get", "global", "package_verifier_enable"]
        cmd = self.cmd_prefix + cmd
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)
        (package_verifier_state, err) = p.communicate()
        package_verifier_state = package_verifier_state.strip()
        cmd = [
            "shell", "settings", "put", "secure", "install_non_market_apps",
            "1"
        ]
        cmd = self.cmd_prefix + cmd
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)

        cmd = [
            "shell", "settings", "put", "global", "package_verifier_enable",
            "0"
        ]
        cmd = self.cmd_prefix + cmd
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)

        cmd = []
        cmd.extend(self.cmd_prefix)
        cmd_string = "install {0}".format(apk)
        cmd.extend(cmd_string.split())
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)

        err = p.stderr.read()
        out = p.stdout.read()

        cmd = [
            "shell", "settings", "put", "secure", "install_non_market_apps",
            unknown_apps_state
        ]
        cmd = self.cmd_prefix + cmd
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)

        cmd = [
            "shell", "settings", "put", "global", "package_verifier_enable",
            package_verifier_state
        ]
        cmd = self.cmd_prefix + cmd
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)

        assert "Success" in out or "ALREADY_EXISTS" in out, "Could: not install apk {0}\Stdout: {1}\nStderr:  {" \
                                                            "2}".format(apk, out, err)

    def uninstall_apk(self, package, timeout=60):
        """install apk"""
        cmd = []
        cmd.extend(self.cmd_prefix)
        cmd_string = "uninstall {0}".format(package)
        cmd.extend(cmd_string.split())
        p = self.run_cmd_linux(cmd,
                               timeout=timeout,
                               ignore_error=True,
                               liveprint=False)
        err = p.stderr.read()
        out = p.stdout.read()
        assert "Success" in out, "Could: not uninstall package {0}\nStdout: {1}\nStderr: {2}\n".format(
            package, out, err)

    def kill_command(self, pid):
        self.run_cmd("kill {0}".format(pid))

    def kill_all(self, pids):
        for pid in pids:
            self.kill_command(pid)

    def cd(self, path):
        raise NotImplementedError("Method not overwritten")

    def set_env(self, var_name, var_value):
        raise NotImplementedError("Method not overwritten")

    def unset_env(self, var_name):
        raise NotImplementedError("Method not overwritten")

    def load_CPU(self):
        """
        loads CPU
        returns the subprocess object
        """
        cmd = "cat /dev/urandom > /dev/null & \
cat /dev/urandom > /dev/null & cat /dev/urandom > /dev/null & \
cat /dev/urandom > /dev/null & cat /dev/urandom > /dev/null"

        return self.run_cmd(cmd, mode="async")

    def clear_logcat(self):
        """clears logcat"""
        self.run_cmd("logcat -c")

    def parse_cmd_output(self,
                         cmd,
                         grep_for=None,
                         multiple_grep=None,
                         left_separator=None,
                         right_separator=None,
                         strip=False,
                         dont_split=False,
                         timeout=60,
                         ignore_error=False):
        """
        By default gets the output from adb shell command
        Can grep for strings or cut for delimiters
        """
        # tmp file name should be uniq to allow getting output from
        # multiple devices at the same time

        tmp_file_name = "tmp_{0}_{1}_{2}".format(
            "5037" if self.port is None else str(self.port),
            self.serial.split(":")[0], str(int(round(time.time() * 1000000))))
        self.run_cmd(cmd,
                     soutfile=tmp_file_name,
                     timeout=timeout,
                     dont_split=dont_split,
                     ignore_error=ignore_error)
        with open(tmp_file_name, 'r') as f:
            string = f.read()
            string = base_utils.parse_string(string,
                                             grep_for=grep_for,
                                             multiple_grep=multiple_grep,
                                             left_separator=left_separator,
                                             right_separator=right_separator,
                                             strip=strip)
        os.remove(tmp_file_name)
        return string

    def parse_logcat(self,
                     grep_for=None,
                     left_separator=None,
                     right_separator=None,
                     strip=False):
        """parses logcat output"""
        cmd = "logcat -d"
        return self.parse_cmd_output(cmd,
                                     grep_for=grep_for,
                                     left_separator=left_separator,
                                     right_separator0=right_separator,
                                     strip=strip)

    def parse_dmesg(self,
                    grep_for=None,
                    left_separator=None,
                    right_separator=None,
                    strip=False):
        """parses dmesg output"""
        cmd = "dmesg"
        return self.parse_cmd_output(cmd,
                                     grep_for=grep_for,
                                     left_separator=left_separator,
                                     right_separator=right_separator,
                                     strip=strip)

    def parse_file(self,
                   file_name,
                   grep_for=None,
                   left_separator=None,
                   right_separator=None,
                   strip=False):
        """parses the file located at file_name"""
        cmd = "cat {0}".format(file_name)
        return self.parse_cmd_output(cmd,
                                     grep_for=grep_for,
                                     left_separator=left_separator,
                                     right_separator=right_separator,
                                     strip=strip)

    def check_ping(self, ip):
        """checks ping to an ip from the device"""
        cmd = "ping -c1 {0}".format(ip)
        return "1 received" in self.parse_cmd_output(cmd)

    def check_interface_up(self, interface):
        """
        checks interface status from netcfg command

        usage:
            adb_conn.check_interface_up(wlan0)
        """
        return "UP" in self.parse_cmd_output("netcfg", grep_for=interface)

    def check_interface_down(self, interface):
        """
        checks interface status from netcfg command

        usage:
            adb_conn.check_interface_up(wlan0)
        """
        return "UP" not in self.parse_cmd_output("netcfg", grep_for=interface)

    def check_interface_has_ip(self, interface):
        """
        checks if interface has an IP address assigned

        usage:
            adb_conn.check_interface_has_ip(wlan0)
        """
        output = self.parse_cmd_output("netcfg", grep_for=interface)
        return output.split()[2].strip() != "0.0.0.0/0"

    def check_interface_has_this_ip(self, interface, ip, mask="24"):
        """
        checks if interface has the given IP address assigned
        and the given mask

        usage:
            adb_conn.check_interface_has_ip(wlan0)
        """
        output = self.parse_cmd_output("netcfg", grep_for=interface)
        return output.split()[2] == "{0}/{1}".format(ip, mask)

    def clear_app_cache(self):
        """clears app cache"""
        # TODO
        pass

    def get_prop(self, prop):
        """get prop from the device"""
        cmd = "getprop {0}".format(prop)
        return self.parse_cmd_output(cmd, strip=True).strip()

    def set_prop(self, prop, value):
        """set prop on the device"""
        self.run_cmd("setprop {0} {1}".format(prop, value))

    def pgrep(self, grep_for=""):
        """returns list of pids that match grep_for"""
        string = self.parse_cmd_output("ps", grep_for=grep_for)
        if self.verbose:
            print "________________________________________________"
            print string
            print "________________________________________________"
        line_separator = "\r\n"
        if line_separator not in string:
            line_separator = "\n"
        pids = []
        for line in string.split(line_separator):
            if grep_for in line:
                try:
                    pid = line.split()[1]
                    if pid.isdigit():
                        pids.append(pid)
                except Exception:
                    pass
        return pids

    def pgrep_common(self, args):
        """returns list of pids for give args.
                Works the same as 'pgrep' in android"""
        string = self.parse_cmd_output("pgrep " + args)
        if self.verbose:
            print "________________________________________________"
            print string
            print "________________________________________________"
        line_separator = "\r\n"
        if line_separator not in string:
            line_separator = "\n"
        output = []
        for line in string.split(line_separator):
            if line != "":
                output.append(line)
        return output

    def get_pid(self, grep_for):
        """return first pid to mach process name"""
        pids = self.pgrep(grep_for=grep_for)
        return None if len(pids) == 0 else pids[0]