def test_fingerprint(self): payload = Payload() payload.add_field('foo', PrimitiveField()) fingerprint1 = payload.fingerprint() self.assertEqual(fingerprint1, payload.fingerprint()) payload.add_field('bar', PrimitiveField()) fingerprint2 = payload.fingerprint() self.assertNotEqual(fingerprint1, fingerprint2) self.assertEqual(fingerprint2, payload.fingerprint()) payload.freeze() self.assertEqual(fingerprint2, payload.fingerprint())
class ManifestEntries(FingerprintedMixin): """Describes additional items to add to the app manifest.""" class ExpectedDictionaryError(Exception): pass def __init__(self, entries=None): """ :param entries: Additional headers, value pairs to add to the MANIFEST.MF. You can just add fixed string header / value pairs. :type entries: dictionary of string : string """ self.payload = Payload() if entries: if not isinstance(entries, dict): raise self.ExpectedDictionaryError("entries must be a dictionary of strings.") for key in entries.keys(): if not isinstance(key, string_types): raise self.ExpectedDictionaryError( "entries must be dictionary of strings, got key {} type {}" .format(key, type(key).__name__)) self.payload.add_fields({ 'entries' : PrimitiveField(entries or {}), }) def fingerprint(self): return self.payload.fingerprint() @property def entries(self): return self.payload.entries
def test_partial_fingerprint(self): payload = Payload() payload.add_field('foo', PrimitiveField()) fingerprint1 = payload.fingerprint() self.assertEqual(fingerprint1, payload.fingerprint(field_keys=('foo',))) payload.add_field('bar', PrimitiveField()) fingerprint2 = payload.fingerprint() self.assertEqual(fingerprint1, payload.fingerprint(field_keys=('foo',))) self.assertNotEqual(fingerprint2, payload.fingerprint(field_keys=('foo',))) self.assertNotEqual(fingerprint2, payload.fingerprint(field_keys=('bar',))) self.assertEqual(fingerprint2, payload.fingerprint(field_keys=('bar', 'foo')))
def test_partial_fingerprint(self): payload = Payload() payload.add_field("foo", PrimitiveField()) fingerprint1 = payload.fingerprint() self.assertEqual(fingerprint1, payload.fingerprint(field_keys=("foo",))) payload.add_field("bar", PrimitiveField()) fingerprint2 = payload.fingerprint() self.assertEqual(fingerprint1, payload.fingerprint(field_keys=("foo",))) self.assertNotEqual(fingerprint2, payload.fingerprint(field_keys=("foo",))) self.assertNotEqual(fingerprint2, payload.fingerprint(field_keys=("bar",))) self.assertEqual(fingerprint2, payload.fingerprint(field_keys=("bar", "foo")))
def test_none(self): payload = Payload() payload.add_field('foo', None) payload2 = Payload() payload2.add_field('foo', PrimitiveField(None)) self.assertNotEqual(payload.fingerprint(), payload2.fingerprint())
class JarRules(FingerprintedMixin): """A set of rules for packaging up a deploy jar. Deploy jars are executable jars with fully self-contained classpaths and as such, assembling them presents problems given jar semantics. One issue is signed jars that must be included on the classpath. These have a signature that depends on the jar contents and assembly of the deploy jar changes the content of the jar, breaking the signatures. For cases like these the signed jars must be verified and then the signature information thrown away. The `Skip <#Skip>`_ rule supports this sort of issue by allowing outright entry exclusion in the final deploy jar. Another issue is duplicate jar entries. Although the underlying zip format supports these, the java jar tool and libraries do not. As such some action must be taken for each duplicate entry such that there are no duplicates in the final deploy jar. The four `Duplicate <#Duplicate>`_ rules support resolution of these cases by allowing 1st wins, last wins, concatenation of the duplicate entry contents or raising an exception. """ @classmethod def skip_signatures_and_duplicates_concat_well_known_metadata(cls, default_dup_action=None, additional_rules=None): """Produces a rule set useful in many deploy jar creation contexts. The rule set skips duplicate entries by default, retaining the 1st encountered. In addition it has the following special handling: - jar signature metadata is dropped - ``java.util.ServiceLoader`` provider-configuration files are concatenated in the order encountered :param default_dup_action: An optional default action to take for duplicates. Defaults to `Duplicate.SKIP` if not specified. :param additional_rules: Optionally one or more jar rules to add to those described above. :returns: JarRules """ default_dup_action = Duplicate.validate_action(default_dup_action or Duplicate.SKIP) additional_rules = assert_list(additional_rules, expected_type=(Duplicate, Skip)) rules = [Skip(r'^META-INF/[^/]+\.SF$'), # signature file Skip(r'^META-INF/[^/]+\.DSA$'), # default signature alg. file Skip(r'^META-INF/[^/]+\.RSA$'), # default signature alg. file Duplicate(r'^META-INF/services/', Duplicate.CONCAT)] # 1 svc fqcn per line return JarRules(rules=rules + additional_rules, default_dup_action=default_dup_action) _DEFAULT = None @classmethod def default(cls): """Returns the default set of jar rules. Can be set with `set_default` but otherwise defaults to `skip_signatures_and_duplicates_concat_well_known_metadata`. """ if cls._DEFAULT is None: cls._DEFAULT = cls.skip_signatures_and_duplicates_concat_well_known_metadata() return cls._DEFAULT @classmethod def set_default(cls, rules): """Sets the default site-wide jar rules.""" if not isinstance(rules, JarRules): raise ValueError('The default rules must be a JarRules instance.') cls._DEFAULT = rules def __init__(self, rules=None, default_dup_action=Duplicate.SKIP): """Creates a new set of jar rules with the default duplicate action of ``Duplicate.SKIP``. :param rules: One or more rules that will be applied in order to jar entries being packaged in a deploy jar. `Skip <#Skip>`_ rules can go here. :param default_dup_action: The default action to take when a duplicate entry is encountered and no explicit rules apply to the entry. """ 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.payload.default_dup_action @property def rules(self): """A copy of the list of explicit entry rules in effect.""" return list(self._rules) def fingerprint(self): hasher = sha1() hasher.update(self.payload.fingerprint()) for rule in self.rules: hasher.update(rule.fingerprint()) return hasher.hexdigest() @property def value(self): return self._jar_rules
class JarRules(FingerprintedMixin): """A set of rules for packaging up a deploy jar. Deploy jars are executable jars with fully self-contained classpaths and as such, assembling them presents problems given jar semantics. One issue is signed jars that must be included on the classpath. These have a signature that depends on the jar contents and assembly of the deploy jar changes the content of the jar, breaking the signatures. For cases like these the signed jars must be verified and then the signature information thrown away. The `Skip <#Skip>`_ rule supports this sort of issue by allowing outright entry exclusion in the final deploy jar. Another issue is duplicate jar entries. Although the underlying zip format supports these, the java jar tool and libraries do not. As such some action must be taken for each duplicate entry such that there are no duplicates in the final deploy jar. The four `Duplicate <#Duplicate>`_ rules support resolution of these cases by allowing 1st wins, last wins, concatenation of the duplicate entry contents or raising an exception. """ @classmethod def skip_signatures_and_duplicates_concat_well_known_metadata( cls, default_dup_action=None, additional_rules=None): """Produces a rule set useful in many deploy jar creation contexts. The rule set skips duplicate entries by default, retaining the 1st encountered. In addition it has the following special handling: - jar signature metadata is dropped - jar indexing files INDEX.LIST are dropped - ``java.util.ServiceLoader`` provider-configuration files are concatenated in the order encountered :param default_dup_action: An optional default action to take for duplicates. Defaults to `Duplicate.SKIP` if not specified. :param additional_rules: Optionally one or more jar rules to add to those described above. :returns: JarRules """ default_dup_action = Duplicate.validate_action(default_dup_action or Duplicate.SKIP) additional_rules = assert_list(additional_rules, expected_type=(Duplicate, Skip)) rules = [ Skip(r'^META-INF/[^/]+\.SF$'), # signature file Skip(r'^META-INF/[^/]+\.DSA$'), # default signature alg. file Skip(r'^META-INF/[^/]+\.RSA$'), # default signature alg. file Skip(r'^META-INF/INDEX.LIST$' ), # interferes with Class-Path: see man jar for i option Duplicate(r'^META-INF/services/', Duplicate.CONCAT) ] # 1 svc fqcn per line return JarRules(rules=rules + additional_rules, default_dup_action=default_dup_action) _DEFAULT = None @classmethod def default(cls): """Returns the default set of jar rules. Can be set with `set_default` but otherwise defaults to `skip_signatures_and_duplicates_concat_well_known_metadata`. """ if cls._DEFAULT is None: cls._DEFAULT = cls.skip_signatures_and_duplicates_concat_well_known_metadata( ) return cls._DEFAULT @classmethod def set_default(cls, rules): """Sets the default site-wide jar rules.""" if not isinstance(rules, JarRules): raise ValueError('The default rules must be a JarRules instance.') cls._DEFAULT = rules def __init__(self, rules=None, default_dup_action=Duplicate.SKIP): """Creates a new set of jar rules with the default duplicate action of ``Duplicate.SKIP``. :param rules: One or more rules that will be applied in order to jar entries being packaged in a deploy jar. `Skip <#Skip>`_ rules can go here. :param default_dup_action: The default action to take when a duplicate entry is encountered and no explicit rules apply to the entry. """ 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, key_arg="rules") @property def default_dup_action(self): """The default action to take when a duplicate jar entry is encountered.""" return self.payload.default_dup_action @property def rules(self): """A copy of the list of explicit entry rules in effect.""" return list(self._rules) def fingerprint(self): hasher = sha1() hasher.update(self.payload.fingerprint()) for rule in self.rules: hasher.update(rule.fingerprint()) return hasher.hexdigest() @property def value(self): return self._jar_rules