Esempio n. 1
0
def import_data(importer,
                library_path_map,
                config_location=None,
                progress1=None,
                progress2=None,
                abort=None):
    from calibre.db.cache import import_library
    config_location = config_location or config_dir
    config_location = os.path.abspath(os.path.realpath(config_location))
    total = len(library_path_map) + 1
    library_usage_stats = Counter()
    for i, (library_key, dest) in enumerate(library_path_map.iteritems()):
        if abort is not None and abort.is_set():
            return
        if progress1 is not None:
            progress1(dest, i, total)
        try:
            os.makedirs(dest)
        except EnvironmentError as err:
            if err.errno != errno.EEXIST:
                raise
        if not os.path.isdir(dest):
            raise ValueError('%s is not a directory' % dest)
        import_library(library_key,
                       importer,
                       dest,
                       progress=progress2,
                       abort=abort).close()
        library_usage_stats[dest] = importer.metadata['libraries'].get(
            library_key, 1)
    if progress1 is not None:
        progress1(_('Settings and plugins'), total - 1, total)

    if abort is not None and abort.is_set():
        return
    base_dir = tempfile.mkdtemp(dir=os.path.dirname(config_location))
    importer.export_config(base_dir, library_usage_stats)
    if os.path.lexists(config_location):
        if os.path.islink(config_location) or os.path.isfile(config_location):
            os.remove(config_location)
        else:
            shutil.rmtree(config_location, ignore_errors=True)
            if os.path.exists(config_location):
                try:
                    shutil.rmtree(config_location)
                except EnvironmentError:
                    if not iswindows:
                        raise
                    time.sleep(1)
                    shutil.rmtree(config_location)
    try:
        os.rename(base_dir, config_location)
    except EnvironmentError:
        time.sleep(2)
        os.rename(base_dir, config_location)
    from calibre.gui2 import gprefs
    gprefs.refresh()

    if progress1 is not None:
        progress1(_('Completed'), total, total)
Esempio n. 2
0
def import_data(importer, library_path_map, config_location=None, progress1=None, progress2=None, abort=None):
    from calibre.db.cache import import_library
    config_location = config_location or config_dir
    config_location = os.path.abspath(os.path.realpath(config_location))
    total = len(library_path_map) + 1
    library_usage_stats = Counter()
    for i, (library_key, dest) in enumerate(library_path_map.iteritems()):
        if abort is not None and abort.is_set():
            return
        if progress1 is not None:
            progress1(dest, i, total)
        try:
            os.makedirs(dest)
        except EnvironmentError as err:
            if err.errno != errno.EEXIST:
                raise
        if not os.path.isdir(dest):
            raise ValueError('%s is not a directory' % dest)
        import_library(library_key, importer, dest, progress=progress2, abort=abort).close()
        library_usage_stats[dest] = importer.metadata['libraries'].get(library_key, 1)
    if progress1 is not None:
        progress1(_('Settings and plugins'), total - 1, total)

    if abort is not None and abort.is_set():
        return
    base_dir = tempfile.mkdtemp(dir=os.path.dirname(config_location))
    importer.export_config(base_dir, library_usage_stats)
    if os.path.lexists(config_location):
        if os.path.islink(config_location) or os.path.isfile(config_location):
            os.remove(config_location)
        else:
            shutil.rmtree(config_location, ignore_errors=True)
            if os.path.exists(config_location):
                try:
                    shutil.rmtree(config_location)
                except EnvironmentError:
                    if not iswindows:
                        raise
                    time.sleep(1)
                    shutil.rmtree(config_location)
    try:
        os.rename(base_dir, config_location)
    except EnvironmentError:
        time.sleep(2)
        os.rename(base_dir, config_location)
    from calibre.gui2 import gprefs
    gprefs.refresh()

    if progress1 is not None:
        progress1(_('Completed'), total, total)
Esempio n. 3
0
 def test_export_import(self):
     from calibre.db.cache import import_library
     from calibre.utils.exim import Exporter, Importer
     cache = self.init_cache()
     for part_size in (1 << 30, 100, 1):
         with TemporaryDirectory('export_lib') as tdir, TemporaryDirectory('import_lib') as idir:
             exporter = Exporter(tdir, part_size=part_size)
             cache.export_library('l', exporter)
             importer = Importer(tdir)
             ic = import_library('l', importer, idir)
             self.assertEqual(cache.all_book_ids(), ic.all_book_ids())
             for book_id in cache.all_book_ids():
                 self.assertEqual(cache.cover(book_id), ic.cover(book_id), 'Covers not identical for book: %d' % book_id)
                 for fmt in cache.formats(book_id):
                     self.assertEqual(cache.format(book_id, fmt), ic.format(book_id, fmt))
Esempio n. 4
0
 def test_export_import(self):
     from calibre.db.cache import import_library
     from calibre.utils.exim import Exporter, Importer
     cache = self.init_cache()
     for part_size in (1 << 30, 100, 1):
         with TemporaryDirectory('export_lib') as tdir, TemporaryDirectory('import_lib') as idir:
             exporter = Exporter(tdir, part_size=part_size)
             cache.export_library('l', exporter)
             exporter.commit()
             importer = Importer(tdir)
             ic = import_library('l', importer, idir)
             self.assertEqual(cache.all_book_ids(), ic.all_book_ids())
             for book_id in cache.all_book_ids():
                 self.assertEqual(cache.cover(book_id), ic.cover(book_id), 'Covers not identical for book: %d' % book_id)
                 for fmt in cache.formats(book_id):
                     self.assertEqual(cache.format(book_id, fmt), ic.format(book_id, fmt))
                     self.assertEqual(cache.format_metadata(book_id, fmt)['mtime'], cache.format_metadata(book_id, fmt)['mtime'])