Better cleanup package manifest fields

This commit is contained in:
Ivan Kravets
2021-11-26 14:13:06 +02:00
parent f63d899c42
commit 6753121a6a
3 changed files with 36 additions and 44 deletions

View File

@ -334,7 +334,7 @@ class LibraryJsonManifestParser(BaseManifestParser):
if "authors" in data: if "authors" in data:
data["authors"] = self._parse_authors(data["authors"]) data["authors"] = self._parse_authors(data["authors"])
if "platforms" in data: if "platforms" in data:
data["platforms"] = self._parse_platforms(data["platforms"]) or None data["platforms"] = self._fix_platforms(data["platforms"]) or None
if "export" in data: if "export" in data:
data["export"] = self._parse_export(data["export"]) data["export"] = self._parse_export(data["export"])
if "dependencies" in data: if "dependencies" in data:
@ -367,16 +367,11 @@ class LibraryJsonManifestParser(BaseManifestParser):
return [self.cleanup_author(author) for author in raw] return [self.cleanup_author(author) for author in raw]
@staticmethod @staticmethod
def _parse_platforms(raw): def _fix_platforms(items):
assert isinstance(raw, list) assert isinstance(items, list)
result = [] if "espressif" in items:
# renamed platforms items[items.index("espressif")] = "espressif8266"
for item in raw: return items
if item == "espressif":
item = "espressif8266"
if item not in result:
result.append(item)
return result
@staticmethod @staticmethod
def _parse_export(raw): def _parse_export(raw):
@ -485,7 +480,7 @@ class LibraryPropertiesManifestParser(BaseManifestParser):
repository=repository or None, repository=repository or None,
description=self._parse_description(data), description=self._parse_description(data),
platforms=self._parse_platforms(data) or ["*"], platforms=self._parse_platforms(data) or ["*"],
keywords=self._parse_keywords(data), keywords=self._parse_keywords(data) or None,
export=self._parse_export(), export=self._parse_export(),
) )
) )
@ -527,18 +522,17 @@ class LibraryPropertiesManifestParser(BaseManifestParser):
lines[0] += "." lines[0] += "."
return " ".join(lines) return " ".join(lines)
@staticmethod def _parse_keywords(self, properties):
def _parse_keywords(properties): return self.str_to_list(
result = [] re.split(
for item in re.split(r"[\s/]+", properties.get("category", "uncategorized")): r"[\s/]+",
item = item.strip() properties.get("category", ""),
if not item: ),
continue lowercase=True,
result.append(item.lower()) unique=True,
return result )
@staticmethod def _parse_platforms(self, properties):
def _parse_platforms(properties):
result = [] result = []
platforms_map = { platforms_map = {
"avr": "atmelavr", "avr": "atmelavr",
@ -557,9 +551,9 @@ class LibraryPropertiesManifestParser(BaseManifestParser):
continue continue
if arch == "*": if arch == "*":
return ["*"] return ["*"]
if arch in platforms_map and platforms_map[arch] not in result: if arch in platforms_map:
result.append(platforms_map[arch]) result.append(platforms_map[arch])
return result return self.str_to_list(result, lowercase=True, unique=True)
def _parse_authors(self, properties): def _parse_authors(self, properties):
if "author" not in properties: if "author" not in properties:
@ -655,19 +649,17 @@ class PlatformJsonManifestParser(BaseManifestParser):
def parse(self, contents): def parse(self, contents):
data = json.loads(contents) data = json.loads(contents)
if "keywords" in data: if "keywords" in data:
data["keywords"] = self.str_to_list(data["keywords"], sep=",", unique=True) data["keywords"] = self.str_to_list(
if "frameworks" in data: data["keywords"], sep=",", lowercase=True, unique=True
data["frameworks"] = self._parse_frameworks(data["frameworks"]) )
if "frameworks" in data and not isinstance(data["frameworks"], dict):
data["frameworks"] = self.str_to_list(
data["frameworks"].keys(), lowercase=True, unique=True
)
if "packages" in data: if "packages" in data:
data["dependencies"] = self._parse_dependencies(data["packages"]) data["dependencies"] = self._parse_dependencies(data["packages"])
return data return data
@staticmethod
def _parse_frameworks(raw):
if not isinstance(raw, dict):
return None
return [name.lower() for name in raw.keys()]
@staticmethod @staticmethod
def _parse_dependencies(raw): def _parse_dependencies(raw):
result = [] result = []
@ -694,16 +686,13 @@ class PackageJsonManifestParser(BaseManifestParser):
data = self._parse_repository(data) data = self._parse_repository(data)
return data return data
@staticmethod def _parse_system(self, data):
def _parse_system(data):
if "system" not in data: if "system" not in data:
return data return data
if data["system"] in ("*", ["*"], "all"): if data["system"] in ("*", ["*"], "all"):
del data["system"] del data["system"]
return data return data
if not isinstance(data["system"], list): data["system"] = self.str_to_list(data["system"], lowercase=True, unique=True)
data["system"] = [data["system"]]
data["system"] = [s.strip().lower() for s in data["system"]]
return data return data
@staticmethod @staticmethod

View File

@ -202,7 +202,7 @@ def test_install_from_registry(isolated_pio_core, tmpdir_factory):
lm.install("AsyncMqttClient-esphome @ 0.8.4", silent=True) lm.install("AsyncMqttClient-esphome @ 0.8.4", silent=True)
assert len(lm.get_installed()) == 3 assert len(lm.get_installed()) == 3
pkg = lm.get_package("AsyncTCP-esphome") pkg = lm.get_package("AsyncTCP-esphome")
assert pkg.metadata.spec.owner == "ottowinter" assert pkg.metadata.spec.owner == "esphome"
assert not lm.get_package("non-existing-package") assert not lm.get_package("non-existing-package")
# mbed library # mbed library
assert lm.install("wolfSSL", silent=True) assert lm.install("wolfSSL", silent=True)

View File

@ -206,6 +206,7 @@ version=1.2.3
author=SomeAuthor <info AT author.com>, Maintainer Author (nickname) <www.example.com> author=SomeAuthor <info AT author.com>, Maintainer Author (nickname) <www.example.com>
maintainer=Maintainer Author (nickname) <www.example.com> maintainer=Maintainer Author (nickname) <www.example.com>
sentence=This is Arduino library sentence=This is Arduino library
category=Signal Input/Output
customField=Custom Value customField=Custom Value
depends=First Library (=2.0.0), Second Library (>=1.2.0), Third depends=First Library (=2.0.0), Second Library (>=1.2.0), Third
ignore_empty_field= ignore_empty_field=
@ -229,7 +230,8 @@ includes=Arduino.h, Arduino Space.hpp
{"name": "SomeAuthor", "email": "info@author.com"}, {"name": "SomeAuthor", "email": "info@author.com"},
{"name": "Maintainer Author", "maintainer": True}, {"name": "Maintainer Author", "maintainer": True},
], ],
"keywords": ["uncategorized"], "category": "Signal Input/Output",
"keywords": ["signal", "input", "output"],
"headers": ["Arduino.h", "Arduino Space.hpp"], "headers": ["Arduino.h", "Arduino Space.hpp"],
"customField": "Custom Value", "customField": "Custom Value",
"depends": "First Library (=2.0.0), Second Library (>=1.2.0), Third", "depends": "First Library (=2.0.0), Second Library (>=1.2.0), Third",
@ -297,6 +299,7 @@ maintainer=Rocket Scream Electronics
assert data["authors"] == [ assert data["authors"] == [
{"name": "Rocket Scream Electronics", "maintainer": True} {"name": "Rocket Scream Electronics", "maintainer": True}
] ]
assert "keywords" not in data
def test_library_json_schema(): def test_library_json_schema():
@ -559,7 +562,7 @@ def test_platform_json_schema():
"name": "atmelavr", "name": "atmelavr",
"title": "Atmel AVR", "title": "Atmel AVR",
"description": "Atmel AVR 8- and 32-bit MCUs deliver a unique combination of performance, power efficiency and design flexibility. Optimized to speed time to market-and easily adapt to new ones-they are based on the industrys most code-efficient architecture for C and assembly programming.", "description": "Atmel AVR 8- and 32-bit MCUs deliver a unique combination of performance, power efficiency and design flexibility. Optimized to speed time to market-and easily adapt to new ones-they are based on the industrys most code-efficient architecture for C and assembly programming.",
"keywords": "arduino, atmel, avr", "keywords": "arduino, atmel, avr, MCU",
"homepage": "http://www.atmel.com/products/microcontrollers/avr/default.aspx", "homepage": "http://www.atmel.com/products/microcontrollers/avr/default.aspx",
"license": "Apache-2.0", "license": "Apache-2.0",
"engines": { "engines": {
@ -619,7 +622,7 @@ def test_platform_json_schema():
"on the industrys most code-efficient architecture for C and " "on the industrys most code-efficient architecture for C and "
"assembly programming." "assembly programming."
), ),
"keywords": ["arduino", "atmel", "avr"], "keywords": ["arduino", "atmel", "avr", "mcu"],
"homepage": "http://www.atmel.com/products/microcontrollers/avr/default.aspx", "homepage": "http://www.atmel.com/products/microcontrollers/avr/default.aspx",
"license": "Apache-2.0", "license": "Apache-2.0",
"repository": { "repository": {
@ -648,7 +651,7 @@ def test_package_json_schema():
"description": "SCons software construction tool", "description": "SCons software construction tool",
"keywords": "SCons, build", "keywords": "SCons, build",
"homepage": "http://www.scons.org", "homepage": "http://www.scons.org",
"system": ["linux_armv6l", "linux_armv7l", "linux_armv8l"], "system": ["linux_armv6l", "linux_armv7l", "linux_armv8l", "LINUX_ARMV7L"],
"version": "3.30101.0" "version": "3.30101.0"
} }
""" """