bwrap 1.0.0.pre.alpha2 → 1.0.0.pre.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,7 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
3
  # force_encoding modifies string, so can’t freeze strings.
4
4
 
5
- require_relative "output"
6
-
7
5
  # Logging methods.
8
6
  class Bwrap::Output::Log
9
7
  @@log_file = nil
@@ -15,12 +13,12 @@ class Bwrap::Output::Log
15
13
 
16
14
  # Writes given string to log.
17
15
  def self.write_to_log str
18
- @@log_file&.write str.force_encoding("UTF-8")
16
+ @@log_file&.write str.dup.force_encoding("UTF-8")
19
17
  end
20
18
 
21
19
  # Writes given string to log.
22
20
  def self.puts_to_log str
23
- @@log_file&.puts str.force_encoding("UTF-8")
21
+ @@log_file&.puts str.dup.force_encoding("UTF-8")
24
22
  end
25
23
 
26
24
  # Closes an open log file.
@@ -40,7 +38,7 @@ class Bwrap::Output::Log
40
38
  @@log_file = log_file
41
39
 
42
40
  at_exit do
43
- ::Bwrap::Output::Log.close_log_file
41
+ Bwrap::Output::Log.close_log_file
44
42
  end
45
43
  end
46
44
  end
@@ -0,0 +1,181 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Have variables like $CHILD_STATUS which is alias of $?.
4
+ require "English"
5
+
6
+ require "bwrap/execution/labels"
7
+
8
+ require_relative "levels"
9
+ require_relative "log"
10
+
11
+ # Methods useful for output handling.
12
+ #
13
+ # There is four different log levels:
14
+ #
15
+ # @!description_list
16
+ # @term error
17
+ # @description
18
+ # Causes execution to halt either to exit with requested code or,
19
+ # if requested, raises an exception.
20
+ # @term warning
21
+ # @description Outputs given text to STDERR.
22
+ # @term verbose
23
+ # @description Outputs given text if verbose, debug or trace flag is set.
24
+ # @term debug
25
+ # @description Outputs given text if debug or trace flag is set.
26
+ # @term trace
27
+ # @description Outputs given text if trace flag is set.
28
+ #
29
+ # Output levels can be enabled with {.handle_output_options}.
30
+ #
31
+ # When using {Bwrap::Bwrap}, {Bwrap::Bwrap#parse_command_line_arguments}
32
+ # causes output levels to be set if relevant CLI arguments have been
33
+ # given. TODO: Add documentation about CLI args somewhere. Maybe README?
34
+ module Bwrap::Output
35
+ # @see #verbose?
36
+ def self.verbose?
37
+ Bwrap::Output::Levels.verbose?
38
+ end
39
+
40
+ # @see #debug?
41
+ def self.debug?
42
+ Bwrap::Output::Levels.debug?
43
+ end
44
+
45
+ # @see #trace?
46
+ def self.trace?
47
+ Bwrap::Output::Levels.trace?
48
+ end
49
+
50
+ # Takes hash of options received from Optimist and checks output related flags.
51
+ def self.handle_output_options options
52
+ Bwrap::Output::Levels.handle_output_options options
53
+ end
54
+
55
+ # Handler used by #trace to output given string.
56
+ def self.trace_output str, raw: false, log_callback: 1
57
+ return unless trace?
58
+
59
+ if raw
60
+ print str
61
+ else
62
+ out = Bwrap::Output::Levels.trace_print_formatted str, log_callback: (log_callback + 1)
63
+ end
64
+ Bwrap::Output::Log.puts_to_log out || str
65
+ end
66
+
67
+ # Handler used by #debug to output given string.
68
+ def self.debug_output str, raw: false, log_callback: 1
69
+ return unless debug?
70
+
71
+ if raw
72
+ print str
73
+ else
74
+ out = Bwrap::Output::Levels.debug_print_formatted str, log_callback: (log_callback + 1)
75
+ end
76
+ Bwrap::Output::Log.puts_to_log out || str
77
+ end
78
+
79
+ # Handler used by #verb to output given string.
80
+ def self.verb_output str, raw: false, log_callback: 1
81
+ return unless verbose?
82
+
83
+ if raw
84
+ print str
85
+ else
86
+ out = Bwrap::Output::Levels.verbose_print_formatted str, log_callback: (log_callback + 1)
87
+ end
88
+ Bwrap::Output::Log.puts_to_log out || str
89
+ end
90
+
91
+ # Handler used by #warn to output given string.
92
+ def self.warn_output str, raw: false, log_callback: 1
93
+ if raw
94
+ print str
95
+ else
96
+ out = Bwrap::Output::Levels.warning_print_formatted str, log_callback: (log_callback + 1)
97
+ end
98
+ Bwrap::Output::Log.puts_to_log out || str
99
+ end
100
+
101
+ # Aborts current process.
102
+ #
103
+ # Use this instead of Ruby’s #exit in order to filter out dummy #exit calls from the code.
104
+ def self.error_output str = nil, label: :unspecified, log_callback: 1, raise_exception: false
105
+ unless str.nil?
106
+ out = Bwrap::Output::Levels.error_print_formatted str, log_callback: (log_callback + 1)
107
+ Bwrap::Output::Log.puts_to_log out
108
+ end
109
+
110
+ exit_code = Bwrap::Execution::Labels.resolve_exit_code(label)
111
+ raise str if raise_exception
112
+
113
+ exit exit_code
114
+ end
115
+
116
+ # @return true if --verbose, --debug or --trace has been passed, false if not.
117
+ private def verbose?
118
+ Bwrap::Output::Levels.verbose?
119
+ end
120
+
121
+ # @return true if --debug or --trace has been passed, false if not.
122
+ private def debug?
123
+ Bwrap::Output::Levels.debug?
124
+ end
125
+
126
+ # @return true if --trace has been passed, false if not.
127
+ private def trace?
128
+ Bwrap::Output::Levels.trace?
129
+ end
130
+
131
+ # @!group Outputters
132
+
133
+ # Outputs given string if trace flag has been set.
134
+ #
135
+ # Output flags can be set with {.handle_output_options}.
136
+ #
137
+ # @param str String to be outputted
138
+ # @param raw [Boolean] If true, disables output formatting
139
+ private def trace str, raw: false
140
+ Bwrap::Output.trace_output(str, raw: raw, log_callback: 2)
141
+ end
142
+
143
+ # Outputs given string if debug flag has been set.
144
+ #
145
+ # Output flags can be set with {.handle_output_options}.
146
+ #
147
+ # @param str String to be outputted
148
+ # @param raw [Boolean] If true, disables output formatting
149
+ private def debug str, raw: false
150
+ Bwrap::Output.debug_output(str, raw: raw, log_callback: 2)
151
+ end
152
+
153
+ # Outputs given string if verbose flag has been set.
154
+ #
155
+ # Output flags can be set with {.handle_output_options}.
156
+ #
157
+ # @param str String to be outputted
158
+ # @param raw [Boolean] If true, disables output formatting
159
+ private def verb str, raw: false
160
+ Bwrap::Output.verb_output(str, raw: raw, log_callback: 2)
161
+ end
162
+
163
+ # Outputs given string to `$stderr`.
164
+ #
165
+ # @param str String to be outputted
166
+ # @param raw [Boolean] If true, disables output formatting
167
+ private def warn str, raw: false
168
+ Bwrap::Output.warn_output(str, raw: raw, log_callback: 2)
169
+ end
170
+
171
+ # Outputs given string to `$stderr` and halts execution.
172
+ #
173
+ # @param str String to be outputted
174
+ # @param label [Symbol] Exit label accepted by {Bwrap::Execution.resolve_exit_code}
175
+ # @param raise_exception [Boolean] if true, an exception is raised instead of just existing with exit code.
176
+ private def error str = nil, label: :unspecified, raise_exception: false
177
+ Bwrap::Output.error_output(str, label: label, log_callback: 2, raise_exception: raise_exception)
178
+ end
179
+
180
+ # @!endgroup
181
+ end
data/lib/bwrap/output.rb CHANGED
@@ -1,153 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Have variables like $CHILD_STATUS which is alias of $?.
4
- require "English"
5
-
6
- require "bwrap/execution/labels"
7
- require_relative "output/colors"
8
- require_relative "output/levels"
9
- require_relative "output/log"
10
-
11
- # Extends the module with outputting methods.
3
+ # Declare Output module here so Bwrap::Output module is
4
+ # already declared for Output module classes, to avoid
5
+ # a circular dependency.
6
+ #
7
+ # See output/output.rb for documentation.
12
8
  module Bwrap::Output
13
- include Bwrap::Output::Colors
14
-
15
- # @see #verbose?
16
- def self.verbose?
17
- Bwrap::Output::Levels.verbose?
18
- end
19
-
20
- # @see #debug?
21
- def self.debug?
22
- Bwrap::Output::Levels.debug?
23
- end
24
-
25
- # @see #trace?
26
- def self.trace?
27
- Bwrap::Output::Levels.trace?
28
- end
29
-
30
- # Takes hash of options received from Optimist and checks output related flags.
31
- def self.handle_output_options options
32
- Bwrap::Output::Levels.handle_output_options options
33
- end
34
-
35
- # Handler used by #trace to output given string.
36
- def self.trace_output str, raw: false, log_callback: 1
37
- return unless trace?
38
-
39
- if raw
40
- print str
41
- else
42
- out = Bwrap::Output::Levels.trace_print_formatted str, log_callback: (log_callback + 1)
43
- end
44
- Bwrap::Output::Log.puts_to_log out || str
45
- end
46
-
47
- # Handler used by #debug to output given string.
48
- def self.debug_output str, raw: false, log_callback: 1
49
- return unless debug?
50
-
51
- if raw
52
- print str
53
- else
54
- out = Bwrap::Output::Levels.debug_print_formatted str, log_callback: (log_callback + 1)
55
- end
56
- Bwrap::Output::Log.puts_to_log out || str
57
- end
58
-
59
- # Handler used by #verb to output given string.
60
- def self.verb_output str, raw: false, log_callback: 1
61
- return unless verbose?
62
-
63
- if raw
64
- print str
65
- else
66
- out = Bwrap::Output::Levels.verbose_print_formatted str, log_callback: (log_callback + 1)
67
- end
68
- Bwrap::Output::Log.puts_to_log out || str
69
- end
70
-
71
- # Handler used by #warn to output given string.
72
- def self.warn_output str, raw: false, log_callback: 1
73
- if raw
74
- print str
75
- else
76
- out = Bwrap::Output::Levels.warning_print_formatted str, log_callback: (log_callback + 1)
77
- end
78
- Bwrap::Output::Log.puts_to_log out || str
79
- end
80
-
81
- # Aborts current process.
82
- #
83
- # Use this instead of Ruby’s #exit in order to filter out dummy #exit calls from the code.
84
- def self.error_output str = nil, label: :unspecified, log_callback: 1
85
- unless str.nil?
86
- out = Bwrap::Output::Levels.error_print_formatted str, log_callback: (log_callback + 1)
87
- Bwrap::Output::Log.puts_to_log out
88
- end
89
-
90
- exit Bwrap::Execution::Labels.resolve_exit_code(label)
91
- end
92
-
93
- # @return true if --verbose, --debug or --trace has been passed, false if not.
94
- private def verbose?
95
- Bwrap::Output::Levels.verbose?
96
- end
97
-
98
- # @return true if --debug or --trace has been passed, false if not.
99
- private def debug?
100
- Bwrap::Output::Levels.debug?
101
- end
102
-
103
- # @return true if --trace has been passed, false if not.
104
- private def trace?
105
- Bwrap::Output::Levels.trace?
106
- end
107
-
108
- # Outputs given string if trace flag has been set.
109
- #
110
- # Output flags can be set with {.handle_output_options}.
111
- #
112
- # @param str String to be outputted
113
- # @param raw [Boolean] If true, disables output formatting
114
- private def trace str, raw: false
115
- Bwrap::Output.trace_output(str, raw: raw, log_callback: 2)
116
- end
117
-
118
- # Outputs given string if debug flag has been set.
119
- #
120
- # Output flags can be set with {.handle_output_options}.
121
- #
122
- # @param str String to be outputted
123
- # @param raw [Boolean] If true, disables output formatting
124
- private def debug str, raw: false
125
- Bwrap::Output.debug_output(str, raw: raw, log_callback: 2)
126
- end
127
-
128
- # Outputs given string if verbose flag has been set.
129
- #
130
- # Output flags can be set with {.handle_output_options}.
131
- #
132
- # @param str String to be outputted
133
- # @param raw [Boolean] If true, disables output formatting
134
- private def verb str, raw: false
135
- Bwrap::Output.verb_output(str, raw: raw, log_callback: 2)
136
- end
137
-
138
- # Outputs given string to `$stderr`.
139
- #
140
- # @param str String to be outputted
141
- # @param raw [Boolean] If true, disables output formatting
142
- private def warn str, raw: false
143
- Bwrap::Output.warn_output(str, raw: raw, log_callback: 2)
144
- end
145
-
146
- # Outputs given string to `$stderr` and halts execution.
147
- #
148
- # @param str String to be outputted
149
- # @param label [Symbol] Exit label accepted by {Bwrap::Execution.resolve_exit_code}
150
- private def error str = nil, label: :unspecified
151
- Bwrap::Output.error_output(str, label: label, log_callback: 2)
152
- end
153
9
  end
10
+
11
+ require_relative "output/output_impl"
data/lib/bwrap/version.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # bwrap command runner tools.
4
3
  module Bwrap
5
4
  # Current version of bwrap.
6
- VERSION = "1.0.0-alpha2"
5
+ VERSION = "1.0.0-beta1"
7
6
  end
8
7
 
9
8
  require "deep-cover" if ENV["DEEP_COVER"]
data/lib/bwrap.rb CHANGED
@@ -1,74 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "optimist"
4
-
5
- #require "deep-cover" if ENV["DEEP_COVER"]
6
-
7
- require "bwrap/version"
8
- require "bwrap/args/construct"
9
- require "bwrap/config"
10
- require "bwrap/execution"
11
-
12
- # Executes bwrap command using given configuration.
13
- class Bwrap::Bwrap
14
- include Bwrap::Execution
15
-
16
- def initialize config
17
- @config = config
18
-
19
- parse_command_line_arguments
20
- end
21
-
22
- # Runs given command inside bwrap.
23
- #
24
- # @param command [String, Array] Command, with necessary arguments, to be executed inside bwrap
25
- def run command
26
- construct = Bwrap::Args::Construct.new
27
- construct.config = @config
28
- bwrap_args = construct.construct_bwrap_args
29
-
30
- exec_command = [ "bwrap" ]
31
- exec_command += bwrap_args
32
- exec_command += %W{ #{command} --new-instance }
33
- exec_command += ARGV unless ARGV.empty?
34
-
35
- execute exec_command
36
-
37
- construct.cleanup
38
- end
39
-
40
- # Parses command line arguments given to caller script.
41
- private def parse_command_line_arguments
42
- options = optimist_cli_args
43
-
44
- Bwrap::Output.handle_output_options options
45
- end
46
-
47
- # Parses global bwrap flags using Optimist.
48
- private def optimist_cli_args
49
- Optimist.options do
50
- version ::Bwrap::VERSION
51
-
52
- banner "Usage:"
53
- banner " #{$PROGRAM_NAME} [global options]\n \n"
54
- banner "Global options:"
55
- opt :verbose,
56
- "Show verbose output",
57
- short: "v"
58
- opt :debug,
59
- "Show debug output (useful when debugging a problem)",
60
- short: "d"
61
- opt :trace,
62
- "Show trace output (noisiest, probably not useful for most of time)",
63
- short: :none
64
- opt :version,
65
- "Print version and exit",
66
- short: "V"
67
- opt :help,
68
- "Show help message",
69
- short: "h"
70
-
71
- educate_on_error
72
- end
73
- end
3
+ require "bwrap/bwrap"
4
+
5
+ # ruby-bwrap provides easy-to-use interface to run complex programs in sandboxes created with
6
+ # {https://github.com/containers/bubblewrap bubblewrap}.
7
+ #
8
+ # To run a program inside bubblewrap, a wrapper executable can be created. For example:
9
+ #
10
+ # require "bwrap"
11
+ #
12
+ # config = Bwrap::Config.new
13
+ # config.user = "dummy_user"
14
+ # config.full_system_mounts = true
15
+ # config.binaries_from = %w{
16
+ # /bin
17
+ # /usr/bin
18
+ # }
19
+ #
20
+ # bwrap = Bwrap::Bwrap.new config
21
+ # bwrap.parse_command_line_arguments
22
+ # bwrap.run "/bin/true"
23
+ #
24
+ # There also are few generic utilities, {Bwrap::Output} for handling output of scripts and
25
+ # {Bwrap::Execution} to run executables.
26
+ module Bwrap
27
+ # Empty module.
74
28
  end
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.0.0.pre.alpha2
4
+ version: 1.0.0.pre.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samu Voutilainen
@@ -34,36 +34,22 @@ cert_chain:
34
34
  X4ioQwEn1/9tHs19VO1CLF58451HgEo1BXd7eWLmV1V5cqw0YWok1ly4L/Su/Phf
35
35
  MRxVMHiVAqY=
36
36
  -----END CERTIFICATE-----
37
- date: 2021-11-14 00:00:00.000000000 Z
37
+ date: 2021-12-12 00:00:00.000000000 Z
38
38
  dependencies:
39
- - !ruby/object:Gem::Dependency
40
- name: optimist
41
- requirement: !ruby/object:Gem::Requirement
42
- requirements:
43
- - - "~>"
44
- - !ruby/object:Gem::Version
45
- version: '3.0'
46
- type: :runtime
47
- prerelease: false
48
- version_requirements: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - "~>"
51
- - !ruby/object:Gem::Version
52
- version: '3.0'
53
39
  - !ruby/object:Gem::Dependency
54
40
  name: bundler
55
41
  requirement: !ruby/object:Gem::Requirement
56
42
  requirements:
57
43
  - - ">="
58
44
  - !ruby/object:Gem::Version
59
- version: '1.17'
45
+ version: '1.16'
60
46
  type: :development
61
47
  prerelease: false
62
48
  version_requirements: !ruby/object:Gem::Requirement
63
49
  requirements:
64
50
  - - ">="
65
51
  - !ruby/object:Gem::Version
66
- version: '1.17'
52
+ version: '1.16'
67
53
  - !ruby/object:Gem::Dependency
68
54
  name: rake
69
55
  requirement: !ruby/object:Gem::Requirement
@@ -120,7 +106,8 @@ dependencies:
120
106
  - - "~>"
121
107
  - !ruby/object:Gem::Version
122
108
  version: '3.7'
123
- description: For now this just reserves this name. Please be back later.
109
+ description: For now this is tailored to my needs, so this may or may not be of any
110
+ use.
124
111
  email:
125
112
  - smar@smar.fi
126
113
  executables: []
@@ -133,20 +120,28 @@ files:
133
120
  - lib/bwrap.rb
134
121
  - lib/bwrap/args/args.rb
135
122
  - lib/bwrap/args/bind.rb
123
+ - lib/bwrap/args/bind/library.rb
124
+ - lib/bwrap/args/bind/mime.rb
136
125
  - lib/bwrap/args/construct.rb
137
126
  - lib/bwrap/args/environment.rb
127
+ - lib/bwrap/args/features.rb
128
+ - lib/bwrap/args/library.rb
138
129
  - lib/bwrap/args/machine_id.rb
139
130
  - lib/bwrap/args/mount.rb
131
+ - lib/bwrap/bwrap.rb
140
132
  - lib/bwrap/config.rb
133
+ - lib/bwrap/config/features.rb
141
134
  - lib/bwrap/execution.rb
135
+ - lib/bwrap/execution/exceptions.rb
142
136
  - lib/bwrap/execution/execute.rb
143
137
  - lib/bwrap/execution/execution.rb
144
138
  - lib/bwrap/execution/labels.rb
139
+ - lib/bwrap/execution/path.rb
145
140
  - lib/bwrap/output.rb
146
141
  - lib/bwrap/output/colors.rb
147
142
  - lib/bwrap/output/levels.rb
148
143
  - lib/bwrap/output/log.rb
149
- - lib/bwrap/output/output.rb
144
+ - lib/bwrap/output/output_impl.rb
150
145
  - lib/bwrap/version.rb
151
146
  homepage: https://git.sr.ht/~smar/ruby-bwrap
152
147
  licenses:
@@ -155,6 +150,7 @@ metadata:
155
150
  homepage_uri: https://git.sr.ht/~smar/ruby-bwrap
156
151
  source_code_uri: https://git.sr.ht/~smar/ruby-bwrap
157
152
  changelog_uri: https://git.sr.ht/~smar/ruby-bwrap/tree/master/item/CHANGELOG.md
153
+ rubygems_mfa_required: 'false'
158
154
  post_install_message:
159
155
  rdoc_options: []
160
156
  require_paths:
@@ -174,5 +170,5 @@ rubyforge_project:
174
170
  rubygems_version: 2.7.6.3
175
171
  signing_key:
176
172
  specification_version: 4
177
- summary: 'THIS WILL BE IN FUTURE: Framework to create commands for bwrap'
173
+ summary: Framework to create commands for bwrap
178
174
  test_files: []
metadata.gz.sig CHANGED
Binary file
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bwrap/version"
4
-
5
- # Outputting utilities.
6
- module Bwrap::Output
7
- # Nya.
8
- end