fs: add expanduser method

this should help users specify leading `~` in various Meson options and variables
without refactoring lots of places inside Meson itself.
pull/6595/head
Michael Hirsch, Ph.D 5 years ago committed by Michael Hirsch, Ph.D
parent 4556343d95
commit dcb7043403
  1. 16
      docs/markdown/Fs-module.md
  2. 9
      mesonbuild/modules/fs.py
  3. 8
      test cases/common/227 fs module/meson.build

@ -37,7 +37,7 @@ by the string is a symbolic link.
### is_absolute
Return a boolean indicating if the path string specified is absolute for this computer, WITHOUT expanding `~`.
Return a boolean indicating if the path string specified is absolute, WITHOUT expanding `~`.
Examples:
@ -96,7 +96,19 @@ fs.is_samepath(p, s) # false
## Filename modification
The files need not actually exist yet for these methods, as they are just string manipulation.
The files need not actually exist yet for these path string manipulation methods.
### expanduser
A path string with a leading `~` is expanded to the user home directory
Examples:
```meson
fs.expanduser('~') # home directory
fs.expanduser('~/foo') # <homedir>/foo
```
### as_posix

@ -14,7 +14,7 @@
import typing as T
import hashlib
from pathlib import Path, PurePath
from pathlib import Path, PurePath, PureWindowsPath
from .. import mlog
from . import ExtensionModule
@ -60,6 +60,13 @@ class FSModule(ExtensionModule):
val = str(val)
return ModuleReturnValue(val, [])
@stringArgs
@noKwargs
def expanduser(self, state: 'ModuleState', args: T.Sequence[str], kwargs: dict) -> ModuleReturnValue:
if len(args) != 1:
raise MesonException('fs.expanduser takes exactly one argument.')
return ModuleReturnValue(str(Path(args[0]).expanduser()), [])
@stringArgs
@noKwargs
def is_absolute(self, state: 'ModuleState', args: T.Sequence[str], kwargs: dict) -> ModuleReturnValue:

@ -30,8 +30,12 @@ assert(fs.is_dir('subprojects'), 'Dir not detected correctly.')
assert(not fs.is_dir('meson.build'), 'File detected as a dir.')
assert(not fs.is_dir('nonexisting'), 'Bad path detected as a dir.')
assert(fs.is_dir('~'), 'expanduser not working')
assert(not fs.is_file('~'), 'expanduser not working')
assert(fs.is_dir('~'), 'home directory not detected')
assert(not fs.is_file('~'), 'home directory detected as file')
# -- expanduser
assert(fs.expanduser('~') != '~','expanduser failed')
assert(fs.expanduser('~/foo').endswith('foo'), 'expanduser with tail failed')
# -- as_posix
assert(fs.as_posix('/') == '/', 'as_posix idempotent')

Loading…
Cancel
Save