rUtilAnts 0.3.0.20110825 → 1.0.0.20120223

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2009 - 2011 Muriel Salvan (murielsalvan@users.sourceforge.net)
2
+ # Copyright (c) 2009 - 2012 Muriel Salvan (muriel@x-aeon.com)
3
3
  # Licensed under the terms specified in LICENSE file. No warranty is provided.
4
4
  #++
5
5
 
@@ -8,20 +8,20 @@ module RUtilAnts
8
8
  module Misc
9
9
 
10
10
  # Set these methods into the Object namespace
11
- def self.initializeMisc
11
+ def self.install_misc_on_object
12
12
  Object.module_eval('include RUtilAnts::Misc')
13
13
  end
14
14
 
15
15
  # Cache the access to a given code result, based on the caller ID
16
- # This can be used to cache variables computation (ex. lVar = cachedVar{ lengthyFctToComputeVar } )
16
+ # This can be used to cache variables computation (ex. lVar = cached_var{ lengthyFctToComputeVar } )
17
17
  #
18
- # Parameters:
18
+ # Parameters::
19
19
  # * *&iCode* (_CodeBlock_): The code called to compute the result
20
- # ** Return:
21
- # *** _Object_: The computed result
22
- # Return:
20
+ # * Return::
21
+ # * _Object_: The computed result
22
+ # Return::
23
23
  # * _Object_: The result of the code
24
- def cachedVar(&iCode)
24
+ def cached_var(&iCode)
25
25
  if (defined?(@RUtilAnts_Misc_CachedVars) == nil)
26
26
  @RUtilAnts_Misc_CachedVars = {}
27
27
  end
@@ -36,13 +36,13 @@ module RUtilAnts
36
36
 
37
37
  # Get a valid file name, taking into account platform specifically prohibited characters in file names.
38
38
  #
39
- # Parameters:
39
+ # Parameters::
40
40
  # * *iFileName* (_String_): The original file name wanted
41
- # Return:
41
+ # Return::
42
42
  # * _String_: The correct file name
43
- def getValidFileName(iFileName)
44
- if ((defined?($rUtilAnts_Platform_Info) != nil))
45
- return iFileName.gsub(/[#{Regexp.escape($rUtilAnts_Platform_Info.getProhibitedFileNamesCharacters)}]/, '_')
43
+ def get_valid_file_name(iFileName)
44
+ if (defined?(getProhibitedFileNamesCharacters) != nil)
45
+ return iFileName.gsub(/[#{Regexp.escape(getProhibitedFileNamesCharacters)}]/, '_')
46
46
  else
47
47
  return iFileName
48
48
  end
@@ -50,24 +50,24 @@ module RUtilAnts
50
50
 
51
51
  # Extract a Zip archive in a given system dependent lib sub-directory
52
52
  #
53
- # Parameters:
53
+ # Parameters::
54
54
  # * *iZipFileName* (_String_): The zip file name to extract content from
55
55
  # * *iDirName* (_String_): The name of the directory to store the zip to
56
- # Return:
56
+ # Return::
57
57
  # * _Exception_: Error, or nil in case of success
58
- def extractZipFile(iZipFileName, iDirName)
58
+ def extract_zip_file(iZipFileName, iDirName)
59
59
  rError = nil
60
60
 
61
61
  # Use RDI if possible to ensure the dependencies on zlib.dll and rubyzip
62
62
  if (defined?(RDI) != nil)
63
- lRDIInstaller = RDI::Installer.getMainInstance
63
+ lRDIInstaller = RDI::Installer.get_main_instance
64
64
  if (lRDIInstaller != nil)
65
65
  # First, test that the DLL exists.
66
66
  # If it does not exist, we can't install it, because ZLib.dll is downloadable only in ZIP format (kind of stupid ;-) )
67
67
  lDLLDep = nil
68
- case $rUtilAnts_Platform_Info.os
68
+ case os
69
69
  when OS_WINDOWS
70
- lDLLDep = RDI::Model::DependencyDescription.new('ZLib DLL').addDescription( {
70
+ lDLLDep = RDI::Model::DependencyDescription.new('ZLib DLL').add_description( {
71
71
  :Testers => [
72
72
  {
73
73
  :Type => 'DynamicLibraries',
@@ -78,21 +78,21 @@ module RUtilAnts
78
78
  :Installers => []
79
79
  } )
80
80
  else
81
- logBug "Sorry, installing ZLib on your platform #{$rUtilAnts_Platform_Info.os} is not yet supported."
81
+ log_bug "Sorry, installing ZLib on your platform #{os} is not yet supported."
82
82
  end
83
83
  if ((lDLLDep != nil) and
84
- (!lRDIInstaller.testDependency(lDLLDep)))
84
+ (!lRDIInstaller.test_dependency(lDLLDep)))
85
85
  # Try adding the default local location for libraries
86
- lRDIInstaller.ensureLocationInContext('LibraryPath', lRDIInstaller.getDefaultInstallLocation('Download', RDI::DEST_LOCAL))
86
+ lRDIInstaller.ensure_location_in_context('LibraryPath', lRDIInstaller.get_default_install_location('Download', RDI::DEST_LOCAL))
87
87
  # Try again
88
- if (!lRDIInstaller.testDependency(lDLLDep))
89
- logErr "zlib.dll is not installed in your system.\nUnfortunately RDI can't help because the only way to install it is to download it through a ZIP file.\nPlease install it manually from http://zlib.net (you can do it now and click OK once it is installed)."
88
+ if (!lRDIInstaller.test_dependency(lDLLDep))
89
+ log_err "zlib.dll is not installed in your system.\nUnfortunately RDI can't help because the only way to install it is to download it through a ZIP file.\nPlease install it manually from http://zlib.net (you can do it now and click OK once it is installed)."
90
90
  end
91
91
  end
92
92
  # Then, ensure the gem dependency
93
- rError, lCMApplied, lIgnored, lUnresolved = lRDIInstaller.ensureDependencies(
93
+ rError, lCMApplied, lIgnored, lUnresolved = lRDIInstaller.ensure_dependencies(
94
94
  [
95
- RDI::Model::DependencyDescription.new('RubyZip').addDescription( {
95
+ RDI::Model::DependencyDescription.new('RubyZip').add_description( {
96
96
  :Testers => [
97
97
  {
98
98
  :Type => 'RubyRequires',
@@ -152,10 +152,10 @@ module RUtilAnts
152
152
  # Execute a code block after having changed current directory.
153
153
  # Ensure the directory will be changed back at the end of the block, even if exceptions are thrown.
154
154
  #
155
- # Parameters:
155
+ # Parameters::
156
156
  # * *iDir* (_String_): The directory to change into
157
157
  # * *CodeBlock*: Code called once the current directory has been changed
158
- def changeDir(iDir)
158
+ def change_dir(iDir)
159
159
  lOldDir = Dir.getwd
160
160
  Dir.chdir(iDir)
161
161
  begin
@@ -167,7 +167,7 @@ module RUtilAnts
167
167
  Dir.chdir(lOldDir)
168
168
  end
169
169
 
170
- # Constants used for fileMutex
170
+ # Constants used for file_mutex
171
171
  # There was no lock on the mutex
172
172
  FILEMUTEX_NO_LOCK = 0
173
173
  # There was a lock on the mutex, but former process did not exist anymore
@@ -178,36 +178,36 @@ module RUtilAnts
178
178
  FILEMUTEX_INVALID_LOCK = 3
179
179
  # Execute a code block protected by a file mutex
180
180
  #
181
- # Parameters:
181
+ # Parameters::
182
182
  # * *iProcessID* (_String_): Process ID to be used to identify the mutex
183
183
  # * *CodeBlock*: The code called if the mutex is taken
184
- # Return:
184
+ # Return::
185
185
  # * _Integer_: Error code
186
- def fileMutex(iProcessID)
186
+ def file_mutex(iProcessID)
187
187
  rResult = FILEMUTEX_NO_LOCK
188
188
 
189
189
  # Prevent concurrent execution
190
190
  require 'tmpdir'
191
191
  lLockFile = "#{Dir.tmpdir}/FileMutex_#{iProcessID}.lock"
192
192
  if (File.exists?(lLockFile))
193
- logErr "Another instance of process #{iProcessID} is already running. Delete file #{lLockFile} if it is not."
193
+ log_err "Another instance of process #{iProcessID} is already running. Delete file #{lLockFile} if it is not."
194
194
  begin
195
195
  lDetails = nil
196
196
  File.open(lLockFile, 'r') do |iFile|
197
197
  lDetails = eval(iFile.read)
198
198
  end
199
- logErr "Details of the running instance: #{lDetails.inspect}"
199
+ log_err "Details of the running instance: #{lDetails.inspect}"
200
200
  # If the process does not exist anymore, remove the lock file
201
201
  # TODO: Adapt this to non Unix systems
202
202
  if (File.exists?("/proc/#{lDetails[:PID]}"))
203
203
  rResult = FILEMUTEX_LOCK_TAKEN
204
204
  else
205
- logErr "Process #{lDetails[:PID]} does not exist anymore. Removing lock file."
205
+ log_err "Process #{lDetails[:PID]} does not exist anymore. Removing lock file."
206
206
  File.unlink(lLockFile)
207
207
  rResult = FILEMUTEX_ZOMBIE_LOCK
208
208
  end
209
209
  rescue Exception
210
- logErr "Invalid lock file #{lLockFile}: #{$!}."
210
+ log_err "Invalid lock file #{lLockFile}: #{$!}."
211
211
  rResult = FILEMUTEX_INVALID_LOCK
212
212
  end
213
213
  end
@@ -229,7 +229,7 @@ module RUtilAnts
229
229
  begin
230
230
  File.unlink(lLockFile)
231
231
  rescue Exception
232
- logErr "Exception while deleting lock file #{lLockFile}: #{$!}"
232
+ log_err "Exception while deleting lock file #{lLockFile}: #{$!}"
233
233
  end
234
234
  raise
235
235
  end
@@ -238,6 +238,39 @@ module RUtilAnts
238
238
  return rResult
239
239
  end
240
240
 
241
+ # Replace variables of the form %{VariableName} from a string.
242
+ # Take values from a hash having :VariableName => 'VariableValue'.
243
+ # Works also recursively if a variable value contains again %{OtherVariableName}.
244
+ # Does not check if variables really exist (will replace any unknown variable with '').
245
+ #
246
+ # Parameters::
247
+ # * *iStr* (_String_): The string to replace from
248
+ # * *iVars* (<em>map<Symbol,String></em>): The variables
249
+ # Return::
250
+ # * _String_: The string replaced
251
+ def replace_vars(iStr, iVars)
252
+ rStr = iStr
253
+
254
+ lFinished = false
255
+ while (!lFinished)
256
+ lMatch = rStr.match(/^(.*[^%])%\{([^\}]+)\}(.*)$/)
257
+ if (lMatch == nil)
258
+ # Try with %{ at the beginning of the string
259
+ lMatch = rStr.match(/^%\{([^\}]+)\}(.*)$/)
260
+ if (lMatch == nil)
261
+ # No more occurrences
262
+ lFinished = true
263
+ else
264
+ rStr = "#{iVars[lMatch[1].to_sym]}#{lMatch[2]}"
265
+ end
266
+ else
267
+ rStr = "#{lMatch[1]}#{iVars[lMatch[2].to_sym]}#{lMatch[3]}"
268
+ end
269
+ end
270
+
271
+ return rStr
272
+ end
273
+
241
274
  end
242
275
 
243
276
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2011 Muriel Salvan (murielsalvan@users.sourceforge.net)
2
+ # Copyright (c) 2011 - 2012 Muriel Salvan (muriel@x-aeon.com)
3
3
  # Licensed under the terms specified in LICENSE file. No warranty is provided.
4
4
  #++
5
5
 
@@ -17,7 +17,7 @@ module RUtilAnts
17
17
  end
18
18
 
19
19
  # Set these methods into the Object namespace
20
- def self.initializeMySQLPool
20
+ def self.install_mysql_pool_on_object
21
21
  Object.module_eval('include RUtilAnts::MySQLPool')
22
22
  end
23
23
 
@@ -26,15 +26,15 @@ module RUtilAnts
26
26
  # Reuse existing connections.
27
27
  # If the given password is nil, we force the reuse of an existing connection.
28
28
  #
29
- # Parameters:
29
+ # Parameters::
30
30
  # * *iHost* (_String_): The host to connect to
31
31
  # * *iDBName* (_String_): The MySQL database name to connect to
32
32
  # * *iUser* (_String_): The user
33
33
  # * *iPassword* (_String_): The password. If nil, this will only try to reuse an existing connection [optional = nil]
34
- # Return:
34
+ # Return::
35
35
  # * _Exception_: An error, or nil in case of success
36
36
  # * _MySQL_: The MySQL connection, or nil in case of failure
37
- def connectToMySQL(iHost, iDBName, iUser, iPassword = nil)
37
+ def connect_to_mysql(iHost, iDBName, iUser, iPassword = nil)
38
38
  rError = nil
39
39
  rMySQL = nil
40
40
 
@@ -59,7 +59,7 @@ module RUtilAnts
59
59
  # lMySQL.options(Mysql::OPT_WRITE_TIMEOUT, 28800)
60
60
  # lMySQL.real_connect(iHost, iUser, iPassword, iDBName)
61
61
  rescue Exception
62
- logErr "Error while creating MySQL connection to #{lDBKey.inspect}: #{$!}.\n#{$!.backtrace.join("\n")}"
62
+ log_err "Error while creating MySQL connection to #{lDBKey.inspect}: #{$!}.\n#{$!.backtrace.join("\n")}"
63
63
  rError = $!
64
64
  lMySQL = nil
65
65
  end
@@ -80,11 +80,11 @@ module RUtilAnts
80
80
  return rError, rMySQL
81
81
  end
82
82
 
83
- # Close a MySQL connection created with connectToMySQL
83
+ # Close a MySQL connection created with connect_to_mysql
84
84
  #
85
- # Parameters:
85
+ # Parameters::
86
86
  # * *iMySQLConnection* (_MySQL_): The MySQL connection to close
87
- def closeMySQL(iMySQLConnection)
87
+ def close_mysql(iMySQLConnection)
88
88
  # Find the connection
89
89
  if (defined?($RUtilAnts_MySQLPool_Pool) == nil)
90
90
  $RUtilAnts_MySQLPool_Pool = {}
@@ -100,24 +100,24 @@ module RUtilAnts
100
100
 
101
101
  # Setup a MySQL connection, and ensure it is closed once the client code ends.
102
102
  #
103
- # Parameters:
103
+ # Parameters::
104
104
  # * *iHost* (_String_): The host to connect to
105
105
  # * *iDBName* (_String_): The MySQL database name to connect to
106
106
  # * *iUser* (_String_): The user
107
107
  # * *iPassword* (_String_): The password. If nil, this will only try to reuse an existing connection [optional = nil]
108
108
  # * *CodeBlock*: The code executed with the MySQL connection
109
- # ** *iMySQL* (_MySQL_): The MySQL connection
110
- # Return:
109
+ # * *iMySQL* (_MySQL_): The MySQL connection
110
+ # Return::
111
111
  # * _Exception_: An error, or nil in case of success
112
- def setupMySQLConnection(iHost, iDBName, iUser, iPassword = nil)
112
+ def setup_mysql_connection(iHost, iDBName, iUser, iPassword = nil)
113
113
  rError = nil
114
114
 
115
- rError, lMySQL = connectToMySQL(iHost, iDBName, iUser, iPassword)
115
+ rError, lMySQL = connect_to_mysql(iHost, iDBName, iUser, iPassword)
116
116
  if (rError == nil)
117
117
  begin
118
118
  yield(lMySQL)
119
119
  ensure
120
- closeMySQL(lMySQL)
120
+ close_mysql(lMySQL)
121
121
  end
122
122
  end
123
123
 
@@ -127,16 +127,16 @@ module RUtilAnts
127
127
  # Get a prepared statement for a given SQL string of a given MySQL connection.
128
128
  # Use the cache of prepared statements.
129
129
  #
130
- # Parameters:
130
+ # Parameters::
131
131
  # * *iMySQLConnection* (_MySQL_): The MySQL connection
132
132
  # * *iStrSQL* (_String_): The SQL statement to prepare
133
133
  # * *iAdditionalOptions* (<em>map<Symbol,Object></em>): Additional options [optional = {}]
134
- # ** *:LeaveOpen* (_Boolean_): Do we NOT close the opened statement once it is not used anymore ? [optional = false]
135
- # Return:
134
+ # * *:leave_open* (_Boolean_): Do we NOT close the opened statement once it is not used anymore ? [optional = false]
135
+ # Return::
136
136
  # * <em>MySQL::Statement</em>: The MySQL statement
137
- def getPreparedStatement(iMySQLConnection, iStrSQL, iAdditionalOptions = {})
137
+ def get_prepared_statement(iMySQLConnection, iStrSQL, iAdditionalOptions = {})
138
138
  # Parse options
139
- lLeaveOpen = iAdditionalOptions[:LeaveOpen] || false
139
+ lLeaveOpen = iAdditionalOptions[:leave_open] || false
140
140
  # Find the prepared statements set
141
141
  lPreparedStatements = $RUtilAnts_MySQLPool_Pool[findMySQLConnectionKey(iMySQLConnection)][2]
142
142
  if (lPreparedStatements[iStrSQL] == nil)
@@ -157,12 +157,12 @@ module RUtilAnts
157
157
  return lPreparedStatements[iStrSQL][0]
158
158
  end
159
159
 
160
- # Close a previously created prepared statement using getPreparedStatement.
160
+ # Close a previously created prepared statement using get_prepared_statement.
161
161
  #
162
- # Parameters:
162
+ # Parameters::
163
163
  # * *iMySQLConnection* (_MySQL_): The MySQL connection
164
164
  # * *iPreparedStatement* (<em>MySQL::Statement</em>): The MySQL prepared statement
165
- def closePreparedStatement(iMySQLConnection, iPreparedStatement)
165
+ def close_prepared_statement(iMySQLConnection, iPreparedStatement)
166
166
  lPreparedStatements = $RUtilAnts_MySQLPool_Pool[findMySQLConnectionKey(iMySQLConnection)][2]
167
167
  lFound = false
168
168
  lPreparedStatements.each do |iStrSQL, ioPreparedStatementInfo|
@@ -186,19 +186,19 @@ module RUtilAnts
186
186
 
187
187
  # Setup a prepared statement, and ensure it will be closed
188
188
  #
189
- # Parameters:
189
+ # Parameters::
190
190
  # * *iMySQLConnection* (_MySQL_): The MySQL connection
191
191
  # * *iStrSQL* (_String_): The SQL statement to prepare
192
192
  # * *iAdditionalOptions* (<em>map<Symbol,Object></em>): Additional options [optional = {}]
193
- # ** *:LeaveOpen* (_Boolean_): Do we NOT close the opened statement once it is not used anymore ? [optional = false]
193
+ # * *:leave_open* (_Boolean_): Do we NOT close the opened statement once it is not used anymore ? [optional = false]
194
194
  # * *CodeBlock*: The code executed once the statement is prepared
195
- # ** *iPreparedStatement* (<em>MySQL::Statement</em>): The prepared statement
196
- def setupPreparedStatement(iMySQLConnection, iStrSQL, iAdditionalOptions = {})
197
- lStatement = getPreparedStatement(iMySQLConnection, iStrSQL, iAdditionalOptions)
195
+ # * *iPreparedStatement* (<em>MySQL::Statement</em>): The prepared statement
196
+ def setup_prepared_statement(iMySQLConnection, iStrSQL, iAdditionalOptions = {})
197
+ lStatement = get_prepared_statement(iMySQLConnection, iStrSQL, iAdditionalOptions)
198
198
  begin
199
199
  yield(lStatement)
200
200
  ensure
201
- closePreparedStatement(iMySQLConnection, lStatement)
201
+ close_prepared_statement(iMySQLConnection, lStatement)
202
202
  end
203
203
  end
204
204
 
@@ -206,9 +206,9 @@ module RUtilAnts
206
206
 
207
207
  # Find the MySQL connection key
208
208
  #
209
- # Parameters:
209
+ # Parameters::
210
210
  # * *iMySQLConnection* (_MySQL_): The MySQL connection
211
- # Return:
211
+ # Return::
212
212
  # * _String_: Host name, or nil if none
213
213
  # * _String_: DB name
214
214
  # * _String_: User name
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2009 - 2011 Muriel Salvan (murielsalvan@users.sourceforge.net)
2
+ # Copyright (c) 2009 - 2012 Muriel Salvan (muriel@x-aeon.com)
3
3
  # Licensed under the terms specified in LICENSE file. No warranty is provided.
4
4
  #++
5
5
 
@@ -7,27 +7,44 @@ module RUtilAnts
7
7
 
8
8
  module Platform
9
9
 
10
+ module PlatformInterface
11
+
12
+ # Initialize the module
13
+ def self.includePlatformSpecificModule
14
+ # Require the platform info
15
+ begin
16
+ require "rUtilAnts/Platforms/#{RUBY_PLATFORM}/PlatformInfo"
17
+ rescue Exception
18
+ if (!defined?(log_bug))
19
+ require 'rUtilAnts/Logging'
20
+ RUtilAnts::Logging::install_logger_on_object
21
+ end
22
+ log_bug "Current platform #{RUBY_PLATFORM} is not supported (#{$!})."
23
+ raise RuntimeError, "Current platform #{RUBY_PLATFORM} is not supported (#{$!})."
24
+ end
25
+ # Include the platform specific module
26
+ PlatformInterface.module_eval('include RUtilAnts::Platform::PlatformInfo')
27
+ end
28
+
29
+ includePlatformSpecificModule
30
+
31
+ end
32
+
33
+ class Manager
34
+
35
+ include PlatformInterface
36
+
37
+ end
38
+
10
39
  # OS constants
11
40
  OS_WINDOWS = 0
12
41
  OS_LINUX = 1
13
42
  OS_CYGWIN = 2
14
43
 
15
44
  # Initialize the platform info
16
- def self.initializePlatform
17
- # Require the platform info
18
- begin
19
- require "rUtilAnts/Platforms/#{RUBY_PLATFORM}/PlatformInfo"
20
- rescue Exception
21
- if (!defined?(logBug))
22
- require 'rUtilAnts/Logging'
23
- RUtilAnts::Logging::initializeLogging(File.expand_path(File.dirname(__FILE__)), '')
24
- end
25
- logBug "Current platform #{RUBY_PLATFORM} is not supported (#{$!})."
26
- raise RuntimeError, "Current platform #{RUBY_PLATFORM} is not supported (#{$!})."
27
- end
28
- # Create the corresponding object
29
- $rUtilAnts_Platform_Info = PlatformInfo.new
30
- Object.module_eval('include RUtilAnts::Platform')
45
+ def self.install_platform_on_object
46
+ require 'rUtilAnts/SingletonProxy'
47
+ RUtilAnts::make_singleton_proxy(RUtilAnts::Platform::PlatformInterface, Object)
31
48
  end
32
49
 
33
50
  end