Refactor black into packages in src/ dir (#1376)

- Move black.py to src/black/__init__.py
- Have setuptools_scm make src/_black_version.py and exclude from git
- Move blackd.py to src/blackd/__init__.py
- Move blib2to3/ to src/
- Update `setup.py`
- Update unittests to pass
  - Mostly path fixing + resolving
- Update CI
  - pre-commit config
  - appveyor + travis

Tested on my mac with python 3.7.5 via:
```
python3 -m venv /tmp/tb3
/tmp/tb3/bin/pip install --upgrade setuptools pip coverage pre-commit
/tmp/tb2/bin/pip install ~/repos/black/
cd ~/repos/black/
/tmp/tb2/bin/coverage run tests/test_black.py
/tmp/tb3/bin/pre-commit run -a
/tmp/tb3/bin/black --help
/tmp/tb3/bin/black ~/repos/ptr/ptr.py
```
This commit is contained in:
Cooper Lees 2020-05-08 08:50:50 -07:00 committed by GitHub
parent 0721f11731
commit 2082a325fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 34 additions and 32 deletions

View File

@ -7,12 +7,12 @@ build: off
test_script: test_script:
- C:\Python36\python.exe tests/test_black.py - C:\Python36\python.exe tests/test_black.py
- C:\Python36\python.exe -m mypy black.py blackd.py tests/test_black.py blib2to3 - C:\Python36\python.exe -m mypy src/black src/blackd tests/test_black.py src/blib2to3
after_test: after_test:
- C:\Python36\python.exe -m pip install pyinstaller - C:\Python36\python.exe -m pip install pyinstaller
- "%CMD_IN_ENV% C:\\Python36\\python.exe -m PyInstaller --clean -F --add-data - "%CMD_IN_ENV% C:\\Python36\\python.exe -m PyInstaller --clean -F --add-data
blib2to3/;blib2to3 black.py" src/blib2to3/;blib2to3 src/black/__init__.py"
artifacts: artifacts:
- path: dist/black.exe - path: dist/black.exe

View File

@ -1,5 +1,5 @@
[report] [report]
omit = omit =
blib2to3/* src/blib2to3/*
tests/data/* tests/data/*
*/site-packages/* */site-packages/*

2
.gitignore vendored
View File

@ -9,7 +9,7 @@ black.egg-info
build/ build/
dist/ dist/
pip-wheel-metadata/ pip-wheel-metadata/
_black_version.py src/_black_version.py
.idea .idea
.eggs .eggs
.dmypy.json .dmypy.json

View File

@ -1,6 +1,6 @@
# Note: don't use this config for your own repositories. Instead, see # Note: don't use this config for your own repositories. Instead, see
# "Version control integration" in README.md. # "Version control integration" in README.md.
exclude: ^(blib2to3/|profiling/|tests/data/) exclude: ^(src/blib2to3/|profiling/|tests/data/)
repos: repos:
- repo: local - repo: local
hooks: hooks:

View File

@ -33,7 +33,7 @@ matrix:
- python: 3.9-dev - python: 3.9-dev
before_deploy: before_deploy:
- pip install pyinstaller - pip install pyinstaller
- pyinstaller --clean -F --add-data blib2to3/:blib2to3 black.py - pyinstaller --clean -F --add-data src/blib2to3/:blib2to3 src/black/__init__.py
deploy: deploy:
provider: releases provider: releases
api_key: api_key:

View File

@ -1,4 +1,4 @@
# Copyright (C) 2018 Łukasz Langa # Copyright (C) 2020 Łukasz Langa
from setuptools import setup from setuptools import setup
import sys import sys
import os import os
@ -28,14 +28,14 @@ def get_long_description() -> str:
if USE_MYPYC: if USE_MYPYC:
mypyc_targets = [ mypyc_targets = [
"black.py", "src/black/__init__.py",
"blib2to3/pytree.py", "src/blib2to3/pytree.py",
"blib2to3/pygram.py", "src/blib2to3/pygram.py",
"blib2to3/pgen2/parse.py", "src/blib2to3/pgen2/parse.py",
"blib2to3/pgen2/grammar.py", "src/blib2to3/pgen2/grammar.py",
"blib2to3/pgen2/token.py", "src/blib2to3/pgen2/token.py",
"blib2to3/pgen2/driver.py", "src/blib2to3/pgen2/driver.py",
"blib2to3/pgen2/pgen.py", "src/blib2to3/pgen2/pgen.py",
] ]
from mypyc.build import mypycify from mypyc.build import mypycify
@ -48,7 +48,7 @@ def get_long_description() -> str:
setup( setup(
name="black", name="black",
use_scm_version={ use_scm_version={
"write_to": "_black_version.py", "write_to": "src/_black_version.py",
"write_to_template": 'version = "{version}"\n', "write_to_template": 'version = "{version}"\n',
}, },
description="The uncompromising code formatter.", description="The uncompromising code formatter.",
@ -60,9 +60,10 @@ def get_long_description() -> str:
url="https://github.com/psf/black", url="https://github.com/psf/black",
project_urls={"Changelog": "https://github.com/psf/black/blob/master/CHANGES.md"}, project_urls={"Changelog": "https://github.com/psf/black/blob/master/CHANGES.md"},
license="MIT", license="MIT",
py_modules=["black", "blackd", "_black_version"], py_modules=["_black_version"],
ext_modules=ext_modules, ext_modules=ext_modules,
packages=["blib2to3", "blib2to3.pgen2"], packages=["blackd", "black", "blib2to3", "blib2to3.pgen2"],
package_dir={"": "src"},
package_data={"blib2to3": ["*.txt"]}, package_data={"blib2to3": ["*.txt"]},
python_requires=">=3.6", python_requires=">=3.6",
zip_safe=False, zip_safe=False,

View File

@ -35,6 +35,7 @@
fs = partial(black.format_str, mode=black.FileMode()) fs = partial(black.format_str, mode=black.FileMode())
THIS_FILE = Path(__file__) THIS_FILE = Path(__file__)
THIS_DIR = THIS_FILE.parent THIS_DIR = THIS_FILE.parent
PROJECT_ROOT = THIS_DIR.parent
DETERMINISTIC_HEADER = "[Deterministic header]" DETERMINISTIC_HEADER = "[Deterministic header]"
EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)" EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)"
PY36_ARGS = [ PY36_ARGS = [
@ -54,7 +55,7 @@ def read_data(name: str, data: bool = True) -> Tuple[str, str]:
name += ".py" name += ".py"
_input: List[str] = [] _input: List[str] = []
_output: List[str] = [] _output: List[str] = []
base_dir = THIS_DIR / "data" if data else THIS_DIR base_dir = THIS_DIR / "data" if data else PROJECT_ROOT
with open(base_dir / name, "r", encoding="utf8") as test: with open(base_dir / name, "r", encoding="utf8") as test:
lines = test.readlines() lines = test.readlines()
result = _input result = _input
@ -202,43 +203,43 @@ def test_self(self) -> None:
self.checkSourceFile("tests/test_black.py") self.checkSourceFile("tests/test_black.py")
def test_black(self) -> None: def test_black(self) -> None:
self.checkSourceFile("black.py") self.checkSourceFile("src/black/__init__.py")
def test_pygram(self) -> None: def test_pygram(self) -> None:
self.checkSourceFile("blib2to3/pygram.py") self.checkSourceFile("src/blib2to3/pygram.py")
def test_pytree(self) -> None: def test_pytree(self) -> None:
self.checkSourceFile("blib2to3/pytree.py") self.checkSourceFile("src/blib2to3/pytree.py")
def test_conv(self) -> None: def test_conv(self) -> None:
self.checkSourceFile("blib2to3/pgen2/conv.py") self.checkSourceFile("src/blib2to3/pgen2/conv.py")
def test_driver(self) -> None: def test_driver(self) -> None:
self.checkSourceFile("blib2to3/pgen2/driver.py") self.checkSourceFile("src/blib2to3/pgen2/driver.py")
def test_grammar(self) -> None: def test_grammar(self) -> None:
self.checkSourceFile("blib2to3/pgen2/grammar.py") self.checkSourceFile("src/blib2to3/pgen2/grammar.py")
def test_literals(self) -> None: def test_literals(self) -> None:
self.checkSourceFile("blib2to3/pgen2/literals.py") self.checkSourceFile("src/blib2to3/pgen2/literals.py")
def test_parse(self) -> None: def test_parse(self) -> None:
self.checkSourceFile("blib2to3/pgen2/parse.py") self.checkSourceFile("src/blib2to3/pgen2/parse.py")
def test_pgen(self) -> None: def test_pgen(self) -> None:
self.checkSourceFile("blib2to3/pgen2/pgen.py") self.checkSourceFile("src/blib2to3/pgen2/pgen.py")
def test_tokenize(self) -> None: def test_tokenize(self) -> None:
self.checkSourceFile("blib2to3/pgen2/tokenize.py") self.checkSourceFile("src/blib2to3/pgen2/tokenize.py")
def test_token(self) -> None: def test_token(self) -> None:
self.checkSourceFile("blib2to3/pgen2/token.py") self.checkSourceFile("src/blib2to3/pgen2/token.py")
def test_setup(self) -> None: def test_setup(self) -> None:
self.checkSourceFile("setup.py") self.checkSourceFile("setup.py")
def test_piping(self) -> None: def test_piping(self) -> None:
source, expected = read_data("../black", data=False) source, expected = read_data("src/black/__init__", data=False)
result = BlackRunner().invoke( result = BlackRunner().invoke(
black.main, black.main,
["-", "--fast", f"--line-length={black.DEFAULT_LINE_LENGTH}"], ["-", "--fast", f"--line-length={black.DEFAULT_LINE_LENGTH}"],
@ -1663,7 +1664,7 @@ def test_assert_equivalent_different_asts(self) -> None:
def test_symlink_out_of_root_directory(self) -> None: def test_symlink_out_of_root_directory(self) -> None:
path = MagicMock() path = MagicMock()
root = THIS_DIR root = THIS_DIR.resolve()
child = MagicMock() child = MagicMock()
include = re.compile(black.DEFAULT_INCLUDES) include = re.compile(black.DEFAULT_INCLUDES)
exclude = re.compile(black.DEFAULT_EXCLUDES) exclude = re.compile(black.DEFAULT_EXCLUDES)