Skip to content

Commit c6be043

Browse files
committed
Initial rubocop formatting
This commit includes formatting changes needed when running `rubocop` with the `bixby` rules. The `.rubocop.yml` has additional changes to the default rules. It should get closer to the `bixby` rules over time. This also includes a change to the bag info file so that it points to the current GitHub repo and not the rubyforge address.
1 parent b8db2a8 commit c6be043

23 files changed

+680
-740
lines changed

.rubocop.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
inherit_gem:
2+
bixby: bixby_default.yml
3+
Rails/Delegate:
4+
Enabled: false
5+
Rails/Date:
6+
Enabled: false
7+
RSpec/InstanceVariable:
8+
Enabled: false
9+
Metrics/BlockLength:
10+
Enabled: false
11+
Metrics/ModuleLength:
12+
Enabled: false
13+
Metrics/MethodLength:
14+
Enabled: false
15+
RSpec/ExampleLength:
16+
Enabled: false
17+
Style/Semicolon:
18+
Enabled: false
19+
Style/ClassVars:
20+
Enabled: false
21+
Metrics/AbcSize:
22+
Enabled: false

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
rvm:
2-
- 2.3
3-
- 2.2
2+
- 2.5
43
branches:
54
only:
65
- master

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
source 'https://rubygems.org'
22

3-
gemspec
3+
gemspec

Rakefile

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ Bundler.setup(:default, :development, :test)
55
require 'rake'
66
require 'rdoc/task'
77
require 'rspec/core/rake_task'
8+
require 'rubocop/rake_task'
89

910
Bundler::GemHelper.install_tasks
1011

12+
desc 'Run rubocop'
13+
task :rubocop do
14+
RuboCop::RakeTask.new
15+
end
16+
1117
RSpec::Core::RakeTask.new do |t|
1218
t.pattern = 'spec/**/*_spec.rb'
13-
t.rspec_opts = %w(--format documentation --color)
19+
t.rspec_opts = %w[--format documentation --color]
1420
end
1521

16-
task :default => [:spec]
22+
task default: [:rubocop, :spec]

bagit.gemspec

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ Gem::Specification.new do |spec|
1818
spec.add_dependency 'validatable', '~> 1.6'
1919
spec.add_dependency 'docopt', '~> 0.5.0'
2020

21+
spec.add_development_dependency 'bixby'
2122
spec.add_development_dependency 'bundler'
22-
spec.add_development_dependency 'rake', '~> 10.4'
23-
spec.add_development_dependency 'rspec', '~> 3'
2423
spec.add_development_dependency 'coveralls'
2524
spec.add_development_dependency 'pry'
2625
spec.add_development_dependency 'pry-byebug'
26+
spec.add_development_dependency 'rake', '~> 10.4'
27+
spec.add_development_dependency 'rspec', '~> 3'
2728

28-
spec.files = `git ls-files`.split($/)
29+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
2930
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
3031
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
3132
spec.require_paths = ["lib"]

bin/bagit

+30-33
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,34 @@ Options:
3131
3232
DOCOPT
3333

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
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
4141

4242
begin
43-
opts = Docopt::docopt(doc, version: BagIt::VERSION)
43+
opts = Docopt.docopt(doc, version: BagIt::VERSION)
4444

45-
unless opts['validate']
46-
bag = BagIt::Bag.new(opts['BAGPATH'])
47-
end
45+
bag = BagIt::Bag.new(opts['BAGPATH']) unless opts['validate']
4846
#####################################
4947
# commands that don't alter the bag #
5048
#####################################
5149
if opts['validate']
5250
bag = BagIt::Bag.new(opts['BAGPATH'])
5351

54-
if opts['--oxum']
55-
valid = bag.valid_oxum?
56-
else
57-
valid = bag.valid?
58-
end
52+
valid = if opts['--oxum']
53+
bag.valid_oxum?
54+
else
55+
bag.valid?
56+
end
5957

6058
if valid
6159
logger.info(valid)
6260
else
63-
logger.error("#{valid}: #{bag.errors.full_messages.join(', ')}" )
61+
logger.error("#{valid}: #{bag.errors.full_messages.join(', ')}")
6462
end
6563

6664
# validation commands MUST NOT change manifest or bag-info files
@@ -79,35 +77,34 @@ begin
7977
exit
8078
end
8179

82-
8380
######################################
8481
# commands that may cause data loss! #
8582
######################################
8683

87-
#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.
8885

8986
# handle add/delete bag data files
9087
unless opts['-f'].nil?
91-
#TODO: add files in nested directories
92-
opts['-f'].each { |datafile|
88+
# TODO: add files in nested directories
89+
opts['-f'].each do |datafile|
9390
begin
94-
if opts['add'] or opts['new']
91+
if opts['add'] || opts['new']
9592
bag.add_file(File.basename(datafile), datafile)
9693
elsif opts['delete']
9794
bag.remove_file(File.basename(datafile))
9895
end
99-
rescue Exception => e
96+
rescue StandardError => e
10097
logger.error("Failed operation on bag file: #{e.message}")
10198
end
102-
}
99+
end
103100
end
104101

105102
# handle adding tag files
106103
unless opts['-t'].nil?
107-
#TODO: add files in nested directories
108-
opts['-t'].each { |tagfile|
104+
# TODO: add files in nested directories
105+
opts['-t'].each do |tagfile|
109106
begin
110-
if opts['add'] or opts['new']
107+
if opts['add'] || opts['new']
111108
# if it does, try to manifest it
112109
if File.exist?(File.join(bag.bag_dir, File.basename(tagfile)))
113110
bag.add_tag_file(tagfile)
@@ -120,20 +117,20 @@ begin
120117
elsif opts['remove']
121118
bag.remove_tag_file(File.basename(tagfile))
122119
end
123-
rescue Exception => e
120+
rescue StandardError => e
124121
logger.error("Failed operation on tag file: #{e.message}".red)
125122
end
126-
}
123+
end
127124
end
128125

129126
# if we haven't quit yet, we need to re-manifest
130127
# only do tags if tag files have been explictly added/removed or it is explictly called
131-
bag.tagmanifest! if opts['-T'] or not opts['-t'].nil?
128+
bag.tagmanifest! if opts['-T'] || !opts['-t'].nil?
132129

133-
unless opts['-a'].nil?
134-
bag.manifest!(algo: opts['<algo>'])
135-
else
130+
if opts['-a'].nil?
136131
bag.manifest!
132+
else
133+
bag.manifest!(algo: opts['<algo>'])
137134
end
138135

139136
rescue Docopt::Exit => e

lib/bagit.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
require 'logger'
1111
module BagIt
1212
# The version of the BagIt specification the code is conforming to.
13-
SPEC_VERSION = '0.97'
13+
SPEC_VERSION = '0.97'.freeze
1414
end

lib/bagit/bag.rb

+20-27
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,16 @@ class Bag
1616
include Fetch # fetch related functionality
1717

1818
# Make a new Bag based at path
19-
def initialize(path, info={}, create=false)
20-
21-
19+
def initialize(path, info = {}, _create = false)
2220
@bag_dir = path
2321
# 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
22+
FileUtils.mkdir bag_dir unless File.directory? bag_dir
23+
FileUtils.mkdir data_dir unless File.directory? data_dir
2624

2725
# 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
26+
write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8") unless File.exist? bagit_txt_file
3127

32-
unless File.exist? bag_info_txt_file
33-
write_bag_info(info)
34-
end
35-
28+
write_bag_info(info) unless File.exist? bag_info_txt_file
3629
end
3730

3831
# Return the path to the data directory
@@ -50,32 +43,32 @@ def tag_files
5043
files = []
5144
if tagmanifest_files != []
5245
File.open(tagmanifest_files.first) do |f|
53-
f.each_line{|line| files << File.join(@bag_dir, line.split(' ')[1])}
46+
f.each_line { |line| files << File.join(@bag_dir, line.split(' ')[1]) }
5447
end
5548
end
5649
files
5750
end
5851

5952
# Add a bag file at the given path relative to data_dir
60-
def add_file(relative_path, src_path=nil)
53+
def add_file(relative_path, src_path = nil)
6154
path = File.join(data_dir, relative_path)
6255
raise "Bag file exists: #{relative_path}" if File.exist? path
63-
FileUtils::mkdir_p File.dirname(path)
56+
FileUtils.mkdir_p File.dirname(path)
6457

65-
if src_path.nil?
66-
f = File.open(path, 'w') { |io| yield io }
67-
else
68-
f = FileUtils::cp src_path, path
69-
end
58+
f = if src_path.nil?
59+
File.open(path, 'w') { |io| yield io }
60+
else
61+
FileUtils.cp src_path, path
62+
end
7063
write_bag_info
71-
return f
64+
f
7265
end
7366

7467
# Remove a bag file at the given path relative to data_dir
7568
def remove_file(relative_path)
7669
path = File.join(data_dir, relative_path)
7770
raise "Bag file does not exist: #{relative_path}" unless File.exist? path
78-
FileUtils::rm path
71+
FileUtils.rm path
7972
end
8073

8174
# Retrieve the IO handle for a file in the bag at a given path relative to
@@ -88,28 +81,28 @@ def get(relative_path)
8881

8982
# Test if this bag is empty (no files)
9083
def empty?
91-
self.bag_files.empty?
84+
bag_files.empty?
9285
end
9386

9487
# Get all bag file paths relative to the data dir
9588
def paths
96-
self.bag_files.collect { |f| f.sub(data_dir + '/', '') }
89+
bag_files.collect { |f| f.sub(data_dir + '/', '') }
9790
end
9891

9992
# Get the Oxum for the payload files
10093
def payload_oxum
10194
bytes = 0
10295
bag_files.each do |f|
103-
#TODO: filesystem quirks? Are we getting the stream size or the size on disk?
96+
# TODO: filesystem quirks? Are we getting the stream size or the size on disk?
10497
bytes += File.size(f)
10598
end
106-
return bytes.to_s + '.' + bag_files.count.to_s
99+
bytes.to_s + '.' + bag_files.count.to_s
107100
end
108101

109102
# Remove all empty directory trees from the bag
110103
def gc!
111104
Dir.entries(data_dir).each do |f|
112-
unless %w{.. .}.include? f
105+
unless %w[.. .].include? f
113106
abs_path = File.join data_dir, f
114107
File.clean abs_path
115108
end

lib/bagit/fetch.rb

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
require 'open-uri'
22

33
module BagIt
4-
54
module Fetch
6-
75
def fetch_txt_file
86
File.join @bag_dir, 'fetch.txt'
97
end
@@ -16,35 +14,31 @@ def add_remote_file(url, path, size, sha1, md5)
1614

1715
# feth all remote files
1816
def fetch!
19-
2017
open(fetch_txt_file) do |io|
21-
2218
io.readlines.each do |line|
23-
24-
(url, length, path) = line.chomp.split(/\s+/, 3)
25-
26-
add_file(path) do |io|
27-
io.write open(url)
19+
(url, _length, path) = line.chomp.split(/\s+/, 3)
20+
21+
add_file(path) do |file_io|
22+
file_io.write open(url)
2823
end
29-
3024
end
31-
3225
end
3326

34-
# rename the old fetch.txt
27+
rename_old_fetch_txt(fetch_txt_file)
28+
move_current_fetch_txt(fetch_txt_file)
29+
end
30+
31+
def rename_old_fetch_txt(fetch_txt_file)
3532
Dir["#{fetch_txt_file}.?*"].sort.reverse.each do |f|
36-
3733
if f =~ /fetch.txt.(\d+)$/
38-
new_f = File.join File.dirname(f), "fetch.txt.#{$1.to_i + 1}"
39-
FileUtils::mv f, new_f
34+
new_f = File.join File.dirname(f), "fetch.txt.#{Regexp.last_match(1).to_i + 1}"
35+
FileUtils.mv f, new_f
4036
end
41-
4237
end
38+
end
4339

44-
# move the current fetch_txt
45-
FileUtils::mv fetch_txt_file, "#{fetch_txt_file}.0"
40+
def move_current_fetch_txt(fetch_txt_file)
41+
FileUtils.mv fetch_txt_file, "#{fetch_txt_file}.0"
4642
end
47-
4843
end
49-
5044
end

0 commit comments

Comments
 (0)