Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review of (explicit) encoding for files being created in text mode #1088

Merged
merged 22 commits into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0490027
Fix encoding for files created for building package (issue #1027)
vlcinsky May 7, 2019
8447a11
test_api.py use explicit encoding for files.
vlcinsky May 7, 2019
8ac5fdb
get-poetry.py: fix explicit (utf-8) encoding for files open in text mode
vlcinsky May 7, 2019
d632ce2
tests: fix explicit (utf-8) encoding for files open in text mode
vlcinsky May 7, 2019
0f21d56
poetry init: use explicit utf-8 for created pyproject.toml
vlcinsky May 7, 2019
87e8409
installed: use explicit utf-8 for text files
vlcinsky May 7, 2019
4154179
layouts: use explicit utf-8 for open text files
vlcinsky May 7, 2019
d9da150
spdx: explicit utf-8 for open text files
vlcinsky May 7, 2019
1774017
pypi_repository: explicit utf-8 for open text files
vlcinsky May 7, 2019
aff472c
explicit utf-8 for json
vlcinsky May 7, 2019
dadbe50
explicit utf-8 for metadata
vlcinsky May 7, 2019
7cc64b6
explicit utf-8 for requires.txt
vlcinsky May 7, 2019
04004f4
open files with explicit encoding in py27
vlcinsky May 7, 2019
0ffe80b
fix creation of poetry.bat on Windows
vlcinsky May 7, 2019
a06efff
Make get-poetry.py compatible to py2/3
vlcinsky May 7, 2019
c485399
Blacked two files to pass linter test
vlcinsky May 7, 2019
ceaa737
remove extranous encoding related comments
vlcinsky Jun 3, 2019
c0c078e
rewert broken move of import related to ansible
vlcinsky Jun 23, 2019
0996dfc
removed extraneous comments about opening files in UTF-8
vlcinsky Jun 23, 2019
39e8eb9
rewert ansible.release import (keep order of vars unchanged)
vlcinsky Jun 23, 2019
a0097be
Revert to original content (as it comes from external project)
vlcinsky Jun 24, 2019
55398f1
all vendored setup.py files in fixtures reverted back to org
vlcinsky Jun 24, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions get-poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from contextlib import contextmanager
from functools import cmp_to_key
from gzip import GzipFile
from io import UnsupportedOperation
from io import UnsupportedOperation, open

try:
from urllib.error import HTTPError
Expand All @@ -58,6 +58,10 @@
except ImportError:
winreg = None

try:
u = unicode
vlcinsky marked this conversation as resolved.
Show resolved Hide resolved
except NameError:
u = str

WINDOWS = sys.platform.startswith("win") or (sys.platform == "cli" and os.name == "nt")

Expand Down Expand Up @@ -191,6 +195,7 @@ def expanduser(path):


BIN = """#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import sys
import os
Expand All @@ -205,7 +210,7 @@ def expanduser(path):
main()
"""

BAT = '@echo off\r\npython "{poetry_bin}" %*\r\n'
BAT = u('@echo off\r\npython "{poetry_bin}" %*\r\n')


PRE_MESSAGE = """# Welcome to {poetry}!
Expand Down Expand Up @@ -387,7 +392,10 @@ def _compare_versions(x, y):

current_version = None
if os.path.exists(POETRY_LIB):
with open(os.path.join(POETRY_LIB, "poetry", "__version__.py")) as f:
# explicit utf-8 for python source file
with open(
os.path.join(POETRY_LIB, "poetry", "__version__.py"), encoding="utf-8"
) as f:
version_content = f.read()

current_version_re = re.match(
Expand Down Expand Up @@ -561,17 +569,21 @@ def make_bin(self):
os.mkdir(POETRY_BIN, 0o755)

if WINDOWS:
# implicit system encoding for poetry.bat
vlcinsky marked this conversation as resolved.
Show resolved Hide resolved
with open(os.path.join(POETRY_BIN, "poetry.bat"), "w") as f:
f.write(
BAT.format(
poetry_bin=os.path.join(POETRY_BIN, "poetry").replace(
os.environ["USERPROFILE"], "%USERPROFILE%"
u(
BAT.format(
poetry_bin=os.path.join(POETRY_BIN, "poetry").replace(
os.environ["USERPROFILE"], "%USERPROFILE%"
)
)
)
)

with open(os.path.join(POETRY_BIN, "poetry"), "w") as f:
f.write(BIN)
# explicit utf-8 encoding for python source file
with open(os.path.join(POETRY_BIN, "poetry"), "w", encoding="utf-8") as f:
f.write(u(BIN))

if not WINDOWS:
# Making the file executable
Expand All @@ -582,8 +594,9 @@ def make_env(self):
if WINDOWS:
return

# implicit system encoding for env
with open(os.path.join(POETRY_HOME, "env"), "w") as f:
f.write(self.get_export_string())
f.write(u(self.get_export_string()))

def update_path(self):
"""
Expand All @@ -603,12 +616,14 @@ def update_path(self):
if not os.path.exists(profile):
continue

# implicit system encoding for profile
with open(profile, "r") as f:
content = f.read()

if addition not in content:
# implicit system encoding for profile
with open(profile, "a") as f:
f.write(addition)
f.write(u(addition))

updated.append(os.path.relpath(profile, HOME))

Expand Down Expand Up @@ -700,6 +715,7 @@ def remove_from_unix_path(self):
if not os.path.exists(profile):
continue

# implicit system encoding for profile
with open(profile, "r") as f:
content = f.readlines()

Expand All @@ -716,6 +732,7 @@ def remove_from_unix_path(self):

new_content.append(line)

# implicit system encoding for profile
with open(profile, "w") as f:
f.writelines(new_content)

Expand Down
3 changes: 2 additions & 1 deletion poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def handle(self):

return 1

with (Path.cwd() / "pyproject.toml").open("w") as f:
# explicit utf-8 for pyproject.toml
with (Path.cwd() / "pyproject.toml").open("w", encoding="utf-8") as f:
f.write(content)

def _determine_requirements(
Expand Down
3 changes: 2 additions & 1 deletion poetry/installation/pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ def install_directory(self, package):
# We also need it for non-PEP-517 packages
builder = SdistBuilder(Poetry.create(pyproject.parent), NullEnv(), NullIO())

with open(setup, "w") as f:
# explicit utf-8 for python source
with open(setup, "w", encoding="utf-8") as f:
f.write(decode(builder.build_setup()))

if package.develop:
Expand Down
4 changes: 3 additions & 1 deletion poetry/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import jsonschema

from io import open
from typing import List

SCHEMA_DIR = os.path.join(os.path.dirname(__file__), "schemas")
Expand All @@ -19,7 +20,8 @@ def validate_object(obj, schema_name): # type: (dict, str) -> List[str]
if not os.path.exists(schema):
raise ValueError("Schema {} does not exist.".format(schema_name))

with open(schema) as f:
# explicit utf-8 for JSON
with open(schema, encoding="utf-8") as f:
schema = json.loads(f.read())

validator = jsonschema.Draft7Validator(schema)
Expand Down
6 changes: 4 additions & 2 deletions poetry/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def _create_tests(self, path):
tests.mkdir()
tests_init.touch(exist_ok=False)

with tests_default.open("w") as f:
# explicit utf-8 for python source
with tests_default.open("w", encoding="utf-8") as f:
f.write(
TESTS_DEFAULT.format(
package_name=self._package_name, version=self._version
Expand All @@ -152,5 +153,6 @@ def _write_poetry(self, path):

poetry = path / "pyproject.toml"

with poetry.open("w") as f:
# explicit utf-8 for python source
with poetry.open("w", encoding="utf-8") as f:
f.write(content)
3 changes: 2 additions & 1 deletion poetry/layouts/src.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ def _create_default(self, path):

package_path.mkdir(parents=True)

with package_init.open("w") as f:
# explicit utf-8 for python source
with package_init.open("w", encoding="utf-8") as f:
f.write(DEFAULT.format(version=self._version))
3 changes: 2 additions & 1 deletion poetry/layouts/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ def _create_default(self, path):

package_path.mkdir()

with package_init.open("w") as f:
# explicit utf-8 for python source
with package_init.open("w", encoding="utf-8") as f:
f.write(DEFAULT.format(version=self._version))
6 changes: 3 additions & 3 deletions poetry/masonry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
dist_info.mkdir()

if "scripts" in poetry.local_config or "plugins" in poetry.local_config:
with (dist_info / "entry_points.txt").open("w") as f:
with (dist_info / "entry_points.txt").open("w", encoding="utf-8") as f:
builder._write_entry_points(f)

with (dist_info / "WHEEL").open("w") as f:
with (dist_info / "WHEEL").open("w", encoding="utf-8") as f:
builder._write_wheel_file(f)

with (dist_info / "METADATA").open("w") as f:
with (dist_info / "METADATA").open("w", encoding="utf-8") as f:
builder._write_metadata_file(f)

return dist_info.name
Expand Down
3 changes: 2 additions & 1 deletion poetry/masonry/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def from_package(cls, package): # type: (...) -> Metadata
meta.version = normalize_version(package.version.text)
meta.summary = package.description
if package.readme:
with package.readme.open() as f:
# explicit utf-8 for readme
with package.readme.open(encoding="utf-8") as f:
meta.description = f.read()

meta.keywords = ",".join(package.keywords)
Expand Down
3 changes: 2 additions & 1 deletion poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ def search_for_directory(
reqs = []
requires = egg_info / "requires.txt"
if requires.exists():
with requires.open() as f:
# explicit utf-8 for requires.txt
with requires.open(encoding="utf-8") as f:
reqs = parse_requires(f.read())
finally:
os.chdir(current_dir)
Expand Down
6 changes: 4 additions & 2 deletions poetry/repositories/pypi_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ def _get_info_from_sdist(

requires = egg_info / "requires.txt"
if requires.exists():
with requires.open() as f:
# explicit utf-8 for requires.txt
with requires.open(encoding="utf-8") as f:
info["requires_dist"] = parse_requires(f.read())

return info
Expand All @@ -545,7 +546,8 @@ def _get_info_from_sdist(

requires = egg_info / "requires.txt"
if requires.exists():
with requires.open() as f:
# explicit utf-8 for requires.txt
with requires.open(encoding="utf-8") as f:
info["requires_dist"] = parse_requires(f.read())

return info
Expand Down
5 changes: 4 additions & 1 deletion poetry/spdx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os

from io import open

from .license import License
from .updater import Updater

Expand All @@ -26,7 +28,8 @@ def load_licenses():

licenses_file = os.path.join(os.path.dirname(__file__), "data", "licenses.json")

with open(licenses_file) as f:
# explicit utf-8 for JSON
with open(licenses_file, encoding="utf-8") as f:
data = json.loads(f.read())

for name, license in data.items():
Expand Down
5 changes: 4 additions & 1 deletion poetry/spdx/updater.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os

from io import open

try:
from urllib.request import urlopen
except ImportError:
Expand All @@ -20,7 +22,8 @@ def dump(self, file=None):

licenses_url = self._base_url + "licenses.json"

with open(file, "w") as f:
# explicit utf-8 for JSON
with open(file, "w", encoding="utf-8") as f:
f.write(
json.dumps(self.get_licenses(licenses_url), indent=2, sort_keys=True)
)
Expand Down
6 changes: 4 additions & 2 deletions tests/console/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,17 @@ def repo():
def poetry(repo):
p = Poetry.create(Path(__file__).parent.parent / "fixtures" / "simple_project")

with p.file.path.open() as f:
# explicit utf-8 for poetry file
with p.file.path.open(encoding="utf-8") as f:
content = f.read()

p.pool.remove_repository("pypi")
p.pool.add_repository(repo)

yield p

with p.file.path.open("w") as f:
# explicit utf-8 for poetry file
with p.file.path.open("w", encoding="utf-8") as f:
f.write(content)


Expand Down
5 changes: 4 additions & 1 deletion tests/fixtures/git/github.com/demo/no-version/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@


def read_version():
with open(os.path.join(os.path.dirname(__file__), "demo", "__init__.py")) as f:
# explicit utf-8 for python source
with open(
os.path.join(os.path.dirname(__file__), "demo", "__init__.py"), encoding="utf-8"
) as f:
for line in f:
if line.startswith("__version__ = "):
return ast.literal_eval(line[len("__version__ = ") :].strip())
Expand Down
6 changes: 3 additions & 3 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ def test_prepare_metadata_for_build_wheel():
assert (dist_info / "WHEEL").exists()
assert (dist_info / "METADATA").exists()

with (dist_info / "entry_points.txt").open() as f:
with (dist_info / "entry_points.txt").open(encoding="utf-8") as f:
assert entry_points == decode(f.read())

with (dist_info / "WHEEL").open() as f:
with (dist_info / "WHEEL").open(encoding="utf-8") as f:
assert wheel_data == decode(f.read())

with (dist_info / "METADATA").open() as f:
with (dist_info / "METADATA").open(encoding="utf-8") as f:
assert metadata == decode(f.read())
3 changes: 2 additions & 1 deletion tests/repositories/test_legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def _get(self, endpoint):

fixture = self.FIXTURES / (name + ".html")

with fixture.open() as f:
# explicit utf-8 for HTML
with fixture.open(encoding="utf-8") as f:
return Page(self._url + endpoint, f.read(), {})

def _download(self, url, dest):
Expand Down
3 changes: 2 additions & 1 deletion tests/repositories/test_pypi_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def _get(self, url):
if not fixture.exists():
fixture = self.JSON_FIXTURES / (name + ".json")

with fixture.open() as f:
# explicit utf-8 for JSON
with fixture.open(encoding="utf-8") as f:
return json.loads(f.read())

def _download(self, url, dest):
Expand Down
Loading