Пример #1
0
 def copyInsideDir(self,
                   r_dir_src,
                   r_dir_dest,
                   overwrite=True,
                   progressBar=None):
     """
     Copy the content a directory to a new location
     """
     dir_src = xbmc.makeLegalFilename(r_dir_src)
     dir_dest = xbmc.makeLegalFilename(r_dir_dest)
     list_dir = os.listdir(dir_src)
     for file in list_dir:
         src = os.path.join(dir_src, file)
         dst = os.path.join(dir_dest, file)
         if os.path.isfile(src):
             if not os.path.isdir(os.path.dirname(dst)):
                 os.makedirs(os.path.dirname(dst))
             if not overwrite and os.path.isfile(dst):
                 os.unlink(dst)
             shutil2.copyfile(src,
                              dst,
                              overwrite=overwrite,
                              progressBar=progressBar,
                              curPercent=100)
         elif os.path.isdir(src):
             if not overwrite and os.path.isdir(dst):
                 shutil2.rmtree(dst)
             shutil2.copytree(src,
                              dst,
                              overwrite=overwrite,
                              progressBar=progressBar,
                              curPercent=100)
Пример #2
0
def copy_inside_dir(dirname,
                    destination,
                    overwrite=True,
                    progressBar=None,
                    percentage=100):
    list_dir = os.listdir(dirname)
    for file in list_dir:
        src = os.path.join(dirname, file)
        dst = os.path.join(destination, file)
        if os.path.isfile(src):
            if not os.path.isdir(os.path.dirname(dst)):
                os.makedirs(os.path.dirname(dst))
            if not overwrite and os.path.isfile(dst):
                os.unlink(dst)
            shutil2.copyfile(src,
                             dst,
                             overwrite=overwrite,
                             progressBar=progressBar,
                             percentage=percentage)
        elif os.path.isdir(src):
            if not overwrite and os.path.isdir(dst):
                shutil2.rmtree(dst)
            shutil2.copytree(src,
                             dst,
                             overwrite=overwrite,
                             progressBar=progressBar,
                             percentage=percentage)
Пример #3
0
def unzip( filename, destination=None, report=False ):
    from zipfile import ZipFile
    base_dir = ""
    if destination is None:
        destination = os.path.dirname( filename ) #=> extraction in current directory
    try:
        zip = ZipFile( filename, "r" )
        namelist = zip.namelist()
        total_items = len( namelist ) or 1
        diff = 100.0 / total_items
        percent = 0
        # nom du dossier racine
        is_root_dir = True
        if "/" in namelist[ 0 ]:
            if namelist[ 0 ].endswith( "/" ):
                root_dir = namelist[ 0 ]
                if not root_dir.endswith( "/" ) and ( zip.getinfo( root_dir ).file_size > 0 ):
                    is_root_dir = False
                else:
                    for i in namelist:
                        if not root_dir in i:
                            is_root_dir = False
                            break
            else:
                root_dir = namelist[ 0 ].split("/")[0]
        else:
            is_root_dir = False

        # si root_dir n'est pas un dossier ou n'est pas la racine, on se base sur le nom de l'archive
        base_dir = os.path.join( destination, root_dir.rstrip( "/" )[:42] ) # xbox filename limitation
        if not is_root_dir:#root_dir.endswith( "/" ) and ( zip.getinfo( root_dir ).file_size > 0 ):
            root_dir = os.path.basename( os.path.splitext( filename )[ 0 ] )
            destination = os.path.join( destination, root_dir[:42] )
            base_dir = destination
        if os.path.isdir( base_dir ):
            shutil2.rmtree( base_dir )
        os.makedirs( base_dir )
        for count, item in enumerate( namelist ):
            percent += diff
            if report:
                if DIALOG_PROGRESS.iscanceled():
                    break
                DIALOG_PROGRESS.update( int( percent ), _( 30188 ) % ( count + 1, total_items ), item, _( 30110 ) )
            if not item.endswith( "/" ):
                root, name = os.path.split( item )
                directory = os.path.normpath( os.path.join( destination, root.replace(root_dir.rstrip( "/" ),root_dir.rstrip( "/" )[:42]) ) )
                if not os.path.isdir( directory ): os.makedirs( directory )
                filename = makeLegalFilename( os.path.join( directory, name ), True )
                file( filename, "wb" ).write( zip.read( item ) )
        zip.close()
        del zip
        return base_dir, True
    except:
        print_exc()
    return "", False
Пример #4
0
 def copyDir(self, r_dir_src, r_dir_dest, overwrite=True, progressBar=None):
     """
     Copy a directory to a new location
     """
     # dir_src  = xbmc.makeLegalFilename( r_dir_src )
     # dir_dest = xbmc.makeLegalFilename( r_dir_dest )
     dir_src = r_dir_src
     dir_dest = r_dir_dest
     if not overwrite and os.path.isdir(dir_dest):
         shutil2.rmtree(dir_dest)
     shutil2.copytree(dir_src, dir_dest, overwrite=overwrite, progressBar=progressBar, curPercent=100)
Пример #5
0
def unzip(filename, destination=None, report=False):
    from zipfile import ZipFile
    base_dir = ""
    if destination is None:
        destination = os.path.dirname(
            filename)  #=> extraction in current directory
    try:
        zip = ZipFile(filename, "r")
        namelist = zip.namelist()
        total_items = len(namelist) or 1
        diff = 100.0 / total_items
        percent = 0
        # nom du dossier racine
        root_dir = namelist[0]
        is_root_dir = True
        # si root_dir n'est pas un dossier ou n'est pas la racine, on se base sur le nom de l'archive
        #print root_dir
        if not root_dir.endswith("/") and (zip.getinfo(root_dir).file_size >
                                           0):
            is_root_dir = False
        else:
            for i in namelist:
                #print root_dir in i, i
                if not root_dir in i:
                    is_root_dir = False
                    break
        base_dir = os.path.join(destination, root_dir.rstrip("/"))
        if not is_root_dir:  #root_dir.endswith( "/" ) and ( zip.getinfo( root_dir ).file_size > 0 ):
            root_dir = os.path.basename(os.path.splitext(filename)[0])
            destination = os.path.join(destination, root_dir)
            base_dir = destination
        if os.path.isdir(base_dir):
            shutil2.rmtree(base_dir)
        os.makedirs(base_dir)
        for count, item in enumerate(namelist):
            percent += diff
            if report:
                if DIALOG_PROGRESS.iscanceled():
                    break
                DIALOG_PROGRESS.update(int(percent),
                                       _(188) % (count + 1, total_items), item,
                                       _(110))
                #print round( percent, 2 ), item
            if not item.endswith("/"):
                root, name = os.path.split(item)
                directory = os.path.normpath(os.path.join(destination, root))
                if not os.path.isdir(directory): os.makedirs(directory)
                file(os.path.join(directory, name), "wb").write(zip.read(item))
        zip.close()
        del zip
        return base_dir, True
    except:
        print_exc()
    return "", False
Пример #6
0
def copy_dir(dirname,
             destination,
             overwrite=True,
             progressBar=None,
             percentage=100):
    if not overwrite and os.path.isdir(destination):
        shutil2.rmtree(destination)
    shutil2.copytree(dirname,
                     destination,
                     overwrite=overwrite,
                     progressBar=progressBar,
                     percentage=percentage)
Пример #7
0
def copy_inside_dir( dirname, destination, overwrite=True, progressBar=None, percentage=100 ):
    list_dir = os.listdir( dirname )
    for file in list_dir:
        src = os.path.join( dirname, file )
        dst = os.path.join( destination, file )
        if os.path.isfile( src ):
            if not os.path.isdir( os.path.dirname( dst ) ):
                os.makedirs( os.path.dirname( dst ) )
            if not overwrite and os.path.isfile( dst ):
                os.unlink( dst )
            shutil2.copyfile( src, dst, overwrite=overwrite, progressBar=progressBar, percentage=percentage )
        elif os.path.isdir( src ):
            if not overwrite and os.path.isdir( dst ):
                shutil2.rmtree( dst )
            shutil2.copytree( src, dst, overwrite=overwrite, progressBar=progressBar, percentage=percentage )
Пример #8
0
 def copyDir(self, r_dir_src, r_dir_dest, overwrite=True, progressBar=None):
     """
     Copy a directory to a new location
     """
     #dir_src  = xbmc.makeLegalFilename( r_dir_src )
     #dir_dest = xbmc.makeLegalFilename( r_dir_dest )
     dir_src = r_dir_src
     dir_dest = r_dir_dest
     if not overwrite and os.path.isdir(dir_dest):
         shutil2.rmtree(dir_dest)
     shutil2.copytree(dir_src,
                      dir_dest,
                      overwrite=overwrite,
                      progressBar=progressBar,
                      curPercent=100)
Пример #9
0
 def copyInsideDir(self, r_dir_src, r_dir_dest, overwrite=True, progressBar=None):
     """
     Copy the content a directory to a new location
     """
     dir_src = xbmc.makeLegalFilename(r_dir_src)
     dir_dest = xbmc.makeLegalFilename(r_dir_dest)
     list_dir = os.listdir(dir_src)
     for file in list_dir:
         src = os.path.join(dir_src, file)
         dst = os.path.join(dir_dest, file)
         if os.path.isfile(src):
             if not os.path.isdir(os.path.dirname(dst)):
                 os.makedirs(os.path.dirname(dst))
             if not overwrite and os.path.isfile(dst):
                 os.unlink(dst)
             shutil2.copyfile(src, dst, overwrite=overwrite, progressBar=progressBar, curPercent=100)
         elif os.path.isdir(src):
             if not overwrite and os.path.isdir(dst):
                 shutil2.rmtree(dst)
             shutil2.copytree(src, dst, overwrite=overwrite, progressBar=progressBar, curPercent=100)
Пример #10
0
def copy_dir( dirname, destination, overwrite=True, progressBar=None, percentage=100 ):
    if not overwrite and os.path.isdir( destination ):
        shutil2.rmtree( destination )
    shutil2.copytree( dirname, destination, overwrite=overwrite, progressBar=progressBar, percentage=percentage )
Пример #11
0
def copy_dir( dirname, destination, overwrite=True ):
    if not overwrite and os.path.isdir( destination ):
        shutil2.rmtree( destination )
    shutil2.copytree( dirname, destination, overwrite=overwrite )
Пример #12
0
def unrar( filename, destination=None, report=False ):
    from rarfile import RarFile
    base_dir = ""
    if destination is None:
        destination = os.path.dirname( filename )
    try:
        rar = RarFile( filename, "r" )
        namelist = rar.namelist()
        total_items = len( namelist ) or 1
        diff = 100.0 / total_items
        percent = 0
        # nom du dossier racine
        root_dir = namelist[ -1 ]
        is_root_dir = True
        # si root_dir n'est pas un dossier ou n'est pas la racine, on se base sur le nom de l'archive
        #print root_dir
        if not rar.getinfo( root_dir ).isdir():
            is_root_dir = False
        else:
            for i in namelist:
                #print root_dir in i, i
                if not root_dir in i:
                    is_root_dir = False
                    break
        if not is_root_dir:#rar.getinfo( root_dir ).isdir():
            root_dir = os.path.basename( os.path.splitext( filename )[ 0 ] )
        base_dir = os.path.join( destination, root_dir )
        if os.path.isdir( base_dir ):
            shutil2.rmtree( base_dir )
        os.makedirs( base_dir )
        time_sleep = get_time_sleep( filename )
        # avec cette methode on extract dans le dossier ou est l'archive
        ok = executebuiltin( 'XBMC.Extract(%s)' % ( filename, ) )
        #sleep une seconde ou plus selon la drosseur du fichier le temps que le builtin est fini car l'action suivante
        # "os.listdir" est excecuter avant la fin du builtin
        sleep( time_sleep )
        # si le dossier base_dir est vide on move les items de namelist dedans
        if not os.listdir( base_dir ):
            for item in namelist:
                src = os.path.normpath( os.path.join( os.path.dirname( filename ), item ) )
                dst = os.path.normpath( os.path.join( base_dir, item ) )
                if not rar.getinfo( item ).isdir():
                    if not os.path.isdir( os.path.dirname( dst ) ):
                        os.makedirs( os.path.dirname( dst ) )
                    shutil2.move( src, dst, overwrite=True )
                elif os.path.exists( src ) and not os.listdir( src ):
                    shutil2.rmtree( src )
        #maintenant on verifier l'extraction d'xbmc avec la liste de la lib rarfile
        if os.path.isdir( base_dir ):
            size = 0
            list_size = 0
            if not root_dir in namelist:
                list_size -= 1
            namelist = [ os.path.split( item )[ 1 ] for item in namelist ]
            for root, dirs, files in os.walk( base_dir, topdown=False ):
                percent += diff
                list_size += 1
                for file in files:
                    percent += diff
                    list_size += 1
                    if report:
                        if DIALOG_PROGRESS.iscanceled():
                            break
                        DIALOG_PROGRESS.update( int( percent ), Language( 187 ) % ( list_size, total_items ), file, Language( 110 ) )
                        #print round( percent, 2 ), file
                    if file in namelist:
                        size += os.path.getsize( os.path.join( root, file ) )
                    else:
                        print "Error %s est dans la liste de depart!" % file
            #print size
            if not size:
                print "Error for extracting rar: %s" % filename
        rar.close()
        del rar
        # si list_size est pas declarer une erreur automatique est creer ;)
        return base_dir, list_size == total_items
    except:
        print_exc()
    return "", False
Пример #13
0
def unrar(filename, destination=None, report=False):
    from rarfile import RarFile
    base_dir = ""
    if destination is None:
        destination = os.path.dirname(filename)
    try:
        rar = RarFile(filename, "r")
        namelist = rar.namelist()
        total_items = len(namelist) or 1
        diff = 100.0 / total_items
        percent = 0
        # nom du dossier racine
        root_dir = namelist[-1]
        is_root_dir = True
        # si root_dir n'est pas un dossier ou n'est pas la racine, on se base sur le nom de l'archive
        if not rar.getinfo(root_dir).isdir():
            is_root_dir = False
        else:
            for i in namelist:
                #print root_dir in i, i
                if not root_dir in i:
                    is_root_dir = False
                    break
        if not is_root_dir:  #rar.getinfo( root_dir ).isdir():
            root_dir = os.path.basename(os.path.splitext(filename)[0])
        base_dir = os.path.join(destination, root_dir)
        if os.path.isdir(base_dir):
            shutil2.rmtree(base_dir)
        os.makedirs(base_dir)
        time_sleep = get_time_sleep(filename)
        # avec cette methode on extract dans le dossier ou est l'archive
        ok = executebuiltin('XBMC.Extract(%s)' % (filename, ))
        #sleep une seconde ou plus selon la drosseur du fichier le temps que le builtin est fini car l'action suivante
        # "os.listdir" est excecuter avant la fin du builtin
        sleep(time_sleep)
        # si le dossier base_dir est vide on move les items de namelist dedans
        if not os.listdir(base_dir):
            for item in namelist:
                src = os.path.normpath(
                    os.path.join(os.path.dirname(filename), item))
                dst = os.path.normpath(os.path.join(base_dir, item))
                if not rar.getinfo(item).isdir():
                    if not os.path.isdir(os.path.dirname(dst)):
                        os.makedirs(os.path.dirname(dst))
                    shutil2.move(src, dst, overwrite=True)
                elif os.path.exists(src) and not os.listdir(src):
                    shutil2.rmtree(src)
        #maintenant on verifier l'extraction d'xbmc avec la liste de la lib rarfile
        if os.path.isdir(base_dir):
            size = 0
            list_size = 0
            if not root_dir in namelist:
                list_size -= 1
            namelist = [os.path.split(item)[1] for item in namelist]
            for root, dirs, files in os.walk(base_dir, topdown=False):
                percent += diff
                list_size += 1
                for file in files:
                    percent += diff
                    list_size += 1
                    if report:
                        if DIALOG_PROGRESS.iscanceled():
                            break
                        DIALOG_PROGRESS.update(
                            int(percent),
                            _(30187) % (list_size, total_items), file,
                            _(30110))
                        #print round( percent, 2 ), file
                    if file in namelist:
                        size += os.path.getsize(os.path.join(root, file))
                    else:
                        print "Error %s est dans la liste de depart!" % file
            #print size
            if not size:
                print "Error for extracting rar: %s" % filename
        rar.close()
        del rar
        # si list_size est pas declarer une erreur automatique est creer ;)
        return base_dir, list_size == total_items
    except:
        print_exc()
    return "", False
Пример #14
0
def copy_dir(dirname, destination, overwrite=True):
    if not overwrite and os.path.isdir(destination):
        shutil2.rmtree(destination)
    shutil2.copytree(dirname, destination, overwrite=overwrite)