From 4f03bc7716365e45607a0341e2e0d0a329d82e62 Mon Sep 17 00:00:00 2001 From: Soo Hwan Na Date: Sat, 6 Jul 2024 15:44:30 +0900 Subject: [PATCH] TgBot++: android_builder: Complete the upload part --- .../scripts/subprocess_utils.py | 43 +++++++++++++++---- src/android_builder/scripts/upload_file.bash | 43 +++++++++++++++++++ src/android_builder/scripts/upload_file.py | 26 ++++++++--- src/android_builder/tasks/PerBuildData.hpp | 10 +++-- src/android_builder/tasks/ROMBuildTask.cpp | 6 +-- src/android_builder/tasks/UploadFileTask.hpp | 16 +++---- src/command_modules/runtime/cmd_RomBuild.cpp | 37 ++++++++-------- 7 files changed, 132 insertions(+), 49 deletions(-) create mode 100644 src/android_builder/scripts/upload_file.bash diff --git a/src/android_builder/scripts/subprocess_utils.py b/src/android_builder/scripts/subprocess_utils.py index 279c7f62..12889226 100644 --- a/src/android_builder/scripts/subprocess_utils.py +++ b/src/android_builder/scripts/subprocess_utils.py @@ -7,20 +7,47 @@ print = custom_print.custom_print -def run_command(command: str) -> bool: +def run_command_with_output(command: str, testing: bool = False, testingRet: bool = False): print("Running command: %s" % command) sys.stdout.flush() - ret = 0 if testing: - return testingRet + if testingRet: + return 'SomeOutput', 'SomeError' + else: + return None, None + try: - subprocess.check_call(command, shell=True, executable="/bin/bash") + # Use subprocess.run instead of subprocess.check_call for capturing output + result = subprocess.run(command, shell=True, executable="/bin/bash", + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + # Check return code to handle errors + if result.returncode != 0: + errstr = f"Command '{command}' failed with exit code {result.returncode}\n" + errstr += result.stderr.decode('utf-8') + print(errstr) + return None, result.stderr.decode('utf-8') + + # Return output and error as strings + output = result.stdout.decode('utf-8') + error = result.stderr.decode('utf-8') + return output, error + except subprocess.CalledProcessError as e: - errstr = f"Command '{command}' failed with exit code {e.returncode}" - print(errstr) - ret = e.returncode + errstr = f"Command '{command}' failed with exception {str(e)}\n" + print(errstr) + return None, str(e) + + except Exception as e: + errstr = f"Exception occurred while running command '{command}': {str(e)}\n" + print(errstr) + return None, str(e) + finally: sys.stdout.flush() sys.stderr.flush() - return ret == 0 \ No newline at end of file + +def run_command(command: str) -> bool: + s, _ = run_command_with_output(command) + return s is not None diff --git a/src/android_builder/scripts/upload_file.bash b/src/android_builder/scripts/upload_file.bash new file mode 100644 index 00000000..a4d2fb5f --- /dev/null +++ b/src/android_builder/scripts/upload_file.bash @@ -0,0 +1,43 @@ +#!/bin/bash + +# Check if file path is provided as argument +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +# Assign the file path provided as argument to a variable +file_path=$1 + +# Check if the file exists +if [ ! -f "$file_path" ]; then + echo "File not found: $file_path" + exit 1 +fi + +# Make the GET request to retrieve server information +server_info=$(curl -s -X GET 'https://api.gofile.io/servers') + +# Extract the server name +server=$(echo "$server_info" | jq -r '.data.servers[0].name') + +# Check if the server name is empty +if [ -z "$server" ]; then + echo "Failed to get the server name." + exit 1 +fi + +# Upload the file using the server name +upload_result=$(curl --progress-bar -X POST "https://$server.gofile.io/contents/uploadfile" -F "file=@$file_path") + +# Extract and print the download link +download_link=$(echo "$upload_result" | jq -r '.data.downloadPage') + +# Check if the download link is empty +if [ -z "$download_link" ]; then + echo "Failed to get the download link." + exit 1 +fi + +# Print the download link +echo "Download link: $download_link" diff --git a/src/android_builder/scripts/upload_file.py b/src/android_builder/scripts/upload_file.py index c1ba3905..16575f9a 100644 --- a/src/android_builder/scripts/upload_file.py +++ b/src/android_builder/scripts/upload_file.py @@ -1,5 +1,10 @@ import os import requests +import custom_print +import subprocess_utils +from requests.exceptions import SSLError + +print = custom_print.custom_print def upload_to_gofile(device_name, file_prefix) -> str: # Define the directory to search for files @@ -22,12 +27,21 @@ def upload_to_gofile(device_name, file_prefix) -> str: # GoFile API URL gofile_api_url = "https://api.gofile.io/uploadFile" - # Open the file in binary mode - with open(file_to_upload, 'rb') as file: - files = {'file': (filename, file)} - - # Upload the file to GoFile - response = requests.post(gofile_api_url, files=files) + try: + # Open the file in binary mode + with open(file_to_upload, 'rb') as file: + files = {'file': (filename, file)} + + # Upload the file to GoFile + response = requests.post(gofile_api_url, files=files) + except SSLError as e: + print(f"SSL certificate verification failed: {e}") + dir_path = os.path.dirname(os.path.realpath(__file__)) + out, err = subprocess_utils.run_command_with_output(f'bash {dir_path}/upload_file.bash {file_to_upload}') + return f""" + STDOUT: {out} + STDERR: {err} + """ # Check the response from GoFile if response.status_code == 200: diff --git a/src/android_builder/tasks/PerBuildData.hpp b/src/android_builder/tasks/PerBuildData.hpp index e29ecbd8..633df8c1 100644 --- a/src/android_builder/tasks/PerBuildData.hpp +++ b/src/android_builder/tasks/PerBuildData.hpp @@ -1,18 +1,22 @@ #pragma once -#include #include +#include + struct PerBuildData { enum class Result { SUCCESS, ERROR_NONFATAL, ERROR_FATAL }; BuildConfig bConfig; RomConfig rConfig; std::filesystem::path scriptDirectory; struct ResultData { + static constexpr int MSG_SIZE = 250; Result value{}; - std::array msg{}; + std::array msg{}; void setMessage(const std::string& message) { - CHECK(message.size() < msg.size()); + LOG_IF(WARNING, message.size() > msg.size()) + << "Message size is " << message.size() + << " bytes, which exceeds limit"; std::strncpy(msg.data(), message.c_str(), msg.size() - 1); } [[nodiscard]] std::string getMessage() const noexcept { diff --git a/src/android_builder/tasks/ROMBuildTask.cpp b/src/android_builder/tasks/ROMBuildTask.cpp index 389e50ed..a547a4a4 100644 --- a/src/android_builder/tasks/ROMBuildTask.cpp +++ b/src/android_builder/tasks/ROMBuildTask.cpp @@ -67,8 +67,8 @@ bool ROMBuildTask::runFunction() { std::istreambuf_iterator()); if (errorLogContent.find("FAILED:") != std::string::npos) { // Ninja error - resultdata->setMessage(errorLogContent.substr( - errorLogContent.find_first_of('\n'))); + resultdata->setMessage(errorLogContent.substr(0, + errorLogContent.find_first_of("Command:"))); } else { // Probably makefile? resultdata->setMessage(errorLogContent); @@ -110,7 +110,7 @@ void ROMBuildTask::onNewStdoutBuffer(ForkAndRun::BufferType& buffer) { << std::endl; buildInfoBuffer << "Last updated on: " << fromTP(now) << std::endl; buildInfoBuffer << "Target ROM: " << data.rConfig.name - << " branch: " << data.rConfig.branch; + << ", branch: " << data.rConfig.branch << std::endl; buildInfoBuffer << "Target device: " << data.bConfig.device << std::endl; buildInfoBuffer << "Job count: " << guessJobCount(); if (_get_used_mem->call(nullptr, &memUsage)) { diff --git a/src/android_builder/tasks/UploadFileTask.hpp b/src/android_builder/tasks/UploadFileTask.hpp index 75aed849..d5cb4c71 100644 --- a/src/android_builder/tasks/UploadFileTask.hpp +++ b/src/android_builder/tasks/UploadFileTask.hpp @@ -46,7 +46,7 @@ struct UploadFileTask : ForkAndRun { return false; } LOG(INFO) << resultString; - wrapper.sendMessageOnExit(resultString); + data.result->setMessage(resultString); return true; } @@ -58,16 +58,15 @@ struct UploadFileTask : ForkAndRun { * @param exitCode The exit code of the subprocess. */ void onExit(int exitCode) override { - const auto& bot = wrapper.getBot(); switch (exitCode) { case EXIT_SUCCESS: - bot_sendMessage(bot, wrapper.getChatId(), - "Upload completed successfully"); - + data.result->value = PerBuildData::Result::SUCCESS; break; case EXIT_FAILURE: + data.result->value = PerBuildData::Result::ERROR_FATAL; + break; default: - bot_sendMessage(bot, wrapper.getChatId(), "Upload failed"); + break; } } @@ -79,11 +78,8 @@ struct UploadFileTask : ForkAndRun { * @param data The data object containing the necessary configuration and * paths. */ - explicit UploadFileTask(MessageWrapper& wrapper, PerBuildData data) - : wrapper(wrapper), data(std::move(data)) {} + explicit UploadFileTask(PerBuildData data) : data(std::move(data)) {} private: - MessageWrapper& - wrapper; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) PerBuildData data; }; \ No newline at end of file diff --git a/src/command_modules/runtime/cmd_RomBuild.cpp b/src/command_modules/runtime/cmd_RomBuild.cpp index f930de37..dd978866 100644 --- a/src/command_modules/runtime/cmd_RomBuild.cpp +++ b/src/command_modules/runtime/cmd_RomBuild.cpp @@ -153,6 +153,19 @@ bool build(PerBuildData data, const Bot& bot, const Message::Ptr& message) { } return false; } + +void upload(PerBuildData data, const Bot& bot, const Message::Ptr& message) { + PerBuildData::ResultData uploadResult; + auto uploadmsg = bot_sendMessage(bot, message->chat->id, "Will upload"); + + data.result = &uploadResult; + UploadFileTask uploadTask(data); + if (!uploadTask.execute()) { + bot_editMessage(bot, uploadmsg, "Could'nt initialize upload"); + } else { + bot_editMessage(bot, uploadmsg, uploadResult.getMessage()); + } +} } // namespace static void romBuildCommand(const Bot& bot, const Message::Ptr message) { @@ -188,26 +201,12 @@ static void romBuildCommand(const Bot& bot, const Message::Ptr message) { } std::filesystem::current_path(kBuildDirectory, ec); - // Sync the repository - if (!repoSync(PBData, bot, message)) { - returnToCwd(); - return; - } - - // Build the ROM - if (!build(PBData, bot, message)) { - returnToCwd(); - return; + if (repoSync(PBData, bot, message)) { + // Build the ROM + if (build(PBData, bot, message)) { + upload(PBData, bot, message); + } } - -#if 0 - bot_sendMessage(bot, wrapper.getChatId(), "Will upload"); - - bool uploadResult = false; - PBData.result = &uploadResult; - UploadFileTask uploadTask(wrapper, PBData); - uploadTask.execute(); -#endif returnToCwd(); }