<commit_before>"""
Unit tests for format checking
"""

from __future__ import print_function

from nose.plugins.skip import SkipTest

import os
import subprocess

import cleverhans
from cleverhans.devtools.tests.docscrape import docstring_errors
from cleverhans.devtools.list_files import list_files
from cleverhans.utils import shell_call

# Enter a manual list of files that are allowed to violate PEP8 here
whitelist_pep8 = [
    # This file is broken but could be fixed
    "../examples/multigpu_advtrain/test_attack_multigpu.py"
]

# We don't want to test RL-attack because it has so many dependencies
# not used elsewhere, and pylint wants to import them all
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py") if "RL-attack" in path])
# Similarly, we don't want to require robust_vision_benchmark installed
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "robust_vision_benchmark" in path])
# Similarly, we don't want to require that cloud be installed
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "cloud_client" in path])
# This example has more dependencies too
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "facenet_adversarial_faces" in path])
# This too
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "madry_lab_challenges" in path])



whitelist_docstrings = [
]


def test_format_pep8():
  """
  Test if pep8 is respected.
  """
  files_to_check = []
  module_dir = cleverhans.__path__[0]
  for path in list_files(".py"):
    rel_path = os.path.relpath(path, module_dir)
    if rel_path in whitelist_pep8:
      continue
    else:
      files_to_check.append(path)
  repo_dir = os.path.join(module_dir, os.pardir)
  rcpath = os.path.join(repo_dir, '.pylintrc')
  assert os.path.exists(rcpath)
  bad_out = ""
  for f in files_to_check:
    # We must run pylint via the command line and subprocess because of
    # problems with the pylint module.
    # The documentation claims you can run it as a python module, but
    # the documentation is wrong: https://github.com/PyCQA/pylint/issues/1870
    # If you run the version described in the linked issue, pylint
    # calls sys.exit once it is done, so it kills the test.
    try:
      out = shell_call(['pylint', '--rcfile', rcpath, f])
    except subprocess.CalledProcessError as e:
      try:
        bad_out = bad_out + "\n" + e.output
      except TypeError:
        endline_type_str = str(type("\n"))
        raise TypeError(("Could not add: bad_out (type %s), "
                         "endline char (type %s), "
                         " and e.output (type %s)") %
                        (str(type(bad_out)), endline_type_str,
                         str(type(e.output))))
  if len(bad_out) > 0:
    raise ValueError(bad_out)
<commit_msg>attempt at python3 support<commit_after>"""
Unit tests for format checking
"""

from __future__ import print_function

from nose.plugins.skip import SkipTest

import os
import subprocess

import cleverhans
from cleverhans.devtools.tests.docscrape import docstring_errors
from cleverhans.devtools.list_files import list_files
from cleverhans.utils import shell_call

# Enter a manual list of files that are allowed to violate PEP8 here
whitelist_pep8 = [
    # This file is broken but could be fixed
    "../examples/multigpu_advtrain/test_attack_multigpu.py"
]

# We don't want to test RL-attack because it has so many dependencies
# not used elsewhere, and pylint wants to import them all
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py") if "RL-attack" in path])
# Similarly, we don't want to require robust_vision_benchmark installed
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "robust_vision_benchmark" in path])
# Similarly, we don't want to require that cloud be installed
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "cloud_client" in path])
# This example has more dependencies too
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "facenet_adversarial_faces" in path])
# This too
whitelist_pep8.extend([os.path.relpath(path, cleverhans.__path__[0])
                       for path in list_files(".py")
                       if "madry_lab_challenges" in path])



whitelist_docstrings = [
]


def test_format_pep8():
  """
  Test if pep8 is respected.
  """
  files_to_check = []
  module_dir = cleverhans.__path__[0]
  for path in list_files(".py"):
    rel_path = os.path.relpath(path, module_dir)
    if rel_path in whitelist_pep8:
      continue
    else:
      files_to_check.append(path)
  repo_dir = os.path.join(module_dir, os.pardir)
  rcpath = os.path.join(repo_dir, '.pylintrc')
  assert os.path.exists(rcpath)
  bad_out = ""
  for f in files_to_check:
    # We must run pylint via the command line and subprocess because of
    # problems with the pylint module.
    # The documentation claims you can run it as a python module, but
    # the documentation is wrong: https://github.com/PyCQA/pylint/issues/1870
    # If you run the version described in the linked issue, pylint
    # calls sys.exit once it is done, so it kills the test.
    try:
      out = shell_call(['pylint', '--rcfile', rcpath, f])
    except subprocess.CalledProcessError as e:
      bad_out = bad_out + "\n" + e.output.decode("utf-8")
  if len(bad_out) > 0:
    raise ValueError(bad_out)

{'fraction_non_alphanumeric': 0.09337299619897538, 'fraction_numerical': 0.0072715253677078165, 'mean_word_length': 2.8944658944658945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4878744', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1882, 'n_words': 558}