Skip to content

Commit

Permalink
manifest: add new Manifest.encoding constant = 'utf-8'
Browse files Browse the repository at this point in the history
No functional change: just a lot less duplication & hardcoding.

This can help testing and supporting other encodings. It does not hurt
in any case.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
  • Loading branch information
marc-hb committed May 28, 2024
1 parent 94dcb58 commit 645e7a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ def update_importer(self, project, path):
self.updated.add(project.name)

try:
return _manifest_content_at(project, path)
return _manifest_content_at(project, path, Manifest.encoding)
except FileNotFoundError:
# FIXME we need each project to have back-pointers
# to the manifest file where it was defined, so we can
Expand Down
17 changes: 10 additions & 7 deletions src/west/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def _west_commands_merge(wc1: List[str], wc2: List[str]) -> List[str]:
def _default_importer(project: 'Project', file: str) -> NoReturn:
raise ManifestImportFailed(project, file)

def _manifest_content_at(project: 'Project', path: PathType,
def _manifest_content_at(project: 'Project', path: PathType, mf_encoding: str,
rev: str = QUAL_MANIFEST_REV_BRANCH) \
-> ImportedContentType:
# Get a list of manifest data from project at path
Expand Down Expand Up @@ -278,7 +278,7 @@ def _manifest_content_at(project: 'Project', path: PathType,

if ptype == 'blob':
# Importing a file: just return its content.
return project.read_at(path, rev=rev).decode('utf-8')
return project.read_at(path, rev=rev).decode(mf_encoding)
elif ptype == 'tree':
# Importing a tree: return the content of the YAML files inside it.
ret = []
Expand All @@ -287,7 +287,7 @@ def _manifest_content_at(project: 'Project', path: PathType,
pathobj = PurePosixPath(path)
for f in filter(_is_yml, project.listdir_at(path, rev=rev,
encoding=git_filenames_encoding)):
ret.append(project.read_at(pathobj / f, rev=rev).decode('utf-8'))
ret.append(project.read_at(pathobj / f, rev=rev).decode(mf_encoding))
return ret
else:
raise MalformedManifest(f"can't decipher project {project.name} "
Expand Down Expand Up @@ -1187,6 +1187,9 @@ class Manifest:
'''The parsed contents of a west manifest file.
'''

# TODO: make this a new west config: 'manifest.encoding'
encoding: str = 'utf-8'

@staticmethod
def from_topdir(topdir: Optional[PathType] = None,
config: Optional[Configuration] = None,
Expand Down Expand Up @@ -1905,7 +1908,7 @@ def get_option(option, default=None):
current_relpath = manifest_path / manifest_file
current_abspath = topdir_abspath / current_relpath
try:
current_data = current_abspath.read_text(encoding='utf-8')
current_data = current_abspath.read_text(encoding=Manifest.encoding)
except FileNotFoundError:
raise MalformedConfig(
f'file not found: manifest file {current_abspath} '
Expand Down Expand Up @@ -2204,7 +2207,7 @@ def _import_pathobj_from_self(self, pathobj_abs: Path,
child_ctx = self._ctx._replace(
current_abspath=pathobj_abs,
current_relpath=pathobj,
current_data=pathobj_abs.read_text(encoding='utf-8')
current_data=pathobj_abs.read_text(encoding=Manifest.encoding)
)
try:
Manifest(topdir=self.topdir, internal_import_ctx=child_ctx)
Expand Down Expand Up @@ -2242,7 +2245,7 @@ def _import_map_from_self(self, imp: Dict) -> None:
path_prefix=path_prefix,
current_abspath=import_abs,
current_relpath=pathobj / import_abs.name,
current_data=import_abs.read_text(encoding='utf-8')
current_data=import_abs.read_text(encoding=Manifest.encoding)
)
try:
Manifest(topdir=self.topdir, internal_import_ctx=child_ctx)
Expand Down Expand Up @@ -2575,7 +2578,7 @@ def _import_content_from_project(self, project: Project,
if not (self._ctx.import_flags & ImportFlag.FORCE_PROJECTS) and \
project.is_cloned():
try:
content = _manifest_content_at(project, path)
content = _manifest_content_at(project, path, Manifest.encoding)
except MalformedManifest as mm:
self._malformed(mm.args[0])
except FileNotFoundError:
Expand Down

0 comments on commit 645e7a3

Please sign in to comment.