示例#1
0
def sync_dir():
    "Sync source dir with dest dir"
    LOG.warning('Start sync dir ...')
    # Launch database connection
    with Db(db_name='sync',
            db_file='%s/db.json' % ARGS.target,
            dry_run=DRY_RUN,
            safe_dump=True) as db:
        # For each files
        for dir_path, dirs, files in os.walk(ARGS.source):
            if not files: continue
            # For each files in dir
            for file_name in files:
                # Filter db file
                if (file_name == 'db.json'
                    or file_name == 'db.json.orig'):
                    continue
                # Exemple : -s Video/foo -t dest
                # Give : src = Video/foo : relative = foo : dst = dest/foo
                dir_source      = dir_path
                dir_relative    = re.sub(r'%s/?' % ARGS.source,'', dir_source)
                dir_dest        = join(ARGS.target, dir_relative)
                file_source     = join(dir_source, file_name)
                file_relative   = join(dir_relative, file_name)
                file_dest       = join(dir_dest, file_name)
    
                if db.get(file_relative) is None:
                    if is_include(file_relative) \
                    or not is_exclude(file_relative):
                        create_dir(dir_dest, dry_run=DRY_RUN)
                        try:
                            copy_file(file_source, file_dest, dry_run=DRY_RUN)
                        except IOError as error:
                            LOG.critical("Error can't copy file %s : %s"
                                         % (file_dest, error))
                        if check_file_consistency(file_source, file_dest, dry_run=DRY_RUN):
                            db.save(file_relative, 'unused')
                        else:
                            LOG.critical("Error file is not consistent "
                                         "the sum don't match")
        # Clean empty dir after sync
        remove_empty_dir(ARGS.target, dry_run=DRY_RUN)
    def _launch_app(self):
        # Init window
        self._init_window()
        title = ARGS.directory

        # Key mapping
        key = Keycode()
        c = None
        active_pos = 1
        # Get dir content
        line_number=30
        current_relativ_dir = ''
        directory_pages = self._paginate(self._dir_explore(root=ARGS.directory, directory=''),
                                         lines=line_number)
        page = directory_pages.get_content()
        while c != key.code_q:
            #LOG.critical('%s' % c)

            # Get selected item
            item = page[active_pos - 1]

            # -- DOWN
            if c == key.code_down:
              if active_pos < len_without_none(page):
                active_pos += 1
              else:
                active_pos = 1
            # -- UP
            elif c == key.code_up:
              if active_pos > 1:
                active_pos -= 1
              else:
                active_pos = len_without_none(page)
            # -- RIGHT
            elif c == key.code_right:
                # Change directory page and get content
                directory_pages.next()
                page = directory_pages.get_content()
                # Clear screen
                self._window.clear()
                active_pos = 1
            # -- LEFT
            elif c == key.code_left:
                # Change directory page and get content
                directory_pages.previous()
                page = directory_pages.get_content()
                # Clear screen
                self._window.clear()
                active_pos = 1
            # -- H
            elif c == key.code_h:
                self._display_help()
            # -- RETURN or DEL
            elif c in [key.code_return, key.code_del]:
                if item is not None and item.get('type') == 'file' \
                and self._display_del_confirm(item['name']):
                    # Unmark if marked
                    if item['marked']:
                        with Db(db_name='library', db_file='%s/db.json' % ARGS.directory) as db:
                            db.remove(join(current_relativ_dir, item['name']))
                    # Delete file
                    os.remove(join(ARGS.directory, current_relativ_dir, item['name']))
                    # Launch empty dir clean
                    remove_empty_dir(ARGS.directory)
                    # In case your current dir is remove goto root dir
                    if not os.path.isdir(join(ARGS.directory, current_relativ_dir)):
                        current_relativ_dir = ''

                    # Refresh explore
                    directory_pages = self._paginate(
                                            self._dir_explore(root=ARGS.directory, directory=current_relativ_dir),
                                            lines=line_number)
                    # Paginate the content
                    page = directory_pages.get_content()
                    self._window.clear()
            # -- SPACE
            elif c == key.code_space:
                if item.get('type') == 'file':
                    # mark as viewed
                    with Db(db_name='library', db_file='%s/db.json' % ARGS.directory) as db:
                        # Unmark
                        if db.get(join(current_relativ_dir,item['name'])):
                            db.remove(join(current_relativ_dir,item['name'].decode('utf-8')))
                            item['marked'] = False
                        # Mark
                        else:
                            db.save(join(current_relativ_dir,item['name']), 'unused')
                            item['marked'] = True
            # -- ENTER
            elif c == key.code_enter:
                if item.get('type') == 'directory':
                    # Change current dir
                    if item['name'] == '..':
                        current_relativ_dir = dirname(current_relativ_dir)
                    else:
                        current_relativ_dir = join(current_relativ_dir, item['name'])
                    # explore new dir
                    directory_pages = self._paginate(
                                            self._dir_explore(root=ARGS.directory, directory=current_relativ_dir),
                                            lines=line_number)
                    # Paginate the content
                    page = directory_pages.get_content()
                    # Clear screen
                    self._window.clear()
                    active_pos = 1
            # -- V
            elif c == key.code_v:
                video_path = join(ARGS.directory, current_relativ_dir, item['name'])
                LOG.warning('Launch vlc on %s' % video_path)
                subprocess.call("vlc '%s'" % video_path, shell=True)
                self._window.clear()

            # Display menu
            self._display_menu(title, page, active_pos)

            # Pages status
            self._window.addstr(len(page) + 6, 1,
                    " -- Page %s/%s" % (directory_pages._current, directory_pages._total))
            # Get key
            c = self._window.getch()
        return active_pos