s3repo 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5985eb821b9ae69a840ae755eeab39f9b0f59f46
4
- data.tar.gz: 9ee25f6bce8c773786e2407fa9003cbe5cb7188a
3
+ metadata.gz: 0641c73158e5144c4a988efb266b2fa57b2cde5d
4
+ data.tar.gz: 5ef0a969a378659cbc9f2aeb5f16b1a962890fc2
5
5
  SHA512:
6
- metadata.gz: 15ea01a2f0f96fd33158c39456d3b7bc53c25d2e129355ebccdf7ed02b683d2a7765fc9dba372d6311ab1e12a16515e60cbc787bc74a5f5e0c3d5299085309d4
7
- data.tar.gz: 0d08b62db3819acd33bbb578291422d50bfa9cbf2f413d6d16c106ebfaf37597707ece3cff8e321e7fff7e815cea59debb3fe45d282ea409e3f7122be52595b4
6
+ metadata.gz: 047abc0e5e6448278c265082985f14d7a7da59026720658f42899ecd5547b2f9be88770ea5091e35ce6bb97ba66488ac9b516c09d7290066d198f52c9db433a4
7
+ data.tar.gz: e6272c8574e44c6c830d87ba15718c70c0c10f0ba619d3e5ee2855c61f3830122514a2514cd933ffbf0fe86ea0f7ce5a28b35c655a5381782d3e0bc45e5a69a3
data/lib/s3repo.rb CHANGED
@@ -14,6 +14,4 @@ end
14
14
  require 's3repo/base'
15
15
  require 's3repo/cache'
16
16
  require 's3repo/client'
17
- require 's3repo/package'
18
- require 's3repo/metadata'
19
17
  require 's3repo/repo'
data/lib/s3repo/cache.rb CHANGED
@@ -12,51 +12,39 @@ module S3Repo
12
12
  [partialdir, cachedir].each { |x| FileUtils.mkdir_p x }
13
13
  end
14
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 }
15
+ def serve(key, refresh = true)
16
+ path = expand_path key
17
+ download(key, path) if refresh || !cached?(path)
18
+ File.open(path) { |fh| fh.read }
19
+ rescue Aws::S3::Errors::NoSuchKey
20
+ nil
20
21
  end
21
22
 
22
23
  private
23
24
 
24
- def expand_path(path)
25
- File.absolute_path(path, cachedir)
25
+ def expand_path(key)
26
+ File.absolute_path(key, cachedir)
26
27
  end
27
28
 
28
- def cached?(epath)
29
- File.exist? epath
29
+ def cached?(path)
30
+ File.exist? path
30
31
  end
31
32
 
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
33
+ def download(key, path)
34
+ FileUtils.mkdir_p File.dirname(path)
35
+ tmpfile = Tempfile.create(key, partialdir)
36
+ object = client.get_object(
37
+ key: key, if_none_match: etags[key], response_target: tmpfile
38
+ )
44
39
  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')
40
+ File.rename tmpfile.path, path
41
+ etags[key] = object.etag
42
+ rescue Aws::S3::Errors::NotModified
43
+ return
52
44
  end
53
45
 
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 }
46
+ def etags
47
+ @etags ||= {}
60
48
  end
61
49
 
62
50
  def cachedir
data/lib/s3repo/repo.rb CHANGED
@@ -1,30 +1,38 @@
1
+ require 'basiccache'
2
+
1
3
  module S3Repo
2
4
  ##
3
5
  # Repo object, represents an Arch repo inside an S3 bucket
4
6
  class Repo < Base
7
+ def initialize(params = {})
8
+ super
9
+ end
10
+
5
11
  def add_package(file)
6
12
  upload!(file)
7
- package = Package.new(client: client, key: file)
8
- metadata.add_package(package)
9
- package
10
13
  end
11
14
 
12
15
  def packages
13
- @packages ||= parse_packages
16
+ meta_cache.cache { parse_packages }
14
17
  end
15
18
 
16
- def serve(path, recheck = true)
17
- cache.serve(path, recheck)
19
+ def include?(key)
20
+ !packages.find { |x| x.key == key }.nil?
18
21
  end
19
22
 
20
- def metadata
21
- @metadata ||= Metadata.new(client: client)
23
+ def serve(key)
24
+ refresh = !key.match(/\.pkg\.tar\.xz$/)
25
+ file_cache.serve(key, refresh)
22
26
  end
23
27
 
24
28
  private
25
29
 
26
- def cache
27
- @cache ||= Cache.new(client: client, tmpdir: @options[:tmpdir])
30
+ def file_cache
31
+ @file_cache ||= Cache.new(client: client, tmpdir: @options[:tmpdir])
32
+ end
33
+
34
+ def meta_cache
35
+ @meta_cache ||= BasicCache::TimeCache.new lifetime: 600
28
36
  end
29
37
 
30
38
  def upload!(file)
@@ -36,9 +44,9 @@ module S3Repo
36
44
  end
37
45
 
38
46
  def parse_packages
39
- resp = client.list_objects(bucket: bucket).contents.map(&:key)
40
- resp.select! { |x| x.match(/.*\.pkg\.tar\.xz$/) }
41
- resp.map { |x| Package.new(client: client, key: x) }
47
+ client.list_objects(bucket: bucket).contents.select do |x|
48
+ x.key.match(/.*\.pkg\.tar\.xz$/)
49
+ end
42
50
  end
43
51
  end
44
52
  end
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.2'
3
+ s.version = '0.0.3'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
 
6
6
  s.summary = 'S3 Archlinux repo library'
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency 'mercenary', '~> 0.3.4'
18
18
  s.add_dependency 'aws-sdk', '~> 2.0.0'
19
+ s.add_dependency 'basiccache', '~> 1.0.0'
19
20
 
20
21
  s.add_development_dependency 'rubocop', '~> 0.28.0'
21
22
  s.add_development_dependency 'rake', '~> 10.4.0'
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.2
4
+ version: 0.0.3
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-28 00:00:00.000000000 Z
11
+ date: 2015-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mercenary
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: basiccache
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rubocop
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -128,8 +142,6 @@ files:
128
142
  - lib/s3repo/base.rb
129
143
  - lib/s3repo/cache.rb
130
144
  - lib/s3repo/client.rb
131
- - lib/s3repo/metadata.rb
132
- - lib/s3repo/package.rb
133
145
  - lib/s3repo/repo.rb
134
146
  - s3repo.gemspec
135
147
  - spec/s3repo_spec.rb
@@ -1,9 +0,0 @@
1
- module S3Repo
2
- ##
3
- # Metadata object, represents a package DB
4
- class Metadata < Base
5
- def add_package(package)
6
- puts "Adding #{package}"
7
- end
8
- end
9
- end
@@ -1,13 +0,0 @@
1
- module S3Repo
2
- ##
3
- # Package object, represents a single package tarball
4
- class Package < Base
5
- private
6
-
7
- def info
8
- lines = `tar xOf #{@options[:file]} .PKGINFO`.split("\n")
9
- lines.reject! { |x| x.match(/^#/) }
10
- lines.map { |x| x.split(' = ', 2) }.to_h
11
- end
12
- end
13
- end