Skip to content

Commit

Permalink
Modernize all headers to Python 3 style (pantsbuild#7926)
Browse files Browse the repository at this point in the history
Now that we require Python 3, we can simplify our header to remove `from __future__` and `# encoding=utf-8` entries, which both no-op in Python 3.

We use a [simple script](https://github.com/foursquare/fsqio/blob/4c8d43ad4140e42430f0b93dc9902e7851c24b7f/scripts/fsqio/python3-port-utils/pants/update_headers.py) to do this, along with updating `check_header.py` to only support Python 3 style headers.
  • Loading branch information
Eric-Arellano authored Jun 22, 2019
1 parent 9bed8ae commit ea74e99
Show file tree
Hide file tree
Showing 1,187 changed files with 23 additions and 3,604 deletions.
49 changes: 12 additions & 37 deletions build-support/bin/check_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@
from common import die


EXPECTED_HEADER_PY2 = dedent("""\
# coding=utf-8
# Copyright YYYY 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
""")

EXPECTED_HEADER_PY3 = dedent("""\
EXPECTED_HEADER = dedent("""\
# Copyright YYYY Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
""")

EXPECTED_NUM_PY2_LINES = 6
EXPECTED_NUM_PY3_LINES = 3
EXPECTED_NUM_LINES = 3

_current_year = str(datetime.datetime.now().year)
_current_century_regex = re.compile(r'20\d\d')
Expand All @@ -47,10 +37,7 @@ def main() -> None:
failures = '\n '.join(str(failure) for failure in header_parse_failures)
die(f"""\
ERROR: All .py files other than __init__.py should start with the header:
{EXPECTED_HEADER_PY3}
If they must support Python 2 still, they should start with the header:
{EXPECTED_HEADER_PY2}
{EXPECTED_HEADER}
---
Expand All @@ -75,27 +62,17 @@ def check_header(filename: str, *, is_newly_created: bool = False) -> None:
"""Raises `HeaderCheckFailure` if the header doesn't match."""
lines = get_header_lines(filename)
check_header_present(filename, lines)
is_py3_file = all("from __future__" not in line for line in lines)
if is_py3_file:
lines = lines[:EXPECTED_NUM_PY3_LINES]
copyright_line_index = 0
expected_header = EXPECTED_HEADER_PY3
else:
copyright_line_index = 1
expected_header = EXPECTED_HEADER_PY2
check_copyright_year(
filename, copyright_line=lines[copyright_line_index], is_newly_created=is_newly_created
)
check_matches_header(
filename, lines, expected_header=expected_header, copyright_line_index=copyright_line_index
filename, copyright_line=lines[0], is_newly_created=is_newly_created
)
check_matches_header(filename, lines)


def get_header_lines(filename: str) -> List[str]:
try:
with open(filename, 'r') as f:
# We grab an extra line in case there is a shebang.
lines = [f.readline() for _ in range(0, EXPECTED_NUM_PY2_LINES + 1)]
lines = [f.readline() for _ in range(0, EXPECTED_NUM_LINES + 1)]
except IOError as e:
raise HeaderCheckFailure(f"{filename}: error while reading input ({e})")
# If a shebang line is included, remove it. Otherwise, we will have conservatively grabbed
Expand All @@ -106,7 +83,7 @@ def get_header_lines(filename: str) -> List[str]:

def check_header_present(filename: str, lines: List[str]) -> None:
num_nonempty_lines = len([line for line in lines if line])
if num_nonempty_lines < EXPECTED_NUM_PY3_LINES:
if num_nonempty_lines < EXPECTED_NUM_LINES:
raise HeaderCheckFailure(f"{filename}: missing the expected header")


Expand All @@ -123,21 +100,19 @@ def check_copyright_year(filename: str, *, copyright_line: str, is_newly_created
)


def check_matches_header(
filename: str, lines: List[str], *, expected_header: str, copyright_line_index: int
) -> None:
copyright_line = lines[copyright_line_index]
def check_matches_header(filename: str, lines: List[str]) -> None:
copyright_line = lines[0]
sanitized_lines = lines.copy()
sanitized_lines[copyright_line_index] = "# Copyright YYYY" + copyright_line[16:]
if "".join(sanitized_lines) != expected_header:
sanitized_lines[0] = "# Copyright YYYY" + copyright_line[16:]
if "".join(sanitized_lines) != EXPECTED_HEADER:
raise HeaderCheckFailure(f"{filename}: header does not match the expected header")


def check_dir(directory: str, newly_created_files: Iterable[str]) -> List[HeaderCheckFailure]:
header_parse_failures: List[HeaderCheckFailure] = []
for root, dirs, files in os.walk(directory):
for f in files:
if not f.endswith('.py') or os.path.basename(f) == '__init__.py':
if not f.endswith('.py') or os.path.basename(f) == '__init__.py' or root.endswith('contrib/python/checks/checker'):
continue
filename = os.path.join(root, f)
try:
Expand Down
3 changes: 0 additions & 3 deletions build-support/travis/generate_travis_yml.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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 pkg_resources
import pystache

Expand Down
3 changes: 0 additions & 3 deletions build-support/travis/generate_travis_yml_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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

from generate_travis_yml import generate_travis_yml


Expand Down
3 changes: 0 additions & 3 deletions contrib/avro/src/python/pants/contrib/avro/register.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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

from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.goal.task_registrar import TaskRegistrar as task

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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

from pants.backend.jvm.targets.jvm_target import JvmTarget
from pants.base.payload import Payload

Expand Down
3 changes: 0 additions & 3 deletions contrib/avro/src/python/pants/contrib/avro/tasks/avro_gen.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 os

from pants.backend.jvm.targets.java_library import JavaLibrary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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

from textwrap import dedent

from pants.build_graph.build_file_aliases import BuildFileAliases
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 os

from pants_test.pants_run_integration_test import PantsRunIntegrationTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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 pycountry

from pants.contrib.awslambda.python.examples.hello_lib import say_hello
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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


def say_hello(s):
print('Hello {}!'.format(s))
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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

from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.goal.task_registrar import TaskRegistrar as task

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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

from pants.backend.python.subsystems.python_tool_base import PythonToolBase


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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

from pants.backend.python.targets.python_binary import PythonBinary
from pants.base.exceptions import TargetDefinitionException
from pants.base.payload import Payload
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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

from pants.backend.python.tasks.python_tool_prep_base import PythonToolInstance, PythonToolPrepBase

from pants.contrib.awslambda.python.subsystems.lambdex import Lambdex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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 functools
import os
import shutil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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 os

from pants.util.contextutil import temporary_dir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# 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 ast
import logging
import re
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# 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

from builtins import open
from textwrap import dedent

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 logging

from pants.base.build_environment import get_buildroot
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2018 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 logging

from pants.base.build_environment import get_buildroot
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 logging
from collections import defaultdict

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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

from pants.goal.task_registrar import TaskRegistrar as task

from pants.contrib.buildrefactor.buildozer import Buildozer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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


def prepare_dependencies(self):
self.add_to_build_file('a', 'java_library(name="a")')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 os
import re
from builtins import open
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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

from pants_test.pants_run_integration_test import PantsRunIntegrationTest


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 os

from pants.backend.jvm.targets.java_library import JavaLibrary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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

from pants_test.pants_run_integration_test import PantsRunIntegrationTest


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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

from pants.goal.task_registrar import TaskRegistrar as task

from pants.contrib.codeanalysis.tasks.bundle_entries import BundleEntries
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 os
import shutil

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# coding=utf-8
# Copyright 2017 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 os
from builtins import next

Expand Down
Loading

0 comments on commit ea74e99

Please sign in to comment.