Beispiel #1
0
  def create_sources_field(self, sources, sources_rel_path, key_arg=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """
    if sources is None:
      # Make sure we don't apply the defaulting to uses of this method other than for
      # creating a sources= field (e.g., we also use this for creating resources= fields).
      # Note that the check for supports_default_sources() precedes the subsystem check.
      # This is so that tests don't need to set up the subsystem when creating targets that
      # legitimately do not require sources.
      if (key_arg is None or key_arg == 'sources') and self.supports_default_sources():
        deprecated_conditional(
          lambda: True,
          '1.11.0.dev0',
          'Default sources should always be parsed through the engine not by create_sources_field. '
          'This code should be unreachable, and this message should never be displayed. '
          'If you see this message, please contact pants-dev. '
          'Class which caused this message: {}'.format(self.__class__.__name__)
        )
        sources = self.default_sources(sources_rel_path)
      else:
        sources = FilesetWithSpec.empty(sources_rel_path)
    elif isinstance(sources, (set, list, tuple)):
      if sources:
        # Received a literal sources list: convert to a FilesetWithSpec via Files.
        deprecated_conditional(
          lambda: True,
          '1.11.0.dev0',
          ('Passing collections as the value of the sources argument to create_sources_field is '
           'deprecated, and now takes a slow path. Instead, class {} should have its sources '
           'argument populated by the engine, either by using the standard parsing pipeline, or by '
           'requesting a SourcesField product from the v2 engine.').format(self.__class__.__name__)
        )
        sources = Files.create_fileset_with_spec(sources_rel_path, *sources)
      else:
        sources = FilesetWithSpec.empty(sources_rel_path)
    elif not isinstance(sources, FilesetWithSpec):
      key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
      raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}"
                                            .format(key_arg_section, type(sources)))
    elif not isinstance(sources, EagerFilesetWithSpec):
      deprecated_conditional(
        lambda: True,
        '1.11.0.dev0',
        ('FilesetWithSpec sources values are deprecated except for EagerFilesetWithSpec values. '
         'Saw value of type {}').format(type(sources))
      )


    return SourcesField(sources=sources)
Beispiel #2
0
    def create_sources_field(self, sources, sources_rel_path, key_arg=None):
        """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """
        if sources is None:
            # Make sure we don't apply the defaulting to uses of this method other than for
            # creating a sources= field (e.g., we also use this for creating resources= fields).
            # Note that the check for supports_default_sources() precedes the subsystem check.
            # This is so that tests don't need to set up the subsystem when creating targets that
            # legitimately do not require sources.
            if (key_arg is None or key_arg
                    == 'sources') and self.supports_default_sources():
                deprecated_conditional(
                    lambda: True, '1.11.0.dev0',
                    'Default sources should always be parsed through the engine not by create_sources_field. '
                    'This code should be unreachable, and this message should never be displayed. '
                    'If you see this message, please contact pants-dev. '
                    'Class which caused this message: {}'.format(
                        self.__class__.__name__))
                sources = self.default_sources(sources_rel_path)
            else:
                sources = FilesetWithSpec.empty(sources_rel_path)
        elif isinstance(sources, (set, list, tuple)):
            if sources:
                # Received a literal sources list: convert to a FilesetWithSpec via Files.
                deprecated_conditional(lambda: True, '1.11.0.dev0', (
                    'Passing collections as the value of the sources argument to create_sources_field is '
                    'deprecated, and now takes a slow path. Instead, class {} should have its sources '
                    'argument populated by the engine, either by using the standard parsing pipeline, or by '
                    'requesting a SourcesField product from the v2 engine.'
                ).format(self.__class__.__name__))
                sources = Files.create_fileset_with_spec(
                    sources_rel_path, *sources)
            else:
                sources = FilesetWithSpec.empty(sources_rel_path)
        elif not isinstance(sources, FilesetWithSpec):
            key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
            raise TargetDefinitionException(
                self,
                "Expected {}a glob, an address or a list, but was {}".format(
                    key_arg_section, type(sources)))
        elif not isinstance(sources, EagerFilesetWithSpec):
            deprecated_conditional(lambda: True, '1.11.0.dev0', (
                'FilesetWithSpec sources values are deprecated except for EagerFilesetWithSpec values. '
                'Saw value of type {}').format(type(sources)))

        return SourcesField(sources=sources)
Beispiel #3
0
    def create_sources_field(self, sources, sources_rel_path, key_arg=None):
        """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """
        if not sources:
            sources = FilesetWithSpec.empty(sources_rel_path)
        elif not isinstance(sources, FilesetWithSpec):
            key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
            raise TargetDefinitionException(
                self,
                "Expected {}a glob, an address or a list, but was {}".format(
                    key_arg_section, type(sources)))
        elif not isinstance(sources, EagerFilesetWithSpec):
            deprecated_conditional(lambda: True, '1.12.0.dev0', (
                'FilesetWithSpec sources values are deprecated except for EagerFilesetWithSpec values. '
                'Saw value of type {}').format(type(sources)))

        return SourcesField(sources=sources)
Beispiel #4
0
  def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """

    if isinstance(sources, Addresses):
      # Currently, this is only created by the result of from_target() which takes a single argument
      if len(sources.addresses) != 1:
        raise self.WrongNumberOfAddresses(
          "Expected a single address to from_target() as argument to {spec}"
          .format(spec=address.spec))
      referenced_address = Address.parse(sources.addresses[0], relative_to=sources.rel_path)
      return DeferredSourcesField(ref_address=referenced_address)
    elif sources is None:
      sources = FilesetWithSpec.empty(sources_rel_path)
    elif not isinstance(sources, FilesetWithSpec):
      # Received a literal sources list: convert to a FilesetWithSpec via Files.
      sources = Files.create_fileset_with_spec(sources_rel_path, *sources)

    return SourcesField(sources=sources)
Beispiel #5
0
    def create_sources_field(self, sources, sources_rel_path, key_arg=None):
        """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """
        if sources is None:
            # Make sure we don't apply the defaulting to uses of this method other than for
            # creating a sources= field (e.g., we also use this for creating resources= fields).
            # Note that the check for supports_default_sources() precedes the subsystem check.
            # This is so that tests don't need to set up the subsystem when creating targets that
            # legitimately do not require sources.
            if ((key_arg is None or key_arg == 'sources')
                    and self.supports_default_sources() and self.Arguments.
                    global_instance().get_options().implicit_sources):
                sources = self.default_sources(sources_rel_path)
            else:
                sources = FilesetWithSpec.empty(sources_rel_path)
        elif isinstance(sources, (set, list, tuple)):
            # Received a literal sources list: convert to a FilesetWithSpec via Files.
            sources = Files.create_fileset_with_spec(sources_rel_path,
                                                     *sources)
        elif not isinstance(sources, FilesetWithSpec):
            key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
            raise TargetDefinitionException(
                self,
                "Expected {}a glob, an address or a list, but was {}".format(
                    key_arg_section, type(sources)))

        return SourcesField(sources=sources)
Beispiel #6
0
  def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """

    if sources is None:
      sources = FilesetWithSpec.empty(sources_rel_path)
    elif isinstance(sources, FilesetWithSpec):
      pass
    elif isinstance(sources, (set, list, tuple)):
      # Received a literal sources list: convert to a FilesetWithSpec via Files.
      sources = Files.create_fileset_with_spec(sources_rel_path, *sources)
    else:
      key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
      raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}"
                                            .format(key_arg_section, type(sources)))

    return SourcesField(sources=sources)
Beispiel #7
0
  def create_sources_field(self, sources, sources_rel_path, key_arg=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """
    if sources is None:
      # Make sure we don't apply the defaulting to uses of this method other than for
      # creating a sources= field (e.g., we also use this for creating resources= fields).
      # Note that the check for supports_default_sources() precedes the subsystem check.
      # This is so that tests don't need to set up the subsystem when creating targets that
      # legitimately do not require sources.
      if ((key_arg is None or key_arg == 'sources') and
          self.supports_default_sources() and
          self.Arguments.global_instance().get_options().implicit_sources):
        sources = self.default_sources(sources_rel_path)
      else:
        sources = FilesetWithSpec.empty(sources_rel_path)
    elif isinstance(sources, (set, list, tuple)):
      # Received a literal sources list: convert to a FilesetWithSpec via Files.
      sources = Files.create_fileset_with_spec(sources_rel_path, *sources)
    elif not isinstance(sources, FilesetWithSpec):
      key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
      raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}"
                                            .format(key_arg_section, type(sources)))

    return SourcesField(sources=sources)
Beispiel #8
0
  def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """

    if isinstance(sources, Addresses):
      # Currently, this is only created by the result of from_target() which takes a single argument
      if len(sources.addresses) != 1:
        key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
        spec_section = " to '{}'".format(address.spec) if address else ""
        raise self.WrongNumberOfAddresses(
          "Expected {key_arg_section}a single address to from_target() as argument{spec_section}"
          .format(key_arg_section=key_arg_section, spec_section=spec_section))
      referenced_address = Address.parse(sources.addresses[0], relative_to=sources.rel_path)
      return DeferredSourcesField(ref_address=referenced_address)
    elif sources is None:
      sources = FilesetWithSpec.empty(sources_rel_path)
    elif isinstance(sources, FilesetWithSpec):
      pass
    elif isinstance(sources, (set, list, tuple)):
      # Received a literal sources list: convert to a FilesetWithSpec via Files.
      sources = Files.create_fileset_with_spec(sources_rel_path, *sources)
    else:
      key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
      raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}"
                                            .format(key_arg_section, type(sources)))

    return SourcesField(sources=sources)
Beispiel #9
0
    def create_sources_field(self,
                             sources,
                             sources_rel_path,
                             address=None,
                             key_arg=None):
        """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """

        if isinstance(sources, Addresses):
            # Currently, this is only created by the result of from_target() which takes a single argument
            if len(sources.addresses) != 1:
                raise self.WrongNumberOfAddresses(
                    "Expected a single address to from_target() as argument to {spec}"
                    .format(spec=address.spec))
            referenced_address = Address.parse(sources.addresses[0],
                                               relative_to=sources.rel_path)
            return DeferredSourcesField(ref_address=referenced_address)
        elif sources is None:
            sources = FilesetWithSpec.empty(sources_rel_path)
        elif not isinstance(sources, FilesetWithSpec):
            # Received a literal sources list: convert to a FilesetWithSpec via Files.
            sources = Files.create_fileset_with_spec(sources_rel_path,
                                                     *sources)

        return SourcesField(sources=sources)
Beispiel #10
0
  def create_sources_field(self, sources, sources_rel_path, address=None, key_arg=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """

    if isinstance(sources, Addresses):
      # Currently, this is only created by the result of from_target() which takes a single argument
      if len(sources.addresses) != 1:
        key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
        spec_section = " to '{}'".format(address.spec) if address else ""
        raise self.WrongNumberOfAddresses(
          "Expected {key_arg_section}a single address to from_target() as argument{spec_section}"
          .format(key_arg_section=key_arg_section, spec_section=spec_section))
      referenced_address = Address.parse(sources.addresses[0], relative_to=sources.rel_path)
      return DeferredSourcesField(ref_address=referenced_address)
    elif sources is None:
      sources = FilesetWithSpec.empty(sources_rel_path)
    elif isinstance(sources, FilesetWithSpec):
      pass
    elif isinstance(sources, (set, list, tuple)):
      # Received a literal sources list: convert to a FilesetWithSpec via Files.
      sources = Files.create_fileset_with_spec(sources_rel_path, *sources)
    else:
      key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
      raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}"
                                            .format(key_arg_section, type(sources)))

    return SourcesField(sources=sources)
    def test_passes_fileset_with_spec_through(self):
        self.create_file('foo/a.txt', 'a_contents')

        fileset = FilesetWithSpec('foo', 'a.txt', lambda: ['a.txt'])
        sf = SourcesField(sources=fileset)

        self.assertIs(fileset, sf.sources)
        self.assertEqual(['a.txt'], list(sf.source_paths))
Beispiel #12
0
  def create_sources_field(self, sources, sources_rel_path, key_arg=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!

    :API: public

    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """
    if not sources:
      sources = FilesetWithSpec.empty(sources_rel_path)
    elif not isinstance(sources, FilesetWithSpec):
      key_arg_section = "'{}' to be ".format(key_arg) if key_arg else ""
      raise TargetDefinitionException(self, "Expected {}a glob, an address or a list, but was {}"
                                            .format(key_arg_section, type(sources)))

    return SourcesField(sources=sources)
Beispiel #13
0
 def _sources_field(self):
     sources_field = self.payload.get_field('sources')
     if sources_field is not None:
         return sources_field
     return SourcesField(
         sources=FilesetWithSpec.empty(self.address.spec_path))
Beispiel #14
0
 def _sources_field(self):
   sources_field = self.payload.get_field('sources')
   if sources_field is not None:
     return sources_field
   return SourcesField(sources=FilesetWithSpec.empty(self.address.spec_path))