Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 018ecbe

Browse files
Michael GraueryuzhengZ
authored andcommitted
BUG: Refs #212. Work towards exporting batchmake scripts.
This commit breaks out much functionality into KWBatchmakeComponent.php and also further into a library (KWUtils.php). Included are tests for both of these, along with testing data. The functionality provided would allow for creating a tmp work directory in the batchmake module, copying in the needed .bms and .bmm files, compiling the batchmake script, creating the condor DAG and scripts from the .bms file, and submitting the DAG to condor.
1 parent 2d9f85d commit 018ecbe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2092
-86
lines changed

library/KWUtils.php

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
<?php
2+
/*=========================================================================
3+
Program: MIDAS Server
4+
Language: PHP/HTML/Java/Javascript/SQL
5+
Date: $Date$
6+
Version: $Revision$
7+
8+
Copyright (c) Kitware Inc. 28 Corporate Drive. All rights reserved.
9+
Clifton Park, NY, 12065, USA.
10+
11+
See Copyright.txt for details.
12+
This software is distributed WITHOUT ANY WARRANTY; without even
13+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14+
PURPOSE. See the above copyright notices for more information.
15+
=========================================================================*/
16+
?>
17+
<?php
18+
/**
19+
* globally useful utility functions.
20+
*/
21+
class KWUtils
22+
{
23+
24+
CONST DEFAULT_MKDIR_MODE = 0775;
25+
26+
/**
27+
* @method mkDir
28+
* @TODO what to do with errors in a way that is consistent with error reporting
29+
* Will create the directory $dir and set the filemode so that the newly
30+
* created dir is writable by the current user.
31+
* @return true on success, false otherwise
32+
*/
33+
public static function mkDir($dir, $mode = self::DEFAULT_MKDIR_MODE)
34+
{
35+
if(!file_exists($dir) && !mkdir($dir, $mode, true))
36+
{
37+
return false;
38+
}
39+
// change file mode
40+
// even though we are swallowing the error messages, we return false
41+
// if the operation can't be completed
42+
if(!is_writeable($dir) || @!chmod($dir, $mode))
43+
{
44+
return false;
45+
}
46+
return true;
47+
}
48+
49+
/**
50+
* @method createSubDirectories recursively create subdirectories starting at
51+
* baseDirectory, sequentially creating each of the directories in the
52+
* subDirectories array, according to the passed in mode.
53+
* @param $baseDirectory the first directory to create
54+
* @param $subDirectories an array of directories that will be created in a
55+
* recursive fashion, each one appending to the last as a deeper subdirectory
56+
* of baseDirectory
57+
* @param the mode to create the new directories
58+
*/
59+
public static function createSubDirectories($baseDirectory, $subDirectories, $mode = self::DEFAULT_MKDIR_MODE)
60+
{
61+
if(!file_exists($baseDirectory) )
62+
{
63+
throw new Zend_Exception($baseDirectory . ' does not exist');
64+
}
65+
$relpath = '';
66+
foreach($subDirectories as $directory)
67+
{
68+
$relpath .= $directory . "/";
69+
70+
if(!KwUtils::mkDir($baseDirectory . $relpath, $mode))
71+
{
72+
throw new Zend_Exception($baseDirectory . $relpath . ' could not be created');
73+
}
74+
}
75+
return $baseDirectory . $relpath;
76+
}
77+
78+
/**
79+
* @method isWindows()
80+
* @return True if the current platform is windows
81+
*/
82+
public static function isWindows()
83+
{
84+
return (strtolower(substr(PHP_OS, 0, 3)) == "win");
85+
}
86+
87+
88+
/**
89+
* @method escapeCommand
90+
* will escape a command respecting the format of the current platform
91+
* @param $command, the command to be escaped
92+
* @return the $command, $escaped for the current platform
93+
* @TODO, how to test this?
94+
*/
95+
public static function escapeCommand($command )
96+
{
97+
// if windows platform, add extra double-quote
98+
// See http://www.mail-archive.com/internals@lists.php.net/msg29874.html
99+
if(KWUtils::isWindows() )
100+
{
101+
$command = '"'.$command.'"';
102+
}
103+
104+
return $command;
105+
}
106+
107+
/**
108+
* @method appendStringIfNot will append the string $ext to
109+
* $subject if it is not already a suffix of $subject
110+
* @param $subject, the string to be appended to
111+
* @param $ext, the extension to check for and append
112+
* @return $subject, will end with the suffix $ext
113+
*/
114+
public static function appendStringIfNot($subject, $ext)
115+
{
116+
if(!(substr($subject, strlen($subject) - strlen($ext)) === $ext) )
117+
{
118+
$subject .= $ext;
119+
}
120+
return $subject;
121+
}
122+
123+
/**
124+
* @method exec
125+
* will execute a command, respecting the format of the current platform.
126+
* @param $command to be executed, with all arguments, and formatted correctly
127+
* @param $output, a reference to put the output of the command
128+
* @param $chdir, the dir to change to for execution, if any
129+
* @param $return_val, a reference to put the return value of the command
130+
* the temporary work dir
131+
*/
132+
public static function exec($command, &$output = null, $chdir = "", &$return_val = null)
133+
{
134+
if(!empty($chdir) && is_dir($chdir))
135+
{
136+
if(!chdir($chdir))
137+
{
138+
throw new Zend_Exception("Failed to change directory: [".$chdir."]");
139+
}
140+
}
141+
// on Linux need to add redirection to handle stderr
142+
$redirect_error = KWUtils::isLinux() ? " 2>&1" : "";
143+
exec(KWUtils::escapeCommand($command) . $redirect_error, $output, $return_val);
144+
}
145+
146+
147+
/**
148+
* @method isLinux()
149+
* @return True if the current platform is Linux
150+
*/
151+
public static function isLinux()
152+
{
153+
return (strtolower(substr(PHP_OS, 0, 5)) == "linux");
154+
}
155+
156+
157+
158+
/**
159+
* @method prepareExecCommand
160+
* will prepare an executable application and params for command line
161+
* execution, including escaping and quoting arguments.
162+
* @param $app_name, the application to be executed
163+
* @param $params, an array of arguments to the application
164+
* @return the full command line command, escaped and quoted, will throw a
165+
* Zend_Exception if the app is not in the path and not executable
166+
*/
167+
public static function prepareExecCommand($app_name, $params = array())
168+
{
169+
// Check if application is executable, if not, see if you can find it
170+
// in the path
171+
if(!KWUtils::isExecutable($app_name, false))
172+
{
173+
$app_name = KWUtils::findApp($app_name, true);
174+
}
175+
176+
// escape parameters
177+
$escapedParams = array();
178+
foreach($params as $param)
179+
{
180+
$escapedParams[] = escapeshellarg($param);
181+
}
182+
183+
// glue together app_name and params using spaces
184+
return escapeshellarg($app_name)." ".implode(" ", $escapedParams);
185+
}
186+
187+
188+
/**
189+
* @method isExecutable will return true if the app can be found and is
190+
* executable, can optionally look in the path.
191+
* @param string $app_name, the app to check
192+
* @param boolean $check_in_path, if true, will search in path for app
193+
* @return True if app_name is found and executable, False otherwise
194+
*/
195+
public static function isExecutable($app_name, $check_in_path = false)
196+
{
197+
if(!is_executable($app_name ))
198+
{
199+
if($check_in_path)
200+
{
201+
try
202+
{
203+
if(KWUtils::findApp($app_name, true))
204+
{
205+
return true;
206+
}
207+
}
208+
catch(Zend_Exception $ze)
209+
{
210+
return false;
211+
}
212+
}
213+
return false;
214+
}
215+
return true;
216+
}
217+
218+
/**
219+
* @method findApp will return the absolute path of an application
220+
* @param $app_name, the name of the application
221+
* @param $check_execution_flag, whether to include in the check that the
222+
* application is executable
223+
* @return the path to the application, throws a Zend_Exception if the app
224+
* can't be found, or if $check_execution_flag is set and the app is not
225+
* executable.
226+
*/
227+
public static function findApp($app_name, $check_execution_flag )
228+
{
229+
$PHP_PATH_SEPARATOR = ":";
230+
// split path
231+
$path_list = explode($PHP_PATH_SEPARATOR, getenv("PATH"));
232+
233+
// loop through paths
234+
foreach($path_list as $path)
235+
{
236+
$status = false;
237+
$path_to_app = KWUtils::appendStringIfNot($path, DIRECTORY_SEPARATOR).$app_name;
238+
if($check_execution_flag)
239+
{
240+
if(is_executable($path_to_app))
241+
{
242+
$status = true;
243+
break;
244+
}
245+
}
246+
else
247+
{
248+
if(file_exists($path_to_app))
249+
{
250+
$status = true;
251+
break;
252+
}
253+
}
254+
}
255+
if(!$status)
256+
{
257+
throw new Zend_Exception("Failed to locate the application: [".$app_name."] [check_execution_flag:".$check_execution_flag."]");
258+
}
259+
return $path_to_app;
260+
}
261+
262+
263+
264+
265+
/**
266+
* @method formatAppName
267+
* Format the application name according to the platform.
268+
*/
269+
public static function formatAppName($app_name)
270+
{
271+
if(substr(PHP_OS, 0, 3) == "WIN")
272+
{
273+
$app_name = KWUtils::appendStringIfNot($app_name, ".exe");
274+
}
275+
return $app_name;
276+
}
277+
278+
279+
}

modules/batchmake/Notification.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ class Batchmake_Notification extends MIDAS_Notification
2121
public function init()
2222
{
2323
$this->addCallBack('CALLBACK_CORE_GET_DASHBOARD', 'getDashboard');
24+
$this->addCallBack('CALLBACK_CORE_GET_LEFT_LINKS', 'getLeftLink');
2425
}//end init
2526

26-
27-
/** generate Dashboard information */
27+
28+
/**
29+
*@method getDashboard
30+
* will generate information about this module to display on the Dashboard
31+
*@return array with key being a string describing if the configuration of
32+
* the module is correct or not, and value being a 1/0 for the same info.
33+
*/
2834
public function getDashboard()
2935
{
3036
$return = array();
@@ -38,5 +44,21 @@ public function getDashboard()
3844
}
3945
return $return;
4046
}
47+
48+
49+
/**
50+
*@method getLeftLink
51+
* will generate a link for this module to be displayed in the main view.
52+
*@return ['batchmake' => [ link to batchmake module, module icon image path]]
53+
*/
54+
public function getLeftLink()
55+
{
56+
$fc = Zend_Controller_Front::getInstance();
57+
$moduleWebroot = $fc->getBaseUrl() . MIDAS_BATCHMAKE_MODULE;
58+
return array(ucfirst(MIDAS_BATCHMAKE_MODULE) => array($moduleWebroot, $moduleWebRoot . '/public/images/cmake.png'));
59+
}
60+
4161
} //end class
62+
63+
4264
?>

modules/batchmake/constant/module.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
define("MIDAS_BATCHMAKE_CHECK_IF_CHMODABLE", 0x10);
1717
define("MIDAS_BATCHMAKE_CHECK_IF_CHMODABLE_RW", 0x16); // 0x10 + 0x6
1818

19-
define("MIDAS_BATCHMAKE_DEFAULT_MKDIR_MODE", 0775);
19+
2020

2121
// Condor executables
2222
define("MIDAS_BATCHMAKE_CONDOR_STATUS", "condor_status");
@@ -26,7 +26,10 @@
2626

2727
// Batchmake executable
2828
define("MIDAS_BATCHMAKE_EXE", "BatchMake");
29-
29+
// Batchmake temporary execution dir
30+
define("MIDAS_BATCHMAKE_SSP_DIR", "SSP");
31+
32+
3033
// Extension automatically appended to dagman
3134
// description file when 'condor_dag_submit' generates it
3235
define("MIDAS_BATCHMAKE_CONDOR_DAGMAN_EXT", ".condor.sub");
@@ -88,7 +91,10 @@
8891
define("MIDAS_BATCHMAKE_NOT_FOUND_ON_CURRENT_SYSTEM_STRING", 'Not found on the current system');
8992
define("MIDAS_BATCHMAKE_FILE_OR_DIRECTORY_DOESNT_EXIST_STRING", "File or directory doesn't exist:");
9093

91-
94+
define("MIDAS_BATCHMAKE_NO_SCRIPT_SPECIFIED", "No script specified");
95+
define("MIDAS_BATCHMAKE_NO_SCRIPT_FOUND", "No script found at: ");
96+
define("MIDAS_BATCHMAKE_CREATE_TMP_DIR_FAILED", "Failed to create temporary directory: ");
97+
define("MIDAS_BATCHMAKE_SYMLINK_FAILED", "Failed to create symbolic link: ");
9298

9399

94100
// property keys

modules/batchmake/controllers/ConfigController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ public function testconfigAction()
136136
}
137137

138138
$config_status = $this->ModuleComponent->KWBatchmake->testconfig($configPropertiesParamVals);
139-
echo JsonComponent::encode($config_status);
139+
$jsonout = JsonComponent::encode($config_status);
140+
echo $jsonout;
141+
// echo JsonComponent::encode($config_status);
140142
}//end testconfigAction
141143

142144

0 commit comments

Comments
 (0)