sysadmin 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +17 -43
- data/VERSION +1 -1
- data/doc/ChangeLog +14 -0
- data/doc/README +15 -12
- data/doc/README.ja +15 -12
- data/lib/sysadmin.rb +5 -5
- data/lib/sysadmin/dir_ext.rb +35 -0
- data/lib/sysadmin/file_ext.rb +70 -0
- data/script/build +1 -1
- data/spec/lib/sysadmin/dir_ext_spec.rb +43 -0
- data/spec/lib/sysadmin/file_ext_spec.rb +118 -0
- data/spec/lib/sysadmin_spec.rb +13 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/test_dir/.dir1/.file6 +0 -0
- data/spec/test_dir/.file1 +0 -0
- data/spec/test_dir/dir2/dir4/file4 +0 -0
- data/spec/test_dir/dir2/file3 +0 -0
- data/spec/test_dir/dir3/file5 +0 -0
- data/spec/test_dir/dir6/dir7/.file7 +0 -0
- data/spec/test_dir/file2 +0 -0
- data/sysadmin.gemspec +15 -8
- metadata +15 -8
- data/lib/sysadmin/file_handler.rb +0 -29
- data/lib/sysadmin/file_string.rb +0 -55
- data/test/lib/file_handler_test.rb +0 -40
- data/test/lib/file_string_1_test.rb +0 -71
- data/test/lib/file_string_2_test.rb +0 -32
- data/test/sysadmin_test.rb +0 -16
data/Rakefile
CHANGED
@@ -4,39 +4,14 @@ require 'rubygems'
|
|
4
4
|
|
5
5
|
require 'rspec/core'
|
6
6
|
require 'rspec/core/rake_task'
|
7
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
-
spec.rspec_opts = ["-c"]
|
9
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
10
|
-
end
|
11
|
-
|
12
|
-
namespace :spec do
|
13
|
-
desc "Run RSpec for plugins"
|
14
|
-
RSpec::Core::RakeTask.new(:plugins) do |spec|
|
15
|
-
spec.rspec_opts = ["-c"]
|
16
|
-
spec.pattern = FileList['spec/plugins/**/*_spec.rb']
|
17
|
-
end
|
18
7
|
|
19
|
-
|
20
|
-
RSpec::Core::RakeTask.new(:
|
21
|
-
spec.rspec_opts = ["-c"]
|
22
|
-
spec.pattern = FileList['spec
|
8
|
+
task :spec do
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
10
|
+
spec.rspec_opts = ["-c","-fs"]
|
11
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
23
12
|
end
|
24
13
|
end
|
25
14
|
|
26
|
-
require 'jeweler'
|
27
|
-
Jeweler::Tasks.new do |gem|
|
28
|
-
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
29
|
-
gem.name = "sysadmin"
|
30
|
-
gem.homepage = "http://github.com/id774/sysadmin"
|
31
|
-
gem.license = "GPL"
|
32
|
-
gem.summary = %Q{Sysadmin Ruby}
|
33
|
-
gem.description = %Q{System Administration General Library}
|
34
|
-
gem.email = "idnanashi@gmail.com"
|
35
|
-
gem.authors = ["id774"]
|
36
|
-
# dependencies defined in Gemfile
|
37
|
-
end
|
38
|
-
Jeweler::RubygemsDotOrgTasks.new
|
39
|
-
|
40
15
|
if /^1\.9\./ =~ RUBY_VERSION
|
41
16
|
desc "Run RSpec code examples with simplecov"
|
42
17
|
task :simplecov do
|
@@ -46,7 +21,7 @@ if /^1\.9\./ =~ RUBY_VERSION
|
|
46
21
|
else
|
47
22
|
desc "Run RSpec code examples with rcov"
|
48
23
|
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
49
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
24
|
+
spec.pattern = FileList['spec/lib/**/*_spec.rb']
|
50
25
|
exclude_files = [
|
51
26
|
"gems",
|
52
27
|
]
|
@@ -55,17 +30,16 @@ else
|
|
55
30
|
end
|
56
31
|
end
|
57
32
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
end
|
33
|
+
require 'jeweler'
|
34
|
+
Jeweler::Tasks.new do |gem|
|
35
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
36
|
+
gem.name = "sysadmin"
|
37
|
+
gem.homepage = "http://github.com/id774/sysadmin"
|
38
|
+
gem.license = "GPL"
|
39
|
+
gem.summary = %Q{Sysadmin Ruby}
|
40
|
+
gem.description = %Q{System Administration General Library}
|
41
|
+
gem.email = "idnanashi@gmail.com"
|
42
|
+
gem.authors = ["id774"]
|
43
|
+
# dependencies defined in Gemfile
|
71
44
|
end
|
45
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/doc/ChangeLog
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
=== 0.1.0 / 2012-07-17
|
2
|
+
|
3
|
+
* Re-construction
|
4
|
+
|
5
|
+
* Removed Sysadmin::FileHandler and Sysadmin::FileString
|
6
|
+
|
7
|
+
* New module added "FileExtension" and "DirExtension"
|
8
|
+
|
9
|
+
They extend the Ruby classes.
|
10
|
+
Backward compatibility is nothing perfectly.
|
11
|
+
|
12
|
+
* Testing framework migration from Test::Unit to RSpec
|
13
|
+
|
14
|
+
|
1
15
|
=== 0.0.5 / 2012-06-06
|
2
16
|
|
3
17
|
* Add Class
|
data/doc/README
CHANGED
@@ -22,39 +22,42 @@ $ gem install sysadmin
|
|
22
22
|
Module
|
23
23
|
======
|
24
24
|
|
25
|
-
- Sysadmin::
|
25
|
+
- Sysadmin::FileExtension
|
26
26
|
|
27
|
-
|
27
|
+
Extend the File class.
|
28
28
|
|
29
|
-
- Sysadmin::
|
29
|
+
- Sysadmin::DirExtension
|
30
30
|
|
31
|
-
Extend the
|
31
|
+
Extend the Dir class.
|
32
32
|
|
33
33
|
|
34
34
|
======
|
35
35
|
Method
|
36
36
|
======
|
37
37
|
|
38
|
-
*
|
38
|
+
* File.zread (file)
|
39
|
+
|
40
|
+
Referred by Sysadmin::FileHandler.
|
41
|
+
Read the file regardless of the uncompressed / compressed gz.
|
42
|
+
|
43
|
+
* File.append_line(file, str)
|
39
44
|
|
40
45
|
Append a string to a file.
|
41
46
|
|
42
|
-
*
|
47
|
+
* File.new_line(file, str)
|
43
48
|
|
44
49
|
Create a new file.
|
45
50
|
|
46
|
-
*
|
51
|
+
* File.replace_line(file, src, out)
|
47
52
|
|
48
53
|
Replace the string in the file.
|
49
54
|
|
50
|
-
*
|
55
|
+
* FileString.remove_line(file, str)
|
51
56
|
|
52
57
|
Remove the rows that match the string from the file.
|
53
58
|
|
59
|
+
* Dir.filelist(dir, subdirectory=false)
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
Referred by Sysadmin::FileHandler.
|
58
|
-
Read the file regardless of the uncompressed / compressed gz.
|
61
|
+
Return the file lists in the directory.
|
59
62
|
|
60
63
|
|
data/doc/README.ja
CHANGED
@@ -22,39 +22,42 @@ $ gem install sysadmin
|
|
22
22
|
モジュール
|
23
23
|
==========
|
24
24
|
|
25
|
-
- Sysadmin::
|
25
|
+
- Sysadmin::FileExtension
|
26
26
|
|
27
|
-
|
27
|
+
File クラスを拡張する
|
28
28
|
|
29
|
-
- Sysadmin::
|
29
|
+
- Sysadmin::DirExtension
|
30
30
|
|
31
|
-
|
31
|
+
Dir クラスを拡張する
|
32
32
|
|
33
33
|
|
34
34
|
========
|
35
35
|
メソッド
|
36
36
|
========
|
37
37
|
|
38
|
-
*
|
38
|
+
* File.zread(file)
|
39
|
+
|
40
|
+
Sysadmin::FileHandler を呼ぶと追加される
|
41
|
+
gz 圧縮 / 非圧縮に関わらずファイルを読み込む
|
42
|
+
|
43
|
+
* File.append_line(file, str)
|
39
44
|
|
40
45
|
ファイルに文字列を追記する
|
41
46
|
|
42
|
-
*
|
47
|
+
* File.new_line(file, str)
|
43
48
|
|
44
49
|
ファイルを新規作成する
|
45
50
|
|
46
|
-
*
|
51
|
+
* File.replace_line(file, src, out)
|
47
52
|
|
48
53
|
ファイル内の文字列を置換する
|
49
54
|
|
50
|
-
*
|
55
|
+
* File.remove_line(file, str)
|
51
56
|
|
52
57
|
ファイルから文字列にマッチする行を取り除く
|
53
58
|
|
59
|
+
* Dir.filelist(dir, subdirectory=false)
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
Sysadmin::FileHandler を呼ぶと追加される
|
58
|
-
gz 圧縮 / 非圧縮に関わらずファイルを読み込む
|
61
|
+
ディレクトリ内のファイル一覧を配列で返す
|
59
62
|
|
60
63
|
|
data/lib/sysadmin.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# Name:: Sysadmin
|
2
|
+
# Name:: Sysadmin
|
3
3
|
# Author:: 774 <http://id774.net>
|
4
4
|
# Created:: Mar 23, 2012
|
5
|
-
# Updated::
|
5
|
+
# Updated:: Jul 17, 2012
|
6
6
|
# Copyright:: 774 Copyright (c) 2012
|
7
7
|
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
|
8
8
|
|
9
9
|
module Sysadmin
|
10
|
-
VERSION = "0.0
|
10
|
+
VERSION = "0.1.0"
|
11
11
|
USER_DIR = "/."
|
12
12
|
ROOT_DIR = File.expand_path("..", File.dirname(__FILE__))
|
13
13
|
$:.unshift ROOT_DIR
|
14
14
|
$:.unshift ROOT_DIR + '/lib'
|
15
15
|
$:.unshift ROOT_DIR + '/lib/sysadmin'
|
16
16
|
|
17
|
-
require '
|
18
|
-
require '
|
17
|
+
require 'file_ext'
|
18
|
+
require 'dir_ext'
|
19
19
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Name:: Sysadmin::DirExtension
|
2
|
+
# Author:: 774 <http://id774.net>
|
3
|
+
# Created:: Jul 17, 2012
|
4
|
+
# Updated:: Jul 17, 2012
|
5
|
+
# Copyright:: 774 Copyright (c) 2012
|
6
|
+
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
|
7
|
+
|
8
|
+
module Sysadmin
|
9
|
+
module DirExtension
|
10
|
+
|
11
|
+
def Dir.filelist(dir, sub_directory = false)
|
12
|
+
array = Array.new
|
13
|
+
if (FileTest.file?(dir))
|
14
|
+
array << dir
|
15
|
+
else
|
16
|
+
if sub_directory
|
17
|
+
dir = dir + "/**/"
|
18
|
+
else
|
19
|
+
dir = dir + "/"
|
20
|
+
end
|
21
|
+
Dir::glob(dir).each do |d|
|
22
|
+
if (FileTest.directory?(d))
|
23
|
+
Dir::foreach(d) do |f|
|
24
|
+
if (FileTest.file?(d+f))
|
25
|
+
array << d+f
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
return array
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Name:: Sysadmin::FileExtension
|
2
|
+
# Author:: 774 <http://id774.net>
|
3
|
+
# Created:: Jul 17, 2012
|
4
|
+
# Updated:: Jul 17, 2012
|
5
|
+
# Copyright:: 774 Copyright (c) 2012
|
6
|
+
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
|
7
|
+
|
8
|
+
module Sysadmin
|
9
|
+
module FileExtension
|
10
|
+
require 'zlib'
|
11
|
+
Zlib::GZIP_MAGIC = "\x1F\x8B"
|
12
|
+
Zlib::GZIP_MAGIC.force_encoding("ASCII-8BIT") if RUBY_VERSION >= "1.9"
|
13
|
+
|
14
|
+
def File.zread(file)
|
15
|
+
Object.module_eval do
|
16
|
+
open(file) {|f|
|
17
|
+
magic = f.read(2)
|
18
|
+
f.rewind
|
19
|
+
if magic == Zlib::GZIP_MAGIC
|
20
|
+
Zlib::GzipReader.wrap(f) {|gz|gz.read}
|
21
|
+
else
|
22
|
+
f.read
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def File.append_line(file, str)
|
29
|
+
if File.exist?(file)
|
30
|
+
f = open(file, 'a')
|
31
|
+
else
|
32
|
+
f = open(file, 'w')
|
33
|
+
end
|
34
|
+
f << str
|
35
|
+
f << "\n"
|
36
|
+
f.close
|
37
|
+
end
|
38
|
+
|
39
|
+
def File.new_line(file, str)
|
40
|
+
f = open(file, 'w')
|
41
|
+
f << str
|
42
|
+
f << "\n"
|
43
|
+
f.close
|
44
|
+
end
|
45
|
+
|
46
|
+
def File.replace_line(file, src, out)
|
47
|
+
open(file, "r+") { |f|
|
48
|
+
f.rewind
|
49
|
+
body = f.read
|
50
|
+
body = body.gsub(src) { |tmp|
|
51
|
+
out
|
52
|
+
}
|
53
|
+
f.rewind
|
54
|
+
f.puts body
|
55
|
+
f.truncate(f.tell)
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
def File.remove_line(file, str)
|
60
|
+
out = ""
|
61
|
+
IO.foreach(file) { |line|
|
62
|
+
out << line unless line.include?(str)
|
63
|
+
}
|
64
|
+
open(file, "w") { |f|
|
65
|
+
f.write out
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
data/script/build
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
4
|
+
|
5
|
+
describe Sysadmin::DirExtension, 'Dir クラス拡張' do
|
6
|
+
context 'で filelist メソッドを呼ぶ場合' do
|
7
|
+
describe 'ディレクトリ名を指定すると' do
|
8
|
+
it "ディレクトリ内のファイル一覧が返却される" do
|
9
|
+
test_dir = File.expand_path("../../test_dir", File.dirname(__FILE__))
|
10
|
+
Dir.filelist(test_dir).should have(2).items
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'ファイル名を指定すると' do
|
15
|
+
it "ファイル名が返却される" do
|
16
|
+
test_dir = File.expand_path("../../test_dir/file2", File.dirname(__FILE__))
|
17
|
+
Dir.filelist(test_dir).should have(1).items
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'サブディレクトリを true にしてディレクトリを指定すると' do
|
22
|
+
it "サブディレクトリ内のファイルも返却される" do
|
23
|
+
test_dir = File.expand_path("../../test_dir", File.dirname(__FILE__))
|
24
|
+
Dir.filelist(test_dir, sub_directory = true).should have(6).items
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'サブディレクトリを false にしてディレクトリを指定すると' do
|
29
|
+
it "ディレクトリ内のファイル一覧が返却される (サブディレクトリは見ない)" do
|
30
|
+
test_dir = File.expand_path("../../test_dir", File.dirname(__FILE__))
|
31
|
+
Dir.filelist(test_dir, sub_directory = false).should have(2).items
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'サブディレクトリを true にしてファイル名を指定しても' do
|
36
|
+
it "ファイル名が返却される (サブディレクトリは見ない)" do
|
37
|
+
test_dir = File.expand_path("../../test_dir/file2", File.dirname(__FILE__))
|
38
|
+
Dir.filelist(test_dir).should have(1).items
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'zlib'
|
6
|
+
|
7
|
+
describe Sysadmin::FileExtension, 'File クラス拡張' do
|
8
|
+
context 'で非圧縮のファイルを zread した場合' do
|
9
|
+
it "正しく読み込まれる" do
|
10
|
+
try = "uncompressed"
|
11
|
+
expect = "uncompressed\n"
|
12
|
+
@uncompressed = Tempfile::new("test.txt")
|
13
|
+
File.append_line(@uncompressed.path, try)
|
14
|
+
|
15
|
+
File.zread(@uncompressed.path).should == expect
|
16
|
+
@uncompressed.close
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'で圧縮されたファイルを zread した場合' do
|
21
|
+
it "正しく読み込まれる" do
|
22
|
+
try = "compressed"
|
23
|
+
expect = "compressed\n"
|
24
|
+
@compressed = Tempfile::new("test2.txt")
|
25
|
+
Zlib::GzipWriter.open(@compressed.path) do |gz|
|
26
|
+
gz.puts try
|
27
|
+
end
|
28
|
+
|
29
|
+
File.zread(@compressed.path).should == expect
|
30
|
+
@compressed.close
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'で append_line メソッドを呼ぶと' do
|
35
|
+
it "ファイル末尾に文字列行が追加される" do
|
36
|
+
@testfile = Tempfile::new("test.txt")
|
37
|
+
src = 'hoge'
|
38
|
+
expect = "hoge\n"
|
39
|
+
|
40
|
+
3.times do
|
41
|
+
File.append_line(@testfile.path, src)
|
42
|
+
end
|
43
|
+
|
44
|
+
open(@testfile.path) { |file|
|
45
|
+
while line = file.gets
|
46
|
+
line.should == expect
|
47
|
+
end
|
48
|
+
}
|
49
|
+
@testfile.close
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'で replace_line メソッドを呼ぶと' do
|
54
|
+
it "ファイル内の文字列が置換される" do
|
55
|
+
@testfile = Tempfile::new("test.txt")
|
56
|
+
src = 'hoge'
|
57
|
+
try = 'fuga'
|
58
|
+
expect = "fuga\n"
|
59
|
+
|
60
|
+
3.times do
|
61
|
+
File.append_line(@testfile.path, src)
|
62
|
+
end
|
63
|
+
File.replace_line(@testfile.path, src, try)
|
64
|
+
|
65
|
+
open(@testfile.path) { |file|
|
66
|
+
while line = file.gets
|
67
|
+
line.should == expect
|
68
|
+
end
|
69
|
+
}
|
70
|
+
@testfile.close
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'で remove_line メソッドを呼ぶと' do
|
75
|
+
it "ファイル内の引数に適合した行が削除される" do
|
76
|
+
@testfile = Tempfile::new("test.txt")
|
77
|
+
src = 'hoge'
|
78
|
+
replace = 'fuga'
|
79
|
+
erase = 'hoge'
|
80
|
+
expect = "fuga\n"
|
81
|
+
|
82
|
+
3.times do
|
83
|
+
File.append_line(@testfile.path, src)
|
84
|
+
end
|
85
|
+
File.replace_line(@testfile.path, src, replace)
|
86
|
+
2.times do
|
87
|
+
File.append_line(@testfile.path, src)
|
88
|
+
end
|
89
|
+
File.remove_line(@testfile.path, erase)
|
90
|
+
|
91
|
+
open(@testfile.path) { |file|
|
92
|
+
while line = file.gets
|
93
|
+
line.should == expect
|
94
|
+
end
|
95
|
+
}
|
96
|
+
@testfile.close
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'で new_line メソッドを呼ぶと' do
|
101
|
+
it "文字列行のファイルが新規作成される" do
|
102
|
+
@testfile = Tempfile::new("test.txt")
|
103
|
+
init = 'init'
|
104
|
+
second = 'second'
|
105
|
+
expect = "second\n"
|
106
|
+
|
107
|
+
File.new_line(@testfile.path, init)
|
108
|
+
File.new_line(@testfile.path, second)
|
109
|
+
|
110
|
+
open(@testfile.path) { |file|
|
111
|
+
while line = file.gets
|
112
|
+
line.should == expect
|
113
|
+
end
|
114
|
+
}
|
115
|
+
@testfile.close
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
4
|
+
|
5
|
+
describe Sysadmin, 'sysadmin' do
|
6
|
+
context 'のバージョンを参照した場合' do
|
7
|
+
it "バージョンが正しく表示される" do
|
8
|
+
expect = '0.1.0'
|
9
|
+
Sysadmin.const_get(:VERSION).should be_true
|
10
|
+
Sysadmin.const_get(:VERSION).should == expect
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
|
4
|
+
$LOAD_PATH.unshift APP_ROOT
|
5
|
+
$LOAD_PATH.unshift File.join(APP_ROOT)
|
6
|
+
$LOAD_PATH.unshift File.join(APP_ROOT, 'lib')
|
7
|
+
|
8
|
+
require 'sysadmin'
|
9
|
+
|
10
|
+
if ENV['COVERAGE'] == 'on'
|
11
|
+
require 'simplecov'
|
12
|
+
require 'simplecov-rcov'
|
13
|
+
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
|
14
|
+
|
15
|
+
SimpleCov.start do
|
16
|
+
add_filter "spec"
|
17
|
+
add_filter "vendor"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
unless /^1\.9\./ =~ RUBY_VERSION
|
22
|
+
require 'rspec'
|
23
|
+
end
|
24
|
+
|
25
|
+
RSpec.configure do |config|
|
26
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/spec/test_dir/file2
ADDED
File without changes
|
data/sysadmin.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "sysadmin"
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["id774"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-07-17"
|
13
13
|
s.description = "System Administration General Library"
|
14
14
|
s.email = "idnanashi@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,15 +28,22 @@ Gem::Specification.new do |s|
|
|
28
28
|
"doc/README",
|
29
29
|
"doc/README.ja",
|
30
30
|
"lib/sysadmin.rb",
|
31
|
-
"lib/sysadmin/
|
32
|
-
"lib/sysadmin/
|
31
|
+
"lib/sysadmin/dir_ext.rb",
|
32
|
+
"lib/sysadmin/file_ext.rb",
|
33
33
|
"script/.gitkeep",
|
34
34
|
"script/build",
|
35
|
+
"spec/lib/sysadmin/dir_ext_spec.rb",
|
36
|
+
"spec/lib/sysadmin/file_ext_spec.rb",
|
37
|
+
"spec/lib/sysadmin_spec.rb",
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"spec/test_dir/.dir1/.file6",
|
40
|
+
"spec/test_dir/.file1",
|
41
|
+
"spec/test_dir/dir2/dir4/file4",
|
42
|
+
"spec/test_dir/dir2/file3",
|
43
|
+
"spec/test_dir/dir3/file5",
|
44
|
+
"spec/test_dir/dir6/dir7/.file7",
|
45
|
+
"spec/test_dir/file2",
|
35
46
|
"sysadmin.gemspec",
|
36
|
-
"test/lib/file_handler_test.rb",
|
37
|
-
"test/lib/file_string_1_test.rb",
|
38
|
-
"test/lib/file_string_2_test.rb",
|
39
|
-
"test/sysadmin_test.rb",
|
40
47
|
"vendor/.gitkeep"
|
41
48
|
]
|
42
49
|
s.homepage = "http://github.com/id774/sysadmin"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sysadmin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cucumber
|
@@ -78,15 +78,22 @@ files:
|
|
78
78
|
- doc/README
|
79
79
|
- doc/README.ja
|
80
80
|
- lib/sysadmin.rb
|
81
|
-
- lib/sysadmin/
|
82
|
-
- lib/sysadmin/
|
81
|
+
- lib/sysadmin/dir_ext.rb
|
82
|
+
- lib/sysadmin/file_ext.rb
|
83
83
|
- script/.gitkeep
|
84
84
|
- script/build
|
85
|
+
- spec/lib/sysadmin/dir_ext_spec.rb
|
86
|
+
- spec/lib/sysadmin/file_ext_spec.rb
|
87
|
+
- spec/lib/sysadmin_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
- spec/test_dir/.dir1/.file6
|
90
|
+
- spec/test_dir/.file1
|
91
|
+
- spec/test_dir/dir2/dir4/file4
|
92
|
+
- spec/test_dir/dir2/file3
|
93
|
+
- spec/test_dir/dir3/file5
|
94
|
+
- spec/test_dir/dir6/dir7/.file7
|
95
|
+
- spec/test_dir/file2
|
85
96
|
- sysadmin.gemspec
|
86
|
-
- test/lib/file_handler_test.rb
|
87
|
-
- test/lib/file_string_1_test.rb
|
88
|
-
- test/lib/file_string_2_test.rb
|
89
|
-
- test/sysadmin_test.rb
|
90
97
|
- vendor/.gitkeep
|
91
98
|
homepage: http://github.com/id774/sysadmin
|
92
99
|
licenses:
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# Name:: Sysadmin::FileString
|
2
|
-
# Author:: 774 <http://id774.net>
|
3
|
-
# Created:: Jun 06, 2012
|
4
|
-
# Updated:: Jun 06, 2012
|
5
|
-
# Copyright:: 774 Copyright (c) 2012
|
6
|
-
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
|
7
|
-
|
8
|
-
|
9
|
-
module Sysadmin
|
10
|
-
module FileHandler
|
11
|
-
require 'zlib'
|
12
|
-
Zlib::GZIP_MAGIC = "\x1F\x8B"
|
13
|
-
Zlib::GZIP_MAGIC.force_encoding("ASCII-8BIT") if RUBY_VERSION >= "1.9"
|
14
|
-
|
15
|
-
def File.zread(file)
|
16
|
-
Object.module_eval do
|
17
|
-
open(file) {|f|
|
18
|
-
magic = f.read(2)
|
19
|
-
f.rewind
|
20
|
-
if magic == Zlib::GZIP_MAGIC
|
21
|
-
Zlib::GzipReader.wrap(f) {|gz|gz.read}
|
22
|
-
else
|
23
|
-
f.read
|
24
|
-
end
|
25
|
-
}
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/lib/sysadmin/file_string.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
# Name:: Sysadmin::FileString
|
2
|
-
# Author:: 774 <http://id774.net>
|
3
|
-
# Created:: Mar 23, 2012
|
4
|
-
# Updated:: Jun 04, 2012
|
5
|
-
# Copyright:: 774 Copyright (c) 2012
|
6
|
-
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
|
7
|
-
|
8
|
-
module Sysadmin
|
9
|
-
module FileString
|
10
|
-
def self.append(file, str)
|
11
|
-
if File.exist?(file)
|
12
|
-
f = open(file, 'a')
|
13
|
-
else
|
14
|
-
f = open(file, 'w')
|
15
|
-
end
|
16
|
-
f << str
|
17
|
-
f << "\n"
|
18
|
-
f.close
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.newfile(file, str)
|
22
|
-
f = open(file, 'w')
|
23
|
-
f << str
|
24
|
-
f << "\n"
|
25
|
-
f.close
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.replaceFile(f, src, out)
|
29
|
-
f.rewind
|
30
|
-
body = f.read
|
31
|
-
body = body.gsub(src) { |tmp|
|
32
|
-
out
|
33
|
-
}
|
34
|
-
f.rewind
|
35
|
-
f.puts body
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.replace(file, src, out)
|
39
|
-
open(file, "r+") { |f|
|
40
|
-
replaceFile(f, src, out)
|
41
|
-
f.truncate(f.tell)
|
42
|
-
}
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.delete(file, str)
|
46
|
-
out = ""
|
47
|
-
IO.foreach(file) { |line|
|
48
|
-
out << line unless line.include?(str)
|
49
|
-
}
|
50
|
-
open(file, "w") { |f|
|
51
|
-
f.write out
|
52
|
-
}
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
require 'test/unit'
|
6
|
-
require 'tempfile'
|
7
|
-
require 'zlib'
|
8
|
-
require 'sysadmin'
|
9
|
-
|
10
|
-
class Test_FileHandler < Test::Unit::TestCase
|
11
|
-
def test_zread_uncompressed
|
12
|
-
@uncompressed = Tempfile::new("test.txt")
|
13
|
-
try = "uncompressed"
|
14
|
-
expect = "uncompressed\n"
|
15
|
-
|
16
|
-
Sysadmin::FileString.append(@uncompressed.path, try)
|
17
|
-
|
18
|
-
text = File.zread(@uncompressed.path)
|
19
|
-
assert_not_nil(text)
|
20
|
-
assert_equal(expect, text)
|
21
|
-
|
22
|
-
@uncompressed.close
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_zread_compressed
|
26
|
-
try = "compressed"
|
27
|
-
expect = "compressed\n"
|
28
|
-
|
29
|
-
@compressed = Tempfile::new("test2.txt")
|
30
|
-
Zlib::GzipWriter.open(@compressed.path) do |gz|
|
31
|
-
gz.puts try
|
32
|
-
end
|
33
|
-
|
34
|
-
text = File.zread(@compressed.path)
|
35
|
-
assert_not_nil(text)
|
36
|
-
assert_equal(expect, text)
|
37
|
-
|
38
|
-
@compressed.close
|
39
|
-
end
|
40
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
require 'test/unit'
|
6
|
-
require 'tempfile'
|
7
|
-
require 'sysadmin'
|
8
|
-
|
9
|
-
class Test_FileString < Test::Unit::TestCase
|
10
|
-
def setup
|
11
|
-
@testfile = Tempfile::new("test.txt")
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_append
|
15
|
-
src = 'hoge'
|
16
|
-
expect = "hoge\n"
|
17
|
-
|
18
|
-
3.times do
|
19
|
-
Sysadmin::FileString.append(@testfile.path, src)
|
20
|
-
end
|
21
|
-
|
22
|
-
open(@testfile.path) { |file|
|
23
|
-
while line = file.gets
|
24
|
-
assert_equal(expect, line)
|
25
|
-
end
|
26
|
-
}
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_replace
|
30
|
-
src = 'hoge'
|
31
|
-
try = 'fuga'
|
32
|
-
expect = "fuga\n"
|
33
|
-
|
34
|
-
3.times do
|
35
|
-
Sysadmin::FileString.append(@testfile.path, src)
|
36
|
-
end
|
37
|
-
Sysadmin::FileString.replace(@testfile.path, src, try)
|
38
|
-
|
39
|
-
open(@testfile.path) { |file|
|
40
|
-
while line = file.gets
|
41
|
-
assert_equal(expect, line)
|
42
|
-
end
|
43
|
-
}
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_delete
|
47
|
-
src = 'hoge'
|
48
|
-
replace = 'fuga'
|
49
|
-
erase = 'hoge'
|
50
|
-
expect = "fuga\n"
|
51
|
-
|
52
|
-
3.times do
|
53
|
-
Sysadmin::FileString.append(@testfile.path, src)
|
54
|
-
end
|
55
|
-
Sysadmin::FileString.replace(@testfile.path, src, replace)
|
56
|
-
2.times do
|
57
|
-
Sysadmin::FileString.append(@testfile.path, src)
|
58
|
-
end
|
59
|
-
Sysadmin::FileString.delete(@testfile.path, erase)
|
60
|
-
|
61
|
-
open(@testfile.path) { |file|
|
62
|
-
while line = file.gets
|
63
|
-
assert_equal(expect, line)
|
64
|
-
end
|
65
|
-
}
|
66
|
-
end
|
67
|
-
|
68
|
-
def teardown
|
69
|
-
@testfile.close(true)
|
70
|
-
end
|
71
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
require 'test/unit'
|
6
|
-
require 'tempfile'
|
7
|
-
require 'sysadmin'
|
8
|
-
|
9
|
-
class Test_FileString < Test::Unit::TestCase
|
10
|
-
def setup
|
11
|
-
@testfile = Tempfile::new("test.txt")
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_newfile
|
15
|
-
init = 'init'
|
16
|
-
second = 'second'
|
17
|
-
expect = "second\n"
|
18
|
-
|
19
|
-
Sysadmin::FileString.newfile(@testfile.path, init)
|
20
|
-
Sysadmin::FileString.newfile(@testfile.path, second)
|
21
|
-
|
22
|
-
open(@testfile.path) { |file|
|
23
|
-
while line = file.gets
|
24
|
-
assert_equal(expect, line)
|
25
|
-
end
|
26
|
-
}
|
27
|
-
end
|
28
|
-
|
29
|
-
def teardown
|
30
|
-
@testfile.close(true)
|
31
|
-
end
|
32
|
-
end
|
data/test/sysadmin_test.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
require 'test/unit'
|
6
|
-
require 'tempfile'
|
7
|
-
require 'sysadmin'
|
8
|
-
|
9
|
-
class Test_Sysadmin < Test::Unit::TestCase
|
10
|
-
def test_version
|
11
|
-
expect = '0.0.5'
|
12
|
-
version = Sysadmin.const_get(:VERSION)
|
13
|
-
p "sysadmin #{version}"
|
14
|
-
assert_equal(expect, version)
|
15
|
-
end
|
16
|
-
end
|