From 26aed79338d4258f4b08fcc97dc815e94ec49765 Mon Sep 17 00:00:00 2001 From: Michael Larabel Date: Mon, 23 Jun 2008 14:29:34 -0400 Subject: Add pts_module object for providing some common functions to use by PTS modules, switch system_monitor to using these functions. --- PTS-12-CHANGE-LOG | 1 + pts-core/functions/pts-functions.php | 14 ---- pts-core/functions/pts-functions_modules.php | 4 +- pts-core/modules/system_monitor.php | 16 ++--- pts-core/objects/pts_module.php | 95 ++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 pts-core/objects/pts_module.php diff --git a/PTS-12-CHANGE-LOG b/PTS-12-CHANGE-LOG index 67d9ea3..753de58 100644 --- a/PTS-12-CHANGE-LOG +++ b/PTS-12-CHANGE-LOG @@ -3,3 +3,4 @@ - Add list-modules option for listing all available/installed modules - Add module-info option for viewing information on a module - Drop pts-functions_monitor.php now that it's all modulized. +- Drop PTS_MONITOR_DIR environmental variable and pts_save_user_file() function from pts-core diff --git a/pts-core/functions/pts-functions.php b/pts-core/functions/pts-functions.php index ba4d942..95e89f6 100644 --- a/pts-core/functions/pts-functions.php +++ b/pts-core/functions/pts-functions.php @@ -77,7 +77,6 @@ define("ETC_DIR", PTS_DIR . "pts/etc/"); define("MODULE_DIR", PTS_DIR . "pts-core/modules/"); define("RESULTS_VIEWER_DIR", PTS_DIR . "pts-core/pts-results-viewer/"); define("PTS_USER_DIR", getenv("PTS_USER_DIR")); -define("PTS_MONITOR_DIR", PTS_USER_DIR . "system-monitor" . '/'); define("FONT_DIR", RESULTS_VIEWER_DIR . "fonts/"); pts_config_init(); @@ -102,7 +101,6 @@ $PTS_GLOBAL_ID = 1; $PTS_MODULES = array(); $PTS_MODULE_CURRENT = FALSE; -pts_auto_modules_ready($PTS_MODULES); pts_load_modules($PTS_MODULES); pts_module_process("__startup"); register_shutdown_function("pts_module_process", "__shutdown"); @@ -187,18 +185,6 @@ function pts_copy($from, $to) if(!is_file($to) || md5_file($from) != md5_file($to)) copy($from, $to); } -function pts_save_user_file($save_name = null, $contents = null, $directory = '/') -{ - if(!is_dir(PTS_MONITOR_DIR)) - mkdir(PTS_MONITOR_DIR); - - if($directory != '/') - if(!is_dir(PTS_MONITOR_DIR . $directory)) - mkdir(PTS_MONITOR_DIR . $directory); - - if(!empty($save_name) && !empty($contents)) - file_put_contents(PTS_USER_DIR . $extension . $directory . $save_name, $contents); -} function pts_gd_available() { if(!extension_loaded("gd")) diff --git a/pts-core/functions/pts-functions_modules.php b/pts-core/functions/pts-functions_modules.php index 5f09fd6..652cea0 100644 --- a/pts-core/functions/pts-functions_modules.php +++ b/pts-core/functions/pts-functions_modules.php @@ -37,8 +37,8 @@ function pts_auto_modules_ready(&$modules_list) } function pts_load_modules(&$modules_list) { - // TODO: Detect other modules to load - // pts_auto_modules_ready($modules_list); + pts_auto_modules_ready($modules_list); + $GLOBALS["PTS_MODULE_CURRENT"] = FALSE; // Clean-up modules list array_unique($modules_list); diff --git a/pts-core/modules/system_monitor.php b/pts-core/modules/system_monitor.php index 11124f5..70ffcbf 100644 --- a/pts-core/modules/system_monitor.php +++ b/pts-core/modules/system_monitor.php @@ -299,11 +299,9 @@ class system_monitor extends pts_module_interface if(pts_gd_available()) { $image_list = array(); - pts_save_user_file(); - pts_save_user_file(null, null, "/pts-monitor-viewer/"); - pts_copy(RESULTS_VIEWER_DIR . "pts-monitor-viewer.html", PTS_MONITOR_DIR . "pts-monitor-viewer.html"); - pts_copy(RESULTS_VIEWER_DIR . "pts.js", PTS_MONITOR_DIR . "pts-monitor-viewer/pts.js"); - pts_copy(RESULTS_VIEWER_DIR . "pts-viewer.css", PTS_MONITOR_DIR . "pts-monitor-viewer/pts-viewer.css"); + pts_module::copy_file(RESULTS_VIEWER_DIR . "pts-monitor-viewer.html", "pts-monitor-viewer.html"); + pts_module::copy_file(RESULTS_VIEWER_DIR . "pts.js", "pts-monitor-viewer/pts.js"); + pts_module::copy_file(RESULTS_VIEWER_DIR . "pts-viewer.css", "pts-monitor-viewer/pts-viewer.css"); $image_count = 0; foreach($type_index as $key => $sub_array) @@ -334,9 +332,8 @@ class system_monitor extends pts_module_interface $first_run = false; } } - $t->loadGraphVersion(PTS_VERSION); - $t->save_graph(PTS_MONITOR_DIR . THIS_RUN_TIME . '-' . $image_count . ".png"); + $t->save_graph(pts_module::save_dir() . THIS_RUN_TIME . '-' . $image_count . ".png"); $t->renderGraph(); array_push($image_list, THIS_RUN_TIME . '-' . $image_count . ".png"); @@ -360,9 +357,10 @@ class system_monitor extends pts_module_interface if(count($m_array[0]) > 1 && !empty($url)) { - file_put_contents(PTS_MONITOR_DIR . "link-latest.html", "Phoronix Test Suite + $file = pts_module::save_file("link-latest.html", "Phoronix Test Suite "); - display_web_browser(PTS_MONITOR_DIR . "link-latest.html"); + if($file != FALSE) + display_web_browser($file); } } diff --git a/pts-core/objects/pts_module.php b/pts-core/objects/pts_module.php new file mode 100644 index 0000000..a7fee6b --- /dev/null +++ b/pts-core/objects/pts_module.php @@ -0,0 +1,95 @@ +. +*/ + +class pts_module +{ + public static function save_dir() + { + $prefix_dir = PTS_USER_DIR . "local/"; + + if(!is_dir($prefix_dir)) + mkdir($prefix_dir); + + return $prefix_dir . self::module_name() . "/"; + } + public static function save_file($file, $contents = NULL) + { + // Saves a file for a module + + $save_base_dir = self::save_dir(); + + if(!is_dir($save_base_dir)) + mkdir($save_base_dir); + + if(($extra_dir = dirname($file)) != "." && !is_dir($save_base_dir . $extra_dir)) + mkdir($save_base_dir . $extra_dir); + + if(!empty($contents)) + { + if(file_put_contents($save_base_dir . $file, $contents) != FALSE) + return $save_base_dir . $file; + } + + return FALSE; + } + public static function copy_file($from_file, $to_file) + { + // Copy a file for a module + + $save_base_dir = self::save_dir(); + + if(!is_dir($save_base_dir)) + mkdir($save_base_dir); + + if(($extra_dir = dirname($to_file)) != "." && !is_dir($save_base_dir . $extra_dir)) + mkdir($save_base_dir . $extra_dir); + + + if(is_file($from_file) && (!is_file($save_base_dir . $to_file) || md5_file($from_file) != md5_file($save_base_dir . $to_file))) + if(copy($from_file, $save_base_dir . $to_file)) + return $save_base_dir . $to_file; + + return FALSE; + } + private static function module_name() + { + $module_name = "unknown"; + + if($GLOBALS["PTS_MODULE_CURRENT"] != FALSE) + { + $module_name = $GLOBALS["PTS_MODULE_CURRENT"]; + } + else + { + $bt = debug_backtrace(); + + for($i = 0; $i < count($bt) && $module_name == "unknown"; $i++) + if($bt[$i]["class"] != "pts_module") + $module_name = $bt[$i]["class"]; + } + + return $module_name; + } +} + +?> -- cgit