bagit 0.3.5 → 0.4.4

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
- SHA1:
3
- metadata.gz: e581d4e4b632ebf75e7958de8dffef75425e695d
4
- data.tar.gz: cacad3cbb8385dc5600732730ede21751683d034
2
+ SHA256:
3
+ metadata.gz: 4b4fd9e3eb682521a82c7cd25ad0320088fba8fa907876a48c39320e2b245478
4
+ data.tar.gz: ec3971461e6304cc457e50323b369ce0d61a6b3df5f3ac445601c9f34d456f67
5
5
  SHA512:
6
- metadata.gz: 385db1340cf4707f0ffa118d515b1d1d5739abc15750758466bce2b3f8d34bc221da766f3f353c3d095c27f1f20f63b6dab951a4d2e638b47174bc126fdc3f5a
7
- data.tar.gz: 0577b97788dc9cd9871760653b380c719a7eb9192391c57e5632b6ef56855fca2ddcdb92ba2932a4b692417fe8167f0a0b2f5a5376456b0399c27012818c6393
6
+ metadata.gz: 78dff8b4378080349c13e9794302792e981f36db37f67e206658c554ecf0e025ba3c4332a5cd9f53f48a53935c5cbce9fb4435f28badccd0dacdf556b9ef30e7
7
+ data.tar.gz: 98828d498d5fdb72551587d4a4b766a579470e911d0056debe8098f9a59cd89c04cd2e1e204f02332b3a0813e2b3967a2ac7f6bae91895463bb7144590e54fb4
data/.gitignore CHANGED
@@ -8,4 +8,5 @@ vendor
8
8
  Gemfile.lock
9
9
  .bundle
10
10
  .project
11
- .idea
11
+ .idea
12
+ coverage
@@ -1,6 +1,5 @@
1
1
  rvm:
2
- - 2.3
3
- - 2.2
2
+ - 2.5
4
3
  branches:
5
4
  only:
6
5
  - master
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- gemspec
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/README.md CHANGED
@@ -30,7 +30,7 @@ Example: making a bag
30
30
  end
31
31
 
32
32
  # generate the manifest and tagmanifest files
33
- bag.manifest!
33
+ bag.manifest!(algo: 'sha256')
34
34
 
35
35
  Example: validating an existing bag
36
36
  -----------------------------------
data/Rakefile CHANGED
@@ -1,16 +1,19 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
1
+ # frozen_string_literal: true
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
3
5
  Bundler.setup(:default, :development, :test)
4
6
 
5
- require 'rake'
6
- require 'rdoc/task'
7
- require 'rspec/core/rake_task'
7
+ require "rake"
8
+ require "rdoc/task"
9
+ require "rspec/core/rake_task"
10
+ require "standard/rake"
8
11
 
9
12
  Bundler::GemHelper.install_tasks
10
13
 
11
14
  RSpec::Core::RakeTask.new do |t|
12
- t.pattern = 'spec/**/*_spec.rb'
13
- t.rspec_opts = %w(--format documentation --color)
15
+ t.pattern = "spec/**/*_spec.rb"
16
+ t.rspec_opts = %w[--format documentation --color]
14
17
  end
15
18
 
16
- task :default => [:spec]
19
+ task default: [:standard, :spec]
@@ -1,30 +1,34 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'bagit/version'
5
+ require "bagit/version"
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "bagit"
8
- spec.version = BagIt::VERSION
9
- spec.summary = "BagIt package generation and validation"
8
+ spec.name = "bagit"
9
+ spec.version = BagIt::VERSION
10
+ spec.summary = "BagIt package generation and validation"
10
11
  spec.description = "Ruby Library and Command Line tools for bagit"
11
- spec.email = "jamie@jamielittle.org"
12
- spec.homepage = 'http://github.com/tipr/bagit'
13
- spec.authors = ["Tom Johnson, Francesco Lazzarino, Jamie Little"]
14
- spec.license = "MIT"
12
+ spec.email = "jamie@jamielittle.org"
13
+ spec.homepage = "http://github.com/tipr/bagit"
14
+ spec.authors = ["Tom Johnson, Francesco Lazzarino, Jamie Little"]
15
+ spec.license = "MIT"
16
+
17
+ spec.required_ruby_version = "~> 2.0"
15
18
 
16
- spec.required_ruby_version = '~> 2.0'
17
-
18
- spec.add_dependency 'validatable', '~> 1.6'
19
- spec.add_dependency 'docopt', '~> 0.5.0'
19
+ spec.add_dependency "validatable", "~> 1.6"
20
+ spec.add_dependency "docopt", "~> 0.5.0"
20
21
 
21
- spec.add_development_dependency 'bundler'
22
- spec.add_development_dependency 'rake', '~> 10.4'
23
- spec.add_development_dependency 'rspec', '~> 3'
24
- spec.add_development_dependency 'coveralls'
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "coveralls"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-byebug"
26
+ spec.add_development_dependency "rake", ">= 12.3.3"
27
+ spec.add_development_dependency "rspec", "~> 3"
28
+ spec.add_development_dependency "standard"
25
29
 
26
- spec.files = `git ls-files`.split($/)
27
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
28
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
30
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
31
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
32
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
29
33
  spec.require_paths = ["lib"]
30
34
  end
data/bin/bagit CHANGED
@@ -1,24 +1,25 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bagit'
4
5
  require 'docopt'
5
6
  require 'logger'
6
-
7
7
  logger = Logger.new(STDOUT)
8
8
 
9
9
  doc = <<DOCOPT
10
10
  BagIt.
11
11
 
12
- Usage:
13
- bagit add [-f <file>...] [-t <tagfile>...] BAGPATH
12
+ Usage:
13
+ bagit add [-f <file>...] [-t <tagfile>...] [-a <algo>] BAGPATH
14
14
  bagit delete [-f <file>...] [-t <tagfile>...] BAGPATH
15
15
  bagit remove [-t <tagfile>...] BAGPATH
16
- bagit validate [-o] BAGPATH
16
+ bagit validate [-o] BAGPATH
17
17
  bagit manifest [-T] BAGPATH
18
- bagit list [--tags | --all] BAGPATH
18
+ bagit list [--tags | --all] BAGPATH
19
19
  bagit -h | --version
20
20
 
21
21
  Options:
22
+ -a A selected hashing algorithm. (md5, sha1, sha256)
22
23
  -h --help Show this help screen.
23
24
  --version Show version.
24
25
  -f <file> File to add to/delete from bag. Repeatable.
@@ -30,34 +31,36 @@ Options:
30
31
 
31
32
  DOCOPT
32
33
 
33
- # Possible commands for bag-info write
34
- #
35
- # bagit new [--source-organization <org>] [--organization-address <org-addr>] [--contact-name <contact>]
36
- # [--contact-phone <phone>] [--contact-email <email>] [--external-description <ext-desc>]
37
- # [--external-identifier <ext-id>] [--group-identifier <group-id>] [--count <count>]
38
- # [--internal-sender-identifier <sender-id>] [--internal-sender-description <sender-desc>]
39
- # [--bag-info-entry <label> <value>] [-f <file>...] [-t <tagfile>...] BAGPATH
34
+ # Possible commands for bag-info write
35
+ #
36
+ # bagit new [--source-organization <org>] [--organization-address <org-addr>] [--contact-name <contact>]
37
+ # [--contact-phone <phone>] [--contact-email <email>] [--external-description <ext-desc>]
38
+ # [--external-identifier <ext-id>] [--group-identifier <group-id>] [--count <count>]
39
+ # [--internal-sender-identifier <sender-id>] [--internal-sender-description <sender-desc>]
40
+ # [--bag-info-entry <label> <value>] [-f <file>...] [-t <tagfile>...] BAGPATH
40
41
 
41
42
  begin
42
- opts = Docopt::docopt(doc, version: BagIt::VERSION)
43
+ opts = Docopt.docopt(doc, version: BagIt::VERSION)
43
44
 
44
- unless opts['validate']
45
- bag = BagIt::Bag.new(opts['BAGPATH'])
46
- end
45
+ bag = BagIt::Bag.new(opts['BAGPATH']) unless opts['validate']
47
46
  #####################################
48
47
  # commands that don't alter the bag #
49
48
  #####################################
50
49
  if opts['validate']
51
- if File.exists?(opts['BAGPATH']+'/bag-info.txt') && File.directory?(opts['BAGPATH']+'/data') && File.exists?(opts['BAGPATH']+'/manifest-md5.txt') || File.exists?(opts['BAGPATH']+'/manifest-sha1.txt')
52
- bag = BagIt::Bag.new(opts['BAGPATH'])
53
- if opts['--oxum']
54
- logger.info(bag.valid_oxum?.to_s)
55
- else
56
- logger.info(bag.valid?.to_s)
57
- end
50
+ bag = BagIt::Bag.new(opts['BAGPATH'])
51
+
52
+ valid = if opts['--oxum']
53
+ bag.valid_oxum?
54
+ else
55
+ bag.valid?
56
+ end
57
+
58
+ if valid
59
+ logger.info(valid)
58
60
  else
59
- logger.error("Not a valid bag")
61
+ logger.error("#{valid}: #{bag.errors.full_messages.join(', ')}")
60
62
  end
63
+
61
64
  # validation commands MUST NOT change manifest or bag-info files
62
65
  exit
63
66
  end
@@ -74,59 +77,52 @@ begin
74
77
  exit
75
78
  end
76
79
 
77
-
78
80
  ######################################
79
81
  # commands that may cause data loss! #
80
82
  ######################################
81
83
 
82
- #TODO: implement delete for data and tag files; remove for tag files.
84
+ # TODO: implement delete for data and tag files; remove for tag files.
83
85
 
84
86
  # handle add/delete bag data files
85
- unless opts['-f'].nil?
86
- #TODO: add files in nested directories
87
- opts['-f'].each { |datafile|
88
- begin
89
- if opts['add'] or opts['new']
90
- bag.add_file(File.basename(datafile), datafile)
91
- elsif opts['delete']
92
- bag.remove_file(File.basename(datafile))
93
- end
94
- rescue Exception => e
95
- logger.error("Failed operation on bag file: #{e.message}")
96
- end
97
- }
87
+ opts['-f']&.each do |datafile|
88
+ if opts['add'] || opts['new']
89
+ bag.add_file(File.basename(datafile), datafile)
90
+ elsif opts['delete']
91
+ bag.remove_file(File.basename(datafile))
92
+ end
93
+ rescue StandardError => e
94
+ logger.error("Failed operation on bag file: #{e.message}")
98
95
  end
99
96
 
100
97
  # handle adding tag files
101
- unless opts['-t'].nil?
102
- #TODO: add files in nested directories
103
- opts['-t'].each { |tagfile|
104
- begin
105
- if opts['add'] or opts['new']
106
- # if it does, try to manifest it
107
- if File.exist?(File.join(bag.bag_dir, File.basename(tagfile)))
108
- bag.add_tag_file(tagfile)
109
- # otherwise, add it
110
- else
111
- bag.add_tag_file(File.basename(tagfile), tagfile)
112
- end
113
- elsif opts['delete']
114
- bag.delete_tag_file(File.basename(tagfile))
115
- elsif opts['remove']
116
- bag.remove_tag_file(File.basename(tagfile))
117
- end
118
- rescue Exception => e
119
- logger.error("Failed operation on tag file: #{e.message}".red)
98
+ opts['-t']&.each do |tagfile|
99
+ if opts['add'] || opts['new']
100
+ # if it does, try to manifest it
101
+ if File.exist?(File.join(bag.bag_dir, File.basename(tagfile)))
102
+ bag.add_tag_file(tagfile)
103
+ # otherwise, add it
104
+ else
105
+ bag.add_tag_file(File.basename(tagfile), tagfile)
120
106
  end
121
- }
107
+ elsif opts['delete']
108
+ bag.delete_tag_file(File.basename(tagfile))
109
+ elsif opts['remove']
110
+ bag.remove_tag_file(File.basename(tagfile))
111
+ end
112
+ rescue StandardError => e
113
+ logger.error("Failed operation on tag file: #{e.message}".red)
122
114
  end
123
115
 
124
116
  # if we haven't quit yet, we need to re-manifest
125
- # only do tags if tag files have been explictly added/removed or it is explictly called
126
- bag.tagmanifest! if opts['-T'] or not opts['-t'].nil?
127
- bag.manifest!
117
+ # only do tags if tag files have been explictly added/removed or it is explictly called
118
+ bag.tagmanifest! if opts['-T'] || !opts['-t'].nil?
119
+
120
+ if opts['-a'].nil?
121
+ bag.manifest!
122
+ else
123
+ bag.manifest!(algo: opts['<algo>'])
124
+ end
128
125
 
129
126
  rescue Docopt::Exit => e
130
127
  logger.error(e.message.red)
131
128
  end
132
-
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # == About bagit.rb
2
4
  # Author:: Francesco Lazzarino (mailto:flazzarino@gmail.com)
3
5
  # Functionality conforms to the BagIt Spec v0.96:
4
6
  # http://www.cdlib.org/inside/diglib/bagit/bagitspec.html
5
7
 
6
- require 'bagit/bag'
7
- require 'bagit/version'
8
- require 'fileutils'
9
- require 'date'
10
- require 'logger'
8
+ require "bagit/bag"
9
+ require "bagit/version"
10
+ require "fileutils"
11
+ require "date"
12
+ require "logger"
11
13
  module BagIt
12
14
  # The version of the BagIt specification the code is conforming to.
13
- SPEC_VERSION = '0.97'
15
+ SPEC_VERSION = "0.97"
14
16
  end
@@ -1,48 +1,43 @@
1
- require 'bagit/fetch'
2
- require 'bagit/file'
3
- require 'bagit/info'
4
- require 'bagit/manifest'
5
- require 'bagit/string'
6
- require 'bagit/valid'
1
+ # frozen_string_literal: true
2
+
3
+ require "bagit/fetch"
4
+ require "bagit/file"
5
+ require "bagit/info"
6
+ require "bagit/manifest"
7
+ require "bagit/string"
8
+ require "bagit/valid"
7
9
 
8
10
  module BagIt
9
11
  # Represents the state of a bag on a filesystem
10
12
  class Bag
11
13
  attr_reader :bag_dir
12
14
 
13
- include Validity # Validity functionality
14
- include Info # bagit & bag info functionality
15
- include Manifest # manifest related functionality
16
- include Fetch # fetch related functionality
15
+ include Validity # Validity functionality
16
+ include Info # bagit & bag info functionality
17
+ include Manifest # manifest related functionality
18
+ include Fetch # fetch related functionality
17
19
 
18
20
  # Make a new Bag based at path
19
- def initialize(path, info={}, create=false)
20
-
21
-
21
+ def initialize(path, info = {}, _create = false)
22
22
  @bag_dir = path
23
23
  # make the dir structure if it doesn't exist
24
- FileUtils::mkdir bag_dir unless File.directory? bag_dir
25
- FileUtils::mkdir data_dir unless File.directory? data_dir
24
+ FileUtils.mkdir bag_dir unless File.directory? bag_dir
25
+ FileUtils.mkdir data_dir unless File.directory? data_dir
26
26
 
27
27
  # write some tag info if its not there
28
- unless File.exist? bagit_txt_file
29
- write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8")
30
- end
28
+ write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8") unless File.exist? bagit_txt_file
31
29
 
32
- unless File.exist? bag_info_txt_file
33
- write_bag_info(info)
34
- end
35
-
30
+ write_bag_info(info) unless File.exist? bag_info_txt_file
36
31
  end
37
32
 
38
33
  # Return the path to the data directory
39
34
  def data_dir
40
- File.join @bag_dir, 'data'
35
+ File.join @bag_dir, "data"
41
36
  end
42
37
 
43
38
  # Return the paths to each bag file relative to bag_dir
44
39
  def bag_files
45
- Dir[File.join(data_dir, '**', '*')].select { |f| File.file? f }
40
+ Dir[File.join(data_dir, "**", "*")].select { |f| File.file? f }
46
41
  end
47
42
 
48
43
  # Return the paths to each tag file relative to bag_dir
@@ -50,65 +45,66 @@ module BagIt
50
45
  files = []
51
46
  if tagmanifest_files != []
52
47
  File.open(tagmanifest_files.first) do |f|
53
- f.each_line{|line| files << File.join(@bag_dir, line.split(' ')[1])}
48
+ f.each_line { |line| files << File.join(@bag_dir, line.split(" ")[1]) }
54
49
  end
55
50
  end
56
51
  files
57
52
  end
58
53
 
59
- # Add a bag file
60
- def add_file(base_path, src_path=nil)
61
- path = File.join(data_dir, base_path)
62
- raise "Bag file exists: #{base_path}" if File.exist? path
63
- FileUtils::mkdir_p File.dirname(path)
54
+ # Add a bag file at the given path relative to data_dir
55
+ def add_file(relative_path, src_path = nil)
56
+ path = File.join(data_dir, relative_path)
57
+ raise "Bag file exists: #{relative_path}" if File.exist? path
58
+ FileUtils.mkdir_p File.dirname(path)
64
59
 
65
- if src_path.nil?
66
- f = File.open(path, 'w') { |io| yield io }
60
+ f = if src_path.nil?
61
+ File.open(path, "w") { |io| yield io }
67
62
  else
68
- f = FileUtils::cp src_path, path
63
+ FileUtils.cp src_path, path
69
64
  end
70
65
  write_bag_info
71
- return f
66
+ f
72
67
  end
73
68
 
74
- # Remove a bag file
75
- def remove_file(base_path)
76
- path = File.join(data_dir, base_path)
77
- raise "Bag file does not exist: #{base_path}" unless File.exist? path
78
- FileUtils::rm path
69
+ # Remove a bag file at the given path relative to data_dir
70
+ def remove_file(relative_path)
71
+ path = File.join(data_dir, relative_path)
72
+ raise "Bag file does not exist: #{relative_path}" unless File.exist? path
73
+ FileUtils.rm path
79
74
  end
80
75
 
81
- # Retrieve the IO handle for a file in the bag
82
- def get(base_path)
83
- path = File.join(data_dir, base_path)
76
+ # Retrieve the IO handle for a file in the bag at a given path relative to
77
+ # data_dir
78
+ def get(relative_path)
79
+ path = File.join(data_dir, relative_path)
84
80
  return nil unless File.exist?(path)
85
81
  File.open(path)
86
82
  end
87
83
 
88
84
  # Test if this bag is empty (no files)
89
85
  def empty?
90
- self.bag_files.empty?
86
+ bag_files.empty?
91
87
  end
92
88
 
93
89
  # Get all bag file paths relative to the data dir
94
90
  def paths
95
- self.bag_files.collect { |f| f.sub(data_dir + '/', '') }
91
+ bag_files.collect { |f| f.sub(data_dir + "/", "") }
96
92
  end
97
93
 
98
94
  # Get the Oxum for the payload files
99
95
  def payload_oxum
100
96
  bytes = 0
101
97
  bag_files.each do |f|
102
- #TODO: filesystem quirks? Are we getting the stream size or the size on disk?
98
+ # TODO: filesystem quirks? Are we getting the stream size or the size on disk?
103
99
  bytes += File.size(f)
104
100
  end
105
- return bytes.to_s + '.' + bag_files.count.to_s
101
+ bytes.to_s + "." + bag_files.count.to_s
106
102
  end
107
103
 
108
104
  # Remove all empty directory trees from the bag
109
105
  def gc!
110
106
  Dir.entries(data_dir).each do |f|
111
- unless %w{.. .}.include? f
107
+ unless %w[.. .].include? f
112
108
  abs_path = File.join data_dir, f
113
109
  File.clean abs_path
114
110
  end