Skip to content

Commit

Permalink
Merge f4fa3fa into efefbc9
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia authored Aug 8, 2023
2 parents efefbc9 + f4fa3fa commit dfaad47
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 98 deletions.
2 changes: 2 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Options:
Driver TTL (time-to-live) [default: 3600]
--browser-ttl <BROWSER_TTL>
Browser TTL (time-to-live) [default: 3600]
--cache-path <CACHE_PATH>
Local folder used to store downloaded assets (drivers and browsers), local metadata, and configuration file [default: ~/.cache/selenium]
--clear-cache
Clear cache folder (~/.cache/selenium)
--clear-metadata
Expand Down
45 changes: 25 additions & 20 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ use std::path::PathBuf;
use crate::config::ARCH::{ARM64, X32};
use crate::config::OS::{LINUX, MACOS, WINDOWS};
use crate::downloads::{parse_json_from_url, read_version_from_link};
use crate::files::{
compose_driver_path_in_cache, get_cache_folder, path_buf_to_string, BrowserPath,
};
use crate::files::{compose_driver_path_in_cache, path_buf_to_string, BrowserPath};
use crate::logger::Logger;
use crate::metadata::{
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
Expand Down Expand Up @@ -314,19 +312,20 @@ impl ChromeManager {
}
}

fn get_browser_path_in_cache(&self) -> PathBuf {
get_cache_folder()
fn get_browser_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
Ok(self
.get_cache_path()?
.join(self.get_browser_name())
.join(self.get_platform_label())
.join(self.get_browser_version())
.join(self.get_browser_version()))
}

fn get_browser_binary_path_in_cache(&self) -> PathBuf {
let browser_in_cache = self.get_browser_path_in_cache();
fn get_browser_binary_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let browser_in_cache = self.get_browser_path_in_cache()?;
if MACOS.is(self.get_os()) {
browser_in_cache.join(CFT_MACOS_APP_NAME)
Ok(browser_in_cache.join(CFT_MACOS_APP_NAME))
} else {
browser_in_cache.join(self.get_browser_name_with_extension())
Ok(browser_in_cache.join(self.get_browser_name_with_extension()))
}
}
}
Expand Down Expand Up @@ -402,7 +401,7 @@ impl SeleniumManager for ChromeManager {
fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let mut metadata = get_metadata(self.get_logger());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -447,7 +446,7 @@ impl SeleniumManager for ChromeManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}
Ok(driver_version)
}
Expand All @@ -458,7 +457,7 @@ impl SeleniumManager for ChromeManager {
let browser_name = self.browser_name;
let browser_version;
let major_browser_version = self.get_major_browser_version();
let mut metadata = get_metadata(self.get_logger());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

// First, browser version is checked in the local metadata
match get_browser_version_from_metadata(
Expand Down Expand Up @@ -486,7 +485,7 @@ impl SeleniumManager for ChromeManager {
&browser_version,
browser_ttl,
));
write_metadata(&metadata, self.get_logger());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}
}
}
Expand Down Expand Up @@ -536,11 +535,17 @@ impl SeleniumManager for ChromeManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch_folder = self.get_platform_label();
compose_driver_path_in_cache(self.driver_name, os, arch_folder, driver_version)
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
os,
arch_folder,
driver_version,
))
}

fn get_config(&self) -> &ManagerConfig {
Expand All @@ -566,7 +571,7 @@ impl SeleniumManager for ChromeManager {
fn download_browser(&mut self) -> Result<Option<PathBuf>, Box<dyn Error>> {
let browser_version;
let browser_name = self.browser_name;
let mut metadata = get_metadata(self.get_logger());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);
let major_browser_version = self.get_major_browser_version();
let major_browser_version_int = major_browser_version.parse::<i32>().unwrap_or_default();

Expand Down Expand Up @@ -618,7 +623,7 @@ impl SeleniumManager for ChromeManager {
&browser_version,
browser_ttl,
));
write_metadata(&metadata, self.get_logger());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}
}
}
Expand All @@ -628,7 +633,7 @@ impl SeleniumManager for ChromeManager {
));

// Checking if browser version is in the cache
let browser_binary_path = self.get_browser_binary_path_in_cache();
let browser_binary_path = self.get_browser_binary_path_in_cache()?;
if browser_binary_path.exists() {
self.get_logger().debug(format!(
"{} {} already in the cache",
Expand Down Expand Up @@ -657,7 +662,7 @@ impl SeleniumManager for ChromeManager {

uncompress(
&driver_zip_file,
&self.get_browser_path_in_cache(),
&self.get_browser_path_in_cache()?,
self.get_logger(),
None,
)?;
Expand Down
45 changes: 38 additions & 7 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,30 @@
// under the License.

use crate::config::OS::{LINUX, MACOS, WINDOWS};
use crate::files::get_cache_folder;
use crate::shell::run_shell_command_by_os;
use crate::{format_one_arg, Command, REQUEST_TIMEOUT_SEC, UNAME_COMMAND};
use crate::{
default_cache_folder, format_one_arg, path_buf_to_string, Command, REQUEST_TIMEOUT_SEC,
UNAME_COMMAND,
};
use crate::{ARCH_AMD64, ARCH_ARM64, ARCH_X86, TTL_BROWSERS_SEC, TTL_DRIVERS_SEC, WMIC_COMMAND_OS};
use std::cell::RefCell;
use std::env;
use std::env::consts::OS;
use std::error::Error;
use std::fs::read_to_string;
use std::path::Path;
use toml::Table;

thread_local!(static CACHE_PATH: RefCell<String> = RefCell::new(path_buf_to_string(default_cache_folder())));

pub const CONFIG_FILE: &str = "selenium-manager-config.toml";
pub const ENV_PREFIX: &str = "SE_";
pub const VERSION_PREFIX: &str = "-version";
pub const PATH_PREFIX: &str = "-path";
pub const CACHE_PATH_KEY: &str = "cache-path";

pub struct ManagerConfig {
pub cache_path: String,
pub browser_version: String,
pub driver_version: String,
pub browser_path: String,
Expand All @@ -47,6 +55,8 @@ pub struct ManagerConfig {

impl ManagerConfig {
pub fn default(browser_name: &str, driver_name: &str) -> ManagerConfig {
let cache_path = StringKey(vec![CACHE_PATH_KEY], &read_cache_path()).get_value();

let self_os = OS;
let self_arch = if WINDOWS.is(self_os) {
let wmic_command = Command::new_single(WMIC_COMMAND_OS.to_string());
Expand Down Expand Up @@ -77,12 +87,12 @@ impl ManagerConfig {
let browser_path_label = concat(browser_name, PATH_PREFIX);

ManagerConfig {
browser_version: StringKey(vec!["browser-version", browser_version_label.as_str()], "")
cache_path,
browser_version: StringKey(vec!["browser-version", &browser_version_label], "")
.get_value(),
driver_version: StringKey(vec!["driver-version", driver_version_label.as_str()], "")
.get_value(),
browser_path: StringKey(vec!["browser-path", browser_path_label.as_str()], "")
driver_version: StringKey(vec!["driver-version", &driver_version_label], "")
.get_value(),
browser_path: StringKey(vec!["browser-path", &browser_path_label], "").get_value(),
os: StringKey(vec!["os"], self_os).get_value(),
arch: StringKey(vec!["arch"], self_arch.as_str()).get_value(),
proxy: StringKey(vec!["proxy"], "").get_value(),
Expand Down Expand Up @@ -168,6 +178,9 @@ impl StringKey<'_> {
result = env::var(get_env_name(key)).unwrap_or_default()
}
if !result.is_empty() {
if key.eq(CACHE_PATH_KEY) {
write_cache_path(result.clone());
}
return result;
}
}
Expand Down Expand Up @@ -215,7 +228,8 @@ fn get_env_name(suffix: &str) -> String {
}

fn get_config() -> Result<Table, Box<dyn Error>> {
let config_path = get_cache_folder().join(CONFIG_FILE);
let cache_path = read_cache_path();
let config_path = Path::new(&cache_path).to_path_buf().join(CONFIG_FILE);
Ok(read_to_string(config_path)?.parse()?)
}

Expand All @@ -224,3 +238,20 @@ fn concat(prefix: &str, suffix: &str) -> String {
version_label.push_str(suffix);
version_label
}

fn write_cache_path(cache_path: String) {
CACHE_PATH.with(|value| {
*value.borrow_mut() = cache_path;
});
}

fn read_cache_path() -> String {
let mut cache_path: String = path_buf_to_string(default_cache_folder());
CACHE_PATH.with(|value| {
let path: String = (&*value.borrow().to_string()).into();
if !path.is_empty() {
cache_path = path;
}
});
cache_path
}
14 changes: 10 additions & 4 deletions rust/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl SeleniumManager for EdgeManager {

fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let mut major_browser_version = self.get_major_browser_version();
let mut metadata = get_metadata(self.get_logger());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -191,7 +191,7 @@ impl SeleniumManager for EdgeManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}

Ok(driver_version)
Expand Down Expand Up @@ -230,7 +230,7 @@ impl SeleniumManager for EdgeManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch = self.get_arch();
Expand All @@ -251,7 +251,13 @@ impl SeleniumManager for EdgeManager {
} else {
"linux64"
};
compose_driver_path_in_cache(self.driver_name, os, arch_folder, driver_version)
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
os,
arch_folder,
driver_version,
))
}

fn get_config(&self) -> &ManagerConfig {
Expand Down
25 changes: 11 additions & 14 deletions rust/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ impl BrowserPath {
}
}

pub fn create_parent_path_if_not_exists(path: &Path) {
pub fn create_parent_path_if_not_exists(path: &Path) -> Result<(), Box<dyn Error>> {
if let Some(p) = path.parent() {
create_path_if_not_exists(p);
create_path_if_not_exists(p)?;
}
Ok(())
}

pub fn create_path_if_not_exists(path: &Path) {
pub fn create_path_if_not_exists(path: &Path) -> Result<(), Box<dyn Error>> {
if !path.exists() {
fs::create_dir_all(path).unwrap();
fs::create_dir_all(path)?;
}
Ok(())
}

pub fn uncompress(
Expand Down Expand Up @@ -132,7 +134,7 @@ pub fn unzip(
None => continue,
};
if single_file.is_none() {
create_path_if_not_exists(target);
create_path_if_not_exists(target)?;
out_path = target.join(path);
}

Expand All @@ -148,7 +150,7 @@ pub fn unzip(
out_path.display(),
file.size()
));
create_parent_path_if_not_exists(out_path.as_path());
create_parent_path_if_not_exists(out_path.as_path())?;

let mut outfile = File::create(&out_path)?;
io::copy(&mut file, &mut outfile)?;
Expand Down Expand Up @@ -186,27 +188,22 @@ pub fn get_raw_file_name(file_name: &str) -> &str {
raw_file_name
}

pub fn compose_cache_folder() -> PathBuf {
pub fn default_cache_folder() -> PathBuf {
if let Some(base_dirs) = BaseDirs::new() {
return Path::new(base_dirs.home_dir())
.join(String::from(CACHE_FOLDER).replace('/', std::path::MAIN_SEPARATOR_STR));
}
PathBuf::new()
}

pub fn get_cache_folder() -> PathBuf {
let cache_path = compose_cache_folder();
create_path_if_not_exists(&cache_path);
cache_path
}

pub fn compose_driver_path_in_cache(
driver_path: PathBuf,
driver_name: &str,
os: &str,
arch_folder: &str,
driver_version: &str,
) -> PathBuf {
get_cache_folder()
driver_path
.join(driver_name)
.join(arch_folder)
.join(driver_version)
Expand Down
14 changes: 10 additions & 4 deletions rust/src/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl SeleniumManager for FirefoxManager {
fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let mut metadata = get_metadata(self.get_logger());
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);

match get_driver_version_from_metadata(
&metadata.drivers,
Expand Down Expand Up @@ -161,7 +161,7 @@ impl SeleniumManager for FirefoxManager {
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger());
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}

Ok(driver_version)
Expand Down Expand Up @@ -211,7 +211,7 @@ impl SeleniumManager for FirefoxManager {
))
}

fn get_driver_path_in_cache(&self) -> PathBuf {
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch = self.get_arch();
Expand Down Expand Up @@ -241,7 +241,13 @@ impl SeleniumManager for FirefoxManager {
} else {
"linux64"
};
compose_driver_path_in_cache(self.driver_name, os, arch_folder, driver_version)
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
os,
arch_folder,
driver_version,
))
}

fn get_config(&self) -> &ManagerConfig {
Expand Down
Loading

0 comments on commit dfaad47

Please sign in to comment.