Skip to content

Commit

Permalink
TgBot++: android_builder: Complete the upload part
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Jul 6, 2024
1 parent 6507161 commit 4f03bc7
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 49 deletions.
43 changes: 35 additions & 8 deletions src/android_builder/scripts/subprocess_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

def run_command(command: str) -> bool:
s, _ = run_command_with_output(command)
return s is not None
43 changes: 43 additions & 0 deletions src/android_builder/scripts/upload_file.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Check if file path is provided as argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <file_path>"
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"
26 changes: 20 additions & 6 deletions src/android_builder/scripts/upload_file.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions src/android_builder/tasks/PerBuildData.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#pragma once

#include <ConfigParsers.hpp>
#include <absl/log/check.h>

#include <ConfigParsers.hpp>

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<char, 250> msg{};
std::array<char, MSG_SIZE> 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 {
Expand Down
6 changes: 3 additions & 3 deletions src/android_builder/tasks/ROMBuildTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ bool ROMBuildTask::runFunction() {
std::istreambuf_iterator<char>());
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);
Expand Down Expand Up @@ -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)) {
Expand Down
16 changes: 6 additions & 10 deletions src/android_builder/tasks/UploadFileTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct UploadFileTask : ForkAndRun {
return false;
}
LOG(INFO) << resultString;
wrapper.sendMessageOnExit(resultString);
data.result->setMessage(resultString);
return true;
}

Expand All @@ -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;
}
}

Expand All @@ -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;
};
37 changes: 18 additions & 19 deletions src/command_modules/runtime/cmd_RomBuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 4f03bc7

Please sign in to comment.