acopy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/acopy +30 -0
- data/lib/CpData.rb +34 -0
- data/lib/DirCopy.rb +70 -0
- data/lib/FailToCopyException.rb +4 -0
- data/lib/acopy.rb +28 -0
- metadata +52 -0
data/bin/acopy
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#-*- encoding:utf-8 -*-
|
3
|
+
|
4
|
+
require 'acopy.rb'
|
5
|
+
require 'DirCopy.rb'
|
6
|
+
|
7
|
+
if ARGV.size == 0 then
|
8
|
+
puts "Usage: acopy xmlfile.xml"
|
9
|
+
puts "Version 0.1.0"
|
10
|
+
exit 0
|
11
|
+
end
|
12
|
+
|
13
|
+
ARGV.each do |arg|
|
14
|
+
begin
|
15
|
+
cp = ACopy.new arg
|
16
|
+
rescue
|
17
|
+
STDERR.pust("#{arg} is invaild")
|
18
|
+
else
|
19
|
+
cp.read
|
20
|
+
cp.cpdatas.each do |x|
|
21
|
+
puts "[#{x.mode}]FROM::#{x.from} TO::#{x.to}"
|
22
|
+
begin
|
23
|
+
(DirCopy.new(x)).cp()
|
24
|
+
rescue => e
|
25
|
+
puts "[FAIL] Fail to copy #{x.from} :: #{x.to} --- SKIP"
|
26
|
+
puts e
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/CpData.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#-*- encoding:utf-8 -*-
|
2
|
+
|
3
|
+
class CpData
|
4
|
+
attr_reader :from,:to,:mode
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
from = String.new
|
8
|
+
to = String.new
|
9
|
+
mode = -1
|
10
|
+
end
|
11
|
+
|
12
|
+
def setFrom(f)
|
13
|
+
begin
|
14
|
+
r = File::ftype(f)
|
15
|
+
rescue Errno::ENOENT => e
|
16
|
+
STDERR.puts "No such directory. #{f}"
|
17
|
+
else
|
18
|
+
if r == "directory" then
|
19
|
+
@from = f
|
20
|
+
else
|
21
|
+
puts "#{f} is not directory."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def setTo(t)
|
27
|
+
@to = t
|
28
|
+
end
|
29
|
+
|
30
|
+
def setMode(m)
|
31
|
+
@mode = m
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/DirCopy.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#-*- encoding:utf-8 -*-
|
2
|
+
require 'FailToCopyException.rb'
|
3
|
+
|
4
|
+
class DirCopy
|
5
|
+
attr_reader :cpdata
|
6
|
+
|
7
|
+
def initialize(d)
|
8
|
+
@cpdata = d
|
9
|
+
end
|
10
|
+
|
11
|
+
def cp
|
12
|
+
b_copy(@cpdata.from,@cpdata.to)
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
def isDirectoryPath?(p)
|
17
|
+
r = File::ftype(p)
|
18
|
+
return true if r == "directory"
|
19
|
+
return false if r != "directory"
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
def b_copy(from,to)
|
24
|
+
raise(FailToCopyException,"#{from} is not directory.") unless isDirectoryPath?(from)
|
25
|
+
unless File::exist?(to) then
|
26
|
+
begin
|
27
|
+
Dir.mkdir(to)
|
28
|
+
rescue Errno::EACCES
|
29
|
+
STDERR.puts "acopy cannnot make directory :: #{to}"
|
30
|
+
return false
|
31
|
+
rescue
|
32
|
+
STDERR.puts "error. fail to make directory :: #{to}"
|
33
|
+
return false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
(Dir.new(from)).each do |f|
|
38
|
+
if f == "." or f == ".." then
|
39
|
+
next
|
40
|
+
end
|
41
|
+
if isDirectoryPath?(from + File::SEPARATOR + f)
|
42
|
+
return false unless b_copy(from + File::SEPARATOR + f,to + File::SEPARATOR + f)
|
43
|
+
else
|
44
|
+
return false unless cpfile(from + File::SEPARATOR + f,to + File::SEPARATOR + f)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
return true
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
def cpfile(from,to)
|
52
|
+
if File.exists?(to) and @cpdata.mode != :overwrite then
|
53
|
+
return true
|
54
|
+
end
|
55
|
+
|
56
|
+
begin
|
57
|
+
src = open(from)
|
58
|
+
rescue Errno::ENOENT
|
59
|
+
puts "[ERROR] #{from} is no such file or directory."
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
puts "#{to}"
|
63
|
+
dest = open(to,'w')
|
64
|
+
|
65
|
+
dest.write(src.read())
|
66
|
+
dest.close
|
67
|
+
src.close
|
68
|
+
return true
|
69
|
+
end
|
70
|
+
end
|
data/lib/acopy.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#-*- encoding:utf-8 -*-
|
2
|
+
|
3
|
+
require 'rexml/document'
|
4
|
+
require 'CpData.rb'
|
5
|
+
|
6
|
+
class ACopy
|
7
|
+
attr_reader :xml_file_path,:cpdatas
|
8
|
+
|
9
|
+
def initialize(xml_path)
|
10
|
+
@cpdatas = []
|
11
|
+
if File::exist?(xml_path) then
|
12
|
+
@xml_file_path = xml_path
|
13
|
+
else
|
14
|
+
raise("#{xml_path} is invalid.")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def read
|
19
|
+
doc = REXML::Document.new(open(@xml_file_path))
|
20
|
+
doc.elements.each('acopy/copy') do |e|
|
21
|
+
d = CpData.new
|
22
|
+
d.setFrom(e.attributes["from"])
|
23
|
+
d.setTo(e.attributes["to"])
|
24
|
+
d.setMode(e.attributes["mode"].to_sym)
|
25
|
+
@cpdatas[@cpdatas.size] = d
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acopy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- opamp
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-06 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Auto copy tool
|
15
|
+
email: opampg@gmail.com
|
16
|
+
executables:
|
17
|
+
- acopy
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/acopy.rb
|
22
|
+
- lib/CpData.rb
|
23
|
+
- lib/DirCopy.rb
|
24
|
+
- lib/FailToCopyException.rb
|
25
|
+
- bin/acopy
|
26
|
+
homepage: https://bitbucket.org/opampg/acopy
|
27
|
+
licenses:
|
28
|
+
- BSD
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.0.0
|
38
|
+
none: false
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: !binary |-
|
44
|
+
MA==
|
45
|
+
none: false
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.24
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: auto copy tool
|
52
|
+
test_files: []
|