Class: MobyUtil::FileHelper

Inherits:
Object show all
Defined in:
lib/tdriver/util/common/file.rb

Class Method Summary collapse

Class Method Details

.copy_file(source, destination, verbose = false, overwrite = true, create_folders = true, &block) ⇒ Object

Function for copy file(s) to destination folder, folder will be created if it doesn’t exist.

params

source

Array or String containing source filename(s)

destination

String containing of destination folder

args

String containing arguments used for copying

file

String target filename, used to rename source filename on destination

returns

NilClass

raises

IOError

Error occured while creating folder



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/tdriver/util/common/file.rb', line 248

def self.copy_file( source, destination, verbose = false, overwrite = true, create_folders = true, &block )

  source.check_type( String, "Wrong argument type $1 for source file (expected $2)" )
  
  destination.check_type( String, "Wrong argument type $1 for destination file (expected $2)" )

  sources = []

  if File.directory?( source )

    source_folders = source if ( source_folders = MobyUtil::FileHelper.folder_tree( source ) ).empty?

    source_folders.each{ | folder |

      Dir.glob( File.join( folder, "*.*" ) ).each{ | file | 

        sources << [ file, File.join( destination, folder.gsub( source, "" ), "/", File.basename( file ) ) ] 
      }
    }

  else

    if File.basename( source ) =~ /\*/ 
      # retrieve all files when wildcards used
      Dir.glob( File.join( File.dirname( source ), File.basename( source ) ) ).each{ | file | sources << [ file, File.join( destination, File.basename( file ) ) ] }
    else
      # no wildcards
      sources << [ source, destination ]
    end

  end

  sources.each{ | task |

    source, destination = task

    destination_folder = ( destination = MobyUtil::FileHelper.fix_path( destination ) )[ -1 ].chr =~ /[\\\/]/ ? destination : File.dirname( destination )

    # create destination folder if it doesn't exist and create_folders flag is enabled
    MobyUtil::FileHelper.mkdir_path( destination_folder ) if create_folders

    raise RuntimeError.new( "Unable to copy #{ source } to #{ destination } due to source file does not exist" ) unless File.exist?( source )

    ::FileUtils.copy( 

      MobyUtil::FileHelper.fix_path( source ), 
      destination, 
      :verbose => verbose

    ) unless ( !overwrite && File.exist?( destination ) )
  
    # yield given block, can be used eg. changing the target file's access levels etc.
    yield( destination, source, destination_folder ) if block_given?

  }

end

.expand_path(file_path) ⇒ Object

Function for expand tdriver specific relative file paths

params

file_path

String containing (file-) path

returns

String

String containing expanded file path

raises



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tdriver/util/common/file.rb', line 148

def self.expand_path( file_path )

  _file_path = file_path # we don't want to modify original variable

  return _file_path if _file_path.nil? || _file_path.empty?

  _file_path = MobyUtil::FileHelper.fix_path( _file_path )

  File.expand_path( MobyUtil::FileHelper.is_relative_path?( _file_path ) ? File.join( MobyUtil::FileHelper.tdriver_home, _file_path ) : _file_path )

end

.fix_path(path) ⇒ Object

Function to fix folder/file path, e.g. remove duplicate back-/slashes

params

logger_instance

Instance of TDriver logger

returns

String

String presentation of fixed path



87
88
89
90
91
92
93
94
# File 'lib/tdriver/util/common/file.rb', line 87

def self.fix_path( path )      

  path.check_type( String, "Wrong argument type $1 for file path (expected $2)" )

  # replace back-/slashes to File::SEPARATOR
  path.gsub( /[\\\/]/, File::SEPARATOR ).to_s

end

.folder_exist?(path) ⇒ Boolean

Function to verify that given folder exists

params

path

String containing path

returns

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tdriver/util/common/file.rb', line 30

def self.folder_exist?( path )

  begin

    Dir.entries( path ).kind_of?( Array )

  rescue

    false

  end

end

.folder_tree(source, folders = []) ⇒ Object

Function for build list of sub-/folder(s) from given source folder

params

source

String containing source folder

folders

Array internal variable, used in recursion, can be used as result value if given variable is empty array.

returns

Array list of folder names

raises



313
314
315
316
317
318
319
320
321
# File 'lib/tdriver/util/common/file.rb', line 313

def self.folder_tree( source, folders = [] )

  Dir.glob( source + "/*" ).each{ | folder |
    if File.directory?( folder ); folders << folder; MobyUtil::FileHelper.folder_tree( folder, folders ); end
  }

  folders.uniq.compact

end

.get_file(file_path) ⇒ Object

Function for retrieve tdriver configuration file content from absolute or tdrive home path If relative path given, assume that file is located in TDriver home folder

params

file_path

String containing the name and path of file

returns

String

File content

raises

ParameterFileNotFoundError - if empty parameter or file doesn’t exist

ParameterFileParseError

If parsing failes



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/tdriver/util/common/file.rb', line 169

def self.get_file( file_path )

  #p __method__, file_path, caller

  #file_path.check_type( String, "wrong argument type $1 for get_file method (expected $2)")

  # raise exception if file name is empty or nil
  raise EmptyFilenameError, "File name is empty or not defined" if file_path.nil? || file_path.to_s.empty?

  # raise exception if file name is file_path variable format other than string
  raise UnexpectedVariableTypeError.new( "Invalid filename format #{ file_path.class } (expected: String)")  if !file_path.kind_of?( String )

  file_path = MobyUtil::FileHelper.expand_path( file_path )

  # raise exception if file not found
  raise FileNotFoundError.new( "File not found: #{ file_path }" ) unless File.exist?( file_path )

  begin
    # read all content of file
    file_content = IO.read( file_path )
  rescue => ex
    # raise exception if error occured during reading the file
    raise IOError.new("Error occured while reading file #{ file_path }\nDescription: #{ ex.message }")
  end

  # return file content
  file_content

end

.is_relative_path?(path) ⇒ Boolean

Function for expand tdriver specific relative file paths

params

file_path

String containing (file-) path

returns

String

String containing expanded file path

raises

TypeError

Wrong argument type <class> for file path (expected <class>)

ArgumentError

Given path is empty

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tdriver/util/common/file.rb', line 119

def self.is_relative_path?( path )  

  path.check_type( String, "Wrong argument type $1 for file path (expected $2)" )

  #raise ArgumentError.new("Given path is empty") if path.empty?
  path.not_empty( "Filepath must not be empty string" )

  dirname =  File.dirname( path )

  if MobyUtil::EnvironmentHelper.windows?

    # windows
    ( dirname =~ /^[a-z]+:(\\|\/)/i ).nil? && ( dirname[ 0 ].chr =~ /(\\|\/)/ ).nil?    

  else

    # linux
    ( path[ 0 ].chr != '~' ) && ( dirname[ 0 ].chr != File::SEPARATOR )

  end

end

.load_modules(*path) ⇒ Object

Function to load dynamically ruby module(s) from given path

params

path

String containing path

returns



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tdriver/util/common/file.rb', line 48

def self.load_modules( *path )

  # deterimine caller methods working folder
  source_path = File.dirname( MobyUtil::KernelHelper.parse_caller( caller(3).first ).first )
  
  # Compatiblity for Ruby 1.9.2 caller format
  if source_path == "." || source_path[0].to_s == "<" 
    source_path = File.dirname( MobyUtil::KernelHelper.parse_caller( caller(2).first ).first )      
  end
  
  path.each{ | path |

    # expand path if given path is relative path
    path = File.join( source_path, path ) if is_relative_path? path

    # automatically load ruby implementation files if given path is folder
    path = File.join( path, '*.rb' ) if File.directory?( path ) 

    require_files = Dir.glob( MobyUtil::FileHelper.fix_path( path ) )

    raise RuntimeError, "File not found #{ path }" if !File.directory?( path ) && !File.file?( path ) && require_files.empty?

    # load each module found from given folder
    require_files.each { | module_name | 

      # load implementation file
      require module_name

    }

  }

end

.mkdir_path(path) ⇒ Object

Function for create (nested) folder path

params

path

String containing path to be created

returns

NilClass

raises

IOError

Error occured while creating folder



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/tdriver/util/common/file.rb', line 206

def self.mkdir_path( path )

  begin

    current_path = ""

    path.split( File::SEPARATOR ).each{ | folder | 

      if !folder.empty?

        current_path << folder << File::SEPARATOR

        Dir.mkdir( current_path ) unless File.exist?( current_path )

      else

        current_path << File::SEPARATOR

      end  

    } unless File.directory?( path ) && File.exist?( path )

  rescue => exception

    raise IOError.new("Error occured while creating folder #{ current_path } (#{ exception.message })")

  end

  nil

end

.tdriver_homeObject

Private helper function to set tdriver_home directory depending on os. Known possible issue: javaruby or similar, will not return correctly

params

returns

String

String presentation of TDriver home directory



101
102
103
104
105
106
107
108
109
# File 'lib/tdriver/util/common/file.rb', line 101

def self.tdriver_home

  File.expand_path(
    MobyUtil::FileHelper.fix_path( 
      ENV['TDRIVER_HOME'] || ( MobyUtil::EnvironmentHelper.windows? ? "c:/tdriver" : "/tmp/cutedriver/etc/tdriver" ) 
    )
  )

end