Skip to content

Commit

Permalink
Convert JarRule and JarRules to use Payload to help fingerprint its c…
Browse files Browse the repository at this point in the history
…onfiguration

Testing Done:
Unit tests already cover the fingerprinting of these types, CI run at https://travis-ci.org/pantsbuild/pants/builds/59110202

Bugs closed: 1434

Reviewed at https://rbcommons.com/s/twitter/r/2096/
  • Loading branch information
ericzundel committed Apr 19, 2015
1 parent b0ee395 commit b225ea4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
45 changes: 27 additions & 18 deletions src/python/pants/backend/jvm/targets/jvm_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@


class JarRule(FingerprintedMixin, AbstractClass):
def __init__(self, apply_pattern):
def __init__(self, apply_pattern, payload=None):
self.payload = payload or Payload()
if not isinstance(apply_pattern, string_types):
raise ValueError('The supplied apply_pattern is not a string, given: {}'
.format(apply_pattern))
Expand All @@ -31,23 +32,27 @@ def __init__(self, apply_pattern):
raise ValueError('The supplied apply_pattern: {pattern} '
'is not a valid regular expression: {msg}'
.format(pattern=apply_pattern, msg=e))
self.payload.add_fields({
'apply_pattern' : PrimitiveField(apply_pattern),
})

def fingerprint(self):
hasher = sha1()
hasher.update(self._apply_pattern.pattern)
return hasher.hexdigest()
return self.payload.fingerprint()

@property
def apply_pattern(self):
"""The pattern that matches jar entry paths this rule applies to."""
"""The pattern that matches jar entry paths this rule applies to.
:rtype: re.RegexObject
"""
return self._apply_pattern


class Skip(JarRule):
"""A rule that skips adding matched entries to a jar."""

def __repr__(self):
return "Skip(apply_pattern={})".format(self._apply_pattern.pattern)
return "Skip(apply_pattern={})".format(self.payload.apply_pattern)


class Duplicate(JarRule):
Expand Down Expand Up @@ -105,24 +110,24 @@ def __init__(self, apply_pattern, action):
:param action: An action to take to handle one or more duplicate entries. Must be one of:
``Duplicate.SKIP``, ``Duplicate.REPLACE``, ``Duplicate.CONCAT`` or ``Duplicate.FAIL``.
"""
super(Duplicate, self).__init__(apply_pattern)
payload = Payload()
payload.add_fields({
'action' : PrimitiveField(self.validate_action(action)),
})
super(Duplicate, self).__init__(apply_pattern, payload=payload)

self._action = self.validate_action(action)

@property
def action(self):
"""The action to take for any duplicate entries that match this rule's ``apply_pattern``."""
return self._action
return self.payload.action

def fingerprint(self):
hasher = sha1()
hasher.update(super(Duplicate, self).fingerprint())
hasher.update(self._action)
return hasher.hexdigest()
return self.payload.fingerprint()

def __repr__(self):
return "Duplicate(apply_pattern={0}, action={1})".format(self._apply_pattern.pattern,
self._action)
return "Duplicate(apply_pattern={0}, action={1})".format(self.payload.apply_pattern,
self.payload.action)


class JarRules(FingerprintedMixin):
Expand Down Expand Up @@ -152,6 +157,7 @@ def skip_signatures_and_duplicates_concat_well_known_metadata(cls, default_dup_a
has the following special handling:
- jar signature metadata is dropped
- ``java.util.ServiceLoader`` provider-configuration files are concatenated in the order
- ``java.util.ServiceLoader`` provider-configuration files are concatenated in the order
encountered
Expand Down Expand Up @@ -198,13 +204,16 @@ def __init__(self, rules=None, default_dup_action=Duplicate.SKIP):
:param default_dup_action: The default action to take when a duplicate entry is encountered and
no explicit rules apply to the entry.
"""
self._default_dup_action = Duplicate.validate_action(default_dup_action)
self.payload = Payload()
self.payload.add_fields({
'default_dup_action' : PrimitiveField(Duplicate.validate_action(default_dup_action))
})
self._rules = assert_list(rules, expected_type=JarRule)

@property
def default_dup_action(self):
"""The default action to take when a duplicate jar entry is encountered."""
return self._default_dup_action
return self.payload.default_dup_action

@property
def rules(self):
Expand All @@ -213,7 +222,7 @@ def rules(self):

def fingerprint(self):
hasher = sha1()
hasher.update(self.default_dup_action)
hasher.update(self.payload.fingerprint())
for rule in self.rules:
hasher.update(rule.fingerprint())
return hasher.hexdigest()
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/base/payload_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def fingerprint(self):
"""Override this method to implement a fingerprint for your class.
:returns: a sha1 hexdigest hashing the contents of this structure."""
raise NotImplementedError
raise NotImplementedError()


class FingerprintedField(PayloadField):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def test_invalid_apply_pattern(self):
with self.assertRaisesRegexp(ValueError, r'The supplied apply_pattern is not a string'):
Skip(None)
with self.assertRaisesRegexp(ValueError, r'The supplied apply_pattern is not a string'):
Duplicate(None, None)
Duplicate(None, Duplicate.SKIP)
with self.assertRaisesRegexp(ValueError, r'The supplied apply_pattern: \) is not a valid'):
Skip(r')')
with self.assertRaisesRegexp(ValueError, r'The supplied apply_pattern: \) is not a valid'):
Duplicate(r')', None)
Duplicate(r')', Duplicate.SKIP)

def test_bad_action(self):
with self.assertRaisesRegexp(ValueError, r'The supplied action must be one of'):
Expand Down

0 comments on commit b225ea4

Please sign in to comment.