Dahistory 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/Dahistory.gemspec +28 -0
- data/Gemfile +6 -0
- data/README.md +39 -0
- data/Rakefile +3 -0
- data/lib/Dahistory/version.rb +3 -0
- data/lib/Dahistory.rb +108 -0
- data/spec/helper.rb +15 -0
- data/spec/main.rb +34 -0
- data/spec/tests/Dashistory.rb +22 -0
- data/spec/tests/bin.rb +13 -0
- metadata +121 -0
data/.gitignore
ADDED
data/Dahistory.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require "Dahistory/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "Dahistory"
|
8
|
+
s.version = Dahistory::VERSION
|
9
|
+
s.authors = ["da99"]
|
10
|
+
s.email = ["i-hate-spam-45671204@mailinator.com"]
|
11
|
+
s.homepage = "https://github.com/da99/Dahistory"
|
12
|
+
s.summary = %q{Compare file to previous history and backup.}
|
13
|
+
s.description = %q{Compares file to other files in specified dir(s) and backups if it does not exist.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "bacon"
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
s.add_development_dependency 'Bacon_Colored'
|
23
|
+
s.add_development_dependency 'pry'
|
24
|
+
|
25
|
+
# s.rubyforge_project = "Dahistory"
|
26
|
+
# specify any dependencies here; for example:
|
27
|
+
# s.add_runtime_dependency "rest-client"
|
28
|
+
end
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
Dahistory
|
3
|
+
=========
|
4
|
+
|
5
|
+
Short for: da99's gem for file history.
|
6
|
+
|
7
|
+
It helps you compare files and decide if the file should be backed up.
|
8
|
+
It is mainly to help you keep backups of files you overwrite on servers you administer.
|
9
|
+
|
10
|
+
**Example:** You use Chef or Puppet to write a config file, but you want to be sure you
|
11
|
+
are overwriting a file you have encountered before.
|
12
|
+
|
13
|
+
Useage
|
14
|
+
------
|
15
|
+
|
16
|
+
require "Dahistory"
|
17
|
+
|
18
|
+
path = "some/file.txt"
|
19
|
+
Dahistory_File( path )
|
20
|
+
# Checks your ./history directory.
|
21
|
+
# If not found there, saves copy of file in ./pending dir and
|
22
|
+
# raises Dahistory::Pending_File, "pending/HOSTNAME,path,some,file.txt.TIMESTAMP"
|
23
|
+
|
24
|
+
# You review the file,
|
25
|
+
# move the file from the pending directory, and
|
26
|
+
# re-do your last command (Capistrano, Chef, Puppet, etc.)
|
27
|
+
|
28
|
+
Override the default settings:
|
29
|
+
|
30
|
+
Dahistory.check { |o|
|
31
|
+
|
32
|
+
o.file "file/path.txt"
|
33
|
+
o.dirs [ "dir1", "dir2" ]
|
34
|
+
o.backup_dir "./history"
|
35
|
+
o.pending_dir "pending_dir_path"
|
36
|
+
o.backup_file "#{`hostname`}.backup.path.txt"
|
37
|
+
|
38
|
+
}
|
39
|
+
|
data/Rakefile
ADDED
data/lib/Dahistory.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'Dahistory/version'
|
2
|
+
|
3
|
+
|
4
|
+
class Dahistory
|
5
|
+
|
6
|
+
Pending = Class.new(RuntimeError)
|
7
|
+
|
8
|
+
module Class_Methods
|
9
|
+
|
10
|
+
def check file = nil
|
11
|
+
da = Dahistory.new { |o|
|
12
|
+
o.file(file) if file
|
13
|
+
yield(o) if block_given?
|
14
|
+
}
|
15
|
+
|
16
|
+
da.save
|
17
|
+
end
|
18
|
+
|
19
|
+
end # === module
|
20
|
+
|
21
|
+
extend Class_Methods
|
22
|
+
|
23
|
+
def initialize file_path = nil
|
24
|
+
@file = nil
|
25
|
+
file!(file_path) if file_path
|
26
|
+
dirs ['.']
|
27
|
+
pending_dir "./pending"
|
28
|
+
@backup_dir = nil
|
29
|
+
yield(self) if block_given?
|
30
|
+
end
|
31
|
+
|
32
|
+
old_meths = public_instance_methods
|
33
|
+
|
34
|
+
def file path
|
35
|
+
@file = File.expand_path(path)
|
36
|
+
end
|
37
|
+
|
38
|
+
def dirs *args
|
39
|
+
@dirs = args.flatten.map { |dir| File.expand_path dir }
|
40
|
+
end
|
41
|
+
|
42
|
+
def backup_dir dir
|
43
|
+
@backup_dir = File.expand_path(dir)
|
44
|
+
end
|
45
|
+
|
46
|
+
def pending_dir dir = :RETURN
|
47
|
+
@pending_dir = File.expand_path(dir)
|
48
|
+
end
|
49
|
+
|
50
|
+
(public_instance_methods - old_meths ).each { |name|
|
51
|
+
|
52
|
+
alias_method :"#{name}_set", name
|
53
|
+
|
54
|
+
eval %~
|
55
|
+
def #{name} *args, &blok
|
56
|
+
|
57
|
+
if args.empty?
|
58
|
+
|
59
|
+
unless instance_variable_defined?(:@#{name})
|
60
|
+
raise ArgumentError, "Instance variable not set: @#{name}"
|
61
|
+
end
|
62
|
+
@#{name}
|
63
|
+
|
64
|
+
else
|
65
|
+
|
66
|
+
#{name}_set(*args, &blok)
|
67
|
+
#
|
68
|
+
end
|
69
|
+
|
70
|
+
end # === def
|
71
|
+
~
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
def backup_file str = :RETURN
|
76
|
+
if str == :RETURN
|
77
|
+
@backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}"
|
78
|
+
return @backup_file
|
79
|
+
end
|
80
|
+
|
81
|
+
@backup_file = str
|
82
|
+
end
|
83
|
+
|
84
|
+
def source_files
|
85
|
+
dirs.map { |path|
|
86
|
+
full = File.join( File.expand_path(path), "/*")
|
87
|
+
files = Dir.glob( full, File::FNM_DOTMATCH ).select { |unk| File.file?(unk) }
|
88
|
+
}.flatten
|
89
|
+
end
|
90
|
+
|
91
|
+
def save
|
92
|
+
|
93
|
+
content = File.read(file)
|
94
|
+
standard = content.gsub("\r", '')
|
95
|
+
|
96
|
+
old = source_files.detect { |path|
|
97
|
+
raw = File.read(path)
|
98
|
+
raw.gsub("\r","") == standard
|
99
|
+
}
|
100
|
+
|
101
|
+
if !old
|
102
|
+
File.write(backup_file, content)
|
103
|
+
raise Pending, backup_file
|
104
|
+
end
|
105
|
+
|
106
|
+
end # === def
|
107
|
+
|
108
|
+
end # === class Dahistory
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.print e.message, "\n"
|
7
|
+
$stderr.print "Run `bundle install` to install missing gems\n"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'bacon'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
|
15
|
+
Bacon.summary_on_exit
|
data/spec/main.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
require File.expand_path('spec/helper')
|
3
|
+
require 'Dahistory'
|
4
|
+
require 'Bacon_Colored'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
|
8
|
+
FOLDER = "/tmp/Dahistory_tmp"
|
9
|
+
|
10
|
+
def chdir
|
11
|
+
Dir.chdir(FOLDER) { yield }
|
12
|
+
end
|
13
|
+
|
14
|
+
def reset_dirs
|
15
|
+
`rm -rf #{FOLDER}`
|
16
|
+
`mkdir #{FOLDER}`
|
17
|
+
`mkdir #{FOLDER}/files`
|
18
|
+
`mkdir #{FOLDER}/history`
|
19
|
+
`mkdir #{FOLDER}/history/blue`
|
20
|
+
`mkdir #{FOLDER}/history/red`
|
21
|
+
`mkdir #{FOLDER}/history/yellow`
|
22
|
+
`mkdir #{FOLDER}/pending`
|
23
|
+
end
|
24
|
+
|
25
|
+
reset_dirs
|
26
|
+
|
27
|
+
# ======== Include the tests.
|
28
|
+
if ARGV.size > 1 && ARGV[1, ARGV.size - 1].detect { |a| File.exists?(a) }
|
29
|
+
# Do nothing. Bacon grabs the file.
|
30
|
+
else
|
31
|
+
Dir.glob('spec/tests/*.rb').each { |file|
|
32
|
+
require File.expand_path(file.sub('.rb', '')) if File.file?(file)
|
33
|
+
}
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
describe "Dahistory: new pending file" do
|
3
|
+
|
4
|
+
before { reset_dirs }
|
5
|
+
|
6
|
+
it "copies file to pending dir" do
|
7
|
+
|
8
|
+
file = "files/a.txt"
|
9
|
+
target = rand(1000).to_s
|
10
|
+
chdir {
|
11
|
+
File.write(file, target)
|
12
|
+
pending = begin
|
13
|
+
Dahistory.check("files/a.txt")
|
14
|
+
rescue Dahistory::Pending => e
|
15
|
+
e.message
|
16
|
+
end
|
17
|
+
File.read(pending).should == target
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
end # === Dahistory: new pending file
|
22
|
+
|
data/spec/tests/bin.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
bins = Dir.glob("bin/*")
|
3
|
+
|
4
|
+
unless bins.empty?
|
5
|
+
describe "permissions of bin/" do
|
6
|
+
bins.each { |file|
|
7
|
+
it "should chmod 755 for: #{file}" do
|
8
|
+
`stat -c %a #{file}`.strip
|
9
|
+
.should.be == "755"
|
10
|
+
end
|
11
|
+
}
|
12
|
+
end # === permissions of bin/
|
13
|
+
end # === unless bins.empty?
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Dahistory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- da99
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bacon
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: Bacon_Colored
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Compares file to other files in specified dir(s) and backups if it does
|
79
|
+
not exist.
|
80
|
+
email:
|
81
|
+
- i-hate-spam-45671204@mailinator.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- Dahistory.gemspec
|
88
|
+
- Gemfile
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/Dahistory.rb
|
92
|
+
- lib/Dahistory/version.rb
|
93
|
+
- spec/helper.rb
|
94
|
+
- spec/main.rb
|
95
|
+
- spec/tests/Dashistory.rb
|
96
|
+
- spec/tests/bin.rb
|
97
|
+
homepage: https://github.com/da99/Dahistory
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.19
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: Compare file to previous history and backup.
|
121
|
+
test_files: []
|