bwrap 1.1.0 → 1.1.1

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
  SHA256:
3
- metadata.gz: 8e72a18d3f00660f02729fc5bad5c5f10e006fe3021a55d3128f7acd1ab0aace
4
- data.tar.gz: b916e9b3e5fc6cf44adb515e3f52b174549fdd12f4fe275badab1d95ab78bb90
3
+ metadata.gz: 352b23610ac14344695cc17c4bcdaeaf7307b3742983f520581251b4bb7f85a5
4
+ data.tar.gz: e4cfa7fb8ca749e5dfddf11f6eb030fd82af1bd2a15dad5062d9ba4fd9be72fb
5
5
  SHA512:
6
- metadata.gz: 9b2c7793ae6321bbe2ed4ce81883bfb901d5b2683adf5bc4bea66b25318e5219992851c41bbd33c10bafb67cbae826d4fa5ac153901c565e7b746e672af41a6c
7
- data.tar.gz: 7e4cea7fd04e77ad8a5dd97d8279ffc39aa3ef094e7f30c35c11e2287f753d951f015644a1bfcb3205bbd592e5d27d7767b267fcb4d66aa9d80f2fdcee6686a7
6
+ metadata.gz: cb7feb42474faa52ab6cce4cafd66daabf20f8490519a0f950885b1347332d38a6c335de40d6db4c7371e9eb0a0a722352d6e4613db3e3df193688ae896c584e
7
+ data.tar.gz: 90892a26e8efddc5112c4fa22bd1b95e8380f1860df1680031549f0addf485b4229fcdd11d786a63d476bf151483db60eba7682d045d35b08dfccea88e6b5f44
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changes
2
2
 
3
+ ## 1.1.1 (07.06.2022)
4
+
5
+ * Added Bwrap::Execution.popen2e
6
+ * Fixed compatibility with ruby-2.5
7
+
3
8
  ## 1.1.0 (01.06.2022)
4
9
 
5
10
  * No changes.
@@ -40,6 +40,13 @@ class Bwrap::Execution::Execute
40
40
 
41
41
  # @return formatted command.
42
42
  def self.format_command command, rootcmd:
43
+ # Flatten the command if required, so nils can be converted in more of cases.
44
+ # Flattenization is also done in executions, but they also take in account
45
+ # for example rootcmd, so they probably should be done in addition to this one.
46
+ if command.respond_to? :flatten!
47
+ command.flatten!
48
+ end
49
+
43
50
  replace_nils command
44
51
  return command if rootcmd.nil?
45
52
 
@@ -5,6 +5,7 @@ require "bwrap/output"
5
5
  require_relative "exceptions"
6
6
  require_relative "execute"
7
7
  require_relative "path"
8
+ require_relative "popen2e"
8
9
 
9
10
  # Provides methods to execute commands and handle its output.
10
11
  #
@@ -78,9 +79,27 @@ module Bwrap::Execution
78
79
  end
79
80
 
80
81
  # Works similarly to {Open3.popen2e}.
81
- def self.popen2e *cmd, *opts, &block
82
-
82
+ #
83
+ # TODO: If there will be any difference to input syntax, document those differences here.
84
+ # For now, rootcmd option has been implemented.
85
+ #
86
+ # A block is accepted, as does {Open3.popen2e}. For now, at least.
87
+ #
88
+ # TODO: Implement this so that this uses same execution things as other things here.
89
+ # This way bwrap actually can be integrated to this...
90
+ #
91
+ # TODO: Verify default log_callback is correct
92
+ #
93
+ # @warning Only array style commands are accepted. For example, `ls /`
94
+ # is not ok, but `ls` or `%w{ ls / }` is ok.
95
+ def popen2e *cmd, rootcmd: nil, log_callback: 1, log: true, &block
96
+ popen = Bwrap::Execution::Popen2e.new
97
+ popen.dry_run = @dry_run
98
+ popen.popen2e(*cmd, rootcmd: rootcmd, log_callback: log_callback, log: log, &block)
99
+ ensure
100
+ @last_status = $CHILD_STATUS
83
101
  end
102
+ module_function :popen2e
84
103
 
85
104
  # Returns Process::Status instance of last execution.
86
105
  #
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @see {Bwrap::Execution.popen2e}
4
+ class Bwrap::Execution::Popen2e
5
+ attr_writer :dry_run
6
+
7
+ # @see {Bwrap::Execution.popen2e}
8
+ # TODO: Add a test for this (does Ruby have anything I could use here?).
9
+ #
10
+ # @note Also options accepted by {Open3.popen2e} are accepted
11
+ # here, in addition to those specified here.
12
+ def popen2e *cmd, rootcmd: nil, log_callback: 1, log: true, &block
13
+ @rootcmd = rootcmd
14
+ @log_callback = log_callback + 1 # Passing to another method, so need to add one more.
15
+ @log = log
16
+ self.opts_from_input = cmd
17
+
18
+ resolve_actual_command cmd
19
+
20
+ return if @dry_run || Bwrap::Execution::Execute.dry_run
21
+
22
+ open_pipes
23
+ popen_run(@actual_command, [ @in_read, @out_write ], [ @in_write, @out_read ], &block)
24
+ ensure
25
+ if block
26
+ @in_read.close
27
+ @in_write.close
28
+ @out_read.close
29
+ @out_write.close
30
+ end
31
+ end
32
+
33
+ # Sets @opts from `cmd` given to {#popen2e}, if any extra options have been given.
34
+ private def opts_from_input= cmd
35
+ @opts = cmd.last.is_a?(Hash) && cmd.pop.dup || {}
36
+ end
37
+
38
+ private def open_pipes
39
+ @in_read, @in_write = IO.pipe
40
+ @opts[:in] = @in_read
41
+ @in_write.sync = true
42
+
43
+ @out_read, @out_write = IO.pipe
44
+ @opts[[ :out, :err ]] = @out_write
45
+ end
46
+
47
+ # First element may be optional environment variables.
48
+ private def resolve_actual_command cmd
49
+ temp_cmd = cmd.dup
50
+
51
+ # Delete environment hash.
52
+ env_hash = temp_cmd.shift if temp_cmd.first.is_a? Hash
53
+
54
+ # Delete option hash.
55
+ option_hash = temp_cmd.pop if temp_cmd.last.is_a? Hash
56
+
57
+ temp_cmd = Bwrap::Execution::Execute.format_command temp_cmd, rootcmd: @rootcmd
58
+ Bwrap::Execution::Execute.handle_logging temp_cmd, log_callback: @log_callback, log: @log, dry_run: @dry_run
59
+
60
+ temp_cmd.unshift env_hash if env_hash
61
+ temp_cmd.push option_hash if option_hash
62
+
63
+ # If command is an array, there can’t be arrays inside the array (hashes are preserved).
64
+ # For convenience, the array is flattened here, so callers can construct commands more easily.
65
+ temp_cmd.flatten!
66
+
67
+ @actual_command = temp_cmd
68
+ end
69
+
70
+ private def popen_run cmd, child_io, parent_io
71
+ pid = spawn(*cmd, @opts)
72
+ wait_thr = Process.detach(pid)
73
+ child_io.each(&:close)
74
+ result = [ *parent_io, wait_thr ]
75
+
76
+ if defined? yield
77
+ begin
78
+ return yield(*result)
79
+ ensure
80
+ parent_io.each(&:close)
81
+ wait_thr.join
82
+ end
83
+ end
84
+
85
+ result
86
+ end
87
+ end
data/lib/bwrap/version.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Bwrap
4
4
  # Current version of bwrap.
5
- VERSION = "1.1.0"
5
+ VERSION = "1.1.1"
6
6
  end
7
7
 
8
8
  require "deep-cover" if ENV["DEEP_COVER"]
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bwrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samu Voutilainen
@@ -34,7 +34,7 @@ cert_chain:
34
34
  X4ioQwEn1/9tHs19VO1CLF58451HgEo1BXd7eWLmV1V5cqw0YWok1ly4L/Su/Phf
35
35
  MRxVMHiVAqY=
36
36
  -----END CERTIFICATE-----
37
- date: 2022-06-01 00:00:00.000000000 Z
37
+ date: 2022-06-07 00:00:00.000000000 Z
38
38
  dependencies:
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: bundler
@@ -172,6 +172,7 @@ files:
172
172
  - lib/bwrap/execution/execution.rb
173
173
  - lib/bwrap/execution/labels.rb
174
174
  - lib/bwrap/execution/path.rb
175
+ - lib/bwrap/execution/popen2e.rb
175
176
  - lib/bwrap/output.rb
176
177
  - lib/bwrap/output/colors.rb
177
178
  - lib/bwrap/output/levels.rb
metadata.gz.sig CHANGED
Binary file