Skip to content

Commit

Permalink
pathlib: Fix resolve() by overriding it in Python 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mensinda committed Oct 4, 2020
1 parent 77b5c82 commit 1dfaccf
Show file tree
Hide file tree
Showing 40 changed files with 91 additions and 40 deletions.
2 changes: 1 addition & 1 deletion meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.

import sys
from pathlib import Path
from mesonbuild._pathlib import Path

# If we're run uninstalled, add the script directory to sys.path to ensure that
# we always import the correct mesonbuild modules even if PYTHONPATH is mangled
Expand Down
49 changes: 49 additions & 0 deletions mesonbuild/_pathlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2020 The Meson development team

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import typing as T

# Python 3.5 does not have the strict kwarg for resolve and always
# behaves like calling resolve with strict=True in Python 3.6+
#
# This module emulates the behavior of Python 3.6+ by in Python 3.5 by
# overriding the resolve method with a bit of custom logic
#
# TODO: Drop this module as soon as Python 3.5 support is dropped

if T.TYPE_CHECKING:
from pathlib import Path
else:
if sys.version_info.major <= 3 and sys.version_info.minor <= 5:

# Inspired by https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path
import pathlib
import os

# Can not directly inherit from pathlib.Path because the __new__
# operator of pathlib.Path() returns a {Posix,Windows}Path object.
class Path(type(pathlib.Path())):
def resolve(self, strict: bool = False) -> 'Path':
try:
return super().resolve()
except FileNotFoundError:
if strict:
raise
return Path(os.path.normpath(str(self)))

else:
from pathlib import Path

from pathlib import PurePath, PureWindowsPath, PurePosixPath
4 changes: 2 additions & 2 deletions mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from collections import OrderedDict
from functools import lru_cache
from pathlib import Path
from .._pathlib import Path
import enum
import json
import os
Expand Down Expand Up @@ -917,7 +917,7 @@ def generate_depmf_install(self, d):
def get_regen_filelist(self):
'''List of all files whose alteration means that the build
definition needs to be regenerated.'''
deps = [os.path.join(self.build_to_src, df)
deps = [str(Path(self.build_to_src) / df)
for df in self.interpreter.get_build_def_files()]
if self.environment.is_cross_build():
deps.extend(self.environment.coredata.cross_files)
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from collections import OrderedDict
from enum import Enum, unique
import itertools
from pathlib import PurePath, Path
from .._pathlib import PurePath, Path
from functools import lru_cache

from . import backends
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import xml.etree.ElementTree as ET
import uuid
import typing as T
from pathlib import Path, PurePath
from .._pathlib import Path, PurePath

from . import backends
from .. import build
Expand Down Expand Up @@ -1000,7 +1000,7 @@ def gen_vcxproj(self, target, ofname, guid):
if inc_dir not in file_inc_dirs[l]:
file_inc_dirs[l].append(inc_dir)
# Add include dirs to target as well so that "Go to Document" works in headers
if inc_dir not in target_inc_dirs:
if inc_dir not in target_inc_dirs:
target_inc_dirs.append(inc_dir)

# Split compile args needed to find external dependencies
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/cmake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .. import mlog
from contextlib import contextmanager
from subprocess import Popen, PIPE, TimeoutExpired
from pathlib import Path
from .._pathlib import Path
import typing as T
import json

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/cmake/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from ..mesonlib import MesonException
from .. import mlog
from pathlib import Path
from .._pathlib import Path
import typing as T

class CMakeException(MesonException):
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/cmake/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# or an interpreter-based tool.

import subprocess as S
from pathlib import Path
from .._pathlib import Path
from threading import Thread
import typing as T
import re
Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/cmake/fileapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .common import CMakeException, CMakeBuildFile, CMakeConfiguration
import typing as T
from .. import mlog
from pathlib import Path
from .._pathlib import Path
import json
import re

Expand Down Expand Up @@ -74,6 +74,7 @@ def load_reply(self) -> None:

# Debug output
debug_json = self.build_dir / '..' / 'fileAPI.json'
debug_json = debug_json.resolve()
debug_json.write_text(json.dumps(index, indent=2))
mlog.cmd_ci_include(debug_json.as_posix())

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/cmake/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ..compilers.compilers import lang_suffixes, header_suffixes, obj_suffixes, lib_suffixes, is_header
from enum import Enum
from functools import lru_cache
from pathlib import Path
from .._pathlib import Path
import typing as T
import re
from os import environ
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/cmake/traceparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ..mesonlib import version_compare

import typing as T
from pathlib import Path
from .._pathlib import Path
import re
import json
import textwrap
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path
from .._pathlib import Path
import typing as T
import subprocess, os

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/mixins/clike.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import re
import subprocess
import typing as T
from pathlib import Path
from ..._pathlib import Path

from ... import arglist
from ... import mesonlib
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/mixins/pgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import typing as T
import os
from pathlib import Path
from ..._pathlib import Path

from ..compilers import clike_debug_args, clike_optimization_args

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import pickle, os, uuid
import sys
from itertools import chain
from pathlib import PurePath
from ._pathlib import PurePath
from collections import OrderedDict, defaultdict
from .mesonlib import (
MesonException, EnvironmentException, MachineChoice, PerMachine,
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import platform
import typing as T
from enum import Enum
from pathlib import Path, PurePath
from .._pathlib import Path, PurePath

from .. import mlog
from .. import mesonlib
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import re
import functools
import typing as T
from pathlib import Path
from .._pathlib import Path

from .. import mlog
from .. import mesonlib
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import re
import shutil
import subprocess
from pathlib import Path
from .._pathlib import Path

from ..mesonlib import OrderedSet, join_args
from .base import (
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# This file contains the detection logic for miscellaneous external dependencies.

from pathlib import Path
from .._pathlib import Path
import functools
import re
import sysconfig
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/scalapack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path
from .._pathlib import Path
import functools
import os
import typing as T
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .cmake import CMakeInterpreter
from .backend.backends import TestProtocol, Backend

from pathlib import Path, PurePath
from ._pathlib import Path, PurePath
import os
import shutil
import uuid
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mcompile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import shutil
import typing as T
from collections import defaultdict
from pathlib import Path
from ._pathlib import Path

from . import mlog
from . import mesonlib
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import hashlib
import json
from glob import glob
from pathlib import Path
from ._pathlib import Path
from mesonbuild.environment import detect_ninja
from mesonbuild.mesonlib import windows_proof_rmtree, MesonException
from mesonbuild.wrap import wrap
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mesondata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
####


from pathlib import Path
from ._pathlib import Path
import typing as T

if T.TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mesonlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

"""A library of random helper functionality."""
from pathlib import Path
from ._pathlib import Path
import sys
import stat
import time
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/minit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""Code that creates simple startup projects."""

from pathlib import Path
from ._pathlib import Path
from enum import Enum
import subprocess
import shutil
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mintro.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .backend import backends
from .mparser import BaseNode, FunctionNode, ArrayNode, ArgumentNode, StringNode
from .interpreter import Interpreter
from pathlib import PurePath
from ._pathlib import PurePath
import typing as T
import os
import argparse
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import platform
import typing as T
from contextlib import contextmanager
from pathlib import Path
from ._pathlib import Path

"""This is (mostly) a standalone module used to write logging
information about Meson runs. Some output goes to screen,
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import typing as T
import hashlib
from pathlib import Path, PurePath, PureWindowsPath
from .._pathlib import Path, PurePath, PureWindowsPath

from .. import mlog
from . import ExtensionModule
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/pkgconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import os, types
from pathlib import PurePath
from .._pathlib import PurePath

from .. import build
from .. import dependencies
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import shutil
import typing as T

from pathlib import Path
from .._pathlib import Path
from .. import mesonlib
from ..mesonlib import MachineChoice, MesonException
from . import ExtensionModule
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/unstable_external_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import os, subprocess, shlex
from pathlib import Path
from .._pathlib import Path
import typing as T

from . import ExtensionModule, ModuleReturnValue
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/msubprojects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os, subprocess
import argparse
from pathlib import Path
from ._pathlib import Path

from . import mlog
from .mesonlib import quiet_git, verbose_git, GitException, Popen_safe, MesonException
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# A tool to run tests in many different ways.

from pathlib import Path
from ._pathlib import Path
from collections import namedtuple
from copy import deepcopy
import argparse
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/wrap/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import configparser
import typing as T

from pathlib import Path
from .._pathlib import Path
from . import WrapMode
from .. import coredata
from ..mesonlib import verbose_git, quiet_git, GIT, ProgressBar, MesonException
Expand Down
2 changes: 1 addition & 1 deletion run_meson_command_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import unittest
import subprocess
import zipapp
from pathlib import Path
from mesonbuild._pathlib import Path

from mesonbuild.mesonlib import windows_proof_rmtree, python_command, is_windows
from mesonbuild.coredata import version as meson_version
Expand Down
1 change: 1 addition & 0 deletions run_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'mesonbuild/wrap',

# specific files
'mesonbuild/_pathlib.py',
'mesonbuild/arglist.py',
# 'mesonbuild/coredata.py',
'mesonbuild/dependencies/boost.py',
Expand Down
Loading

0 comments on commit 1dfaccf

Please sign in to comment.