<commit_before>import argparse
from datetime import datetime
import os
import stat
import sys

from .archive import Archive
from .store import Store
from .cache import Cache
from .keychain import Keychain
from .helpers import location_validator, format_file_size, format_time,\
    format_file_mode, walk_dir, IncludePattern, ExcludePattern, exclude_path


class Archiver(object):

    def __init__(self):
        self.exit_code = 0

    def open_store(self, location):
        return Store(location.path)

    def print_error(self, msg, *args):
        msg = args and msg % args or msg
        if hasattr(sys.stderr, 'encoding'):
            msg = msg.encode(sys.stderr.encoding, 'ignore')
        self.exit_code = 1
        print >> sys.stderr, msg

    def print_verbose(self, msg, *args, **kw):
        if self.verbose:
            msg = args and msg % args or msg
            if hasattr(sys.stdout, 'encoding'):
                msg = msg.encode(sys.stdout.encoding, 'ignore')
            if kw.get('newline', True):
                print msg
            else:
                print msg,

    def do_init(self, args):
        Store(args.store.path, create=True)
        return self.exit_code

    def do_create(self, args):
        store = self.open_store(args.archive)
        keychain = Keychain(args.keychain)
        try:
            Archive(store, keychain, args.archive.archive)
        except Archive.DoesNotExist:
            pass
        else:
            self.print_error('Archive already exists')
            return self.exit_code
        archive = Archive(store, keychain)
        cache = Cache(store, keychain)
        for path in args.paths:
            for path, st in walk_dir(unicode(path)):
                if exclude_path(path, args.patterns):
                    continue
                self.print_verbose(path)
                if stat.S_ISDIR(st.st_mode):
                    archive.process_dir(path, st)
                elif stat.S_ISLNK(st.st_mode):
                    archive.process_symlink(path, st)
                elif stat.S_ISFIFO(st.st_mode):
                    archive.process_fifo(path, st)
                elif stat.S_ISREG(st.st_mode):
                    try:
                        archive.process_file(path, st, cache)
                    except IOError, e:
                        self.print_error('%s: %s', path, e)
                else:
                    self.print_error('Unknown file type: %s', path)
        archive.save(args.archive.archive)
        cache.save()
        return self.exit_code

    def do_extract(self, args):
        store = self.open_store(args.archive)
        keychain = Keychain(args.keychain)
        archive = Archive(store, keychain, args.archive.archive)
        archive.get_items()
        dirs = []
        for item in archive.items:
            if exclude_path(item['path'], args.patterns):
                continue
            self.print_verbose(item['path'])
            archive.extract_item(item, args.dest)
            if stat.S_ISDIR(item['mode']):
                dirs.append(item)
            if dirs and not item['path'].startswith(dirs[-1]['path']):
                # Extract directories twice to make sure mtime is correctly restored
                archive.extract_item(dirs.pop(-1), args.dest)
        while dirs:
            archive.extract_item(dirs.pop(-1), args.dest)
        return self.exit_code

    def do_delete(self, args):
        store = self.open_store(args.archive)
        keychain = Keychain(args.keychain)
        archive = Archive(store, keychain, args.archive.archive)
        cache = Cache(store, keychain)
        archive.delete(cache)
        return self.exit_code

    def do_list(self, args):
        store = self.open_store(args.src)
        keychain = Keychain(args.keychain)
        if args.src.archive:
            tmap = {1: 'p', 2: 'c', 4: 'd', 6: 'b', 010: '-', 012: 'l', 014: 's'}
            archive = Archive(store, keychain, args.src.archive)
            archive.get_items()<commit_msg>Fix extract command verbose output<commit_after>import argparse
from datetime import datetime
import os
import stat
import sys

from .archive import Archive
from .store import Store
from .cache import Cache
from .keychain import Keychain
from .helpers import location_validator, format_file_size, format_time,\
    format_file_mode, walk_dir, IncludePattern, ExcludePattern, exclude_path


class Archiver(object):

    def __init__(self):
        self.exit_code = 0

    def open_store(self, location):
        return Store(location.path)

    def print_error(self, msg, *args):
        msg = args and msg % args or msg
        if hasattr(sys.stderr, 'encoding'):
            msg = msg.encode(sys.stderr.encoding, 'ignore')
        self.exit_code = 1
        print >> sys.stderr, msg

    def print_verbose(self, msg, *args, **kw):
        if self.verbose:
            msg = args and msg % args or msg
            if hasattr(sys.stdout, 'encoding'):
                msg = msg.encode(sys.stdout.encoding, 'ignore')
            if kw.get('newline', True):
                print msg
            else:
                print msg,

    def do_init(self, args):
        Store(args.store.path, create=True)
        return self.exit_code

    def do_create(self, args):
        store = self.open_store(args.archive)
        keychain = Keychain(args.keychain)
        try:
            Archive(store, keychain, args.archive.archive)
        except Archive.DoesNotExist:
            pass
        else:
            self.print_error('Archive already exists')
            return self.exit_code
        archive = Archive(store, keychain)
        cache = Cache(store, keychain)
        for path in args.paths:
            for path, st in walk_dir(unicode(path)):
                if exclude_path(path, args.patterns):
                    continue
                self.print_verbose(path)
                if stat.S_ISDIR(st.st_mode):
                    archive.process_dir(path, st)
                elif stat.S_ISLNK(st.st_mode):
                    archive.process_symlink(path, st)
                elif stat.S_ISFIFO(st.st_mode):
                    archive.process_fifo(path, st)
                elif stat.S_ISREG(st.st_mode):
                    try:
                        archive.process_file(path, st, cache)
                    except IOError, e:
                        self.print_error('%s: %s', path, e)
                else:
                    self.print_error('Unknown file type: %s', path)
        archive.save(args.archive.archive)
        cache.save()
        return self.exit_code

    def do_extract(self, args):
        store = self.open_store(args.archive)
        keychain = Keychain(args.keychain)
        archive = Archive(store, keychain, args.archive.archive)
        archive.get_items()
        dirs = []
        for item in archive.items:
            if exclude_path(item['path'], args.patterns):
                continue
            self.print_verbose(item['path'].decode('utf-8'))
            archive.extract_item(item, args.dest)
            if stat.S_ISDIR(item['mode']):
                dirs.append(item)
            if dirs and not item['path'].startswith(dirs[-1]['path']):
                # Extract directories twice to make sure mtime is correctly restored
                archive.extract_item(dirs.pop(-1), args.dest)
        while dirs:
            archive.extract_item(dirs.pop(-1), args.dest)
        return self.exit_code

    def do_delete(self, args):
        store = self.open_store(args.archive)
        keychain = Keychain(args.keychain)
        archive = Archive(store, keychain, args.archive.archive)
        cache = Cache(store, keychain)
        archive.delete(cache)
        return self.exit_code

    def do_list(self, args):
        store = self.open_store(args.src)
        keychain = Keychain(args.keychain)
        if args.src.archive:
            tmap = {1: 'p', 2: 'c', 4: 'd', 6: 'b', 010: '-', 012: 'l', 014: 's'}
            archive = Archive(store, keychain, args.src.archive)
            archive.get_items()
{'fraction_non_alphanumeric': 0.10283731879568826, 'fraction_numerical': 0.004584314211374055, 'mean_word_length': 1.9067338854879365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '60251', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2272, 'n_words': 557}