crawling 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b73a78419ddf8c722d1472d4c671caa74143cc14
4
+ data.tar.gz: 7c36f3ee9ba92e5189da56ab39db21aec2cea3b3
5
+ SHA512:
6
+ metadata.gz: be515f16be2dc6f2cde139cda1cc867d62e006a9d2816bbcea5d2f35fca6d3149aac6a24af3f78151e27ddc9af95ea1ffad41f3fd512bebf61757d3db79dd332
7
+ data.tar.gz: 4e20bb27c661023040a4b749d29271b94e5f53f4de67e86f31a53655b455ea3e540621df54e7bd7f6f661fc9d4fbdca25cff32017776e71c8d48b4b682faf147
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /test/tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crawling.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 James Pike
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,33 @@
1
+ # crawling
2
+
3
+ [![build status](https://circleci.com/gh/ohjames/crawling.png)](https://circleci.com/gh/ohjames/crawling)
4
+
5
+ Welcome to crawling, documentation coming soon!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'crawling'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install crawling
22
+
23
+ ## Usage
24
+
25
+ Coming soon.
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ohjames/crawling.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crawling"
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
data/bin/crawling ADDED
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'crawling'
5
+ require 'moister'
6
+ require 'ostruct'
7
+
8
+ module Crawling::Command
9
+ def self.run args
10
+ config = nil
11
+ command = nil
12
+ positionals = nil
13
+
14
+ Moister::SubcommandOptionParser.new do |op|
15
+ op.banner = 'usage: crawling [global options] command [command options]'
16
+
17
+ op.for_all do |op|
18
+ op.on_tail '-h', '--help', 'show this help message' do
19
+ puts op
20
+ exit
21
+ end
22
+ end
23
+
24
+ op.on '-c', '--config dir', 'path to configuration directory', 'config_dir'
25
+ op.on '-H', '--home dir', 'path to home directory', 'home_dir'
26
+
27
+ op.subcommand 'cd', 'run shell in store directory'
28
+ op.subcommand 'add', 'add files to store directory'
29
+ op.subcommand 'get', 'get files from store directory'
30
+ op.subcommand 'diff', 'show differences between store directory and filesystem'
31
+ op.subcommand 'merge', 'merge differences between store directory and filesystem' do |subop|
32
+ op.on '-m', '--merge-app app', 'command used to merge files', 'merge_app'
33
+ end
34
+ op.subcommand 'clone', 'clone or pull latest store using configured commands'
35
+
36
+ parsed_cfg = op.parse(args)
37
+ positionals = parsed_cfg.positionals
38
+ command = parsed_cfg.command
39
+ config = OpenStruct.new parsed_cfg[:config]
40
+ end
41
+
42
+ unless command
43
+ puts 'please supply a command, see --help'
44
+ exit
45
+ end
46
+
47
+ crawling = Crawling::Instance.new(**config.to_h)
48
+ case command
49
+ when 'cd'
50
+ crawling.cd positionals[0]
51
+ when 'add'
52
+ crawling.add positionals
53
+ when 'get'
54
+ crawling.get positionals
55
+ when 'diff'
56
+ crawling.diff positionals
57
+ when 'merge'
58
+ crawling.merge positionals
59
+ when 'clone'
60
+ crawling.clone
61
+ end
62
+ rescue RuntimeError => e
63
+ puts e.to_s
64
+ rescue Interrupt => e
65
+ exit 1
66
+ end
67
+ end
68
+
69
+ Crawling::Command::run ARGV
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.3
data/crawling.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crawling/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crawling"
8
+ spec.version = Crawling::VERSION
9
+ spec.authors = ["James Pike"]
10
+ spec.email = ["github@chilon.net"]
11
+
12
+ spec.summary = %q{Tool to manage personal configuration and environment files.}
13
+ spec.description = spec.summary
14
+ spec.homepage = "http://github.com/ohjames/crawling"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+
34
+ spec.add_dependency "moister", "~> 0.2"
35
+ spec.add_dependency "diffy", "~> 3.1.0"
36
+ end
@@ -0,0 +1,3 @@
1
+ module Crawling
2
+ VERSION = "0.1.0"
3
+ end
data/lib/crawling.rb ADDED
@@ -0,0 +1,213 @@
1
+ require "crawling/version"
2
+ require "diffy"
3
+ require "pathname"
4
+
5
+ module Crawling
6
+ # path to repository for user(s)
7
+ HOME_PARENT_DIR = 'home'
8
+
9
+ # path to repository for the system
10
+ SYSTEM_PARENT_DIR = 'system'
11
+
12
+ N_LINES_DIFF_CONTEXT = 3
13
+
14
+ def self.child_files_recursive path
15
+ Dir.glob("#{path}/**/*", File::FNM_DOTMATCH).reject(&File.method(:directory?))
16
+ end
17
+
18
+ # Like File.cp but also creates the parent directory at destination if it doesn't exist
19
+ def self.copy_file src_file, dest_file
20
+ dest_parent_dir = File.dirname dest_file
21
+ FileUtils.mkdir_p dest_parent_dir unless Dir.exist? dest_parent_dir
22
+ begin
23
+ FileUtils.cp(src_file, dest_file)
24
+ rescue
25
+ raise "could not copy from #{src_file} to #{dest_file}"
26
+ end
27
+ end
28
+
29
+ class Instance
30
+ def initialize(config_dir: nil, home_dir: nil, merge_app: nil)
31
+ @home_dir = home_dir || ENV['HOME']
32
+ @home_pathname = Pathname.new(@home_dir).expand_path
33
+ @config_dir = config_dir || "#{@home_dir}/.config/crawling"
34
+ @config_pathname = Pathname.new(@config_dir).expand_path
35
+ @merge_app = merge_app || 'vimdiff %s %h'
36
+ end
37
+
38
+ def cd(subdir = nil)
39
+ cd_dir = get_config_dir
40
+ cd_dir = Path.join(cd_dir, subdir) if subdir
41
+ raise "directory #{subdir} doesn't exist" unless Dir.exists? cd_dir
42
+
43
+ Dir.chdir cd_dir
44
+ puts "creating shell in #{cd_dir}, type exit or ctrl-D to exit"
45
+ system ENV['SHELL']
46
+ puts "crawling shell exited"
47
+ end
48
+
49
+ def add paths
50
+ raise "add command requires paths" if paths.empty?
51
+
52
+ paths.each do |path|
53
+ raise "path #{path} does not exist" unless File.exists? path
54
+
55
+ each_with_storage_path(files_from path) do |file, storage_file|
56
+ Crawling.copy_file file, storage_file
57
+ end
58
+ end
59
+ end
60
+
61
+ def get paths
62
+ raise "get command requires paths" if paths.empty?
63
+
64
+ each_with_storage_path(paths) do |path, storage_path|
65
+ raise "path #{path} does not exist in storage" unless File.exists? storage_path
66
+
67
+ files_from(storage_path).each do |storage_file|
68
+ sys_path = from_storage_path storage_file
69
+ Crawling.copy_file storage_file, sys_path
70
+ end
71
+ end
72
+ end
73
+
74
+ def diff paths
75
+ each_with_storage_path(files_from_paths_or_all paths) do |file, storage_file|
76
+ missing_from = file_or_storage_file_doesnt_exist file, storage_file
77
+ if missing_from
78
+ puts "#{file}: doesn't exist in #{missing_from}"
79
+ next
80
+ end
81
+
82
+ diff = get_diff storage_file, file
83
+ unless diff == ''
84
+ puts "#{file}:"
85
+ puts diff
86
+ puts
87
+ end
88
+ end
89
+ end
90
+
91
+ def merge paths
92
+ each_with_storage_path(files_from_paths_or_all paths) do |file, storage_file|
93
+ missing_from = file_or_storage_file_doesnt_exist file, storage_file
94
+ if missing_from
95
+ case missing_from
96
+ when 'home'
97
+ puts "#{file}: creating from store"
98
+ Crawling.copy_file storage_file, file
99
+ when 'store'
100
+ puts "#{file}: creating in store from home"
101
+ Crawling.copy_file storage_file, file
102
+ else
103
+ puts "#{file}: does not exist in home or store"
104
+ end
105
+
106
+ next
107
+ end
108
+
109
+ while (diff_string = get_diff storage_file, file) != ''
110
+ print "#{file}: show [d]iff, [m]erge, take [h]ome, take [S]tore, skip [n]ext? "
111
+ answer = STDIN.gets.chomp
112
+ case answer
113
+ when 'd'
114
+ puts diff_string
115
+ puts
116
+ redo
117
+ when 'h'
118
+ Crawling.copy_file file, storage_file
119
+ break
120
+ when 'm'
121
+ system *@merge_app.sub('%s', storage_file).sub('%h', file).split(' ')
122
+ when 'n'
123
+ break
124
+ when 'S'
125
+ Crawling.copy_file storage_file, file
126
+ break
127
+ else
128
+ puts 'please answer with d, h, m, n or S'
129
+ redo
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ def clone
136
+ raise "clone: command not supported yet"
137
+ end
138
+
139
+ private
140
+ def get_config_dir
141
+ FileUtils::mkdir_p @config_dir unless Dir.exists? @config_dir
142
+ @config_dir
143
+ end
144
+
145
+ def relative_path_to target, relative_to
146
+ Pathname.new(target).expand_path.relative_path_from relative_to
147
+ end
148
+
149
+ def each_with_storage_path paths
150
+ paths.each do |path|
151
+ yield path, get_storage_path(path)
152
+ end
153
+ end
154
+
155
+ def get_home_path path
156
+ relative_path_to path, @home_pathname
157
+ end
158
+
159
+ def get_storage_path path
160
+ # TODO: get system or home path depending on whether it is a subdirectory of the current user
161
+ File.join @config_dir, HOME_PARENT_DIR, get_home_path(path)
162
+ end
163
+
164
+ def from_storage_path path
165
+ cfg_rel_path = Pathname.new(relative_path_to path, @config_pathname)
166
+ head, *tail = Pathname(cfg_rel_path).each_filename.to_a
167
+ if head === 'home'
168
+ File.join @home_dir, *tail
169
+ else
170
+ raise "storage type #{head} not supported yet"
171
+ end
172
+ end
173
+
174
+ # if paths is empty then get home paths for all paths in storage, else get the
175
+ # files recursively reachable from the provided paths
176
+ def files_from_paths_or_all paths
177
+ if paths.empty?
178
+ # TODO: also support 'SYSTEM_PARENT_DIR'
179
+ Crawling.child_files_recursive(
180
+ File.join(@config_dir, HOME_PARENT_DIR)
181
+ ).map &method(:from_storage_path)
182
+ else
183
+ paths.map(&method(:files_from)).flatten
184
+ end
185
+ end
186
+
187
+ def files_from path
188
+ Dir.exists?(path) ? Crawling.child_files_recursive(path) : [path]
189
+ end
190
+
191
+ def file_or_storage_file_doesnt_exist file, storage_file
192
+ if not File.exists? file
193
+ if File.exists? storage_file
194
+ 'home'
195
+ else
196
+ 'home directory or store'
197
+ end
198
+ elsif not File.exists? storage_file
199
+ 'store'
200
+ end
201
+ end
202
+
203
+ def get_diff src_file, dest_file
204
+ diff = Diffy::Diff.new(
205
+ src_file,
206
+ dest_file,
207
+ source: 'files',
208
+ include_diff_info: true,
209
+ context: N_LINES_DIFF_CONTEXT
210
+ ).to_s
211
+ end
212
+ end
213
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crawling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Pike
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-16 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: moister
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: diffy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ description: Tool to manage personal configuration and environment files.
84
+ email:
85
+ - github@chilon.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/crawling
97
+ - bin/setup
98
+ - circle.yml
99
+ - crawling.gemspec
100
+ - lib/crawling.rb
101
+ - lib/crawling/version.rb
102
+ homepage: http://github.com/ohjames/crawling
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ allowed_push_host: https://rubygems.org
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Tool to manage personal configuration and environment files.
127
+ test_files: []