# coding=utf-8
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import, division, print_function, unicode_literals

import json
from hashlib import sha1

from future.utils import PY3

from pants.base.payload_field import PayloadField


class PythonArtifact(PayloadField):
  """Represents a Python setup.py-based project."""
  class MissingArgument(Exception): pass

  class UnsupportedArgument(Exception): pass

  UNSUPPORTED_ARGS = frozenset({
    'data_files',
    'package_dir',
    'package_data',
    'packages',
  })

  def __init__(self, **kwargs):
    """
    :param kwargs: Passed to `setuptools.setup
       <https://pythonhosted.org/setuptools/setuptools.html>`_."""
    self._kw = kwargs
    self._binaries = {}

    def has(name):
      value = self._kw.get(name)
      if value is None:
        raise self.MissingArgument('PythonArtifact requires {} to be specified!'.format(name))
      return value

    def misses(name):
      if name in self._kw:
        raise self.UnsupportedArgument('PythonArtifact prohibits {} from being specified'.format(name))

    self._version = has('version')
    self._name = has('name')
    for arg in self.UNSUPPORTED_ARGS:
      misses(arg)

  @property
  def name(self):
    return self._name

  @property
  def version(self):
    return self._version

  @property
  def key(self):
    return '{}=={}'.format(self._name, self._version)

  @property
  def setup_py_keywords(self):
    return self._kw

  @property
  def binaries(self):
    return self._binaries

  def __str__(self):
    return self.name

  def _compute_fingerprint(self):
    fingerprint = sha1(json.dumps((self._kw, self._binaries),
                                  ensure_ascii=True,
                                  allow_nan=False,
                                  sort_keys=True)).hexdigest()
    return fingerprint if PY3 else fingerprint.decode('utf-8')

  def with_binaries(self, *args, **kw):
    """Add binaries tagged to this artifact.

    For example: ::

      provides = setup_py(
        name = 'my_library',
        zip_safe = True
      ).with_binaries(
        my_command = ':my_library_bin'
      )

    This adds a console_script entry_point for the python_binary target
    pointed at by :my_library_bin.  Currently only supports
    python_binaries that specify entry_point explicitly instead of source.

    Also can take a dictionary, e.g.
    with_binaries({'my-command': ':my_library_bin'})
    """
    for arg in args:
      if isinstance(arg, dict):
        self._binaries.update(arg)
    self._binaries.update(kw)
    return self

@@ -228,75 +228,58 @@
 ls%0A%0A
-import json%0Afrom hashlib import sha1%0A%0Afrom future.utils import PY3%0A
+from pants.base.hash_utils import stable_json_hash
 %0Afro
@@ -1688,294 +1688,58 @@
     
-fingerprint = sha1(json.dumps((self._kw, self._binaries),%0A                                  ensure_ascii=True,%0A                                  allow_nan=False,%0A                                  sort_keys=True)).hexdigest()%0A    return fingerprint if PY3 else fingerprint.decode('utf-8'
+return stable_json_hash((self._kw, self._binaries)
 )%0A%0A 

Fix python_artifact fingerprint unicode issue for Python 3 (#6971)