Skip to content

Commit

Permalink
modules/fs: Replace configure_file(copy:) wit fs.copyfile
Browse files Browse the repository at this point in the history
`configure_file` is both an extremely complicated implementation, and
a strange place for copying. It's a bit of a historical artifact, since
the fs module didn't yet exist. It makes more sense to move this to the
fs module and deprecate this `configure_file` version.
  • Loading branch information
dcbaker committed Feb 28, 2022
1 parent 58d4745 commit 4474301
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
19 changes: 19 additions & 0 deletions docs/markdown/Fs-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,22 @@ fs.stem('foo/bar/baz.dll.a') # baz.dll
specified by `path` changes, this will trigger Meson to reconfigure the
project. If the file specified by `path` is a `files()` object it
cannot refer to a built file.


### copyfile

*Since 0.62.0*

Copy a file from the source directory to the build directory

Has the following positional arguments:
- src `File | str`: the file to copy

Has the following optional arguments:
- dest `str`: the destination in the build dir to copy to. If unset will be the basename of the src

Has the following keyword arguments:
- install `bool`: Whether to install the copied file, defaults to false
- install_dir `str`: Where to install the file to
- install_tag: `str`: the install tag to assign to this target
- install_mode `list[str | int]`: the mode to install the file with
17 changes: 17 additions & 0 deletions docs/markdown/snippets/fs_copyfile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## `fs.copyfile` to replace `configure_file(copy : true)`

A new method has been added to the `fs` module, `copyfile`. This method replaces
`configure_file(copy : true)`, but only copies files. It works basically the
same way, except that the output is optional, and will be the same as the input
if unset:

```meson
fs.copyfile('file.txt')
```
Will create a file in the build directory called `file.txt`


```meson
fs.copy('file.txt', 'outfile.txt')
```
Will create a copy renamed to `outfile.txt`
5 changes: 4 additions & 1 deletion mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2230,7 +2230,10 @@ def func_install_subdir(self, node: mparser.BaseNode, args: T.Tuple[str],
'configuration',
(ContainerTypeInfo(dict, (str, int, bool)), build.ConfigurationData, NoneType),
),
KwargInfo('copy', bool, default=False, since='0.47.0'),
KwargInfo(
'copy', bool, default=False, since='0.47.0',
deprecated='0.62.0', deprecated_message='Use fs.copy instead',
),
KwargInfo('encoding', str, default='utf-8', since='0.47.0'),
KwargInfo('format', str, default='meson', since='0.46.0',
validator=in_set_validator({'meson', 'cmake', 'cmake@'})),
Expand Down
64 changes: 60 additions & 4 deletions mesonbuild/modules/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import typing as T
from __future__ import annotations
from pathlib import Path, PurePath, PureWindowsPath
import hashlib
import os
from pathlib import Path, PurePath, PureWindowsPath
import shutil
import typing as T

from ..build import Data, InvalidArguments

from .. import mlog
from . import ExtensionModule
from ..interpreter.type_checking import INSTALL_KW, INSTALL_MODE_KW, INSTALL_TAG_KW, NoneType
from ..interpreterbase import FeatureNew, KwargInfo, typed_kwargs, typed_pos_args, noKwargs
from ..mesonlib import (
File,
FileOrString,
MesonException,
path_is_in_root,
)
from ..interpreterbase import FeatureNew, KwargInfo, typed_kwargs, typed_pos_args, noKwargs
from .. import mlog

if T.TYPE_CHECKING:
from . import ModuleState
from ..interpreter import Interpreter
from ..interpreterbase.baseobjects import TYPE_kwargs
from ..mesonlib import FileMode

from typing_extensions import TypedDict

Expand All @@ -38,6 +45,15 @@ class ReadKwArgs(TypedDict):

encoding: str

class CopyKw(TypedDict):

"""Kwargs for fs.copy"""

install: bool
install_dir: T.Optional[str]
install_mode: FileMode
install_tag: T.Optional[str]


class FSModule(ExtensionModule):

Expand All @@ -60,6 +76,7 @@ def __init__(self, interpreter: 'Interpreter') -> None:
'name': self.name,
'stem': self.stem,
'read': self.read,
'copyfile': self.copy,
})

def _absolute_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path:
Expand Down Expand Up @@ -254,6 +271,45 @@ def read(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: 'Rea
self.interpreter.add_build_def_file(path)
return data

@FeatureNew('fs.copyfile', '0.62.0')
@typed_pos_args('fs.copyfile', (File, str), optargs=[str])
@typed_kwargs(
'fs.copyfile',
INSTALL_KW,
INSTALL_MODE_KW,
INSTALL_TAG_KW,
KwargInfo('install_dir', (str, NoneType)),
)
def copy(self, state: ModuleState, args: T.Tuple[FileOrString, T.Optional[str]],
kwargs: CopyKw) -> File:
"""Copy a file into the build directory at configure time."""
if isinstance(args[0], str):
src = args[0]
else:
src = args[0].absolute_path(state.environment.source_dir, state.environment.build_dir)

if args[1]:
dest = args[1]
else:
dest = args[0] if isinstance(args[0], str) else args[0].fname

# Create the directory and do the copy
os.makedirs(os.path.join(state.environment.build_dir, state.subdir), exist_ok=True)
(ofile_path, ofile_fname) = os.path.split(os.path.join(state.subdir, dest))
ofile_abs = os.path.join(state.environment.build_dir, ofile_path, ofile_fname)
shutil.copy2(os.path.join(state.environment.source_dir, state.subdir, src), ofile_abs)

f = File.from_built_file(state.subdir, ofile_fname)

if kwargs['install']:
if not kwargs['install_dir']:
raise InvalidArguments('"install_dir" must be specified when "install" is true')
state.data.append(Data(
[f], kwargs['install_dir'], kwargs['install_dir'], kwargs['install_mode'],
state.subproject, install_tag=kwargs['install_tag'], data_type='configure'))

return f


def initialize(*args: T.Any, **kwargs: T.Any) -> FSModule:
return FSModule(*args, **kwargs)
29 changes: 29 additions & 0 deletions test cases/common/14 configure file/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
project('configure file test', 'c', meson_version: '>=0.47.0')

fs = import('fs')

conf = configuration_data()

conf.set('var', 'mystring')
Expand Down Expand Up @@ -188,6 +190,9 @@ if ret.returncode() != 0
endif
# Now the same, but using a File object as an argument.
inf2 = files('invalid-utf8.bin.in')[0]
outf = configure_file(input : inf2,
output : 'invalid-utf8.bin',
copy: true)
ret = run_command(check_file, inf2, outf, check: false)
if ret.returncode() != 0
error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
Expand All @@ -202,6 +207,30 @@ if ret.returncode() != 0
error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

# Test the fs replacement
# Test copying of an empty configuration data object
inf = 'invalid-utf8.bin.in'
outf = fs.copyfile(inf, 'invalid-utf8.bin')
ret = run_command(check_file, inf, outf, check: false)
if ret.returncode() != 0
error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

# Now the same, but using a File object as an argument.
inf2 = files('invalid-utf8.bin.in')[0]
outf = fs.copyfile(inf2, 'invalid-utf8.bin')
ret = run_command(check_file, inf2, outf, check: false)
if ret.returncode() != 0
error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

# Test copy of a binary file
outf = fs.copyfile(inf, 'somebinary.bin')
ret = run_command(check_file, inf, outf, check: false)
if ret.returncode() != 0
error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

# Test non isolatin1 encoded input file which fails to decode with utf-8
conf8 = configuration_data()
conf8.set('var', 'foo')
Expand Down

0 comments on commit 4474301

Please sign in to comment.