From 1927875924b16b08f8f850142a5e17c0f08b3bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Th=C3=A9o=20LAURET?= <118362885+eldertek@users.noreply.github.com> Date: Mon, 10 Jul 2023 21:28:22 +0400 Subject: [PATCH 1/3] [fix/enh] Rewrite of yunopaste CLI tool (#1667) * rewrite python * Modify to pipe * alexAubin review * Fix "output" var not existing ... * yunopaste: anonymize_output is too harsh and not yunopaste's job + print_usage ain't called ... * yunopaste: return link to the raw version, less confusing than haste's ui ... --------- Co-authored-by: Alexandre Aubin --- bin/yunopaste | 93 ++++++++++++++------------------------------------- 1 file changed, 25 insertions(+), 68 deletions(-) diff --git a/bin/yunopaste b/bin/yunopaste index edf8d55c8..f6bdecae2 100755 --- a/bin/yunopaste +++ b/bin/yunopaste @@ -1,77 +1,34 @@ -#!/bin/bash +#!/usr/bin/env python3 -set -e -set -u +import sys +import requests +import json -PASTE_URL="https://paste.yunohost.org" +SERVER_URL = "https://paste.yunohost.org" +TIMEOUT = 3 -_die() { - printf "Error: %s\n" "$*" - exit 1 -} +def create_snippet(data): + try: + url = SERVER_URL + "/documents" + response = requests.post(url, data=data.encode('utf-8'), timeout=TIMEOUT) + response.raise_for_status() + dockey = json.loads(response.text)['key'] + return SERVER_URL + "/raw/" + dockey + except requests.exceptions.RequestException as e: + print("\033[31mError: {}\033[0m".format(e)) + sys.exit(1) -check_dependencies() { - curl -V > /dev/null 2>&1 || _die "This script requires curl." -} -paste_data() { - json=$(curl -X POST -s -d "$1" "${PASTE_URL}/documents") - [[ -z "$json" ]] && _die "Unable to post the data to the server." +def main(): + output = sys.stdin.read() - key=$(echo "$json" \ - | python3 -c 'import json,sys;o=json.load(sys.stdin);print(o["key"])' \ - 2>/dev/null) - [[ -z "$key" ]] && _die "Unable to parse the server response." + if not output: + print("\033[31mError: No input received from stdin.\033[0m") + sys.exit(1) - echo "${PASTE_URL}/${key}" -} + url = create_snippet(output) -usage() { - printf "Usage: ${0} [OPTION]... + print("\033[32mURL: {}\033[0m".format(url)) -Read from input stream and paste the data to the YunoHost -Haste server. - -For example, to paste the output of the YunoHost diagnosis, you -can simply execute the following: - yunohost diagnosis show | ${0} - -It will return the URL where you can access the pasted data. - -Options: - -h, --help show this help message and exit -" -} - -main() { - # parse options - while (( ${#} )); do - case "${1}" in - --help|-h) - usage - exit 0 - ;; - *) - echo "Unknown parameter detected: ${1}" >&2 - echo >&2 - usage >&2 - exit 1 - ;; - esac - - shift 1 - done - - # check input stream - read -t 0 || { - echo -e "Invalid usage: No input is provided.\n" >&2 - usage - exit 1 - } - - paste_data "$(cat)" -} - -check_dependencies - -main "${@}" +if __name__ == "__main__": + main() From dfc51ed7c525c61bd0a352002f0d8609da4a0c46 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 10 Jul 2023 19:29:34 +0200 Subject: [PATCH 2/3] Revert "[fix/enh] Rewrite of yunopaste CLI tool (#1667)" This reverts commit 1927875924b16b08f8f850142a5e17c0f08b3bc3. --- bin/yunopaste | 93 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git a/bin/yunopaste b/bin/yunopaste index f6bdecae2..edf8d55c8 100755 --- a/bin/yunopaste +++ b/bin/yunopaste @@ -1,34 +1,77 @@ -#!/usr/bin/env python3 +#!/bin/bash -import sys -import requests -import json +set -e +set -u -SERVER_URL = "https://paste.yunohost.org" -TIMEOUT = 3 +PASTE_URL="https://paste.yunohost.org" -def create_snippet(data): - try: - url = SERVER_URL + "/documents" - response = requests.post(url, data=data.encode('utf-8'), timeout=TIMEOUT) - response.raise_for_status() - dockey = json.loads(response.text)['key'] - return SERVER_URL + "/raw/" + dockey - except requests.exceptions.RequestException as e: - print("\033[31mError: {}\033[0m".format(e)) - sys.exit(1) +_die() { + printf "Error: %s\n" "$*" + exit 1 +} +check_dependencies() { + curl -V > /dev/null 2>&1 || _die "This script requires curl." +} -def main(): - output = sys.stdin.read() +paste_data() { + json=$(curl -X POST -s -d "$1" "${PASTE_URL}/documents") + [[ -z "$json" ]] && _die "Unable to post the data to the server." - if not output: - print("\033[31mError: No input received from stdin.\033[0m") - sys.exit(1) + key=$(echo "$json" \ + | python3 -c 'import json,sys;o=json.load(sys.stdin);print(o["key"])' \ + 2>/dev/null) + [[ -z "$key" ]] && _die "Unable to parse the server response." - url = create_snippet(output) + echo "${PASTE_URL}/${key}" +} - print("\033[32mURL: {}\033[0m".format(url)) +usage() { + printf "Usage: ${0} [OPTION]... -if __name__ == "__main__": - main() +Read from input stream and paste the data to the YunoHost +Haste server. + +For example, to paste the output of the YunoHost diagnosis, you +can simply execute the following: + yunohost diagnosis show | ${0} + +It will return the URL where you can access the pasted data. + +Options: + -h, --help show this help message and exit +" +} + +main() { + # parse options + while (( ${#} )); do + case "${1}" in + --help|-h) + usage + exit 0 + ;; + *) + echo "Unknown parameter detected: ${1}" >&2 + echo >&2 + usage >&2 + exit 1 + ;; + esac + + shift 1 + done + + # check input stream + read -t 0 || { + echo -e "Invalid usage: No input is provided.\n" >&2 + usage + exit 1 + } + + paste_data "$(cat)" +} + +check_dependencies + +main "${@}" From 7c1c147a74e5592f5e312419b0594bb477f18f9c Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 11 Jul 2023 15:46:35 +0200 Subject: [PATCH 3/3] quality: we don't really care about linter for the tests/ folder ... --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 49c78959d..c38df434b 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ deps = py39-black-{run,check}: black py39-mypy: mypy >= 0.900 commands = - py39-lint: flake8 src doc maintenance tests --ignore E402,E501,E203,W503,E741 --exclude src/vendor + py39-lint: flake8 src doc maintenance tests --ignore E402,E501,E203,W503,E741 --exclude src/tests,src/vendor py39-invalidcode: flake8 src bin maintenance --exclude src/tests,src/vendor --select F,E722,W605 py39-black-check: black --check --diff bin src doc maintenance tests py39-black-run: black bin src doc maintenance tests