s3repo 0.0.1 → 0.0.2
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 +4 -4
- data/lib/s3repo.rb +1 -0
- data/lib/s3repo/cache.rb +74 -0
- data/lib/s3repo/client.rb +12 -7
- data/lib/s3repo/repo.rb +9 -2
- data/s3repo.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5985eb821b9ae69a840ae755eeab39f9b0f59f46
|
4
|
+
data.tar.gz: 9ee25f6bce8c773786e2407fa9003cbe5cb7188a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15ea01a2f0f96fd33158c39456d3b7bc53c25d2e129355ebccdf7ed02b683d2a7765fc9dba372d6311ab1e12a16515e60cbc787bc74a5f5e0c3d5299085309d4
|
7
|
+
data.tar.gz: 0d08b62db3819acd33bbb578291422d50bfa9cbf2f413d6d16c106ebfaf37597707ece3cff8e321e7fff7e815cea59debb3fe45d282ea409e3f7122be52595b4
|
data/lib/s3repo.rb
CHANGED
data/lib/s3repo/cache.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
module S3Repo
|
5
|
+
##
|
6
|
+
# Cache object, stores S3 objects on disk
|
7
|
+
class Cache < Base
|
8
|
+
TMPDIRS = [ENV['S3REPO_TMPDIR'], ENV['TMPDIR'], '/tmp/s3repo']
|
9
|
+
|
10
|
+
def initialize(params = {})
|
11
|
+
super
|
12
|
+
[partialdir, cachedir].each { |x| FileUtils.mkdir_p x }
|
13
|
+
end
|
14
|
+
|
15
|
+
def serve(path, recheck = true)
|
16
|
+
epath = expand_path(path)
|
17
|
+
prune(path, epath) if recheck
|
18
|
+
download(path, epath) unless File.exist?(epath)
|
19
|
+
File.open(epath) { |fh| fh.read }
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def expand_path(path)
|
25
|
+
File.absolute_path(path, cachedir)
|
26
|
+
end
|
27
|
+
|
28
|
+
def cached?(epath)
|
29
|
+
File.exist? epath
|
30
|
+
end
|
31
|
+
|
32
|
+
def download(path, epath)
|
33
|
+
FileUtils.mkdir_p File.dirname(epath)
|
34
|
+
tmpfile, etag = safe_download(path)
|
35
|
+
etag_path = "#{epath}-#{etag}"
|
36
|
+
File.rename tmpfile.path, etag_path
|
37
|
+
File.symlink(etag_path, epath)
|
38
|
+
end
|
39
|
+
|
40
|
+
def safe_download(path)
|
41
|
+
tmpfile = Tempfile.create(path.split('/').last, partialdir)
|
42
|
+
object = client.get_object(key: path)
|
43
|
+
tmpfile << object.body.read
|
44
|
+
tmpfile.close
|
45
|
+
[tmpfile, parse_etag(object)]
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_etag(object)
|
49
|
+
tag = object.etag.gsub('"', '')
|
50
|
+
return tag if tag.match(/^\h+$/)
|
51
|
+
fail('Invalid etag')
|
52
|
+
end
|
53
|
+
|
54
|
+
def prune(path, epath)
|
55
|
+
return unless cached? epath
|
56
|
+
current = File.readlink(epath).split('-').last
|
57
|
+
new = parse_etag client.head_object(key: path)
|
58
|
+
return if new == current
|
59
|
+
[epath, File.readlink(epath)].each { |x| File.unlink x }
|
60
|
+
end
|
61
|
+
|
62
|
+
def cachedir
|
63
|
+
File.join(tmpdir, 'cache')
|
64
|
+
end
|
65
|
+
|
66
|
+
def partialdir
|
67
|
+
File.join(tmpdir, 'partial')
|
68
|
+
end
|
69
|
+
|
70
|
+
def tmpdir
|
71
|
+
@tmpdir ||= File.absolute_path(@options[:tmpdir] || TMPDIRS.compact.first)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/s3repo/client.rb
CHANGED
@@ -16,14 +16,19 @@ module S3Repo
|
|
16
16
|
private
|
17
17
|
|
18
18
|
def method_missing(method, *args, &block)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
send(method, args.first)
|
24
|
-
else
|
25
|
-
super
|
19
|
+
return super unless @api.respond_to?(method)
|
20
|
+
define_singleton_method(method) do |*singleton_args|
|
21
|
+
params = build_params(singleton_args)
|
22
|
+
@api.send(method, params)
|
26
23
|
end
|
24
|
+
send(method, args.first)
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_params(args)
|
28
|
+
fail 'Too many arguments given' if args.size > 1
|
29
|
+
params = args.first || {}
|
30
|
+
fail 'Argument must be a hash' unless params.is_a? Hash
|
31
|
+
@defaults.dup.merge!(params)
|
27
32
|
end
|
28
33
|
end
|
29
34
|
end
|
data/lib/s3repo/repo.rb
CHANGED
@@ -9,17 +9,24 @@ module S3Repo
|
|
9
9
|
package
|
10
10
|
end
|
11
11
|
|
12
|
-
def packages
|
13
|
-
@packages = nil unless cache
|
12
|
+
def packages
|
14
13
|
@packages ||= parse_packages
|
15
14
|
end
|
16
15
|
|
16
|
+
def serve(path, recheck = true)
|
17
|
+
cache.serve(path, recheck)
|
18
|
+
end
|
19
|
+
|
17
20
|
def metadata
|
18
21
|
@metadata ||= Metadata.new(client: client)
|
19
22
|
end
|
20
23
|
|
21
24
|
private
|
22
25
|
|
26
|
+
def cache
|
27
|
+
@cache ||= Cache.new(client: client, tmpdir: @options[:tmpdir])
|
28
|
+
end
|
29
|
+
|
23
30
|
def upload!(file)
|
24
31
|
client.put_object(
|
25
32
|
bucket: bucket,
|
data/s3repo.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s3repo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Les Aker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mercenary
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- bin/s3repo
|
127
127
|
- lib/s3repo.rb
|
128
128
|
- lib/s3repo/base.rb
|
129
|
+
- lib/s3repo/cache.rb
|
129
130
|
- lib/s3repo/client.rb
|
130
131
|
- lib/s3repo/metadata.rb
|
131
132
|
- lib/s3repo/package.rb
|