directory-digest 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 712a04620d53d7789977935e441c37cc9c029c53
4
- data.tar.gz: 69f20583777c881851a8496adb63c61c69f88ca2
3
+ metadata.gz: 3fd9036b031759454cc40701c9525d94f04a6c7e
4
+ data.tar.gz: 6fa8ee419a5a4ffa5b87814bf1fb76f938b6736d
5
5
  SHA512:
6
- metadata.gz: 683cf16fa483599216d4138a367ed31da766fc921d52f848e0eb045124329b4c3665705fb18320b1f135eb843d11c7d94d84f8dd5241658be8465e871c5b5af2
7
- data.tar.gz: a833bf25400426641a5629b5b95011b876a927960076a3ecdfd35678d341e1a18a1423019926cb97b1549a89edb625b636ab2596abbe08cf3607966c8b783426
6
+ metadata.gz: 131595aaf403f1ecb34b8097540254cdf621a9ee7e7d8db44f7058f866ec31118a6dbc9bb9fd0615e24b0c86c2260877b331c1cde868a2f6e38f1d75d4c2e80d
7
+ data.tar.gz: 14ebb95c3e7eb6e7ff93f13dc5472adbc3ce1741066c5b46a9b85395e18d3a1d07b731cb672b49b9bc98f62fbc54f13d8c00cbf3d08acc758401db6a49c2476c
@@ -1,13 +1,16 @@
1
1
  require 'openssl'
2
2
  require 'json'
3
+ require 'fileutils'
3
4
 
4
5
  module DirectoryDigest
5
6
  # DirectoryDigest::Digest - Creates a SHA256 digest of a directory's content
6
7
  class Digest
8
+ attr_reader :directory
7
9
  attr_reader :directory_digest
8
10
  attr_reader :file_digests
9
11
 
10
- def initialize(directory_digest, file_digests)
12
+ def initialize(directory, directory_digest, file_digests)
13
+ @directory = directory
11
14
  @directory_digest = directory_digest
12
15
  @file_digests = file_digests
13
16
  end
@@ -33,7 +36,7 @@ module DirectoryDigest
33
36
  end
34
37
  file_digests[path] = file_digest.hexdigest
35
38
  end
36
- Digest.new(directory_digest.hexdigest, file_digests)
39
+ Digest.new(directory, directory_digest.hexdigest, file_digests)
37
40
  end
38
41
 
39
42
  def ==(other)
@@ -53,13 +56,43 @@ module DirectoryDigest
53
56
  }
54
57
  end
55
58
 
59
+ def mirror_from(other, log = proc { |_| })
60
+ log.call("synchronizing #{other.directory} to #{directory}")
61
+ files_copied = 0
62
+ files_deleted = 0
63
+ changes = changes_relative_to(other)
64
+ changes[:removed].merge(changes[:changed]).each do |path, _|
65
+ source_path = "#{other.directory}#{path}"
66
+ destination_path = "#{directory}#{path}"
67
+ destination_directory = File.dirname(destination_path)
68
+ unless Dir.exist?(destination_directory)
69
+ log.call("create directory #{destination_directory}")
70
+ FileUtils.makedirs(destination_directory)
71
+ end
72
+ log.call("copy #{source_path} to #{destination_path}")
73
+ File.open(source_path, 'rb') do |source_file|
74
+ File.open(destination_path, 'wb') do |destination_file|
75
+ destination_file.write(source_file.read(4096)) until source_file.eof?
76
+ end
77
+ end
78
+ files_copied += 1
79
+ end
80
+ changes[:added].each do |path, _|
81
+ destination_path = "#{directory}#{path}"
82
+ log.call("delete #{destination_path}")
83
+ File.delete(destination_path)
84
+ files_deleted += 1
85
+ end
86
+ log.call("synchronized - files copied: #{files_copied} - files deleted: #{files_deleted}")
87
+ end
88
+
56
89
  def to_json
57
- JSON.pretty_generate(directory_digest: directory_digest, file_digests: file_digests)
90
+ JSON.pretty_generate(directory: directory, directory_digest: directory_digest, file_digests: file_digests)
58
91
  end
59
92
 
60
93
  def self.from_json(json)
61
94
  json = JSON.parse(json)
62
- Digest.new(json['directory_digest'], json['file_digests'])
95
+ Digest.new(json['directory'], json['directory_digest'], json['file_digests'])
63
96
  end
64
97
  end
65
98
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: directory-digest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Heald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-08 00:00:00.000000000 Z
11
+ date: 2016-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake