aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/cmake/fileapi.py
diff options
context:
space:
mode:
authorDaniel Mensinger <daniel@mensinger-ka.de>2020-09-29 12:29:15 +0200
committerDaniel Mensinger <daniel@mensinger-ka.de>2020-10-04 10:45:48 +0200
commit77b5c82d0725eb914aa341a617dd9f418de60910 (patch)
treecfdb171f0193e5b74ec3aade2ceb6e6af5865e7f /mesonbuild/cmake/fileapi.py
parent10b44584ff0b1f49ece260a48f89eb59c123616f (diff)
downloadmeson-77b5c82d0725eb914aa341a617dd9f418de60910.zip
meson-77b5c82d0725eb914aa341a617dd9f418de60910.tar.gz
meson-77b5c82d0725eb914aa341a617dd9f418de60910.tar.bz2
cmake: switch to pathlib (fixes #7322)
Diffstat (limited to 'mesonbuild/cmake/fileapi.py')
-rw-r--r--mesonbuild/cmake/fileapi.py89
1 files changed, 43 insertions, 46 deletions
diff --git a/mesonbuild/cmake/fileapi.py b/mesonbuild/cmake/fileapi.py
index 0405145..4ef0caf 100644
--- a/mesonbuild/cmake/fileapi.py
+++ b/mesonbuild/cmake/fileapi.py
@@ -15,21 +15,21 @@
from .common import CMakeException, CMakeBuildFile, CMakeConfiguration
import typing as T
from .. import mlog
-import os
+from pathlib import Path
import json
import re
STRIP_KEYS = ['cmake', 'reply', 'backtrace', 'backtraceGraph', 'version']
class CMakeFileAPI:
- def __init__(self, build_dir: str):
- self.build_dir = build_dir
- self.api_base_dir = os.path.join(self.build_dir, '.cmake', 'api', 'v1')
- self.request_dir = os.path.join(self.api_base_dir, 'query', 'client-meson')
- self.reply_dir = os.path.join(self.api_base_dir, 'reply')
- self.cmake_sources = [] # type: T.List[CMakeBuildFile]
+ def __init__(self, build_dir: Path):
+ self.build_dir = build_dir
+ self.api_base_dir = self.build_dir / '.cmake' / 'api' / 'v1'
+ self.request_dir = self.api_base_dir / 'query' / 'client-meson'
+ self.reply_dir = self.api_base_dir / 'reply'
+ self.cmake_sources = [] # type: T.List[CMakeBuildFile]
self.cmake_configurations = [] # type: T.List[CMakeConfiguration]
- self.kind_resolver_map = {
+ self.kind_resolver_map = {
'codemodel': self._parse_codemodel,
'cmakeFiles': self._parse_cmakeFiles,
}
@@ -41,7 +41,7 @@ class CMakeFileAPI:
return self.cmake_configurations
def setup_request(self) -> None:
- os.makedirs(self.request_dir, exist_ok=True)
+ self.request_dir.mkdir(parents=True, exist_ok=True)
query = {
'requests': [
@@ -50,18 +50,17 @@ class CMakeFileAPI:
]
}
- with open(os.path.join(self.request_dir, 'query.json'), 'w') as fp:
- json.dump(query, fp, indent=2)
+ query_file = self.request_dir / 'query.json'
+ query_file.write_text(json.dumps(query, indent=2))
def load_reply(self) -> None:
- if not os.path.isdir(self.reply_dir):
+ if not self.reply_dir.is_dir():
raise CMakeException('No response from the CMake file API')
- files = os.listdir(self.reply_dir)
root = None
reg_index = re.compile(r'^index-.*\.json$')
- for i in files:
- if reg_index.match(i):
+ for i in self.reply_dir.iterdir():
+ if reg_index.match(i.name):
root = i
break
@@ -74,10 +73,9 @@ class CMakeFileAPI:
index = self._strip_data(index) # Strip unused data (again for loaded files)
# Debug output
- debug_json = os.path.normpath(os.path.join(self.build_dir, '..', 'fileAPI.json'))
- with open(debug_json, 'w') as fp:
- json.dump(index, fp, indent=2)
- mlog.cmd_ci_include(debug_json)
+ debug_json = self.build_dir / '..' / 'fileAPI.json'
+ debug_json.write_text(json.dumps(index, indent=2))
+ mlog.cmd_ci_include(debug_json.as_posix())
# parse the JSON
for i in index['objects']:
@@ -100,17 +98,17 @@ class CMakeFileAPI:
# resolved and the resulting data structure is identical
# to the CMake serve output.
- def helper_parse_dir(dir_entry: T.Dict[str, T.Any]) -> T.Tuple[str, str]:
- src_dir = dir_entry.get('source', '.')
- bld_dir = dir_entry.get('build', '.')
- src_dir = src_dir if os.path.isabs(src_dir) else os.path.join(source_dir, src_dir)
- bld_dir = bld_dir if os.path.isabs(bld_dir) else os.path.join(source_dir, bld_dir)
- src_dir = os.path.normpath(src_dir)
- bld_dir = os.path.normpath(bld_dir)
+ def helper_parse_dir(dir_entry: T.Dict[str, T.Any]) -> T.Tuple[Path, Path]:
+ src_dir = Path(dir_entry.get('source', '.'))
+ bld_dir = Path(dir_entry.get('build', '.'))
+ src_dir = src_dir if src_dir.is_absolute() else source_dir / src_dir
+ bld_dir = bld_dir if bld_dir.is_absolute() else build_dir / bld_dir
+ src_dir = src_dir.resolve()
+ bld_dir = bld_dir.resolve()
return src_dir, bld_dir
- def parse_sources(comp_group: T.Dict[str, T.Any], tgt: T.Dict[str, T.Any]) -> T.Tuple[T.List[str], T.List[str], T.List[int]]:
+ def parse_sources(comp_group: T.Dict[str, T.Any], tgt: T.Dict[str, T.Any]) -> T.Tuple[T.List[Path], T.List[Path], T.List[int]]:
gen = []
src = []
idx = []
@@ -120,9 +118,9 @@ class CMakeFileAPI:
if i >= len(src_list_raw) or 'path' not in src_list_raw[i]:
continue
if src_list_raw[i].get('isGenerated', False):
- gen += [src_list_raw[i]['path']]
+ gen += [Path(src_list_raw[i]['path'])]
else:
- src += [src_list_raw[i]['path']]
+ src += [Path(src_list_raw[i]['path'])]
idx += [i]
return src, gen, idx
@@ -133,8 +131,8 @@ class CMakeFileAPI:
# Parse install paths (if present)
install_paths = []
if 'install' in tgt:
- prefix = tgt['install']['prefix']['path']
- install_paths = [os.path.join(prefix, x['path']) for x in tgt['install']['destinations']]
+ prefix = Path(tgt['install']['prefix']['path'])
+ install_paths = [prefix / x['path'] for x in tgt['install']['destinations']]
install_paths = list(set(install_paths))
# On the first look, it looks really nice that the CMake devs have
@@ -160,7 +158,7 @@ class CMakeFileAPI:
# maybe we can make use of that in addition to the
# implicit dependency detection
tgt_data = {
- 'artifacts': [x.get('path', '') for x in tgt.get('artifacts', [])],
+ 'artifacts': [Path(x.get('path', '')) for x in tgt.get('artifacts', [])],
'sourceDirectory': src_dir,
'buildDirectory': bld_dir,
'name': tgt.get('name', ''),
@@ -269,14 +267,14 @@ class CMakeFileAPI:
self.cmake_configurations += [CMakeConfiguration(cnf_data)]
def _parse_cmakeFiles(self, data: T.Dict[str, T.Any]) -> None:
- assert('inputs' in data)
- assert('paths' in data)
+ assert 'inputs' in data
+ assert 'paths' in data
- src_dir = data['paths']['source']
+ src_dir = Path(data['paths']['source'])
for i in data['inputs']:
- path = i['path']
- path = path if os.path.isabs(path) else os.path.join(src_dir, path)
+ path = Path(i['path'])
+ path = path if path.is_absolute() else src_dir / path
self.cmake_sources += [CMakeBuildFile(path, i.get('isCMake', False), i.get('isGenerated', False))]
def _strip_data(self, data: T.Any) -> T.Any:
@@ -309,14 +307,13 @@ class CMakeFileAPI:
return data
- def _reply_file_content(self, filename: str) -> T.Dict[str, T.Any]:
- real_path = os.path.join(self.reply_dir, filename)
- if not os.path.exists(real_path):
+ def _reply_file_content(self, filename: Path) -> T.Dict[str, T.Any]:
+ real_path = self.reply_dir / filename
+ if not real_path.exists():
raise CMakeException('File "{}" does not exist'.format(real_path))
- with open(real_path, 'r') as fp:
- data = json.load(fp)
- assert isinstance(data, dict)
- for i in data.keys():
- assert isinstance(i, str)
- return data
+ data = json.loads(real_path.read_text())
+ assert isinstance(data, dict)
+ for i in data.keys():
+ assert isinstance(i, str)
+ return data