mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 01:57:13 +02:00
Implement "ManifestParserFactory.new_from_archive" API
This commit is contained in:
@ -17,6 +17,7 @@ import io
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import tarfile
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@ -109,6 +110,17 @@ class ManifestParserFactory(object):
|
|||||||
remote_url,
|
remote_url,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def new_from_archive(path):
|
||||||
|
assert path.endswith("tar.gz")
|
||||||
|
with tarfile.open(path, mode="r:gz") as tf:
|
||||||
|
for t in sorted(ManifestFileType.items().values()):
|
||||||
|
try:
|
||||||
|
return ManifestParserFactory.new(tf.extractfile(t).read(), t)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
raise UnknownManifestError("Unknown manifest file type in %s archive" % path)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def new( # pylint: disable=redefined-builtin
|
def new( # pylint: disable=redefined-builtin
|
||||||
contents, type, remote_url=None, package_dir=None
|
contents, type, remote_url=None, package_dir=None
|
||||||
|
@ -43,7 +43,7 @@ class PackageType(object):
|
|||||||
def from_archive(cls, path):
|
def from_archive(cls, path):
|
||||||
assert path.endswith("tar.gz")
|
assert path.endswith("tar.gz")
|
||||||
manifest_map = cls.get_manifest_map()
|
manifest_map = cls.get_manifest_map()
|
||||||
with tarfile.open(path, mode="r|gz") as tf:
|
with tarfile.open(path, mode="r:gz") as tf:
|
||||||
for t in sorted(cls.items().values()):
|
for t in sorted(cls.items().values()):
|
||||||
for manifest in manifest_map[t]:
|
for manifest in manifest_map[t]:
|
||||||
try:
|
try:
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import tarfile
|
||||||
|
|
||||||
import jsondiff
|
import jsondiff
|
||||||
import pytest
|
import pytest
|
||||||
@ -790,6 +791,21 @@ def test_examples_from_dir(tmpdir_factory):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parser_from_archive(tmpdir_factory):
|
||||||
|
pkg_dir = tmpdir_factory.mktemp("package")
|
||||||
|
pkg_dir.join("package.json").write('{"name": "package.json"}')
|
||||||
|
pkg_dir.join("library.json").write('{"name": "library.json"}')
|
||||||
|
pkg_dir.join("library.properties").write("name=library.properties")
|
||||||
|
|
||||||
|
archive_path = os.path.join(str(pkg_dir), "package.tar.gz")
|
||||||
|
with tarfile.open(archive_path, mode="w|gz") as tf:
|
||||||
|
for item in os.listdir(str(pkg_dir)):
|
||||||
|
tf.add(os.path.join(str(pkg_dir), item), item)
|
||||||
|
|
||||||
|
data = parser.ManifestParserFactory.new_from_archive(archive_path).as_dict()
|
||||||
|
assert data["name"] == "library.json"
|
||||||
|
|
||||||
|
|
||||||
def test_broken_schemas():
|
def test_broken_schemas():
|
||||||
# missing required field
|
# missing required field
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
|
Reference in New Issue
Block a user