2020-05-26 22:01:32 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
2020-10-28 22:52:48 +02:00
|
|
|
import tempfile
|
2020-05-26 22:01:32 +03:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import click
|
2021-04-21 20:51:54 +03:00
|
|
|
from tabulate import tabulate
|
2020-05-26 22:01:32 +03:00
|
|
|
|
2020-10-28 22:52:48 +02:00
|
|
|
from platformio import fs
|
2021-04-21 20:51:54 +03:00
|
|
|
from platformio.clients.account import AccountClient
|
2020-05-26 22:01:32 +03:00
|
|
|
from platformio.clients.registry import RegistryClient
|
2021-04-21 20:51:54 +03:00
|
|
|
from platformio.exception import UserSideException
|
|
|
|
from platformio.package.manifest.parser import ManifestParserFactory
|
2021-04-23 22:02:07 +03:00
|
|
|
from platformio.package.manifest.schema import ManifestSchema, ManifestValidationError
|
2020-07-31 15:42:26 +03:00
|
|
|
from platformio.package.meta import PackageSpec, PackageType
|
2020-05-26 22:01:32 +03:00
|
|
|
from platformio.package.pack import PackagePacker
|
2020-12-28 13:08:12 +02:00
|
|
|
from platformio.package.unpack import FileUnpacker, TARArchiver
|
2020-05-26 22:01:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
def validate_datetime(ctx, param, value): # pylint: disable=unused-argument
|
2020-05-27 22:30:16 +03:00
|
|
|
if not value:
|
|
|
|
return value
|
2020-05-26 22:01:32 +03:00
|
|
|
try:
|
|
|
|
datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
|
|
|
|
except ValueError as e:
|
|
|
|
raise click.BadParameter(e)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2021-04-23 22:02:07 +03:00
|
|
|
def load_manifest_from_archive(path):
|
|
|
|
return ManifestSchema().load_manifest(
|
|
|
|
ManifestParserFactory.new_from_archive(path).as_dict()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def check_package_duplicates(
|
2021-07-12 15:06:06 +03:00
|
|
|
owner, type, name, version, system
|
2021-04-23 22:02:07 +03:00
|
|
|
): # pylint: disable=redefined-builtin
|
2021-07-12 15:06:06 +03:00
|
|
|
found = False
|
2021-04-23 22:02:07 +03:00
|
|
|
items = (
|
|
|
|
RegistryClient()
|
|
|
|
.list_packages(filters=dict(types=[type], names=[name]))
|
|
|
|
.get("items")
|
|
|
|
)
|
|
|
|
if not items:
|
|
|
|
return True
|
2021-07-12 15:06:06 +03:00
|
|
|
# duplicated version by owner / system
|
|
|
|
found = False
|
|
|
|
for item in items:
|
|
|
|
if item["owner"]["username"] != owner or item["version"]["name"] != version:
|
|
|
|
continue
|
|
|
|
if not system:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
published_systems = []
|
|
|
|
for f in item["version"]["files"]:
|
|
|
|
published_systems.extend(f.get("system", []))
|
|
|
|
found = set(system).issubset(set(published_systems))
|
|
|
|
if found:
|
2021-04-23 22:02:07 +03:00
|
|
|
raise UserSideException(
|
|
|
|
"The package `%s/%s@%s` is already published in the registry"
|
|
|
|
% (owner, name, version)
|
|
|
|
)
|
|
|
|
other_owners = [
|
|
|
|
item["owner"]["username"]
|
|
|
|
for item in items
|
|
|
|
if item["owner"]["username"] != owner
|
|
|
|
]
|
|
|
|
if other_owners:
|
|
|
|
click.secho(
|
|
|
|
"\nWarning! A package with the name `%s` is already published by the next "
|
|
|
|
"owners: %s\n" % (name, ", ".join(other_owners)),
|
|
|
|
fg="yellow",
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-08-28 14:08:26 +03:00
|
|
|
@click.group("package", short_help="Package manager")
|
2020-05-26 22:01:32 +03:00
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-05-26 22:17:55 +03:00
|
|
|
@cli.command("pack", short_help="Create a tarball from a package")
|
2020-07-15 23:16:46 +03:00
|
|
|
@click.argument(
|
|
|
|
"package",
|
|
|
|
required=True,
|
|
|
|
default=os.getcwd,
|
|
|
|
metavar="<source directory, tar.gz or zip>",
|
|
|
|
)
|
2020-07-08 13:46:36 +03:00
|
|
|
@click.option(
|
|
|
|
"-o", "--output", help="A destination path (folder or a full path to file)"
|
|
|
|
)
|
|
|
|
def package_pack(package, output):
|
2020-05-26 22:17:55 +03:00
|
|
|
p = PackagePacker(package)
|
2020-07-08 13:46:36 +03:00
|
|
|
archive_path = p.pack(output)
|
2021-04-23 22:02:07 +03:00
|
|
|
# validate manifest
|
|
|
|
try:
|
|
|
|
load_manifest_from_archive(archive_path)
|
|
|
|
except ManifestValidationError as e:
|
|
|
|
os.remove(archive_path)
|
|
|
|
raise e
|
2020-05-27 01:10:35 +03:00
|
|
|
click.secho('Wrote a tarball to "%s"' % archive_path, fg="green")
|
2020-05-26 22:17:55 +03:00
|
|
|
|
|
|
|
|
2020-05-27 01:10:35 +03:00
|
|
|
@cli.command("publish", short_help="Publish a package to the registry")
|
2020-07-15 23:16:46 +03:00
|
|
|
@click.argument(
|
|
|
|
"package",
|
|
|
|
required=True,
|
|
|
|
default=os.getcwd,
|
|
|
|
metavar="<source directory, tar.gz or zip>",
|
|
|
|
)
|
2020-05-26 22:01:32 +03:00
|
|
|
@click.option(
|
|
|
|
"--owner",
|
2020-05-27 01:10:35 +03:00
|
|
|
help="PIO Account username (can be organization username). "
|
2020-05-26 22:01:32 +03:00
|
|
|
"Default is set to a username of the authorized PIO Account",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--released-at",
|
|
|
|
callback=validate_datetime,
|
|
|
|
help="Custom release date and time in the next format (UTC): 2014-06-13 17:08:52",
|
|
|
|
)
|
|
|
|
@click.option("--private", is_flag=True, help="Restricted access (not a public)")
|
2020-05-28 17:06:36 +03:00
|
|
|
@click.option(
|
|
|
|
"--notify/--no-notify",
|
|
|
|
default=True,
|
|
|
|
help="Notify by email when package is processed",
|
|
|
|
)
|
2021-04-21 20:51:54 +03:00
|
|
|
@click.option(
|
|
|
|
"--non-interactive",
|
|
|
|
is_flag=True,
|
|
|
|
help="Do not show interactive prompt",
|
|
|
|
)
|
|
|
|
def package_publish( # pylint: disable=too-many-arguments, too-many-locals
|
|
|
|
package, owner, released_at, private, notify, non_interactive
|
|
|
|
):
|
|
|
|
click.secho("Preparing a package...", fg="cyan")
|
|
|
|
owner = owner or AccountClient().get_logged_username()
|
|
|
|
do_not_pack = not os.path.isdir(package) and isinstance(
|
2020-12-28 13:08:12 +02:00
|
|
|
FileUnpacker.new_archiver(package), TARArchiver
|
2021-04-21 20:51:54 +03:00
|
|
|
)
|
|
|
|
archive_path = None
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir: # pylint: disable=no-member
|
|
|
|
# publish .tar.gz instantly without repacking
|
|
|
|
if do_not_pack:
|
|
|
|
archive_path = package
|
|
|
|
else:
|
|
|
|
with fs.cd(tmp_dir):
|
|
|
|
p = PackagePacker(package)
|
|
|
|
archive_path = p.pack()
|
|
|
|
|
|
|
|
type_ = PackageType.from_archive(archive_path)
|
2021-04-23 22:02:07 +03:00
|
|
|
manifest = load_manifest_from_archive(archive_path)
|
2021-04-21 20:51:54 +03:00
|
|
|
name = manifest.get("name")
|
|
|
|
version = manifest.get("version")
|
|
|
|
data = [
|
|
|
|
("Type:", type_),
|
|
|
|
("Owner:", owner),
|
|
|
|
("Name:", name),
|
|
|
|
("Version:", version),
|
|
|
|
]
|
2021-04-30 18:06:35 +03:00
|
|
|
if manifest.get("system"):
|
2021-05-01 13:44:28 +03:00
|
|
|
data.insert(len(data) - 1, ("System:", ", ".join(manifest.get("system"))))
|
2021-04-21 20:51:54 +03:00
|
|
|
click.echo(tabulate(data, tablefmt="plain"))
|
|
|
|
|
|
|
|
# look for duplicates
|
2021-07-12 15:06:06 +03:00
|
|
|
check_package_duplicates(owner, type_, name, version, manifest.get("system"))
|
2021-04-21 20:51:54 +03:00
|
|
|
|
|
|
|
if not non_interactive:
|
|
|
|
click.confirm(
|
|
|
|
"Are you sure you want to publish the %s %s to the registry?\n"
|
|
|
|
% (
|
|
|
|
type_,
|
|
|
|
click.style(
|
|
|
|
"%s/%s@%s" % (owner, name, version),
|
|
|
|
fg="cyan",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
abort=True,
|
|
|
|
)
|
|
|
|
|
2021-12-20 20:57:18 +02:00
|
|
|
click.secho(
|
|
|
|
"The package publishing may take some time depending "
|
|
|
|
"on your Internet connection and the package size.",
|
|
|
|
fg="yellow",
|
|
|
|
)
|
|
|
|
click.echo("Publishing...")
|
2020-12-28 13:08:12 +02:00
|
|
|
response = RegistryClient().publish_package(
|
2021-04-21 20:51:54 +03:00
|
|
|
owner, type_, archive_path, released_at, private, notify
|
2020-12-28 13:08:12 +02:00
|
|
|
)
|
2021-04-21 20:51:54 +03:00
|
|
|
if not do_not_pack:
|
|
|
|
os.remove(archive_path)
|
2020-12-28 13:08:12 +02:00
|
|
|
click.secho(response.get("message"), fg="green")
|
|
|
|
|
2021-04-21 20:51:54 +03:00
|
|
|
|
2020-05-27 01:10:35 +03:00
|
|
|
@cli.command("unpublish", short_help="Remove a pushed package from the registry")
|
2020-05-27 14:30:27 +03:00
|
|
|
@click.argument(
|
2020-07-14 21:07:09 +03:00
|
|
|
"package", required=True, metavar="[<organization>/]<pkgname>[@<version>]"
|
2020-05-27 14:30:27 +03:00
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--type",
|
|
|
|
type=click.Choice(list(PackageType.items().values())),
|
|
|
|
default="library",
|
|
|
|
help="Package type, default is set to `library`",
|
|
|
|
)
|
2020-05-27 01:10:35 +03:00
|
|
|
@click.option(
|
|
|
|
"--undo",
|
|
|
|
is_flag=True,
|
|
|
|
help="Undo a remove, putting a version back into the registry",
|
|
|
|
)
|
2020-05-27 14:30:27 +03:00
|
|
|
def package_unpublish(package, type, undo): # pylint: disable=redefined-builtin
|
2020-05-27 01:10:35 +03:00
|
|
|
spec = PackageSpec(package)
|
|
|
|
response = RegistryClient().unpublish_package(
|
2021-04-21 20:51:54 +03:00
|
|
|
owner=spec.owner or AccountClient().get_logged_username(),
|
2020-05-27 14:30:27 +03:00
|
|
|
type=type,
|
|
|
|
name=spec.name,
|
2020-09-09 12:17:09 +03:00
|
|
|
version=str(spec.requirements),
|
2020-05-27 14:30:27 +03:00
|
|
|
undo=undo,
|
2020-05-27 01:10:35 +03:00
|
|
|
)
|
|
|
|
click.secho(response.get("message"), fg="green")
|