Removed the use of os.chdir() during patching, instead passing the found library path to the patcher.

- Legacy-Id: 14757
This commit is contained in:
Henrik Levkowetz 2018-03-09 17:54:24 +00:00
parent cf9804f659
commit e5bc9c2e9a

View file

@ -358,23 +358,21 @@ def maybe_patch_library(app_configs, **kwargs):
import os, django import os, django
django_path = os.path.dirname(django.__file__) django_path = os.path.dirname(django.__file__)
library_path = os.path.dirname(django_path) library_path = os.path.dirname(django_path)
saved_cwd = os.getcwd() cwd = os.getcwd()
# All patches in settings.CHECKS_LIBRARY_PATCHES_TO_APPLY must have a # All patches in settings.CHECKS_LIBRARY_PATCHES_TO_APPLY must have a
# relative file path rooted in the django dir, for instance # relative file path rooted in the django dir, for instance
# 'django/db/models/fields/__init__.py' # 'django/db/models/fields/__init__.py'
for patch_file in settings.CHECKS_LIBRARY_PATCHES_TO_APPLY: for patch_file in settings.CHECKS_LIBRARY_PATCHES_TO_APPLY:
try: try:
patch_path = os.path.join(saved_cwd, patch_file) patch_path = os.path.join(cwd, patch_file)
patch_set = patch.fromfile(patch_path) patch_set = patch.fromfile(patch_path)
if patch_set: if patch_set:
os.chdir(library_path) if not patch_set.apply(root=library_path):
if not patch_set.apply():
errors.append(checks.Warning( errors.append(checks.Warning(
"Could not apply patch from file '%s'"%patch_file, "Could not apply patch from file '%s'"%patch_file,
hint="Make sure that the patch file contains a unified diff and has valid file paths", hint="Make sure that the patch file contains a unified diff and has valid file paths",
id="datatracker.W0002", id="datatracker.W0002",
)) ))
os.chdir(saved_cwd)
else: else:
errors.append(checks.Warning( errors.append(checks.Warning(
"Could not parse patch file '%s'"%patch_file, "Could not parse patch file '%s'"%patch_file,