crontabwrap 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/README.rdoc +21 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/bin/crontabwrap +17 -0
- data/lib/crontabwrap.rb +92 -0
- data/test/helper.rb +9 -0
- data/test/test_crontabwrap.rb +28 -0
- metadata +71 -0
data/.document
ADDED
data/.gitignore
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= crontabwrap
|
2
|
+
|
3
|
+
crontab command wrapper for crearing backup for each user
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 wtnabe
|
18
|
+
|
19
|
+
== License
|
20
|
+
|
21
|
+
Two-claused BSD
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- mode: ruby -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "crontabwrap"
|
9
|
+
gem.summary = %Q{crontab command wrapper}
|
10
|
+
gem.description = %Q{crontab command wrapper for creating backup for each user}
|
11
|
+
gem.email = "wtnabe@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/wtnabe/crontabwrap"
|
13
|
+
gem.authors = ["wtnabe"]
|
14
|
+
gem.executables = %w( crontabwrap )
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "crontabwrap #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
data/bin/crontabwrap
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
3
|
+
|
4
|
+
# A wrapper for crontab command.
|
5
|
+
#
|
6
|
+
# All options pass through to crontab command.
|
7
|
+
# If '-e' option given, do `crontab -l > STORE_FILE`
|
8
|
+
#
|
9
|
+
# @since 2007-01-18
|
10
|
+
|
11
|
+
unless ( ENV['PATH'].split( File::PATH_SEPARATOR ).include?( File.dirname( __FILE__ ) ) )
|
12
|
+
$LOAD_PATH << File.expand_path( "../lib", File.dirname( __FILE__ ) )
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'crontabwrap'
|
16
|
+
|
17
|
+
CrontabWrap.new.run
|
data/lib/crontabwrap.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
#
|
6
|
+
# Crontab wrapper class
|
7
|
+
#
|
8
|
+
class CrontabWrap
|
9
|
+
BACKUP_FILE = '.crontab'
|
10
|
+
|
11
|
+
#
|
12
|
+
# construct object
|
13
|
+
#
|
14
|
+
# default BACKUP_FILE is ~/.crontab
|
15
|
+
#
|
16
|
+
def initialize( param = {} )
|
17
|
+
@opt = {
|
18
|
+
:debug => false,
|
19
|
+
:argv => ARGV
|
20
|
+
}.merge( param )
|
21
|
+
@user = ENV['USER']
|
22
|
+
|
23
|
+
parse_args( @opt[:argv] )
|
24
|
+
end
|
25
|
+
attr_reader :user
|
26
|
+
|
27
|
+
#
|
28
|
+
# take all options to crontab
|
29
|
+
#
|
30
|
+
# do backup if '-e' option given
|
31
|
+
#
|
32
|
+
def run
|
33
|
+
argv = @opt[:argv]
|
34
|
+
|
35
|
+
if ( exec( argv ) )
|
36
|
+
if ( argv.include?( '-e' ) )
|
37
|
+
backup( argv )
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# parse command line args
|
44
|
+
#
|
45
|
+
def parse_args( argv )
|
46
|
+
opt = OptionParser.new()
|
47
|
+
opt.on( '-u USER' ) { |user|
|
48
|
+
@user = user
|
49
|
+
}
|
50
|
+
begin
|
51
|
+
opt.parse( argv )
|
52
|
+
rescue
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def echo( argv, opt = '' )
|
57
|
+
STDERR.puts 'crontab ' + argv.join( " " ) + opt
|
58
|
+
end
|
59
|
+
|
60
|
+
def exec( argv, opt = '' )
|
61
|
+
echo( argv, " > #{backup_file( @user )}" ) if ( @opt[:debug] )
|
62
|
+
system( 'crontab ' + argv.join( " " ) + opt )
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# backup crontab content for each user
|
67
|
+
#
|
68
|
+
def backup( argv )
|
69
|
+
argv = replace_switch_to_display( argv )
|
70
|
+
|
71
|
+
exec( argv, " > #{backup_file( @user )}" )
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# replace '-e' option to '-l'
|
76
|
+
#
|
77
|
+
# [Param] Array opts
|
78
|
+
# [Return] Array
|
79
|
+
#
|
80
|
+
def replace_switch_to_display( opts )
|
81
|
+
opts.map { |opt|
|
82
|
+
( opt == '-e' ) ? '-l' : opt
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# backup file name for crontab content
|
88
|
+
#
|
89
|
+
def backup_file( user )
|
90
|
+
return "~#{user}/#{BACKUP_FILE}"
|
91
|
+
end
|
92
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestCrontabwrap < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@obj = ::CrontabWrap.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_parse_args
|
9
|
+
@obj.parse_args( %w( -a -b -c ) )
|
10
|
+
assert_equal( ENV['USER'], @obj.user )
|
11
|
+
@obj.parse_args( %w( -u foo ) )
|
12
|
+
assert_equal( 'foo', @obj.user )
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_replace_switch_to_display
|
16
|
+
assert_equal( %w( -l ),
|
17
|
+
@obj.replace_switch_to_display( %w( -l ) ) )
|
18
|
+
assert_equal( %w( -u foo -l ),
|
19
|
+
@obj.replace_switch_to_display( %w( -u foo -e ) ) )
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_backup_file
|
23
|
+
assert_equal( "~foo/#{::CrontabWrap::BACKUP_FILE}",
|
24
|
+
@obj.backup_file( 'foo' ) )
|
25
|
+
assert_equal( "~root/#{::CrontabWrap::BACKUP_FILE}",
|
26
|
+
@obj.backup_file( 'root' ) )
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crontabwrap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- wtnabe
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-09 00:00:00 +09:00
|
18
|
+
default_executable: crontabwrap
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: crontab command wrapper for creating backup for each user
|
22
|
+
email: wtnabe@gmail.com
|
23
|
+
executables:
|
24
|
+
- crontabwrap
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- .document
|
31
|
+
- .gitignore
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- VERSION
|
35
|
+
- bin/crontabwrap
|
36
|
+
- lib/crontabwrap.rb
|
37
|
+
- test/helper.rb
|
38
|
+
- test/test_crontabwrap.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/wtnabe/crontabwrap
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --charset=UTF-8
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.6
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: crontab command wrapper
|
69
|
+
test_files:
|
70
|
+
- test/helper.rb
|
71
|
+
- test/test_crontabwrap.rb
|