fs: use pathlib.Path, add type hint check

pull/6150/head
Michael Hirsch, Ph.D 5 years ago
parent c6f93b6bf6
commit 3bbd065576
No known key found for this signature in database
GPG Key ID: 6D23CDADAB0294F9
  1. 2
      azure-pipelines.yml
  2. 31
      mesonbuild/modules/fs.py

@ -24,7 +24,7 @@ jobs:
displayName: 'Install Pylint, MyPy'
- script: pylint mesonbuild
displayName: Lint Checks
- script: mypy --follow-imports=skip mesonbuild/mtest.py mesonbuild/minit.py mesonbuild/msetup.py mesonbuild/wrap tools/
- script: mypy --follow-imports=skip mesonbuild/mtest.py mesonbuild/minit.py mesonbuild/msetup.py mesonbuild/wrap tools/ mesonbuild/modules/fs.py
- job: vs2015

@ -12,13 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import typing
from pathlib import Path
from . import ExtensionModule
from . import ModuleReturnValue
from ..mesonlib import MesonException
from ..interpreterbase import stringArgs, noKwargs
if typing.TYPE_CHECKING:
from ..interpreter import ModuleState
class FSModule(ExtensionModule):
@ -28,32 +31,32 @@ class FSModule(ExtensionModule):
@stringArgs
@noKwargs
def exists(self, state, args, kwargs):
def exists(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue:
if len(args) != 1:
MesonException('method takes exactly one argument.')
test_file = os.path.join(state.source_root, state.subdir, args[0])
return ModuleReturnValue(os.path.exists(test_file), [])
test_file = Path(state.source_root) / state.subdir / args[0]
return ModuleReturnValue(test_file.exists(), [])
def _check(self, check_fun, state, args):
def _check(self, check: str, state: 'ModuleState', args: typing.Sequence[str]) -> ModuleReturnValue:
if len(args) != 1:
MesonException('method takes exactly one argument.')
test_file = os.path.join(state.source_root, state.subdir, args[0])
return ModuleReturnValue(check_fun(test_file), [])
test_file = Path(state.source_root) / state.subdir / args[0]
return ModuleReturnValue(getattr(test_file, check)(), [])
@stringArgs
@noKwargs
def is_symlink(self, state, args, kwargs):
return self._check(os.path.islink, state, args)
def is_symlink(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue:
return self._check('is_symlink', state, args)
@stringArgs
@noKwargs
def is_file(self, state, args, kwargs):
return self._check(os.path.isfile, state, args)
def is_file(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue:
return self._check('is_file', state, args)
@stringArgs
@noKwargs
def is_dir(self, state, args, kwargs):
return self._check(os.path.isdir, state, args)
def is_dir(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue:
return self._check('is_dir', state, args)
def initialize(*args, **kwargs):
def initialize(*args, **kwargs) -> FSModule:
return FSModule(*args, **kwargs)

Loading…
Cancel
Save