def unit_generator(dbs, module_name, hostname): """ Generator to produce all units visible to the API caller :param dbs: The list of repo gdm files available to query for data :type dbs: dict :param module_name: The module name to search for :type module_name: str :param hostname: The hostname of server serving modules :type hostname: str """ for repo_id, data in dbs.iteritems(): protocol = data['protocol'] db = data['db'] try: json_data = db[module_name] except KeyError: _LOGGER.debug('module %s not found in repo %s' % (module_name, repo_id)) continue units = json.loads(json_data) for unit in units: yield Unit(name=module_name, db=db, repo_id=repo_id, host=hostname, protocol=protocol, **unit)
def find_version(repo_ids, module_name, version): """ Find a particular version of the requested module :param repo_ids: IDs of repos to search in :type repo_ids: list :param module_name: name of module in form "author/title" :type module_name: str :param version: version to search for :type version: str :return: Unit instance :rtype: puppet.forge.unit.Unit """ dbs = get_repo_data(repo_ids) host = get_host_and_protocol()['host'] ret = None try: for repo_id, data in dbs.iteritems(): units = Unit.units_from_json(module_name, data['db'], repo_id, host, data['protocol']) for unit in units: if unit.version == version: ret = unit break finally: # close database files we don't need to use if ret: del dbs[ret.repo_id] for data in dbs.itervalues(): data['db'].close() return ret
def find_newest(repo_ids, module_name): """ Find the newest version of the requested module :param repo_ids: IDs of repos to search in :type repo_ids: list :param module_name: name of module in form "author/title" :type module_name: str :return: Unit instance, or None if not found :rtype: puppet.forge.unit.Unit """ dbs = get_repo_data(repo_ids) host = get_host_and_protocol()['host'] ret = None try: for repo_id, data in dbs.iteritems(): units = Unit.units_from_json(module_name, data['db'], repo_id, host, data['protocol']) if units: repo_max = max(units) if ret is None or repo_max > ret: ret = repo_max finally: # close database files we don't need to use if ret: del dbs[ret.repo_id] for data in dbs.itervalues(): data['db'].close() return ret
def unit_generator(dbs, module_name, hostname): """ Generator to produce all units visible to the API caller :param dbs: The list of repo gdm files available to query for data :type dbs: dict :param module_name: The module name to search for :type module_name: str :param hostname: The hostname of server serving modules :type hostname: str :return: A generator of pulp_puppet.forge.unit.Unit objects :rtype: generator """ for repo_id, data in dbs.iteritems(): protocol = data['protocol'] db = data['db'] try: json_data = db[module_name] except KeyError: msg_dict = {'module': module_name, 'repo_id': repo_id} msg = _('module %(module)s not found in repo %(repo_id)s') _LOGGER.debug(msg, msg_dict) continue units = json.loads(json_data) for unit in units: yield Unit(name=module_name, db=db, repo_id=repo_id, host=hostname, protocol=protocol, **unit)
def test_valid(self): name = 'me/stuntmodule' db = {name: self.UNIT_JSON} result = Unit.units_from_json(name, db, 'repo1', 'localhost', 'http') self.assertEqual(len(result), 1) self.assertTrue(isinstance(result[0], Unit)) self.assertEqual(result[0].name, name) self.assertEqual(result[0].repo_id, 'repo1') self.assertEqual(result[0].host, 'localhost') self.assertEqual(result[0].protocol, 'http')
def test_valid(self): name = 'me/stuntmodule' db = {name:self.UNIT_JSON} result = Unit.units_from_json(name, db, 'repo1', 'localhost', 'http') self.assertEqual(len(result), 1) self.assertTrue(isinstance(result[0], Unit)) self.assertEqual(result[0].name, name) self.assertEqual(result[0].repo_id, 'repo1') self.assertEqual(result[0].host, 'localhost') self.assertEqual(result[0].protocol, 'http')
def test_not_in_db(self): name = 'me/stuntmodule' db = {} result = Unit.units_from_json(name, db, 'repo1', 'localhost', 'http') self.assertEqual(len(result), 0)