Skip to content

Commit

Permalink
Merge pull request #66 from cverna/remove_pkgdb_criteria
Browse files Browse the repository at this point in the history
Remove the pkgdb criteria.
  • Loading branch information
cverna authored Feb 26, 2019
2 parents ddd3f7f + e3e9704 commit 6091ce9
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 156 deletions.
54 changes: 0 additions & 54 deletions fedbadges/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
graceful,

# These make networked API calls
get_pkgdb_packages_for,
user_exists_in_fas,
)

Expand Down Expand Up @@ -348,7 +347,6 @@ def matches(self, msg):
class Criteria(AbstractTopLevelComparator):
possible = frozenset([
'datanommer',
'pkgdb',
]).union(operators)

def __init__(self, *args, **kwargs):
Expand All @@ -361,8 +359,6 @@ def __init__(self, *args, **kwargs):
def _specialize(self):
if self.attribute == 'datanommer':
self.specialization = DatanommerCriteria(self.expected_value)
elif self.attribute == 'pkgdb':
self.specialization = PkgdbCriteria(self.expected_value)
# TODO -- expand this with other "backends" as necessary
# elif self.attribute == 'fas'
else:
Expand All @@ -381,56 +377,6 @@ def matches(self, msg):
class AbstractSpecializedComparator(AbstractComparator):
pass


class PkgdbCriteria(AbstractSpecializedComparator):
required = possible = frozenset([
'owns',
])

def __init__(self, *args, **kwargs):
super(PkgdbCriteria, self).__init__(*args, **kwargs)

# Validate the owns dict
if not isinstance(self._d['owns'], dict):
raise ValueError("'owns' must be a dict")

owns_fields = frozenset(['user', 'packages'])
argued_fields = frozenset(self._d['owns'].keys())

if not argued_fields.issubset(owns_fields):
raise KeyError(
"%r are not possible fields. Choose from %r" % (
argued_fields.difference(owns_fields),
owns_fields,
))

if not owns_fields.issubset(argued_fields):
raise KeyError(
"%r are missing required fields." % (
owns_fields.difference(argued_fields),
))

if not isinstance(self._d['owns']['packages'], list):
raise ValueError("'packages' must be a list")

def matches(self, msg):
""" A pkgdb criteria check checks if a user owns some packages. """

subs = construct_substitutions(msg)
expectation = format_args(copy.copy(self._d['owns']), subs)
expectation = recursive_lambda_factory(expectation, msg, name='msg')

actual_packages = get_pkgdb_packages_for(
config=fedmsg_config,
user=expectation['user'],
)

# Force lowercase, https://github.com/fedora-infra/tahrir/issues/315
actual_packages = set(map(unicode.lower, actual_packages))

return set(expectation['packages']).issubset(actual_packages)


class DatanommerCriteria(AbstractSpecializedComparator):
required = possible = frozenset([
'filter',
Expand Down
69 changes: 0 additions & 69 deletions fedbadges/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
import fedora.client
import requests

# This is used for our queries against pkgdb
from dogpile.cache import make_region
_cache = make_region()


# These are here just so they're available in globals()
# for compiling lambda expressions
import json
Expand Down Expand Up @@ -122,67 +117,3 @@ def user_exists_in_fas(config, user):
password=config['fas_credentials']['password'],
)
return bool(fas2.person_by_username(user))


def get_pkgdb_packages_for(config, user):
""" Retrieve the list of packages where the specified user some acl.
:arg config: a dict containing the fedmsg config
:arg user: the fas user of the packager whose packages are of
interest.
:return: a set listing all the packages where the specified user has
some ACL.
"""

if not hasattr(_cache, 'backend'):
_cache.configure(**config['fedbadges.rules.cache'])

@_cache.cache_on_arguments()
def _getter(user):
return _get_pkgdb2_packages_for(config, user)

return _getter(user)


def _get_pkgdb2_packages_for(config, username):
log.debug("Requesting pkgdb2 packages for user %r" % username)
if '/' in username:
log.debug('Service user, has no packages')
return set()

def _get_page(page):
req = requests.get('{0}/packager/acl/{1}'.format(
config['fedbadges.rules.utils.pkgdb_url'], username),
params=dict(page=page),
)

if not req.status_code == 200:
raise IOError("Couldn't talk to pkgdb2 for %r, %r, %r" % (
username, req.status_code, req.text))

return req.json()

packages = set()

# We have to request the first page of data to figure out the total number
data = _get_page(1)
if data is None:
return packages

pages = data['page_total']

for i in range(1, pages + 1):

# Avoid requesting the data twice the first time around
if i != 1:
data = _get_page(i)

for pkgacl in data['acls']:
if pkgacl['status'] != 'Approved':
continue

packages.add(pkgacl['packagelist']['package']['name'])

log.debug("done talking with pkgdb2 for now. %r" % packages)
return packages
12 changes: 0 additions & 12 deletions fedmsg.d/badges-global.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,4 @@
},

"fedbadges.datagrepper_url": "https://apps.fedoraproject.org/datagrepper",

# Some configuration for the pkgdb criteria handler
"fedbadges.rules.utils.use_pkgdb2": False,
"fedbadges.rules.utils.pkgdb_url": 'https://admin.fedoraproject.org/pkgdb',
# "fedbadges.rules.utils.pkgdb_url": "http://209.132.184.188/api/",
"fedbadges.rules.cache": {
"backend": "dogpile.cache.dbm",
"expiration_time": 300,
"arguments": {
"filename": "/var/tmp/fedbadges-cache.dbm",
},
},
}
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ PyYAML
datanommer.models
zope.sqlalchemy
requests
dogpile.cache
psutil
moksha
20 changes: 0 additions & 20 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
construct_substitutions,
format_args,
single_argument_lambda_factory,
_get_pkgdb2_packages_for,
)


Expand Down Expand Up @@ -199,22 +198,3 @@ def test_nested_subs(self):
actual = format_args(obj, subs)
eq_(actual, target)


class TestPkgdb2(unittest.TestCase):

@raises(IOError)
def test_wrong_url(self):
config = {
'fedbadges.rules.utils.pkgdb_url':
'https://admin.fedoraproject.org/pkgdb',
}
packages = _get_pkgdb2_packages_for(config, 'ralph')

# This is an integration test that won't run in KOJI
# def test_simple(self):
# config = {
# 'fedbadges.rules.utils.pkgdb_url':
# 'https://admin.fedoraproject.org/pkgdb/api',
# }
# packages = _get_pkgdb2_packages_for(config, 'ralph')
# self.assertIn('pkgwat', packages)

0 comments on commit 6091ce9

Please sign in to comment.