gubbins-shell 0.1.0

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +19 -0
  3. data/LICENSE +289 -0
  4. data/README.md +186 -0
  5. data/lib/gubbins/shell.rb +4 -0
  6. data/lib/shell/all.rb +34 -0
  7. data/lib/shell/base_64.rb +40 -0
  8. data/lib/shell/clam_av.rb +68 -0
  9. data/lib/shell/command.rb +1400 -0
  10. data/lib/shell/convert.rb +312 -0
  11. data/lib/shell/errors.rb +89 -0
  12. data/lib/shell/exiftool.rb +15 -0
  13. data/lib/shell/file.rb +84 -0
  14. data/lib/shell/ghostscript.rb +239 -0
  15. data/lib/shell/identify.rb +111 -0
  16. data/lib/shell/img2_pdf.rb +47 -0
  17. data/lib/shell/magick.rb +111 -0
  18. data/lib/shell/open_ssl.rb +26 -0
  19. data/lib/shell/optional_dependencies.rb +36 -0
  20. data/lib/shell/path.rb +40 -0
  21. data/lib/shell/pdf_detach.rb +86 -0
  22. data/lib/shell/pdffonts.rb +73 -0
  23. data/lib/shell/pdfimages.rb +48 -0
  24. data/lib/shell/pdfinfo.rb +79 -0
  25. data/lib/shell/pdftocairo.rb +24 -0
  26. data/lib/shell/pdftotext.rb +32 -0
  27. data/lib/shell/ping.rb +50 -0
  28. data/lib/shell/power_array.rb +42 -0
  29. data/lib/shell/seven_zip.rb +142 -0
  30. data/lib/shell/tesseract.rb +120 -0
  31. data/lib/shell/testing/mock/shell/clam_av.rb +12 -0
  32. data/lib/shell/testing/mock/shell/command.rb +241 -0
  33. data/lib/shell/testing/mock/shell/configurator.rb +223 -0
  34. data/lib/shell/testing/mock/shell/convert.rb +12 -0
  35. data/lib/shell/testing/mock/shell/file.rb +12 -0
  36. data/lib/shell/testing/mock/shell/ghostscript.rb +13 -0
  37. data/lib/shell/testing/mock/shell/identify.rb +11 -0
  38. data/lib/shell/testing/mock/shell/open_ssl.rb +11 -0
  39. data/lib/shell/testing/mock/shell/tesseract.rb +12 -0
  40. data/lib/shell/testing/mock/shell/unzip.rb +20 -0
  41. data/lib/shell/testing/mock/shell/zip.rb +11 -0
  42. data/lib/shell/testing.rb +15 -0
  43. data/lib/shell/tiff_2_pdf.rb +19 -0
  44. data/lib/shell/unzip.rb +137 -0
  45. data/lib/shell/version.rb +5 -0
  46. data/lib/shell/zbar.rb +123 -0
  47. data/lib/shell/zip.rb +52 -0
  48. data/lib/shell.rb +16 -0
  49. metadata +116 -0
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ # Interface for `unzip`
7
+ class Unzip < Command
8
+ command_path `which unzip`
9
+ option_prefix "-"
10
+ positional_parameters :first
11
+
12
+ # Extracts archive at `input` to a temporary directory
13
+ #
14
+ # When called with a block, the temporary directory is
15
+ # automatically removed after the block returns; your block
16
+ # receives a {Pathname} to the directory.
17
+ #
18
+ # @example
19
+ # ```ruby
20
+ # Shell::Unzip.to_temp("/some/file.zip") do |path|
21
+ # puts path.children
22
+ # end
23
+ # ```
24
+ #
25
+ # When called without a block, the {Pathname} is returned in
26
+ # {Result#value}.
27
+ #
28
+ # ```ruby
29
+ # result = Shell::Unzip.to_temp("/some/file.zip")
30
+ # puts result.value.children
31
+ # ```
32
+ #
33
+ # @param input [Pathname, Fs::File, String]
34
+ # @yieldparam pathname [Pathname] the {Pathname} for the temp dir
35
+ # @return [Shell::Command::Result]
36
+ # `Result#value` [Pathname] only when called without a block
37
+ def self.to_temp(input, &block)
38
+ new.to_temp(input, &block)
39
+ end
40
+
41
+ # Extracts archive at `input` to `output` directory
42
+ #
43
+ # @param input [Pathname, Fs::File, String]
44
+ # @param output [Pathname, Fs::File, String]
45
+ # @return [Shell::Command::Result]
46
+ def self.to(input:, output:)
47
+ new.to(input: input, output: output)
48
+ end
49
+
50
+ # Returns the files contained in the archive as {Pathname} objects
51
+ #
52
+ # @param input [Pathname, Fs::File, String]
53
+ # @return [Shell::Command::Result]
54
+ # `Result#value` [Array<Pathname>]
55
+ def self.list(input)
56
+ new.list(input)
57
+ end
58
+
59
+ # Is the uncompressed size of the archive below `megabytes`?
60
+ #
61
+ # @param input [Pathname, Fs::File, String]
62
+ # @param megabytes [Integer, Float]
63
+ # @return [Shell::Command::Result]
64
+ # `Result#value` [Boolean]
65
+ def self.uncompressed_size_below?(input, megabytes:)
66
+ new.uncompressed_size_below?(input, megabytes: megabytes)
67
+ end
68
+
69
+ # Does this archive test error-free?
70
+ #
71
+ # @param input [Pathname, Fs::File, String]
72
+ # @return [Shell::Command::Result]
73
+ # `Result#value` [Boolean]
74
+ def self.valid?(input)
75
+ new.valid?(input)
76
+ end
77
+
78
+ # @see {self.to_temp}
79
+ def to_temp(input)
80
+ result = nil
81
+
82
+ if block_given?
83
+ Dir.mktmpdir do |tempdir_path|
84
+ result =
85
+ exec(input, d: tempdir_path).on_success do
86
+ yield Pathname.new(tempdir_path)
87
+ end
88
+ end
89
+
90
+ else
91
+ tempdir_path = Dir.mktmpdir
92
+ result = exec(input, d: tempdir_path)
93
+ result.value = Pathname.new(tempdir_path)
94
+ end
95
+
96
+ result
97
+ end
98
+
99
+ # @see {self.to}
100
+ def to(input:, output:)
101
+ exec(input, d: output)
102
+ end
103
+
104
+ # @see {self.list}
105
+ def list(input)
106
+ exec(l: input).on_success do |result|
107
+ result.value =
108
+ result.stdout
109
+ .lines[3...-2]
110
+ .map { |line| Pathname.new(line.split.last) }
111
+ end
112
+ end
113
+
114
+ # @see {self.uncompressed_size_below?}
115
+ def uncompressed_size_below?(input, megabytes:)
116
+ exec(l: input).on_success do |result|
117
+ uncompressed_size =
118
+ result.stdout
119
+ .lines
120
+ .last
121
+ .split
122
+ .first
123
+ .to_i
124
+
125
+ result.value =
126
+ (megabytes * 1_000_000) > uncompressed_size
127
+ end
128
+ end
129
+
130
+ # @see {self.valid?}
131
+ def valid?(input)
132
+ exec(:qq, t: input).tap do |result|
133
+ result.value = result.success?
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shell
4
+ VERSION = "0.1.0"
5
+ end
data/lib/shell/zbar.rb ADDED
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/object/blank"
5
+ require "active_support/core_ext/object/inclusion"
6
+ require "active_support/core_ext/hash/conversions"
7
+ require "active_support/core_ext/hash/indifferent_access"
8
+ require_relative "../shell"
9
+
10
+ module Shell
11
+ class Zbar < Command
12
+ command_path `which zbarimg`
13
+ transform_options quiet: :q
14
+
15
+ # Scan for barcodes in `input:` image file
16
+ #
17
+ # @param input [Pathname, String, Fs::File]
18
+ # @return [Shell::Command::Result]
19
+ # `Result#value` [Array<Hash{Symbol=>String}>]; if the output cannot
20
+ # be parsed, a failed Result with the exception as its `#value`
21
+ def self.scan(input:)
22
+ # `Hash.from_xml` parses with REXML, which has not been a default gem since Ruby 3.0
23
+ require_optional "rexml/document", gem_name: "rexml"
24
+
25
+ exec(input, :quiet, :xml).consider_successful(4)
26
+ .post_process do |result|
27
+ result.value =
28
+ if result.status.zero? && result.stdout
29
+ Hash.from_xml(result.stdout)
30
+ .with_indifferent_access
31
+ .dig(:barcodes, :source, :index, :symbol)
32
+ .then do |symbol|
33
+ case symbol
34
+ when nil
35
+ nil
36
+ when Array
37
+ symbol
38
+ when Hash
39
+ [symbol]
40
+ end
41
+ end
42
+ else
43
+ nil
44
+ end
45
+ end
46
+ end
47
+
48
+ # Extract EPC SCT QR code data from `input:` image file
49
+ #
50
+ # The European Payments Council SEPA Credit Transfer QR code
51
+ # allows transaction data to be embedded in a machine-readable
52
+ # way in printed invoices or money orders, so consumers can pay
53
+ # via bank transfer without having to type in the transfer
54
+ # details.
55
+ #
56
+ # @param input [Pathname, String, Fs::File] an image file;
57
+ # supported formats other than JPG are currently unknown
58
+ # @return [Shell::Command::Result]
59
+ # `Result#value` [Array<Hash{Symbol=>String}>], with `nil` for fields the code leaves
60
+ # empty; the IBAN is returned as written in the code, without validating it. If a code
61
+ # is malformed, a failed Result with the exception as its `#value`
62
+ def self.extract_sepa_credit_transfers(input:)
63
+ scan(input: input).post_process do |result|
64
+ result.value ||= []
65
+
66
+ result.value =
67
+ result.value
68
+ .map do |hash|
69
+ next nil unless hash[:type] == "QR-Code"
70
+
71
+ lines =
72
+ hash[:data].lines.map(&:chomp)
73
+
74
+ next nil unless lines.shift == "BCD"
75
+
76
+ version, character_set, identification, bic, name, iban,
77
+ amount, purpose, remittance_reference, remittance_text, notice = *lines
78
+
79
+ # Version differences are minor; in v2, the BIC field becomes optional,
80
+ # unless the transaction involves a non-EEA country
81
+ next nil unless version.in? %w(001 002)
82
+ next nil unless identification == "SCT"
83
+
84
+ encoding =
85
+ case character_set
86
+ when "1"
87
+ Encoding::UTF_8
88
+ when "2"
89
+ Encoding::ISO_8859_1
90
+ when "3"
91
+ Encoding::ISO_8859_2
92
+ when "4"
93
+ Encoding::ISO_8859_4
94
+ when "5"
95
+ Encoding::ISO_8859_5
96
+ when "6"
97
+ Encoding::ISO_8859_7
98
+ when "7"
99
+ Encoding::ISO_8859_10
100
+ when "8"
101
+ Encoding::ISO_8859_15
102
+ else
103
+ next nil
104
+ end
105
+
106
+ {
107
+ version: version.to_i,
108
+ encoding: encoding,
109
+ bic: bic.presence,
110
+ name: name.presence,
111
+ iban: iban.presence,
112
+ amount: amount.presence,
113
+ purpose: purpose.presence,
114
+ remittance_reference: remittance_reference.presence,
115
+ remittance_text: remittance_text.presence,
116
+ notice: notice.presence
117
+ }
118
+ end
119
+ .compact
120
+ end
121
+ end
122
+ end
123
+ end
data/lib/shell/zip.rb ADDED
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ # Interface for `zip`
7
+ class Zip < Command
8
+ command_path `which zip`
9
+ transform_options recursive: :r,
10
+ flat_files_without_directories: :j
11
+
12
+ # Zip directory at `directory_path` into zip file `:to`
13
+ #
14
+ # @param directory_path [Pathname, String, Fs::File]
15
+ # @param to [Pathname, String, Fs::File]
16
+ # @param relative_to [Pathname, String, Fs::File, Symbol] store
17
+ # subdirectories and files relative to this path instead of the
18
+ # current working directory. Set to `:target` to use `directory_path`
19
+ # @return [Shell::Command::Result]
20
+ def self.from_directory(directory_path, *args, to:, relative_to: nil, **kargs)
21
+ relative_to =
22
+ directory_path if relative_to == :target
23
+
24
+ if relative_to
25
+ # Because we're changing directory, we need to absolutize
26
+ # the path to the zip file in case it isn't already -
27
+ # otherwise we would store the zip in the `relative_to`
28
+ # directory
29
+ to =
30
+ Pathname.new(to.respond_to?(:to_path) ? to.to_path.to_s : to).expand_path
31
+
32
+ relative_to =
33
+ relative_to.respond_to?(:to_path) ? relative_to.to_path.to_s : relative_to.to_s
34
+
35
+ directory_path =
36
+ directory_path.respond_to?(:to_path) ? directory_path.to_path.to_s : directory_path.to_s
37
+
38
+ # `zip` creates relative paths relative to the current
39
+ # working directory IF the given target path is relative
40
+ directory_path =
41
+ Pathname.new(directory_path)
42
+ .expand_path
43
+ .relative_path_from(relative_to)
44
+
45
+ # Not `Dir.chdir`: it is process-wide and raises when another thread is inside one
46
+ exec(to, directory_path, :recursive, *args, chdir: relative_to, **kargs)
47
+ else
48
+ exec(to, directory_path, :recursive, *args, **kargs)
49
+ end
50
+ end
51
+ end
52
+ end
data/lib/shell.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "shell/version"
4
+ require_relative "shell/errors"
5
+ require_relative "shell/command"
6
+
7
+ # Core Shell::Command class and error definitions
8
+ #
9
+ # Concrete command wrappers are not loaded by default.
10
+ # Require them individually or all at once:
11
+ #
12
+ # require 'shell/ping' # Individual wrapper
13
+ # require 'shell/all' # All wrappers
14
+ #
15
+ module Shell
16
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gubbins-shell
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan-Christian Foeh
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-11-18 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activesupport
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '7.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9'
32
+ description: 'Shell::Command is a base class for wrapping command line programs: it
33
+ escapes arguments, builds options from keyword arguments, enforces timeouts, makes
34
+ sure no child process outlives its caller and returns a Result. Wrappers for common
35
+ programs (ImageMagick, Ghostscript, Poppler, Tesseract, zip and others) and RSpec
36
+ mocks for them are included, and loaded only on request.'
37
+ email:
38
+ - jan@programmanstalt.de
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - CHANGELOG.md
44
+ - LICENSE
45
+ - README.md
46
+ - lib/gubbins/shell.rb
47
+ - lib/shell.rb
48
+ - lib/shell/all.rb
49
+ - lib/shell/base_64.rb
50
+ - lib/shell/clam_av.rb
51
+ - lib/shell/command.rb
52
+ - lib/shell/convert.rb
53
+ - lib/shell/errors.rb
54
+ - lib/shell/exiftool.rb
55
+ - lib/shell/file.rb
56
+ - lib/shell/ghostscript.rb
57
+ - lib/shell/identify.rb
58
+ - lib/shell/img2_pdf.rb
59
+ - lib/shell/magick.rb
60
+ - lib/shell/open_ssl.rb
61
+ - lib/shell/optional_dependencies.rb
62
+ - lib/shell/path.rb
63
+ - lib/shell/pdf_detach.rb
64
+ - lib/shell/pdffonts.rb
65
+ - lib/shell/pdfimages.rb
66
+ - lib/shell/pdfinfo.rb
67
+ - lib/shell/pdftocairo.rb
68
+ - lib/shell/pdftotext.rb
69
+ - lib/shell/ping.rb
70
+ - lib/shell/power_array.rb
71
+ - lib/shell/seven_zip.rb
72
+ - lib/shell/tesseract.rb
73
+ - lib/shell/testing.rb
74
+ - lib/shell/testing/mock/shell/clam_av.rb
75
+ - lib/shell/testing/mock/shell/command.rb
76
+ - lib/shell/testing/mock/shell/configurator.rb
77
+ - lib/shell/testing/mock/shell/convert.rb
78
+ - lib/shell/testing/mock/shell/file.rb
79
+ - lib/shell/testing/mock/shell/ghostscript.rb
80
+ - lib/shell/testing/mock/shell/identify.rb
81
+ - lib/shell/testing/mock/shell/open_ssl.rb
82
+ - lib/shell/testing/mock/shell/tesseract.rb
83
+ - lib/shell/testing/mock/shell/unzip.rb
84
+ - lib/shell/testing/mock/shell/zip.rb
85
+ - lib/shell/tiff_2_pdf.rb
86
+ - lib/shell/unzip.rb
87
+ - lib/shell/version.rb
88
+ - lib/shell/zbar.rb
89
+ - lib/shell/zip.rb
90
+ homepage: https://codeberg.org/janfoeh/gubbins-shell
91
+ licenses:
92
+ - EUPL-1.2
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ rubygems_mfa_required: 'true'
96
+ source_code_uri: https://codeberg.org/janfoeh/gubbins-shell
97
+ changelog_uri: https://codeberg.org/janfoeh/gubbins-shell/src/branch/master/CHANGELOG.md
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 3.2.0
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.6.2
113
+ specification_version: 4
114
+ summary: Ruby wrappers for command line programs, with a small DSL for writing your
115
+ own.
116
+ test_files: []