ykutils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e9bf4a1496614553e8da4558a59f381ca144e99c
4
+ data.tar.gz: 3cdd60d495a1479f954f7d918131f3a0de74b56f
5
+ SHA512:
6
+ metadata.gz: 45b7af05299dffc72a8a494ef9183176dff1b85243efa27774e3cc75b6478c7e56a06926128888a412206a22cfa858cd9bb42d05526deb0340bea549d572eff2
7
+ data.tar.gz: 107de770406966509cb4016d3a4842dcf651dfe7f19e61932bb8c639b1c3ea14499b7575d389432ec93b15e1d6e033abf6de07a759378adab8d6910cebe6aae4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ykutils.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Ykutils
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ykutils`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ykutils'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ykutils
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/ykutils/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ykutils"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/ykutils.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "ykutils/version"
2
+
3
+ module Ykutils
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,129 @@
1
+ require 'pp'
2
+
3
+ module Ykutils
4
+ module DebugUtils
5
+ class DebugMes
6
+ def DebugMes.init( debug = false )
7
+ @@debug = debug
8
+ @@buf = []
9
+ end
10
+
11
+ def DebugMes.set_debug( debug )
12
+ @@debug = debug
13
+ end
14
+
15
+ def DebugMes.puts_x( mes )
16
+ @@buf ||= []
17
+ @@buf << mes
18
+ # puts "#{@@buf.size} |#{mes}"
19
+
20
+ end
21
+
22
+ def DebugMes.get
23
+ buf = @@buf
24
+ # DebugMes.clear
25
+
26
+ buf
27
+ end
28
+
29
+ def DebugMes.clear
30
+ @@buf ||= []
31
+ @@buf.clear
32
+ end
33
+ end
34
+
35
+ def clear_d
36
+ DebugMes.clear
37
+ end
38
+
39
+ def puts_d(mes)
40
+ DebugMes.puts_x( mes )
41
+ # puts mes
42
+ end
43
+
44
+ def get_d
45
+ DebugMes.get
46
+ end
47
+
48
+ def puts_current_method
49
+ d_puts (caller(1)[0].split)[-1]
50
+ end
51
+
52
+ def error_exit(n)
53
+ puts "error(#{n})"
54
+ pp caller(0)
55
+ exit(n)
56
+ end
57
+
58
+ def debug_utils_init
59
+ set_debug( false )
60
+ set_warn( 0 )
61
+
62
+ DebugMes.init( false )
63
+ end
64
+
65
+ def set_debug( val )
66
+ @debugutils = val
67
+ DebugMes.set_debug( val )
68
+ end
69
+
70
+ def get_debug
71
+ @debugutils
72
+ end
73
+
74
+ def set_warn( val )
75
+ @warn = val
76
+ end
77
+
78
+ def debug
79
+ @debugutils
80
+ end
81
+
82
+ def d_exit( num )
83
+ exit( num ) if @debugutils
84
+ end
85
+
86
+ def d_puts( str )
87
+ puts(str) if @debugutils
88
+ end
89
+
90
+ def w1_puts( str )
91
+ unless @warn
92
+ set_warn( 0 )
93
+ end
94
+ puts(str) if @debugutils or @warn >= 1
95
+ end
96
+
97
+ def w2_puts( str )
98
+ unless @warn
99
+ set_warn( 0 )
100
+ end
101
+ puts(str) if @debugutils or @warn >= 2
102
+ end
103
+
104
+ def d_caller( num )
105
+ pp caller( 0 ) if @debugutils
106
+ end
107
+
108
+ def puts_no_empty( mes )
109
+ puts mes if mes != ""
110
+ end
111
+
112
+ def d_puts_no_empty( mes )
113
+ puts mes if mes != "" and @debugutils
114
+ end
115
+
116
+ def d_p( it )
117
+ p it if @debugutils
118
+ end
119
+
120
+ def d_pp( it )
121
+ if @debugutils
122
+ pp "@debugutils=#{@debugutils}"
123
+ pp caller(0)
124
+ exit
125
+ end
126
+ pp it if @debugutils
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,443 @@
1
+ require 'ykutils/stringutils'
2
+ require 'ykutils/retcodex'
3
+ require 'ykutils/debugutils'
4
+ require 'ykutils/fileoputils2'
5
+
6
+ module Ykutils
7
+ module FileOpUtils
8
+ include StringUtils
9
+ include DebugUtils
10
+
11
+ WRITABLE_FILE_PATH = 11
12
+ VALID_FILE_PATH = 10
13
+ ABNORMAL_STRING = -5
14
+ INVALID_FILE_PATH = -10
15
+ NOT_WRITABLE_FILE_PATH = -11
16
+
17
+ VALID_PATH = 20
18
+ INVALID_PATH = -20
19
+
20
+ def valid?
21
+ @valid
22
+ end
23
+
24
+ def valid_fpath( fname )
25
+ ret = VALID_FILE_PATH
26
+ if normal_string?( fname )
27
+ unless File.file?( fname )
28
+ ret = INVALID_FILE_PATH
29
+ end
30
+ else
31
+ ret = ABNORMAL_STRING
32
+ end
33
+ ret
34
+ end
35
+
36
+ def valid_path( path )
37
+ ret = VALID_PATH
38
+ if normal_string?( path )
39
+ unless File.exist?( path )
40
+ ret = INVALID_PATH
41
+ end
42
+ else
43
+ ret = ABNORMAL_STRING
44
+ end
45
+ ret
46
+ end
47
+
48
+ def valid_fpath_with_message( fname )
49
+ d_puts( "valid_fpath_with_message|fname=#{fname}" )
50
+
51
+ mes = ""
52
+ ret = valid_fpath(fname)
53
+ ret_bool = (ret == VALID_FILE_PATH ? true : false )
54
+
55
+ case ret
56
+ when ABNORMAL_STRING
57
+ mes = "abnormal string|#{fname}"
58
+ @valid = false
59
+
60
+ d_puts( "1 fname=#{fname}" )
61
+ d_caller(0)
62
+ when INVALID_PATH
63
+ mes = "Can't find #{fname}"
64
+ d_caller(0)
65
+ @valid = false
66
+
67
+ d_puts( "2 fname=#{fname}" )
68
+ d_caller(0)
69
+ else
70
+ mes = ""
71
+ d_puts( "3 fname=#{fname}" )
72
+ end
73
+ RetCode2.new( ret , ret_bool, mes )
74
+ end
75
+
76
+ def valid_fname?( fname )
77
+ ret = false
78
+ if normal_string?( fname )
79
+ if File.file?( fname )
80
+ ret = true
81
+ else
82
+ d_puts( "FileOpUtils:valid_fname? 2" )
83
+ end
84
+ else
85
+ d_puts( "FileOpUtils:valid_fname? 1" )
86
+ end
87
+ ret
88
+ end
89
+
90
+ def valid_readable_fname?( fname )
91
+ ret = false
92
+ if normal_string?( fname ) and File.file?( fname ) and File.readable?( fname )
93
+ ret = true
94
+ end
95
+ ret
96
+ end
97
+
98
+ def valid_writable_fpath_with_message( fname )
99
+ mes = ""
100
+
101
+ ret = valid_fpath(fname)
102
+
103
+ ret_bool = (ret == VALID_FILE_PATH ? true : false )
104
+
105
+ case ret
106
+ when ABNORMAL_STRING
107
+ mes = "abnormal string|#{fname}"
108
+ @valid = false
109
+ d_puts( "1 fname=#{fname}" )
110
+ d_caller(0)
111
+ when INVALID_FILE_PATH
112
+ mes = "Can't find #{fname}"
113
+ d_caller(0)
114
+ @valid = false
115
+ d_puts( "2 fname=#{fname}" )
116
+ d_caller(0)
117
+ else
118
+ if File.writable?( fname )
119
+ mes = ""
120
+ ret = WRITABLE_FILE_PATH
121
+ else
122
+ mes = "Can't write #{fname}"
123
+ @valid = false
124
+ ret = NOT_WRITABLE_FILE_PATH
125
+ ret_bool = false
126
+ d_puts( "3 fname=#{fname}" )
127
+ d_caller(0)
128
+ end
129
+ end
130
+ RetCode2.new( ret , ret_bool, mes )
131
+ end
132
+
133
+ def valid_writable_fname?( fname )
134
+ ret = false
135
+ if normal_string?( fname )
136
+ if File.file?( fname )
137
+ if File.writable?( fname )
138
+ ret = true
139
+ end
140
+ else
141
+ unless File.directory?( fname )
142
+ dir , filename = File.split( fname )
143
+ ret = valid_writable_directory?( dir )
144
+ end
145
+ end
146
+ end
147
+ ret
148
+ end
149
+
150
+ def valid_readable_directory?( path )
151
+ ret = false
152
+ if File.directory?(path)
153
+ if File.readable?(path)
154
+ ret = true
155
+ end
156
+ end
157
+ ret
158
+ end
159
+
160
+ def valid_writable_directory?( path )
161
+ ret = false
162
+ if File.directory?(path)
163
+ if File.writable?(path)
164
+ ret = true
165
+ end
166
+ end
167
+ ret
168
+ end
169
+
170
+ def write_file_with_template(input_file_path , output_file )
171
+ ary = []
172
+ begin
173
+ File.open( input_file_path , "r" ) do |file|
174
+ while line = file.gets
175
+ line.chomp!
176
+ ary << file_content_process(line)
177
+ end
178
+ end
179
+ rescue => ex
180
+ pp ex
181
+ pp ex.traceback
182
+ @valid = false
183
+ end
184
+ if @valid
185
+ content = ary.join("\n")
186
+ begin
187
+ output_file.write( content )
188
+ rescue => ex
189
+ pp ex
190
+ pp ex.traceback
191
+ @valid = false
192
+ end
193
+ end
194
+ end
195
+
196
+ def rewrite_file( from_path_1 , from_path_2 , to_path_1 , to_path_2 )
197
+ d_p( "rewrite_file" )
198
+ d_p( "from_path_1=#{from_path_1}" )
199
+ d_p( "from_path_2=#{from_path_2}" )
200
+ d_p( "to_path_1=#{to_path_1}" )
201
+ d_p( "to_path_2=#{to_path_2}")
202
+
203
+ mes = prepare_file_copy( from_path_1, from_path_2 , to_path_1 , to_path_2 )
204
+
205
+ puts_no_empty( mes )
206
+
207
+
208
+ unless @valid
209
+ return false
210
+ end
211
+
212
+ from_path = File.join( from_path_1, from_path_2 )
213
+ to_path = File.join( to_path_1, to_path_2 )
214
+ ary = []
215
+ begin
216
+ File.open( from_path , "r" ) do |file|
217
+ while line = file.gets
218
+ line.chomp!
219
+ ary << file_content_process(line)
220
+ end
221
+ end
222
+ rescue => ex
223
+ pp ex
224
+ pp ex.traceback
225
+ @valid = false
226
+ end
227
+
228
+ unless @valid
229
+ return false
230
+ end
231
+
232
+ content = ary.join("\n")
233
+
234
+ begin
235
+ File.open( to_path , "w") do |file|
236
+ file.write( content )
237
+ end
238
+ rescue => ex
239
+ pp ex
240
+ pp ex.traceback
241
+ @valid = false
242
+ end
243
+ end
244
+
245
+ def prepare_file_copy( from_path_1, from_path_2 , to_path_1 , to_path_2 )
246
+ mes = ""
247
+
248
+ d_p( "prepare_file_copy" )
249
+ d_p( "from_path_1=#{from_path_1}" )
250
+ d_p( "from_path_2=#{from_path_2}" )
251
+ d_p( "to_path_1=#{to_path_1}" )
252
+ d_p( "to_path_2=#{to_path_2}" )
253
+
254
+ unless prepare_source_dir( from_path_1, from_path_2 )
255
+ mes = "Can't read #{from_path_1}/#{from_path_2}"
256
+ @valid = false
257
+ else
258
+ unless prepare_dest_dir( to_path_1, to_path_2 )
259
+ mes = "Can't write #{to_path_1}/#{to_path_1}"
260
+ d_caller(0)
261
+ @valid = false
262
+ end
263
+ end
264
+
265
+ mes
266
+ end
267
+
268
+ def prepare_source_dir( from_path_1, from_path_2 )
269
+ state = false
270
+ from_path = File.join( from_path_1, from_path_2 )
271
+ if valid_readable_directory?( from_path_1 )
272
+ dirname , filename = File.split( from_path )
273
+ if valid_readable_directory?( dirname )
274
+ if valid_readable_fname?( from_path )
275
+ state = true
276
+ end
277
+ end
278
+ end
279
+ state
280
+ end
281
+
282
+ def prepare_dest_dir( to_path_1, to_path_2 )
283
+ state = true
284
+ unless valid_writable_directory?( to_path_1 )
285
+ if File.exist?( to_path_1 )
286
+ d_caller(0)
287
+ return false
288
+ end
289
+
290
+ begin
291
+ FileUtils.mkdir_p( to_path_1 )
292
+ rescue => ex
293
+ state = false
294
+ pp ex
295
+ pp ex.traceback
296
+ end
297
+ end
298
+ unless valid_writable_directory?( to_path_1 )
299
+ d_caller(0)
300
+ return false
301
+ end
302
+
303
+ to_path = File.join( to_path_1, to_path_2 )
304
+ dirname , filename = File.split( to_path )
305
+ unless File.exist?( dirname )
306
+ begin
307
+ FileUtils.mkdir_p( dirname )
308
+ rescue => ex
309
+ state = false
310
+ pp caller(0)
311
+ end
312
+ end
313
+
314
+ unless state
315
+ d_caller(0)
316
+ return false
317
+ end
318
+
319
+ unless valid_writable_directory?( dirname )
320
+ d_caller(0)
321
+ return false
322
+ end
323
+
324
+ if File.file?( to_path )
325
+ unless valid_writable_fname?( to_path )
326
+ d_caller(0)
327
+ return false
328
+ end
329
+ end
330
+
331
+ true
332
+ end
333
+
334
+ def copy_file( from_path_1 , from_path_2 , to_path_1 , to_path_2 )
335
+ retry_flag = false
336
+ begin
337
+ from_path = File.join( from_path_1, from_path_2 )
338
+ to_path = File.join( to_path_1 , to_path_2 )
339
+ FileUtils.cp( from_path , to_path )
340
+ d_puts( "Copy #{from_path} -> #{to_path}" )
341
+ rescue => ex
342
+ retry_flag = prepare_file_copy( from_path_1 , from_path_2 , to_path_1 , to_path_2 )
343
+ end
344
+
345
+ if retry_flag
346
+ begin
347
+ FileUtils.cp( from_path , to_path )
348
+ d_puts( "Copy #{from_path} -> #{to_path}" )
349
+ rescue => ex
350
+ @valid = false
351
+ end
352
+ end
353
+
354
+ unless @valid
355
+ d_puts( "Don't Copy #{from_path} -> #{to_path}" )
356
+ end
357
+ end
358
+
359
+ def find_file( path , dirname , filename )
360
+ ret = false
361
+ ary = path.split( File::SEPARATOR )
362
+ left = ary.index( dirname )
363
+ d_p( ary )
364
+ d_p(left)
365
+ if left
366
+ sub_ary = ary[(left+1)..-1]
367
+ if sub_ary
368
+ if sub_ary.index( filename )
369
+ ret = true
370
+ end
371
+ end
372
+ end
373
+
374
+ ret
375
+ end
376
+
377
+ def check_filepath( fname_ary )
378
+ ret = RetCode2.new( ret , false, "" )
379
+ mes_ary = []
380
+ fname_ary.each do |fname|
381
+ ret2 = valid_fpath_with_message( fname )
382
+ d_puts("==check_filepath")
383
+ d_puts_no_empty( ret2.mes )
384
+ d_p( ret2 )
385
+ if ret2.bool
386
+ ret.ret = fname
387
+ ret.bool = true
388
+ break
389
+ else
390
+ if ret2.ret == FileOpUtils::ABNORMAL_STRING
391
+ ret.ret = ret2.ret
392
+ mes_ary << "Can't find common (#{common_fname})"
393
+ end
394
+ end
395
+ end
396
+ unless ret.bool
397
+ ret.mes = mes_ary.join("\n")
398
+ end
399
+ ret
400
+ end
401
+
402
+ def make_fpath_list( fname , parent_dir_ary )
403
+ ary = [ fname ]
404
+ parent_dir_ary.each do |dir|
405
+ if dir
406
+ ary << File.join( dir , fname )
407
+ end
408
+ end
409
+ ary
410
+ end
411
+
412
+ def reform_filename( fname , add_string )
413
+ path = Pathname(fname)
414
+ basename = path.basename(".*")
415
+ dirname = path.dirname
416
+ extname = path.extname
417
+
418
+ dirname.join( basename.to_s + add_string + extname.to_s )
419
+ end
420
+
421
+ def determine_encoding(pn)
422
+ encodings = [Encoding::UTF_8 , Encoding::EUC_JP , Encoding::Shift_JIS , Encoding::CP932]
423
+
424
+ valid_enc = nil
425
+ encodings.each do |enc|
426
+ unless valid_enc
427
+ s = pn.expand_path.read(:encoding => enc)
428
+ if s.valid_encoding?
429
+ begin
430
+ cs = s.encode(Encoding::CP932)
431
+ valid_enc = enc
432
+ rescue => ex
433
+ # puts "Conversion Error! #{y}"
434
+ # puts y
435
+ end
436
+ end
437
+ end
438
+ end
439
+
440
+ valid_enc
441
+ end
442
+ end
443
+ end