Commit Graph

52 Commits

Author SHA1 Message Date
Tristan Partin 6a119256a1 Add support for BuildTargetTypes to various fs module functions
The new support was added to fs.name, fs.parent, fs.replace_suffix, and
fs.stem.
2024-02-24 13:09:24 -08:00
Tristan Partin 0b19d1c015 Correct fs.read function name in exception message 2024-02-24 13:09:24 -08:00
Tristan Partin 35c6052723 Fix fs module FeatureNew prefixes to match the function name
They were appending _file to the function name when they were meant to
suggest that the function now supports file objects.
2024-02-24 13:09:24 -08:00
Tristan Partin 9cb44c9c58 Fix bad escape character in doc comment 2024-02-24 13:09:24 -08:00
Tristan Partin db8246b3fc
Alphabetize the fs module methods dict 2024-02-23 13:32:50 -05:00
Tristan Partin 1b40fb1089
Add location nodes to FeatureNews in the fs module 2024-02-23 13:31:08 -05:00
Dylan Baker 37883e7d5f modules/fs: use ModuleState object instead of Interpreter when possible 2024-01-25 10:01:50 -08:00
Dylan Baker e991c4d454 Use SPDX-License-Identifier consistently
This replaces all of the Apache blurbs at the start of each file with an
`# SPDX-License-Identifier: Apache-2.0` string. It also fixes existing
uses to be consistent in capitalization, and to be placed above any
copyright notices.

This removes nearly 3000 lines of boilerplate from the project (only
python files), which no developer cares to look at.

SPDX is in common use, particularly in the Linux kernel, and is the
recommended format for Meson's own `project(license: )` field
2023-12-13 15:19:21 -05:00
Tristan Partin 946a3561c2 Revert "Revert "Add fs.relative_to()""
This reverts commit 84c8905d52.

Fixed the cygwin failure...
2023-08-18 07:46:08 -04:00
Charles Brunet 7cbe37ebd9 Add more descriptive description to CustomTarget
Allow modules using CustomTarget to modify the command description used by ninja backend. This result in more precise logs when building a project.
2023-08-18 00:36:49 -04:00
Eli Schwartz 84c8905d52
Revert "Add fs.relative_to()"
This reverts commit f52bcaa27f.

It did not pass CI, and was merged anyway because there were two CI
errors in the same cygwin job. The other error was not the fault of this
commit, and since cygwin errors were glossed over because they were
"expected", the presence of a new error *added* by this commit was
overlooked.

Per the meson development policy, PRs which result in CI errors
can/should be reverted at will, no questions asked.
2023-08-17 23:56:51 -04:00
Eli Schwartz e313f78465
fix style errors in fs.relative_to implementation
In commit f52bcaa27f a few issues were
added:
- doc typo
- imports for utils.universal are not intended to be directly used, it's
  an internal wrapper that exists solely to make mesonlib work well as
  it always did while simultaneously allowing `meson --internal`
  codepaths to avoid importing anything other than an extremely stripped
  down core
- type annotation specific import was imported at runtime scope
2023-08-17 18:01:41 -04:00
Tristan Partin f52bcaa27f Add fs.relative_to()
Returns a relative path from arg 2 to arg 1 similar to
os.path.relpath().
2023-08-17 17:05:49 -04:00
Xavier Claessens f88a9cd6b3 fs.read(): Catch FileNotFoundError 2023-08-16 14:48:09 -07:00
Eli Schwartz 100456de07
fix broken fs.copyfile function that crashed if you tried to use it
At least, if you tried to use it when passing an install_dir. Because
T.Sequence is horrible and we should never use it, and the annotations
are a lie that produces bugs.

So, fix the annotations on CustomTarget to never allow this to happen
again, and also fix the function too. Move some definitions elsewhere
inline to satisfy the linter.

Fixes #11157
2022-12-11 18:28:39 -05:00
Eli Schwartz cbf4496434
simplify install_tag handling according to the accepted API
There are two problems here: a typing problem, and an algorithm problem.

We expect it to always be passed to CustomTarget() as a list, but we ran
list() on it, which became horribly mangled if you violated the types
and passed a string instead. This caused weird*er* errors and didn't
even do anything. We want to do all validation in the interpreter,
anyway, and make the build level dumb.

Meanwhile we type it as accepting a T.Sequence, which technically
permits... a string, actually. This isn't intentional; the point of
using T.Sequence is out of a misguided idea that APIs are supposed to be
"technically correct" by allowing "anything that fulfills an interface",
which is a flawed concept because we aren't using interfaces here, and
also because "technically string fulfills the same interface as a list,
if we're talking sequences".

Basically:
- mypy is broken by design, because it typechecks "python", not "what we
  wish python to be"
- we do not actually need to graciously permit passing tuples instead of
  lists

As far as historic implementations of this logic go, we have formerly:
- originally, typeslistified anything
- switched to accepting list from the interpreter, redundantly ran list()
  on the list we got, and mishandling API violations passing a string
  (commit 11f9638035)
- switched to accepting anything, stringlistifying it if it was not
  `None`, mishandling `[None]`, and invoking list(x) on a brand new list
  from stringlistify (commit 157d438835)
- stopped stringlistify, just accept T.List[str | None] and re-cast to
  list, violates typing because we use/handle plain None too
  (commit a8521fef70)
- break typing by declaring we accept a simple string, which still
  results in mishandling by converting 'foo' -> ['f', 'o', 'o']
  (commit ac576530c4)

All of this. ALL of it. Is because we tried to be fancy and say we
accept T.Tuple; the only version of this logic that has ever worked
correctly is the original untyped do-all-validation-in-the-build-phase
typeslistified version.

Let's just call it what it is. We want a list | None, and we handle it too.
2022-12-11 18:28:38 -05:00
Dylan Baker 991baf56e9 modules/fs: Replace configure_file(copy:) with fs.copyfile
`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.

This new version works at build time rather than configure time, which
has the disadvantage it can't be passed to `run_command`, but with the
advantage that changes to the input don't require a full reconfigure.
2022-08-18 16:53:36 -04:00
Dylan Baker 6843f56f6b modules: use module level information about new and deprecation
Instead of using FeatureNew/FeatureDeprecated in the module.

The goal here is to be able to handle information about modules in a
single place, instead of having to handle it separately. Each module
simply defines some metadata, and then the interpreter handles the rest.
2022-08-17 16:25:36 -04:00
Eli Schwartz c9938f8f60
move a bunch of imports into TYPE_CHECKING blocks
These are only used for type checking, so don't bother importing them at
runtime.

Generally add future annotations at the same time, to make sure that
existing uses of these imports don't need to be quoted.
2022-03-29 16:44:54 -04:00
Eli Schwartz 688b4bac76 add FeatureNew decorators for various modules that were lacking them
Going back to 0.38, though some of them are far older. The original
implementation of FeatureNew only added backdated feature checks that
far, anyway.
2021-12-31 12:03:57 +02:00
Dylan Baker 6aef800ba8 modules/fs: Use typed_kwargs 2021-05-30 23:32:15 -07:00
Xavier Claessens 2e02ef6592 modules: Add methods dict everywhere
This fix calling random internal methods from meson.build as long as
they were not prefixed by underscore.
2021-05-28 15:17:10 -04:00
Xavier Claessens a734bcfc83 modules: Stop using ModuleReturnValue where it's not needed
It is only needed in functions that need to add targets to the
interpreter.
2021-05-28 15:17:10 -04:00
Xavier Claessens 723c5227a4 modules: Remove snippet methods
The only advantage they have is they have the interpreter in arguments,
but it's already available as self.interpreter. We should discourage
usage of the interpreter API and rely on ModuleState object instead in
the future.

This also lift the restriction that a module method cannot add build
targets, but that was not enforced for snippet methods anyway (and some
modules were doing it) and it's really loose restriction as it should
check for many other things if we wanted to make it consistent.
2021-05-28 15:17:10 -04:00
Florian Fischer 247630b981 modules/fs: support FileOrString arguments
With this change File objects created with the builtin files() function
can be used with the fs submodule like normal strings.

All methods that seem reasonable support FileOrSting arguments.
For example fs.exists() still only takes str arguments because meson
already ensures that File objects do exist when creating them with files().

Each user facing function of the fs module has an additional FeatureNew
check when used with File objects.

The test cases for fs are extended appropriately with tests for File objects.
2021-05-08 21:34:00 +03:00
Eli Schwartz 6a0fabc647
mass rewrite of string formatting to use f-strings everywhere
performed by running "pyupgrade --py36-plus" and committing the results
2021-03-04 17:16:11 -05:00
Eli Schwartz 4340bf34fa
various python neatness cleanups
All changes were created by running

"pyupgrade --py3-only --keep-percent-format"

and committing the results. I have not touched string formatting for
now.

- use set literals
- simplify .format() parameter naming
- remove __future__
- remove default "r" mode for open()
- use OSError rather than compatibility aliases
- remove stray parentheses in function(generator) scopes
2021-03-04 17:11:26 -05:00
Xavier Claessens ba9bfd2bd8 Simplify module API
- ModuleState is now a real class that will have methods in the future
  for actions modules needs, instead of using interpreter internal API.
- New ModuleObject base class, similar to InterpreterObject, that should
  be used by all objects returned by modules. Its methods gets the
  ModuleState passed as first argument. It has a `methods` dictionary to
  define what is public API that can be called from build definition.
- Method return value is not required to be a ModuleReturnValue any
  more, it can be any type that interpreter can holderify, including
  ModuleObject.
- Legacy module API is maintained until we port all modules.

In the future modules should be updated:
- Use methods dict.
- Remove snippets.
- Custom objects returned by modules should all be subclass of
  ModuleObject to get the state iface in their methods.
- Modules should never call into interpreter directly and instead state
  object should have wrapper API.
- Stop using ModuleReturnValue in methods that just return simple
  objects like strings. Possibly remove ModuleReturnValue completely
  since all objects that needs to be processed by interpreter (e.g.
  CustomTarget) should be created through ModuleState API.
2021-03-04 11:33:22 -05:00
Dylan Baker fad0a498fb modules/fs: Use typed_pos_args 2021-02-26 09:34:27 -08:00
Luke Drummond 46e3480f7c Introduce `fs.read` to read a file as a string
Following #7890, this patch introduces the ability to read the contents
of a file to the fs module.

This patch introduces the ability to read files at configure time, but
has some restrictions:
    - binary files are not supported (I don't think this will prove a
    problem, and if people are wanting to do something with binary
    files, they should probably be shelling out to their own script).
    - Only files outside the build directory allowed. This limitation
      should prevent build loops.
Given that reading an arbitrary file at configure time can affect the
configuration in almost arbitrary ways, meson should force a reconfigure
when the given file changes. This is non-configurable, but this can
easily be changed with a future keyword argument.
2021-02-04 17:34:11 +00:00
Antonin Décimo 39ede12aa5 Fix misspells
Signed-off-by: Antonin Décimo <antonin.decimo@gmail.com>
2021-01-13 12:53:10 -05:00
Dylan Baker f6672c7a19 use real pathlib module
We added the _pathlib module to work around defeciencies in python 3.5's
implementation, since we now rely on 3.6 lets drop this
2020-11-20 15:08:40 -08:00
Daniel Mensinger 1dfaccfd91 pathlib: Fix resolve() by overriding it in Python 3.5 2020-10-04 10:45:48 +02:00
Daniel Mensinger 6b1b995b32
typing: fully annotate fs module 2020-09-08 20:15:57 +02:00
Michael Hirsch, Ph.D 2bbd57092f add FeatureNew 2020-02-06 12:54:38 -05:00
Michael Hirsch, Ph.D 5bbeab8ed4 add fs.stem() 2020-02-06 12:54:38 -05:00
Michael Hirsch, Ph.D dcb7043403 fs: add expanduser method
this should help users specify leading `~` in various Meson options and variables
without refactoring lots of places inside Meson itself.
2020-02-06 12:54:38 -05:00
Michael Hirsch, Ph.D 4556343d95 fs: add methods as_posix, is_absolute
fs: make exception specify method name

fs: actually raise exceptions

fs: resolve path e.g.  /opt/foo/.. => /opt/foo

fs: correct behavior of is_symlink
2020-02-06 12:54:38 -05:00
Daniel Mensinger 09b53c534f types: import typing as T (fixes #6333) 2020-01-08 15:28:17 +01:00
Michael Hirsch, Ph.D fb121f6254 fs: rename samefile => is_samepath
is_samepath better reflects the nature of this function--that files
and directories can be compared.

Also, instead of raising exceptions, simply return False when one
or both .is_samepath(path1, path1) don't exist. This is more
intuitive behavior and avoids having an extra if fs.exist() to go
with every fs.is_samepath()
2019-12-19 08:51:31 -05:00
Michael Brockus 7d162487e3 Add raise at the start of MesonException 2019-12-04 10:54:24 +02:00
Xavier Claessens a6f7a1d8c4 fs: Add parent() and name() methods 2019-11-25 14:55:19 -05:00
Michael Hirsch, Ph.D 0cb48cdc79
fs: make replace_suffix not expand file to absolute path, just manipulate the string 2019-11-17 00:22:53 -05:00
Michael Hirsch, Ph.D 2ae96f8595
fs: replace_suffix 2019-11-17 00:17:06 -05:00
Michael Hirsch, Ph.D a320274179
fs: get file size
fs: add samefile
2019-11-17 00:17:04 -05:00
Michael Hirsch, Ph.D 67651271f6
fs: add hash compute method 2019-11-17 00:17:04 -05:00
Michael Hirsch, Ph.D 052d918908
add fs.with_suffix 2019-11-17 00:17:02 -05:00
Michael Hirsch, Ph.D dc8e8f0644
fs: improve exception feedback 2019-11-17 00:17:02 -05:00
Michael Hirsch, Ph.D 4adfd921ae
fs: use expanduser 2019-11-17 00:17:01 -05:00
Michael Hirsch, Ph.D 9fc76b0323
fs: deduplicate functions 2019-11-17 00:17:01 -05:00