def export_config(self, base_dir, library_usage_stats): for key, relpath in self.metadata['config_dir']: f = self.start_file(key, relpath) path = os.path.join(base_dir, relpath.replace('/', os.sep)) try: with lopen(path, 'wb') as dest: shutil.copyfileobj(f, dest) except EnvironmentError: os.makedirs(os.path.dirname(path)) with lopen(path, 'wb') as dest: shutil.copyfileobj(f, dest) f.close() gpath = os.path.join(base_dir, 'global.py') try: with lopen(gpath, 'rb') as f: raw = f.read() except EnvironmentError: raw = b'' try: lpath = library_usage_stats.most_common(1)[0][0] except Exception: lpath = None c = create_global_prefs(StringConfig(raw, 'calibre wide preferences')) c.set('installation_uuid', str(uuid.uuid4())) c.set('library_path', lpath) raw = c.src if not isinstance(raw, bytes): raw = raw.encode('utf-8') with lopen(gpath, 'wb') as f: f.write(raw) gprefs = JSONConfig('gui', base_path=base_dir) gprefs['library_usage_stats'] = dict(library_usage_stats)