def test_replace(self):
   set1 = PinnedJarArtifactSet(pinned_coordinates=[
     M2Coordinate('org', 'foo', '1.2'),
     M2Coordinate('org', 'bar', '7.8'),
   ])
   set1.put(M2Coordinate('org', 'hello', '9'))
   self.assertEquals(3, len(set1))
   set1.put(M2Coordinate('org', 'foo', '1.3'))
   self.assertEquals(3, len(set1))
   self.assertEquals(M2Coordinate('org', 'foo', '1.3'), set1[M2Coordinate('org', 'foo')])
Example #2
0
  def _generate_resolve_ivy(cls, jars, excludes, ivyxml, confs, resolve_hash_name, pinned_artifacts=None,
                    jar_dep_manager=None):
    org = IvyUtils.INTERNAL_ORG_NAME
    name = resolve_hash_name

    extra_configurations = [conf for conf in confs if conf and conf != 'default']

    jars_by_key = OrderedDict()
    for jar in jars:
      jars = jars_by_key.setdefault((jar.org, jar.name), [])
      jars.append(jar)

    manager = jar_dep_manager or JarDependencyManagement.global_instance()
    artifact_set = PinnedJarArtifactSet(pinned_artifacts) # Copy, because we're modifying it.
    for jars in jars_by_key.values():
      for i, dep in enumerate(jars):
        direct_coord = M2Coordinate.create(dep)
        managed_coord = artifact_set[direct_coord]
        if direct_coord.rev != managed_coord.rev:
          # It may be necessary to actually change the version number of the jar we want to resolve
          # here, because overrides do not apply directly (they are exclusively transitive). This is
          # actually a good thing, because it gives us more control over what happens.
          coord = manager.resolve_version_conflict(managed_coord, direct_coord, force=dep.force)
          jars[i] = dep.copy(rev=coord.rev)
        elif dep.force:
          # If this dependency is marked as 'force' and there is no version conflict, use the normal
          # pants behavior for 'force'.
          artifact_set.put(direct_coord)

    dependencies = [cls._generate_jar_template(jars) for jars in jars_by_key.values()]

    # As it turns out force is not transitive - it only works for dependencies pants knows about
    # directly (declared in BUILD files - present in generated ivy.xml). The user-level ivy docs
    # don't make this clear [1], but the source code docs do (see isForce docs) [2]. I was able to
    # edit the generated ivy.xml and use the override feature [3] though and that does work
    # transitively as you'd hope.
    #
    # [1] http://ant.apache.org/ivy/history/2.3.0/settings/conflict-managers.html
    # [2] https://svn.apache.org/repos/asf/ant/ivy/core/branches/2.3.0/
    #     src/java/org/apache/ivy/core/module/descriptor/DependencyDescriptor.java
    # [3] http://ant.apache.org/ivy/history/2.3.0/ivyfile/override.html
    overrides = [cls._generate_override_template(_coord) for _coord in artifact_set]

    excludes = [cls._generate_exclude_template(exclude) for exclude in excludes]

    template_data = TemplateData(
      org=org,
      module=name,
      extra_configurations=extra_configurations,
      dependencies=dependencies,
      excludes=excludes,
      overrides=overrides)

    template_relpath = os.path.join('templates', 'ivy_utils', 'ivy.mustache')
    cls._write_ivy_xml_file(ivyxml, template_data, template_relpath)
 def test_id(self):
   set1 = PinnedJarArtifactSet(pinned_coordinates=[
     M2Coordinate('org', 'foo', '1.2'),
     M2Coordinate('org', 'bar', '7.8'),
     M2Coordinate('org', 'foo', '1.8', ext='tar')
   ])
   self.assertFalse(set1.id is None)
   self.assertFalse(set1._id is None)
   set1.put(M2Coordinate('hello', 'there', '9'))
   self.assertTrue(set1._id is None)
   self.assertFalse(set1.id is None)
   set1.put(M2Coordinate('org', 'foo', '1.2'))
   self.assertFalse(set1._id is None) # Should be no change, because version was already there.
 def test_put_failure(self):
   set1 = PinnedJarArtifactSet()
   with self.assertRaises(PinnedJarArtifactSet.MissingVersion):
     set1.put(M2Coordinate('hello', 'there'))
Example #5
0
    def generate_ivy(cls,
                     targets,
                     jars,
                     excludes,
                     ivyxml,
                     confs,
                     resolve_hash_name=None,
                     pinned_artifacts=None):
        if resolve_hash_name:
            org = IvyUtils.INTERNAL_ORG_NAME
            name = resolve_hash_name
        else:
            org, name = cls.identify(targets)

        extra_configurations = [
            conf for conf in confs if conf and conf != 'default'
        ]

        jars_by_key = OrderedDict()
        for jar in jars:
            jars = jars_by_key.setdefault((jar.org, jar.name), [])
            jars.append(jar)

        manager = JarDependencyManagement.global_instance()
        artifact_set = PinnedJarArtifactSet(
            pinned_artifacts)  # Copy, because we're modifying it.
        for jars in jars_by_key.values():
            for i, dep in enumerate(jars):
                direct_coord = M2Coordinate.create(dep)
                managed_coord = artifact_set[direct_coord]
                if direct_coord.rev != managed_coord.rev:
                    # It may be necessary to actually change the version number of the jar we want to resolve
                    # here, because overrides do not apply directly (they are exclusively transitive). This is
                    # actually a good thing, because it gives us more control over what happens.
                    coord = manager.resolve_version_conflict(managed_coord,
                                                             direct_coord,
                                                             force=dep.force)
                    dep = copy.copy(dep)
                    dep.rev = coord.rev
                    jars[i] = dep
                elif dep.force:
                    # If this dependency is marked as 'force' and there is no version conflict, use the normal
                    # pants behavior for 'force'.
                    artifact_set.put(direct_coord)

        dependencies = [
            cls._generate_jar_template(jars) for jars in jars_by_key.values()
        ]

        # As it turns out force is not transitive - it only works for dependencies pants knows about
        # directly (declared in BUILD files - present in generated ivy.xml). The user-level ivy docs
        # don't make this clear [1], but the source code docs do (see isForce docs) [2]. I was able to
        # edit the generated ivy.xml and use the override feature [3] though and that does work
        # transitively as you'd hope.
        #
        # [1] http://ant.apache.org/ivy/history/2.3.0/settings/conflict-managers.html
        # [2] https://svn.apache.org/repos/asf/ant/ivy/core/branches/2.3.0/
        #     src/java/org/apache/ivy/core/module/descriptor/DependencyDescriptor.java
        # [3] http://ant.apache.org/ivy/history/2.3.0/ivyfile/override.html
        overrides = [
            cls._generate_override_template(coord) for coord in artifact_set
        ]

        excludes = [
            cls._generate_exclude_template(exclude) for exclude in excludes
        ]

        template_data = TemplateData(org=org,
                                     module=name,
                                     extra_configurations=extra_configurations,
                                     dependencies=dependencies,
                                     excludes=excludes,
                                     overrides=overrides)

        template_relpath = os.path.join('templates', 'ivy_utils',
                                        'ivy.mustache')
        template_text = pkgutil.get_data(__name__, template_relpath)
        generator = Generator(template_text, lib=template_data)
        with safe_open(ivyxml, 'w') as output:
            generator.write(output)