예제 #1
0
def move_file(source_file, destination, create_dirs=False, new_filename=None, debug_print=True):
    "Custom file move method using mv command in the background"
    source_file = path_to_str(source_file)
    destination = path_to_str(destination)
    if not util.is_file(source_file):
        if debug_print:
            print(
                f'source {CSTR(source_file, "orange")} does not exist!')
        return False
    if not util.is_dir(destination) and create_dirs:
        os.makedirs(destination)
        if debug_print:
            print(f'move_file: created dir {CSTR(destination, "lblue")}')
    elif not util.is_dir(destination) and not create_dirs:
        if debug_print:
            print(f'destination {CSTR(destination, "red")} does not exists!')
        return False
    if debug_print:
        print(f'moving  {CSTR(source_file, "lblue")}')
    if new_filename:
        command = f'mv \"{source_file}\" \"{destination}/{new_filename}\"'
        if debug_print:
            print(
                f'destination {CSTR(f"{destination}/{new_filename}", "lblue")}')
    else:
        command = f'mv \"{source_file}\" \"{destination}\"'
        if debug_print:
            print(f'destination {CSTR(destination, "lblue")}')
    if local_command(command, hide_output=True, print_info=False):
        if debug_print:
            print(CSTR('done!', 'lgreen'))
        return True
    if debug_print:
        print(CSTR('move failed!', 'red'))
    return False
예제 #2
0
def create_movie_nfo(movie_dir: str, imdb_id: str, debug_print=False):
    mstr = __name__ + ".create_movie_nfo"
    imdb_id = util.parse_imdbid(imdb_id)
    if not imdb_id:
        pfcs(f"o[{mstr}] could not parse imdb-id from e[{imdb_id}]",
             show=debug_print)
        return None
    if not util.is_dir(movie_dir) and exists(movie_dir):
        movie_dir = movie_path(movie_dir)
    if not util.is_dir(movie_dir):
        pfcs(
            f"o[{mstr}] could not determine location of e[{Path(movie_dir).name}]",
            show=debug_print)
        return None
    previous_imdb = get_movie_nfo_imdb_id(movie_dir, debug_print=debug_print)
    file_loc = Path(movie_dir) / 'movie.nfo'
    with open(file_loc, 'w') as file_item:
        file_item.write(f'https://www.imdb.com/title/{imdb_id}')
    if debug_print:
        prev_str = ""
        if previous_imdb:
            prev_str = f" previous id was o[{previous_imdb}]"
        pfcs(
            f"o[{mstr}] wrote g[{imdb_id}] to movie.nfo for g[{Path(movie_dir).name}]{prev_str}"
        )
예제 #3
0
def imdb_from_nfo(show_name: str):
    'return the imdb-id from a tvshow.nfo, or None if unavailalble'
    if not util.is_dir(show_name):
        show_name = os.path.join(SHOW_DIR, show_name)
        if not util.is_dir(show_name):
            return None
    nfo_file = os.path.join(show_name, 'tvshow.nfo')
    if not util.is_file(nfo_file):
        return None
    return util.parse_imdbid_from_file(nfo_file)
예제 #4
0
def save_nfo(show_name, imdb_id: str):
    if not util.is_imdbid(imdb_id):
        return False
    show_path = Path(show_name)
    if not util.is_dir(show_path):
        show_path = Path(SHOW_DIR) / show_name
    if not util.is_dir(show_path):
        return False
    file_path = show_path / "tvshow.nfo"
    if file_path.is_file():
        return False
    with open(show_path / "tvshow.nfo", "w") as nfo_file:
        nfo_file.write(imdb_id)
        return show_path / "tvshow.nfo"
    def mkdir_p(self, path):
        """Provides mkdir -p type functionality on the remote host. That is, all missing parent
        directories are also created. If the directory we are trying to create already exists, we
        do nothing. If path or any of its parents is not a directory an exception is raised.
        path - directory to create on remote host
        """
        _logger.debug('\nRCH--> remote_ch, mkdir_p(%s)', path)
        path = self._sftpify(path)
        pa = self.file_exists(path)
        if pa != None:
            if not util.is_dir(pa):
                raise Exception(self.host+':'+path+' is not a directory')
            return
        # Need to user normpath here since dirname of a directory with a trailing slash
        # is the directory without a slash (a dirname bug?)
        sd = ntpath.splitdrive(path)
        _logger.debug('\nRCH--> sd=%s', sd)
        if sd[1] == '':
            _logger.warning(bcolors.WARNING + '\nRCH--> path=%s is a drive letter. Returning...' +
                            bcolors.ENDC, path)
            return

        np = self.path_module.normpath(path)
        parent = self.path_module.dirname(np)
        assert parent != path
        _logger.debug('\nRCH--> remote_ch, making dir %s', path)
        self.mkdir_p(parent)
        self.sftp.mkdir(np)
예제 #6
0
def move_finished_downloads(extensions=util.video_extensions(),
                            delete_source_dir=True,
                            dest_dir=user_download_dir(),
                            pre_rename=True):
    if not util.is_dir(dest_dir):
        pfcs(f'destination dir does not exist: e[{dest_dir}]')
        return
    count = 0
    found_items = find_finished_downloads(extensions=extensions)
    if not found_items:
        pfcs(
            f'found no completed files with extension(s) i<{extensions}> in NZBGet destination path', format_chars=['<', '>'])
    if found_items:
        print("processing finished downloads...")
    for download in found_items:
        filename = download.name
        containing_dir = download.parent
        rename_log_str = ""
        if pre_rename:
            pre_result = pre_search_from_file(download.name)
            if pre_result:
                filename = f"{pre_result}{download.suffix}"
                rename_log_str = f"\n  renamed i[{filename}]"
        if move_file(download, dest_dir, new_filename=filename, debug_print=False):
            pfcs(f'moved i[{download.name}] to g[{dest_dir}]{rename_log_str}')
            count += 1
            if delete_source_dir:
                shutil.rmtree(containing_dir)
                pfcs(f'removed w[{containing_dir}]')
        else:
            pfcs(f'failed to move e[{download.name}] to w[{dest_dir}]!')
        pfcs(f"d[{'-' * util.terminal_width()}]")
    return count
예제 #7
0
    def mkdir_p(self, path):
        """Provides mkdir -p type functionality on the remote host. That is,
        all missing parent directories are also created. If the directory we are trying to 
        create already exists, we silently do nothing. If path or any of its parents is not
        a directory an exception is raised.
        path - directory to create on remote host
        """
        _logger.debug('mkdir_p(' + path + ')')
        path = self._sftpify(path)
        pa = self.file_exists(path)
        if pa != None:
            #print str(pa)+" "+str(pa.st_mode)
            if not util.is_dir(pa):
                raise Exception(self.host + ':' + path + ' is not a directory')
            return
        # Need to user normpath here since dirname of a directory with a trailing slash
        #  is the directory without a slash (a dirname bug?)
        sd = ntpath.splitdrive(path)
        _logger.debug('sd=' + str(sd))
        if sd[1] == '':
            _logger.debug('path=' + path + ' is a drive letter. Returning...')
            return

        np = self.path_module.normpath(path)
        parent = self.path_module.dirname(np)
        assert parent != path
        self.mkdir_p(parent)
        self.sftp.mkdir(np)
 def mkdir_p(self, path):
     """Provides mkdir -p type functionality on the remote host. That is,
     all missing parent directories are also created. If the directory we are trying to 
     create already exists, we silently do nothing. If path or any of its parents is not
     a directory an exception is raised.
     path - directory to create on remote host
     """
     _logger.debug('mkdir_p('+path+')')
     path = self._sftpify(path)
     pa = self.file_exists(path)
     if pa != None:
         #print str(pa)+" "+str(pa.st_mode)
         if not util.is_dir(pa):
             raise Exception(self.host+':'+path+' is not a directory')
         return
     # Need to user normpath here since dirname of a directory with a trailing slash
     #  is the directory without a slash (a dirname bug?)
     sd = ntpath.splitdrive(path)
     _logger.debug('sd='+str(sd))
     if sd[1] == '':
         _logger.debug('path='+path+' is a drive letter. Returning...')
         return
         
     np = self.path_module.normpath(path)
     parent = self.path_module.dirname(np)
     assert parent != path
     self.mkdir_p(parent)
     self.sftp.mkdir(np)
예제 #9
0
def deserialize(bits):
    assert_type(bits, BitArray, 'deserialize')
    s = bits.tobytes()
    assert_type(s, str, 'deserialize')
#   print 'serialized payload from twitter: %s bytes -> %s bytes' % \
#          (len(bits) / 8.0, len(s))
    print 'serialized payload from twitter: %s bytes' % len(s)
    x = bson.loads(s)
    if not is_file(x) and not is_dir(x):
        raise ArgumentError('FATAL: bad type "%s"' % x['type'])
    return x
예제 #10
0
def _match_subdir(orig_path, sub_str: str):
    if not util.is_dir(orig_path):
        return ""
    for item in os.listdir(orig_path):
        if util.is_file(os.path.join(orig_path, item)):
            continue
        if is_ds_special_dir(item):
            continue
        if sub_str.lower() in item.lower():
            return item  # TODO: handle several matches
    return ""
예제 #11
0
 def rm_r(self, path):
     """Provides rm -r type functionality on the remote host. That is, all files and 
     directories are removed recursively.
     path - file or directory to remove
     """
     path = self._sftpify(path)
     if util.is_dir(self.sftp.stat(path)):
         for f in self.sftp.listdir(path):
             self.rm_r(self.posixpath.join(path, f))
         self.sftp.rmdir(path)
     else:
         self.sftp.remove(path)
예제 #12
0
def get_movie_nfo_imdb_id(movie_dir: str, debug_print=False):
    "Get the imdb-id from a movie.nfo in the movie folder location"
    mstr = __name__ + ".get_movie_nfo_imdb_id"
    if not util.is_dir(movie_dir) and exists(movie_dir):
        movie_dir = movie_path(movie_dir)
    path = Path(movie_dir) / 'movie.nfo'
    if not path.is_file():
        pfcs(f"o[{mstr}] movie.nfo file does not exist in w[{movie_dir}]",
             show=debug_print)
        return None
    with open(path, 'r') as file_item:
        return util.parse_imdbid(file_item.readline())
예제 #13
0
def find_finished_downloads(extensions=util.video_extensions()):
    dst_path = nzbget_dest_path()
    if not util.is_dir(dst_path):
        pfcs(f'NZBGet destination path does not exist: e[{dst_path}]')
    found_files = []
    for dir_name, _, file_list in os.walk(dst_path):
        matched_files = [Path(dir_name) / file_item for file_item in file_list if any(
            ext in file_item for ext in extensions) and 'sample' not in file_item.lower()]
        if len(matched_files) == 1:
            found_files.append(matched_files[0])
        # TODO: handle len == 0 or len > 1
    return found_files
 def rm_r(self, path):
     """Provides rm -r type functionality on the remote host. That is, all files and 
     directories are removed recursively.
     path - file or directory to remove
     """
     path = self._sftpify(path)
     if util.is_dir(self.sftp.stat(path)):
         for f in self.sftp.listdir(path):
             self.rm_r(self.posixpath.join(path,f))
         self.sftp.rmdir(path)
     else:
         self.sftp.remove(path)
예제 #15
0
def move_nzbs_from_download():
    dest_dir = nzbget_nzb_path()
    if not util.is_dir(dest_dir):
        pfcs(f'destination dir does not exist: e[{dest_dir}]')
        return
    count = 0
    for dir_name, _, file_list in os.walk(user_download_dir()):
        nzb_files = [Path(
            dir_name) / file_item for file_item in file_list if file_item.endswith('.nzb')]
        if not nzb_files:
            continue
        for nzb_file in nzb_files:
            if move_file(nzb_file, dest_dir, debug_print=False):
                pfcs(f'moved i[{nzb_file.name}] to g[{dest_dir}]')
                count += 1
            else:
                pfcs(f'failed to move e[{nzb_file.name}] to w[{dest_dir}]!')
    return count
예제 #16
0
def _recursive_dir(parent, dir_name, lv):
    ret = ''

    this_path = parent + dir_name
    if not this_path.endswith('/'):
        this_path += '/'

    target_path = this_path

    list = util.list_dir(target_path)
    for item in list:
        path = this_path + item
        is_dir = util.is_dir(path)
        file_info = util.str_padding(item + ' ' + str(is_dir), ' ', lv) + '\n'
        ret += file_info
        if is_dir:
            ret += _recursive_dir(this_path, item, lv + 1)

    return ret
예제 #17
0
def unpack(payload, tweet_id, downloader, concealer, name_override=None, recur=False):
    assert_type(payload, dict, 'unpack')

    if is_file(payload):
        data, name, perms = payload['data'], payload['name'], int(payload['perms'])
        if name_override:
            name = name_override
        fatal_if_exists(name, 'file')
        print 'unpacked "%s" from tweet_id %s' % (name, tweet_id)
        write_file(name, data)
        print 'permissions: %s' % perms
        chmod(name, perms)
        print ''
    elif is_dir(payload):
        ids, name, perms = payload['ids'], payload['name'], int(payload['perms'])
        if name_override:
            name = name_override
        fatal_if_exists(name, 'directory')
        print 'unpacked "%s" with child tweet_ids %s' % (name, ids)
        write_dir(name)
        print 'permissions: %s' % perms
        chmod(name, perms)
        print ''
        if recur:
            chdir(name)
            for tweet_id in ids:
                payload = downloader(tweet_id)
                payload = concealer.reveal(payload)
                payload = deserialize(payload)
                unpack(payload,
                       tweet_id,
                       downloader,
                       concealer,
                       name_override=None,
                       recur=recur)
            chdir('..')
예제 #18
0
def extract(compressed_file: 'full path', destination, create_dirs=True, overwrite=True):
    "Extract files with fancy color output"
    compressed_file = path_to_str(compressed_file)
    destination = path_to_str(destination)
    if not util.is_file(compressed_file):
        print(
            f'compressed_file {CSTR(compressed_file, "orange")} does not exist!')
        return False
    if not util.is_dir(destination):
        if create_dirs:
            os.makedirs(destination)
            print(f'extract: created dir {CSTR(destination, "lblue")}')
        else:
            print(
                f'extract: destination {CSTR(destination, "orange")} does not exist!')
            return False
    # just support rar for now
    file_name = util.filename_of_path(compressed_file)
    print(f'extracting  {CSTR(file_name, "lblue")}')
    print(f'destination {CSTR(destination, "lblue")}')
    overwrite_arg = '-o+' if overwrite else ''
    command = shlex.split(
        f'unrar e {overwrite_arg} "{compressed_file}" "{destination}"')
    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    while process.poll() is None:
        byte_line = process.stdout.readline()
        line = byte_line.decode()
        if '%' in line:
            percentage_done = util.parse_percent(line)
            print(f'\r{file_name}  {CSTR(percentage_done, "lgreen")}', end='')
    print()
    if process.returncode == 0:
        print(CSTR('done!', 'lgreen'))
        return True
    print(CSTR('extract failed!', 'red'))
    return False
예제 #19
0
def exists(movie_dir_name: str):
    return util.is_dir(movie_path(movie_dir_name))
예제 #20
0
def test_is_dir():
    s = '\n'
    s += './    = ' + str(util.is_dir('./')) + '\n'
    s += 'a.txt = ' + str(util.is_dir('a.txt')) + '\n'
    return s