def test(self): tmpdir = tempfile.mkdtemp(prefix='laymantmp_') makeconf = os.path.join(tmpdir, 'make.conf') reposconf = os.path.join(tmpdir, 'repos.conf') make_txt =\ 'PORTDIR_OVERLAY="\n'\ '$PORTDIR_OVERLAY"' # Create the .conf files so layman doesn't # complain. with fileopen(makeconf, 'w') as f: f.write(make_txt) with fileopen(reposconf, 'w') as f: f.write('') my_opts = { 'installed': HERE + '/testfiles/global-overlays.xml', 'make_conf': makeconf, 'nocheck': 'yes', 'storage': tmpdir, 'repos_conf': reposconf, 'conf_type': ['make.conf', 'repos.conf'], } config = OptionConfig(my_opts) config.set_option('quietness', 3) a = DB(config) config['output'].set_colorize(False) conf = RepoConfManager(config, a.overlays) # Set up our success tracker. success = [] # Add all the overlays in global_overlays.xml. for overlay in a.overlays.keys(): conf_success = conf.add(a.overlays[overlay]) if conf_success == False: success.append(False) else: success.append(True) # Disable one overlay. self.assertTrue(conf.disable(a.overlays['wrobel'])) # Enable disabled overlay. self.assertTrue(conf.enable(a.overlays['wrobel'])) # Delete all the overlays in global_overlays.xml. for overlay in a.overlays.keys(): self.assertTrue(conf.delete(a.overlays[overlay])) # Clean up. os.unlink(makeconf) os.unlink(reposconf) shutil.rmtree(tmpdir)
def _get_installed_db(self, dbreload=False): """returns the list of installed overlays""" if not self._installed_db or dbreload: self._installed_db = DB(self.config) self.output.debug("API._get_installed_db; len(installed) = %s, %s" %(len(self._installed_db.list_ids()), self._installed_db.list_ids()), 5) return self._installed_db
def migrate_database(self, migrate_type): if migrate_type not in DB_TYPES: msg = 'migrate_database() error; invalid migration type: '\ '"%(db_type)s"' % {'db_type': migrate_type} self.output.die(msg) db = DB(self.config) installed = self.config['installed'] old_ext = os.path.splitext(installed)[1] backup_name = installed + '.' + self.config['db_type'] if old_ext == "." + self.config['db_type']: backup_name = installed + '.bak' new_name = installed.replace(old_ext, '.db') if not os.path.isfile(installed): msg = 'migrate_database() error; database file "%(loc)s" does not '\ 'exist!' % {'loc': backup_name} self.output.error(' ' + msg) raise Exception(msg) msg = ' Creating backup of "%(db)s" at:\n "%(loc)s"\n'\ % {'db': installed, 'loc': backup_name} self.output.info(msg) try: if migrate_type in ('json', 'xml'): shutil.copy(installed, backup_name) else: shutil.move(installed, backup_name) except IOError as err: msg = ' migrate_database() error; failed to back up old database '\ 'file.\n Error was: %(err)s' % {'err': err} self.output.error(msg) raise err db.write(installed, migrate_type=migrate_type) try: os.rename(installed, new_name) except OSError as err: msg = ' migrate_database() error: failed to rename old database '\ ' to "%(name)s".\n Error was: %(err)s' % {'err': err} self.output.error(msg) raise err msg = ' Successfully migrated database from "%(from_type)s" to '\ ' "%(to_type)s"\n' % {'from_type': self.config['db_type'], 'to_type': migrate_type} self.output.info(msg) self.set_db_type(migrate_type, os.path.basename(new_name)) msg = ' Warning: Please be sure to update your config file via '\ 'the\n `dispatch-conf` command or you *will* lose database '\ 'functionality!\n' self.output.warn(msg)
def test(self): repo_name = 'tar_test_overlay' temp_dir_path = tempfile.mkdtemp(prefix='laymantmp_') db_file = os.path.join(temp_dir_path, 'installed.xml') make_conf = os.path.join(temp_dir_path, 'make.conf') repo_conf = os.path.join(temp_dir_path, 'repos.conf') tar_source_path = os.path.join(HERE, 'testfiles', 'layman-test.tar.bz2') (_, temp_tarball_path) = tempfile.mkstemp() shutil.copyfile(tar_source_path, temp_tarball_path) # Write overlay collection XML xml_text = '''\ <?xml version="1.0" encoding="UTF-8"?> <repositories xmlns="" version="1.0"> <repo quality="experimental" status="unofficial"> <name>%(repo_name)s</name> <description>XXXXXXXXXXX</description> <owner> <email>[email protected]</email> </owner> <source type="tar">file://%(temp_tarball_url)s</source> </repo> </repositories> '''\ % { 'temp_tarball_url': urllib.pathname2url(temp_tarball_path), 'repo_name': repo_name } (fd, temp_xml_path) = tempfile.mkstemp() my_opts = { 'installed': temp_xml_path, 'conf_type': ['make.conf', 'repos.conf'], 'db_type': 'xml', 'nocheck': 'yes', 'make_conf': make_conf, 'repos_conf': repo_conf, 'storage': temp_dir_path, 'check_official': False } with os.fdopen(fd, 'w') as f: f.write(xml_text) with fileopen(make_conf, 'w') as f: f.write('PORTDIR_OVERLAY="$PORTDIR_OVERLAY"\n') with fileopen(repo_conf, 'w') as f: f.write('') config = OptionConfig(options=my_opts) config.set_option('quietness', 3) a = DB(config) config.set_option('installed', db_file) # Add an overlay to a fresh DB file. b = DB(config) b.add(a.select(repo_name)) # Make sure it's actually installed. specific_overlay_path = os.path.join(temp_dir_path, repo_name) self.assertTrue(os.path.exists(specific_overlay_path)) # Check the DbBase to ensure that it's reading the installed.xml. c = DbBase(config, paths=[ db_file, ]) self.assertEqual(list(c.overlays), ['tar_test_overlay']) # Make sure the configs have been written to correctly. conf = RepoConfManager(config, b.overlays) self.assertEqual(list(conf.overlays), ['tar_test_overlay']) # Delete the overlay from the second DB. b.delete(b.select(repo_name)) self.assertEqual(b.overlays, {}) # Ensure the installed.xml has been cleaned properly. c = DbBase(config, paths=[ db_file, ]) self.assertEqual(c.overlays, {}) conf = RepoConfManager(config, b.overlays) self.assertEqual(conf.overlays, {}) # Clean up. os.unlink(temp_xml_path) os.unlink(temp_tarball_path) shutil.rmtree(temp_dir_path)