file_change_observer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +34 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/file_change_observer.gemspec +29 -0
- data/lib/file_change_observer.rb +124 -0
- data/lib/file_change_observer/version.rb +3 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d24ad0b2698d233571a5629f04027f0d32b654e
|
4
|
+
data.tar.gz: b39bcb167dc9fc5044ebfb865ee3b2350d68c20e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45284603ab33a0c25b21f90a32fa9d401354725c306758f51a82f6f5e1bdf7c53032b2e1fc09db5297ca7f34ae9d6d26d4e0ef8b8a8de72c3f31366f0e667bd3
|
7
|
+
data.tar.gz: de206f8efcb035947b9d7233e4754fdeff7194fca7b51022cd5aed06aed0e9ce3b7129d5bdf2901413048af7af56a7c91e0c4cae059f61169ef7b34eca37ba75
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Burke Libbey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# FileChangeObserver
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
require 'tmpdir'
|
5
|
+
|
6
|
+
tgz_path = Dir.mktmpdir do |dir|
|
7
|
+
FileChangeObserver.tar_changes(dir) do
|
8
|
+
File.write("#{dir}/foo", 'neato')
|
9
|
+
FileUtils.mkdir("#{dir}/bar")
|
10
|
+
File.write("#{dir}/bar/baz", 'it works')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# -rw-r--r-- 0 wheel wheel 5 2 Oct 15:01 foo
|
15
|
+
# -rw-r--r-- 0 wheel wheel 8 2 Oct 15:01 bar/baz
|
16
|
+
system("tar tvf #{tgz_path}")
|
17
|
+
File.unlink(tgz_path)
|
18
|
+
```
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'file_change_observer'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install file_change_observer
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "file_change_observer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "file_change_observer/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "file_change_observer"
|
8
|
+
spec.version = FileChangeObserver::VERSION
|
9
|
+
spec.authors = ["Burke Libbey"]
|
10
|
+
spec.email = ["burke.libbey@shopify.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Tar files changed by a block}
|
13
|
+
spec.description = %q{Tar files changed by a block.}
|
14
|
+
spec.homepage = "https://github.com/burke/file_change_observer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = []
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
|
28
|
+
spec.add_runtime_dependency "rb-fsevent", ">= 0.10.0"
|
29
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'rb-fsevent'
|
2
|
+
require 'rubygems/package'
|
3
|
+
require 'set'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'zlib'
|
6
|
+
require 'pathname'
|
7
|
+
require 'tempfile'
|
8
|
+
|
9
|
+
# FileChangeObserver provides a method to invoke a block while capturing all
|
10
|
+
# filesystem changes made by that block under a particular subtree as a gzipped
|
11
|
+
# tarball.
|
12
|
+
module FileChangeObserver
|
13
|
+
LATENCY = 0.01
|
14
|
+
SENTINEL = ".__observe_files_break_now__".freeze
|
15
|
+
ITEM_IS_DIR = 'ItemIsDir'.freeze
|
16
|
+
SYS_SYNC = 36 # syscall number for sync(3) on darwin
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# Watch a directory and generate a gzipped tarball of all the files
|
20
|
+
# changed, modified, or otherwise touched while the given block is runs.
|
21
|
+
#
|
22
|
+
# See observe_changes for a handful of limitations regarding which changes
|
23
|
+
# are included.
|
24
|
+
def tar_changes(from_root, to: gen_tempfile_path, &block)
|
25
|
+
affected_paths = observe_changes(from_root, &block)
|
26
|
+
root_path = Pathname.new(File.realpath(from_root))
|
27
|
+
|
28
|
+
Zlib::GzipWriter.open(to) do |gz_writer|
|
29
|
+
Gem::Package::TarWriter.new(gz_writer) do |tar_writer|
|
30
|
+
affected_paths.each do |file|
|
31
|
+
add_file(tar_writer, file, root_path)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
to
|
37
|
+
end
|
38
|
+
|
39
|
+
# Set up an fsevents handler on the given root directory, and, while
|
40
|
+
# running the given block, collect a set of all the files changed under
|
41
|
+
# that root.
|
42
|
+
#
|
43
|
+
# The tarball does not include any sort of deletion sentinel for files
|
44
|
+
# deleted by the block, and does not include entries for directories (so
|
45
|
+
# empty directories created by the block will not show up in the tarball).
|
46
|
+
#
|
47
|
+
# Additionally, a file doesn't have to be actually *changed* in order to be
|
48
|
+
# returned; anything that generates an `fsevents` event is sufficient.
|
49
|
+
def observe_changes(from_root)
|
50
|
+
affected_paths = Set.new
|
51
|
+
sentinel_path = File.join(from_root, SENTINEL)
|
52
|
+
|
53
|
+
# if the directory was recently created, it *seems* like it needs to
|
54
|
+
# actually be written out before we start the watch, otherwise we don't
|
55
|
+
# receive any events.
|
56
|
+
#
|
57
|
+
# Unfortunately, sync(3) is a slow/expensive operation and a
|
58
|
+
# newly-created direcotry is probably not the most common use-case. As a
|
59
|
+
# compromise, we sync(3) only if the directory appears to have been
|
60
|
+
# created in the past few seconds.
|
61
|
+
if (Time.now - File.stat(from_root).ctime) < 5
|
62
|
+
Kernel.syscall(SYS_SYNC)
|
63
|
+
end
|
64
|
+
|
65
|
+
fsevent = FSEvent.new
|
66
|
+
fsevent.watch(from_root, file_events: true, latency: LATENCY) do |paths, meta|
|
67
|
+
done = false
|
68
|
+
meta['events'].each do |evt|
|
69
|
+
# Don't track directories. We could change this...
|
70
|
+
next if evt['flags'].include?(ITEM_IS_DIR)
|
71
|
+
path = evt['path']
|
72
|
+
# Because macOS delivers these events with a configurable latency, we
|
73
|
+
# have to wait for it to flush its pipeline before we can escape from
|
74
|
+
# the run loop, so we push this sentinel event after invoking the
|
75
|
+
# block. We know we're done when we receive it.
|
76
|
+
if path.end_with?(SENTINEL)
|
77
|
+
done = true
|
78
|
+
else
|
79
|
+
affected_paths << path
|
80
|
+
end
|
81
|
+
end
|
82
|
+
raise Interrupt if done
|
83
|
+
end
|
84
|
+
thr = Thread.new { fsevent.run }
|
85
|
+
# If we yield before the handler is set up, early events will be
|
86
|
+
# discarded.
|
87
|
+
sleep 1e-6 until fsevent.instance_variable_get(:@running)
|
88
|
+
|
89
|
+
yield
|
90
|
+
|
91
|
+
# Once the thread recieves the event for the sentinel_path, it will
|
92
|
+
# return, and #join will return.
|
93
|
+
FileUtils.touch(sentinel_path)
|
94
|
+
thr.join
|
95
|
+
File.unlink(sentinel_path) # clean up
|
96
|
+
|
97
|
+
affected_paths
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
# Generate a path to a valid (non-existent) tempfile.
|
103
|
+
def gen_tempfile_path
|
104
|
+
tf = Tempfile.new('filechanges.tgz')
|
105
|
+
tf_path = tf.path
|
106
|
+
tf.close
|
107
|
+
tf.unlink
|
108
|
+
tf_path
|
109
|
+
end
|
110
|
+
|
111
|
+
# Add a file to the TarWriter. Since our events may correspond to
|
112
|
+
# now-deleted files, we swallow ENOENT.
|
113
|
+
def add_file(tar_writer, file, root_path)
|
114
|
+
stat = File.stat(file)
|
115
|
+
name = Pathname.new(file).relative_path_from(root_path).to_s
|
116
|
+
tar_writer.add_file_simple(name, stat.mode, stat.size) do |io|
|
117
|
+
File.open(file, 'rb') { |f| IO.copy_stream(f, io) }
|
118
|
+
end
|
119
|
+
rescue Errno::ENOENT
|
120
|
+
nil
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_change_observer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Burke Libbey
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rb-fsevent
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.10.0
|
69
|
+
description: Tar files changed by a block.
|
70
|
+
email:
|
71
|
+
- burke.libbey@shopify.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- file_change_observer.gemspec
|
85
|
+
- lib/file_change_observer.rb
|
86
|
+
- lib/file_change_observer/version.rb
|
87
|
+
homepage: https://github.com/burke/file_change_observer
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.6.13
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Tar files changed by a block
|
111
|
+
test_files: []
|