def scan_archive(path): """Scan an archive from a `path`. :param str path: existing path to the archive. :return: the scanned video. :rtype: :class:`~subliminal.video.Video` """ # check for non-existing path if not os.path.exists(path): raise ValueError('Path does not exist') # check video extension if not path.endswith(ARCHIVE_EXTENSIONS): raise ValueError('%r is not a valid archive extension' % os.path.splitext(path)[1]) dirpath, filename = os.path.split(path) logger.info('Scanning archive %r in %r', filename, dirpath) # rar extension if filename.endswith('.rar'): rar = RarFile(path) # filter on video extensions rar_filenames = [ f for f in rar.namelist() if f.endswith(VIDEO_EXTENSIONS) ] # no video found if not rar_filenames: raise ValueError('No video in archive') # more than one video found if len(rar_filenames) > 1: raise ValueError('More than one video in archive') # guess rar_filename = rar_filenames[0] rar_filepath = os.path.join(dirpath, rar_filename) video = Video.fromguess(rar_filepath, guessit(rar_filepath)) # size video.size = rar.getinfo(rar_filename).file_size else: raise ValueError('Unsupported extension %r' % os.path.splitext(path)[1]) return video
def __init__(self, studentnr, specs, source_filename): Submission.__init__(self, studentnr) try: source_file = RarFile(source_filename) for spec in specs: part = SubmissionPart(spec) self.parts.append(part) for filename in source_file.namelist(): filename = filename.replace('\\', '/') info = source_file.getinfo(filename) if not info.isdir() and spec.matches(filename): part.add_data(filename, source_file.open(filename).read()) except BadRarFile: pass except NotRarFile: pass
def scan_archive(path): """Scan an archive from a `path`. :param str path: existing path to the archive. :return: the scanned video. :rtype: :class:`~subliminal.video.Video` """ # check for non-existing path if not os.path.exists(path): raise ValueError('Path does not exist') # check video extension if not path.endswith(ARCHIVE_EXTENSIONS): raise ValueError('%r is not a valid archive extension' % os.path.splitext(path)[1]) dirpath, filename = os.path.split(path) logger.info('Scanning archive %r in %r', filename, dirpath) # rar extension if filename.endswith('.rar'): rar = RarFile(path) # filter on video extensions rar_filenames = [f for f in rar.namelist() if f.endswith(VIDEO_EXTENSIONS)] # no video found if not rar_filenames: raise ValueError('No video in archive') # more than one video found if len(rar_filenames) > 1: raise ValueError('More than one video in archive') # guess rar_filename = rar_filenames[0] rar_filepath = os.path.join(dirpath, rar_filename) video = Video.fromguess(rar_filepath, guessit(rar_filepath)) # size video.size = rar.getinfo(rar_filename).file_size else: raise ValueError('Unsupported extension %r' % os.path.splitext(path)[1]) return video
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
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