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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5575094474d0415e12cdd36b75d9cbb6a09af19f
4
- data.tar.gz: 77e89871b806a7f07dc1c152c13cb3b4942fdc2a
3
+ metadata.gz: 5985eb821b9ae69a840ae755eeab39f9b0f59f46
4
+ data.tar.gz: 9ee25f6bce8c773786e2407fa9003cbe5cb7188a
5
5
  SHA512:
6
- metadata.gz: c999c20619aa6407b3a143c212c2413198f23558c4515f0e3ec9a17418ea1285936901b4a8cd76faa539d60d547b16cd0416b8baffc106961aecdd329facae5e
7
- data.tar.gz: 370965115705922b94325879ec7baf2a3eb25188921946032ae66ca58c75031ed9c1f1f0a742556f709ebb907f5c82eb5e78610db2bdc4d5aadbe6ba5691673f
6
+ metadata.gz: 15ea01a2f0f96fd33158c39456d3b7bc53c25d2e129355ebccdf7ed02b683d2a7765fc9dba372d6311ab1e12a16515e60cbc787bc74a5f5e0c3d5299085309d4
7
+ data.tar.gz: 0d08b62db3819acd33bbb578291422d50bfa9cbf2f413d6d16c106ebfaf37597707ece3cff8e321e7fff7e815cea59debb3fe45d282ea409e3f7122be52595b4
data/lib/s3repo.rb CHANGED
@@ -12,6 +12,7 @@ module S3Repo
12
12
  end
13
13
 
14
14
  require 's3repo/base'
15
+ require 's3repo/cache'
15
16
  require 's3repo/client'
16
17
  require 's3repo/package'
17
18
  require 's3repo/metadata'
@@ -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
- if @api.respond_to?(method) && args.size == 1 && args.first.is_a?(Hash)
20
- define_singleton_method(method) do |*a|
21
- @api.send(method, @defaults.dup.merge!(a.first))
22
- end
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(cache = true)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 's3repo'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
 
6
6
  s.summary = 'S3 Archlinux repo library'
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.1
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-23 00:00:00.000000000 Z
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