Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v3.1.0 #4

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "advocat"
description = "A complimentary CLI tool to jutge.org"
version = "3.0.0"
version = "3.1.0-dev"
license="GPL-3.0"
authors = ["Roger Díaz Viñolas <[email protected]>"]
readme = "README.md"
Expand Down
17 changes: 17 additions & 0 deletions src/compilation/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{debug, problem};
use std::{fmt, io, path, process};
use crate::fetch::connection_manager::ConnectionManager;
use crate::fetch::resource;
use crate::fetch::resource::Resource;

pub static P1XX: Compiler = Compiler {
command: "g++",
Expand Down Expand Up @@ -147,4 +150,18 @@ impl Compiler<'_> {
self.compile_and_link_second_pass(generated_source, problem.output.as_path())
.map_err(|error| CompileProcessError { pass: 2, error })
}

pub fn write_required_resources<'a>(&self, problem: &problem::Problem, connection: &'a ConnectionManager, resources: &mut Vec<Box<dyn Resource + 'a>>) {
if !problem.has_main {
let main_cc = resource::OnlineResource::new(
problem.work_dir.join("main.cc"),
problem.main_cc_url.clone(),
connection
);
resources.push(Box::new(main_cc));
}

let user_source = resource::UserResource::new(problem.source.to_path_buf());
resources.push(Box::new(user_source));
}
}
40 changes: 22 additions & 18 deletions src/fetch/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{config, debug, warning};
use curl::easy;
use std::io::Write;
use std::{fmt, fs, io, path};
use std::cell::RefCell;

pub enum Error {
CurlError(curl::Error),
Expand Down Expand Up @@ -33,7 +34,7 @@ impl From<io::Error> for Error {
}

pub struct ConnectionManager {
handle: easy::Easy,
handle: RefCell<easy::Easy>,
}

impl ConnectionManager {
Expand All @@ -46,7 +47,7 @@ impl ConnectionManager {
let mut handle = easy::Easy::new();
handle.cookie_file(cookie_store.as_path())?;
handle.cookie_jar(cookie_store.as_path())?;
let mut cm = ConnectionManager { handle };
let cm = ConnectionManager { handle: RefCell::new(handle) };

if cm.check_is_authenticated()? {
debug!("Client is authenticated, reusing previous session")
Expand All @@ -64,16 +65,16 @@ impl ConnectionManager {
Ok(cm)
}

pub fn get_file(&mut self, url: &str, path: &path::Path) -> Result<(), Error> {
pub fn get_file(&self, url: &str, path: &path::Path) -> Result<(), Error> {
debug!("Downloading {} to {}", url, path.to_string_lossy());
let mut handle = self.handle.borrow_mut();
let mut file = fs::File::create(path)?;

self.handle.url(url)?;
self.handle
.write_function(move |data| file.write(data).or(Ok(0)))?;
self.handle.perform()?;
handle.url(url)?;
handle.write_function(move |data| file.write(data).or(Ok(0)))?;
handle.perform()?;

if let Some(content_type) = self.handle.content_type()? {
if let Some(content_type) = handle.content_type()? {
if content_type.contains("html") {
fs::remove_file(path)?;
return Err(Error::AuthError);
Expand All @@ -84,30 +85,33 @@ impl ConnectionManager {
}

fn try_to_authenticate(
&mut self,
&self,
credentials: &credentials::Credentials,
) -> Result<bool, Error> {
if let Some(form) = credentials.build_form() {
debug!("Attempting to authenticate");
self.handle.url("https://jutge.org/")?;
self.handle.nobody(true)?;
self.handle.httppost(form)?;
self.handle.perform()?;
self.handle.nobody(false)?;
let mut handle = self.handle.borrow_mut();
handle.url("https://jutge.org/")?;
handle.nobody(true)?;
handle.httppost(form)?;
handle.perform()?;
handle.nobody(false)?;
debug!("Authentication finished");
drop(handle); // We can't check authentication without borrowing the handle, so we drop it here
self.check_is_authenticated()
} else {
debug!("Unable to generate the authentication form");
Ok(false)
}
}

fn check_is_authenticated(&mut self) -> Result<bool, Error> {
fn check_is_authenticated(&self) -> Result<bool, Error> {
let mut response = Vec::new();
let mut handle = self.handle.borrow_mut();

self.handle.url("https://jutge.org/dashboard")?;
handle.url("https://jutge.org/dashboard")?;
{
let mut transfer = self.handle.transfer();
let mut transfer = handle.transfer();
transfer.write_function(|data| {
response.extend_from_slice(data);
Ok(data.len())
Expand All @@ -117,4 +121,4 @@ impl ConnectionManager {

Ok(!String::from_utf8_lossy(&response).contains("Did you sign in?"))
}
}
}
60 changes: 0 additions & 60 deletions src/fetch/download.rs

This file was deleted.

70 changes: 25 additions & 45 deletions src/fetch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,43 @@
use crate::{config, error, problem, ux, warning};
use std::fmt;
use crate::{compilation, config, error, problem, testing};

mod connection_manager;
pub mod connection_manager;
mod credentials;
mod download;
mod unzip;
pub mod resource;

pub use credentials::Credentials;
use crate::fetch::resource::Resource;

pub fn fetch_resources(
problem: &problem::Problem,
config: &config::Config,
) -> Result<(bool, bool, bool), crate::Error> {
let mut connection =
) -> Result<(bool, bool), crate::Error> {
let connection =
connection_manager::ConnectionManager::new(config).map_err(|e| crate::Error {
description: format!("Couldn't start the connection manager: {}", e),
exitcode: exitcode::IOERR,
})?;

let zip = execute_task("Downloading problem zip", || {
download::download_problem_zip(problem, &mut connection)
});
let main_cc = execute_task("Downloading problem main.cc", || {
download::download_problem_main(problem, &mut connection)
});
let tests = execute_task("Extracting tests", || {
download::unzip_problem_tests(problem)
});

if !zip {
warning!("Unable to retrieve tests!");
}

if !main_cc {
return Err(crate::Error {
description: String::from(
"Unable to retrieve the main.cc file, which is required to compile your binary!",
),
exitcode: exitcode::IOERR,
});
let mut compilation_resources: Vec<Box<dyn Resource>> = Vec::new();
compilation::P1XX.write_required_resources(problem, &connection, &mut compilation_resources);
let mut testing_resources: Vec<Box<dyn Resource>> = Vec::new();
testing::write_required_resources(problem, &connection, &mut testing_resources);

let mut compilation = true;
for resource in compilation_resources {
if let Err(e) = resource.acquire() {
error!("Compilation resource acquisition failed: {}", e);
compilation = false;
}
}

if !tests {
warning!("Unable to unzip tests!");
let mut testing = true;
for resource in testing_resources {
if let Err(e) = resource.acquire() {
error!("Testing resource acquisition failed: {}", e);
testing = false;
}
}

Ok((zip, main_cc, tests))
}

fn execute_task<T, E: fmt::Display + Sized>(name: &str, mut task: T) -> bool
where
T: FnMut() -> (ux::TaskStatus, Option<E>),
{
ux::show_task_status(name, ux::TaskType::Fetch, &ux::TaskStatus::InProgress);
let (status, err) = task();

ux::show_task_status(name, ux::TaskType::Fetch, &status);
if let Some(err) = err {
error!("The task [{}] returned the following error: {}", name, err);
}
status.is_ok()
}
Ok((compilation, testing))
}
87 changes: 87 additions & 0 deletions src/fetch/resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::path;
use crate::fetch::connection_manager::ConnectionManager;
use crate::fetch::unzip;

pub trait Resource {
fn acquire(&self) -> Result<path::PathBuf, String>;
}

pub struct UserResource {
path: path::PathBuf
}

impl UserResource {
pub fn new(path: path::PathBuf) -> UserResource {
UserResource { path }
}
}

impl Resource for UserResource {
fn acquire(&self) -> Result<path::PathBuf, String> {
if self.path.exists() {
Ok(self.path.to_path_buf())
} else {
Err(String::from("The path doesn't exist"))
}
}
}

pub struct OnlineResource<'a> {
download_path: path::PathBuf,
connection_manager: &'a ConnectionManager,
url: String
}

impl<'a> OnlineResource<'a> {
pub fn new(download_path: path::PathBuf, url: String, connection_manager: &ConnectionManager) -> OnlineResource {
OnlineResource {
download_path,
url,
connection_manager
}
}
}

impl<'a> Resource for OnlineResource<'a> {
fn acquire(&self) -> Result<path::PathBuf, String> {
if self.download_path.is_file() {
Ok(self.download_path.to_path_buf())
} else if self.download_path.is_dir() {
Err(String::from("The download path is a directory"))
} else {
self.connection_manager.get_file(&self.url, &self.download_path)
.map_err(|e| e.to_string())?;
Ok(self.download_path.to_path_buf())
}
}
}

pub struct UnzipResource<T: Resource> {
output_path: path::PathBuf,
source_resource: T
}

impl<T: Resource> UnzipResource<T> {
pub fn new(source_resource: T, output_path: path::PathBuf) -> UnzipResource<T> {
UnzipResource {
source_resource,
output_path
}
}
}

impl<T: Resource> Resource for UnzipResource<T> {
fn acquire(&self) -> Result<path::PathBuf, String> {
if self.output_path.is_dir() {
Ok(self.output_path.to_path_buf())
} else if self.output_path.is_file() {
Err(String::from("The extraction path is a file"))
} else {
let source = self.source_resource.acquire()?;
unzip::unzip_samples(source.as_path(), self.output_path.as_path())
.map_err(|e| e.to_string())?;
Ok(self.output_path.to_path_buf())
}

}
}
Loading