artiq-sync 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be69fc189706e6ab245997523aacf7f26478c3b1
4
+ data.tar.gz: 8b197a976d281398171756d65d1ffceda9be015a
5
+ SHA512:
6
+ metadata.gz: 3cea50e96d966c04bca4b0ab1a5bd183b36b11f6d315dc2e02277be631c9fa99d966e1be051a85ee7edc0000649cb435c3bf20e627b844f2e6e2b404c261bd05
7
+ data.tar.gz: b3b2ee978e8fb655c7fc62e12e810e3055dcf09db27d740f64c9f74bd606ff143378aae06ba5d39993cd5242c82aaa3b87e6ded08522bb44fbca8b404fab4492
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ /*.gem
2
+ /Gemfile.lock
3
+ .rvmrc
4
+ Gemfile.lock
5
+ .idea
data/CONTRIBUTORS ADDED
@@ -0,0 +1 @@
1
+ Sam Qiu<sam@samqiu.com>
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/History.txt ADDED
@@ -0,0 +1,9 @@
1
+ === 0.1.1 2015-01-31
2
+
3
+ * 1 major enhancement:
4
+ * Fix http blocking
5
+
6
+ === 0.1.0 2014-09-14
7
+
8
+ * 1 major enhancement:
9
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2014 - http://artiq.in
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 ADDED
File without changes
data/Rakefile ADDED
File without changes
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'artiq-sync'
3
+ s.version = '0.1.3'
4
+ s.date = '2015-05-18'
5
+ s.summary = "For Artiq theme sync"
6
+ s.description = "For Artiq theme sync"
7
+ s.authors = ['Sam Qiu']
8
+ s.email = 'kinsamqiu@gmail.com'
9
+
10
+ s.add_dependency('listen', '~> 2.7')
11
+ s.add_dependency('rest-client', '~> 1.7')
12
+ s.add_dependency('json', '~> 1.8')
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- test/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
17
+ s.require_paths = ['lib']
18
+
19
+ s.homepage = 'https://github.com/ArtiqHQ/sync'
20
+ s.license = 'MIT'
21
+ end
data/bin/artiq_sync ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
4
+
5
+ require 'artiq_sync'
6
+
7
+ Artiq::Sync::Runner.execute(ARGV)
@@ -0,0 +1,142 @@
1
+ require 'listen'
2
+ require 'rest_client'
3
+ require 'json'
4
+ require 'yaml'
5
+
6
+ module Artiq
7
+ module Sync
8
+ class Runner
9
+
10
+ MAX_SIZE = 1024*1024
11
+ WHITELIST = %w(.coffee .js .json .sass .scss .css .jpg .jpeg .png .gif .ico .html .md .txt .ttf .woff .svg .eot .map)
12
+ DIRECTORY = %w(assets layouts partials templates)
13
+
14
+ class << self
15
+ def execute(args)
16
+ puts args
17
+
18
+ config_filename = 'config.yml'
19
+
20
+ if File.exist?(config_filename)
21
+ config = YAML::load_file(config_filename)
22
+ else
23
+ config = {}
24
+ print "Please enter journal's domain, theme name and your API token\n"
25
+ config['domain'] = [(print 'Domain (with http://): '), gets.rstrip][1]
26
+ config['theme'] = [(print 'Theme: '), gets.rstrip][1]
27
+ config['access_token'] = [(print 'Access Token: '), gets.rstrip][1]
28
+ File.open(config_filename, 'a') { |f| f.write config.to_yaml }
29
+ end
30
+
31
+ print "Domain: #{config['domain']}\n"
32
+ print "Theme: #{config['theme']}\n"
33
+ print "\n"
34
+
35
+ if !File.exist?('assets') || !File.exist?('templates') || !File.exist?('layouts')
36
+ print "Starting pull files\n"
37
+ pull(config['domain'], config['theme'], config['access_token'])
38
+ print "Pulling is done\n"
39
+ end
40
+
41
+ watch(config['domain'], config['theme'], config['access_token'])
42
+ end
43
+
44
+ def watch(api_base, theme, access_token)
45
+ puts "Watching: #{Dir.pwd}"
46
+
47
+ listener = Listen.to(Dir.pwd, latency: 1) do |modified, added, removed|
48
+ modified.each do |file|
49
+ next unless allow?(file)
50
+
51
+ Thread.new do
52
+ puts "POST #{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}\n"
53
+ response = RestClient.post("#{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}", {file: File.new(file, 'rb')}, :access_token => access_token) { |response, request, result| response }
54
+ if response.code != 200
55
+ puts response.body
56
+ end
57
+ end.join
58
+ end
59
+
60
+ # New files
61
+ added.each do |file|
62
+ next unless allow?(file)
63
+ Thread.new do
64
+ puts "POST #{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}\n"
65
+ response = RestClient.post("#{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}", {file: File.new(file, 'rb')}, :access_token => access_token) { |response, request, result| response }
66
+ if response.code != 200
67
+ puts response.body
68
+ end
69
+ end.join
70
+ end
71
+
72
+ # Deleted files
73
+ removed.each do |file|
74
+ # next unless allow?(file)
75
+ Thread.new do
76
+ puts "DELETE #{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}\n"
77
+ response = RestClient.delete("#{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}", :access_token => access_token) { |response, request, result| response }
78
+ if response.code != 200
79
+ puts response.body
80
+ end
81
+ end.join
82
+ end
83
+ end
84
+
85
+ listener.start
86
+ sleep
87
+ end
88
+
89
+ # Pulling file from remote
90
+ def pull(api_base, theme, access_token)
91
+ # url = "#{api_base}/api/v1/themes/#{theme}/tree"
92
+ url = [api_base, 'api', 'v1', 'themes', theme, 'tree'].join('/')
93
+ puts url
94
+ response = RestClient.get url, :access_token => access_token
95
+
96
+ response_json = JSON.parse(response)
97
+
98
+ response_json['files'].each do |filename|
99
+ puts filename
100
+ dist = filename[url.length+1, filename.length]
101
+ FileUtils.mkdir_p(File.dirname(dist)) unless File.exist?(File.dirname(dist))
102
+
103
+ response = RestClient.get(filename, :access_token => access_token) { |response, request, result| response }
104
+ if response.code == 200
105
+ open(dist, 'wb') do |file|
106
+ file.write(response.body)
107
+ end
108
+ else
109
+ puts response.code
110
+ end
111
+ end
112
+ end
113
+
114
+ private
115
+ # Cutting this filename
116
+ def cut(file, pwd = Dir.pwd)
117
+ file[pwd.length, file.length]
118
+ end
119
+
120
+ # Allow this file?
121
+ def allow?(file)
122
+ if File.size(file) > MAX_SIZE
123
+ puts "Too large, skip: #{file}"
124
+ return false
125
+ end
126
+
127
+ if !DIRECTORY.include?(file[Dir.pwd.length+1, file.length].split('/').first)
128
+ puts "Directory not allowed: #{file}"
129
+ return false
130
+ end
131
+
132
+ if !WHITELIST.include?(File.extname(file))
133
+ puts "Extension not allowed: #{file}"
134
+ return false
135
+ end
136
+
137
+ true
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
data/lib/artiq_sync.rb ADDED
@@ -0,0 +1 @@
1
+ require 'artiq/sync/runner'
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artiq-sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Sam Qiu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: listen
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ description: For Artiq theme sync
56
+ email: kinsamqiu@gmail.com
57
+ executables:
58
+ - artiq_sync
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CONTRIBUTORS
64
+ - Gemfile
65
+ - History.txt
66
+ - LICENSE
67
+ - README
68
+ - Rakefile
69
+ - artiq_sync.gemspec
70
+ - bin/artiq_sync
71
+ - lib/artiq/sync/runner.rb
72
+ - lib/artiq_sync.rb
73
+ homepage: https://github.com/ArtiqHQ/sync
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: For Artiq theme sync
97
+ test_files: []