From e6ec09cde76aa65f99669bceefd1c66de914bcec Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 16 Jun 2020 11:50:50 +0200 Subject: [PATCH 1/5] Add a command to publish a crate on crates.io --- taskboot/cargo.py | 31 +++++++++++++++++++++++++++++++ taskboot/cli.py | 7 +++++++ taskboot/config.py | 6 ++++++ 3 files changed, 44 insertions(+) create mode 100644 taskboot/cargo.py diff --git a/taskboot/cargo.py b/taskboot/cargo.py new file mode 100644 index 0000000..ddcdd1a --- /dev/null +++ b/taskboot/cargo.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import logging +import subprocess + +from taskboot.config import Configuration + +logger = logging.getLogger(__name__) + + +def cargo_publish(target, args): + """ + Publish a crate on crates.io + """ + + # Load config from file/secret + config = Configuration(args) + assert config.has_cargo_auth(), "Missing Cargo authentication" + + # Check if the crate to be published contains some errors + subprocess.run(["cargo", "publish", "--dry-run"]) + + # Publish the crate on crates.io + error = subprocess.run( + ["cargo", "publish", "--token", config.cargo["token"]], capture_output=True + ) + if error.returncode != 0: + raise Exception(f"Failed to publish the crate") diff --git a/taskboot/cli.py b/taskboot/cli.py index 35ff9d8..5cff02d 100644 --- a/taskboot/cli.py +++ b/taskboot/cli.py @@ -13,6 +13,7 @@ from taskboot.build import build_compose from taskboot.build import build_hook from taskboot.build import build_image +from taskboot.cargo import cargo_publish from taskboot.github import github_release from taskboot.push import heroku_release from taskboot.push import push_artifacts @@ -301,6 +302,12 @@ def main(): ) github_release_cmd.set_defaults(func=github_release) + # Publish on crates.io + cargo_publish_cmd = commands.add_parser( + "cargo-publish", help="Publish a crate on crates.io" + ) + cargo_publish_cmd.set_defaults(func=cargo_publish) + # Always load the target args = parser.parse_args() target = Target(args) diff --git a/taskboot/config.py b/taskboot/config.py index 8b0bc48..8cb2e0b 100644 --- a/taskboot/config.py +++ b/taskboot/config.py @@ -87,3 +87,9 @@ def has_github_auth(self): if github is None: return False return "token" in github + + def has_cargo_auth(self): + cargo = self.config.get("cargo") + if cargo is None: + return False + return "token" in cargo From 4fc868e41d01f4616253a38d4f08c2953c015362 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 16 Jun 2020 16:37:32 +0200 Subject: [PATCH 2/5] Add cargo to Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index fb10bb3..c0c35a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ FROM python:3.7-alpine RUN apk add --no-cache img --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing # Setup other deps -RUN apk add --no-cache git skopeo docker +RUN apk add --no-cache git skopeo docker cargo # Define the working directory WORKDIR taskboot From a75fc250f98522464325581f78d31ce568941d99 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 16 Jun 2020 16:41:04 +0200 Subject: [PATCH 3/5] Fix wrong formatting --- taskboot/cargo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/taskboot/cargo.py b/taskboot/cargo.py index ddcdd1a..c9b805c 100644 --- a/taskboot/cargo.py +++ b/taskboot/cargo.py @@ -27,5 +27,6 @@ def cargo_publish(target, args): error = subprocess.run( ["cargo", "publish", "--token", config.cargo["token"]], capture_output=True ) + if error.returncode != 0: raise Exception(f"Failed to publish the crate") From 5b7d3c996c6afc91889f5bd684599704cfb273a1 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 16 Jun 2020 16:49:24 +0200 Subject: [PATCH 4/5] Fix other errors --- taskboot/cargo.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/taskboot/cargo.py b/taskboot/cargo.py index c9b805c..199ade2 100644 --- a/taskboot/cargo.py +++ b/taskboot/cargo.py @@ -20,13 +20,17 @@ def cargo_publish(target, args): config = Configuration(args) assert config.has_cargo_auth(), "Missing Cargo authentication" - # Check if the crate to be published contains some errors - subprocess.run(["cargo", "publish", "--dry-run"]) + # Build the package to publish on crates.io + try: + subprocess.run(["cargo", "publish", "--dry-run"], check=True) + except subprocess.CalledProcessError: + raise Exception("Failed to build the package to publish on crates.io") # Publish the crate on crates.io - error = subprocess.run( - ["cargo", "publish", "--token", config.cargo["token"]], capture_output=True + # stdout and stderr are captured to avoid leaking the token + proc = subprocess.run( + ["cargo", "publish", "--token", config.cargo["token"]], capture_output=True, ) - if error.returncode != 0: - raise Exception(f"Failed to publish the crate") + if proc.returncode != 0: + raise Exception("Failed to publish the crate on crates.io") From e85b58ab9a6ff94713be37f49a813714f6135253 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Wed, 17 Jun 2020 10:25:02 +0200 Subject: [PATCH 5/5] Fix some nits --- taskboot/cargo.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/taskboot/cargo.py b/taskboot/cargo.py index 199ade2..03cceb7 100644 --- a/taskboot/cargo.py +++ b/taskboot/cargo.py @@ -21,10 +21,7 @@ def cargo_publish(target, args): assert config.has_cargo_auth(), "Missing Cargo authentication" # Build the package to publish on crates.io - try: - subprocess.run(["cargo", "publish", "--dry-run"], check=True) - except subprocess.CalledProcessError: - raise Exception("Failed to build the package to publish on crates.io") + subprocess.run(["cargo", "publish", "--dry-run"], check=True) # Publish the crate on crates.io # stdout and stderr are captured to avoid leaking the token