-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathyk
More file actions
executable file
·314 lines (263 loc) · 8.92 KB
/
yk
File metadata and controls
executable file
·314 lines (263 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env ruby
# encoding: utf-8
require "cheetah"
require "smart_colored/extend"
require "thor"
require "yaml"
BASE_DIR = File.expand_path(File.dirname(__FILE__))
DATA_DIR = "#{BASE_DIR}/data"
STUBS_DIR = "#{BASE_DIR}/stubs"
PATCHES_DIR = "#{BASE_DIR}/patches"
ERROR_FILE = "#{BASE_DIR}/error.log"
# Hash: String -> YastModule
$yast_modules = Hash.new do |hash, missing_key|
#report misspelled modules instead of producing nil
raise "Unknown yast module #{missing_key}"
end
require_relative "lib/exceptions"
require_relative "lib/messages"
require_relative "lib/tsorter"
require_relative "lib/yast_module"
require_relative "lib/threading"
require_relative "lib/commands/clone_command"
require_relative "lib/commands/convert_command"
require_relative "lib/commands/genpatch_command"
require_relative "lib/commands/patch_command"
require_relative "lib/commands/reset_command"
require_relative "lib/commands/restructure_command"
require_relative "lib/commands/ybc_command"
require_relative "lib/commands/ruby_command"
require_relative "lib/commands/makefile_command"
require_relative "lib/commands/build_command"
require_relative "lib/commands/submit_command"
require_relative "lib/commands/package_command"
require_relative "lib/commands/pull_command"
require_relative "lib/commands/test_command"
class CLI < Thor
class_option :debug,
:type => :boolean,
:desc => "Log executed external commands"
class_option :with_deps,
:type => :boolean,
:desc => "Operate also on dependencies (ybc_deps) of specified modules"
class_option :threads,
:type => :numeric,
:desc => "Use max. N threads for parallelized commands (default: N = number of CPUs)"
def initialize(*args)
super *args
if options[:debug]
log = Logger.new(STDOUT)
Cheetah.default_options = { :logger => log }
end
if options[:threads].to_i > 0
Threading.threads_count = options[:threads]
end
load_module_data
end
desc "clone <module>...", "Clone module source from Git repository"
option :ref, :type => :string, :desc => "A ref to clone from"
option :branch, :type => :string, :desc => "A branch to clone from"
option :tag, :type => :string, :desc => "A tag to clone from"
def clone(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::CloneCommand.new(options).apply(mod)
end
end
desc "convert <module>...", "Run all conversion-related tasks for module"
def convert(*module_names)
report_results(module_names) do |mod|
Commands::ConvertCommand.new.apply(mod)
end
end
desc "genpatch <module>...", "Generate module's patch from changes in its work directory"
def genpatch(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::GenpatchCommand.new.apply(mod)
end
end
desc "patch <module>...", "Patch module(s)"
def patch(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::PatchCommand.new.apply(mod)
end
end
desc "reset <module>...", "Revert module(s) work directory to clean state"
def reset(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::ResetCommand.new.apply(mod)
end
end
desc "pull <module>...", "Update the module(s) work directory to the latest state (git pull)"
def pull(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::PullCommand.new.apply(mod)
end
end
desc "restructure <module>...", "Change module(s) work directory structure to fit the Y2DIR scheme"
def restructure(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::RestructureCommand.new.apply(mod)
end
end
desc "ybc <module>...", "Compile module's YCP modules into YCP bytecode"
def ybc(*module_names)
report_results(module_names) do |mod|
Commands::YbcCommand.new.apply(mod)
end
end
desc "ruby <module>...", "Convert module's YCP files into Ruby"
def ruby(*module_names)
report_results(module_names) do |mod|
Commands::RubyCommand.new.apply(mod)
end
end
desc "makefile <module>...", "Generate Makefile.am file(s) for converted module"
def makefile(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::MakefileCommand.new.apply(mod)
end
end
desc "package <module>...", "Create package source for converted module"
def package(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::PackageCommand.new.apply(mod)
end
end
desc "build <module>...", "Build package for converted module locally"
def build(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::BuildCommand.new.apply(mod)
end
end
desc "submit <module>...", "Submit converted module package source to OBS"
def submit(*module_names)
modules = modules_from_names(module_names)
with_modules modules do |mod|
Commands::SubmitCommand.new.apply(mod)
end
end
desc "test <module>...", "Run tests for converted module"
def test(*module_names)
report_results(module_names) do |mod|
Commands::TestCommand.new.apply(mod)
end
end
private
def colorize_error_count(n)
n == 0 ? n.to_s.green : n.to_s.red
end
def report_results(module_names) # :yields: mod
compile_stubs
FileUtils.rm_rf(ERROR_FILE)
counts = {
:ok => 0,
:excluded => 0,
:error_ybc => 0,
:error_y2r => 0,
:error_ruby => 0,
:error_other => 0
}
modules = modules_from_names(module_names)
with_modules modules do |mod|
module_counts = yield(mod)
counts.keys.each { |k| counts[k] += module_counts[k] }
end
Messages.info ""
Messages.info "-----"
Messages.info ""
Messages.info "Total OK: #{counts[:ok].to_s.green}"
Messages.info "Total EXCLUDED: #{counts[:excluded].to_s.green}"
Messages.info "Total ERROR(ybc): #{colorize_error_count(counts[:error_ybc])}"
Messages.info "Total ERROR(y2r): #{colorize_error_count(counts[:error_y2r])}"
Messages.info "Total ERROR(ruby): #{colorize_error_count(counts[:error_ruby])}"
Messages.info "Total ERROR(other): #{colorize_error_count(counts[:error_other])}"
error_counts = counts.values_at(
:error_ybc,
:error_y2r,
:error_ruby,
:error_other
)
exit 1 if error_counts.any?(&:nonzero?)
end
def compile_stubs
Dir["#{STUBS_DIR}/*.ycp"].each do |file|
Cheetah.run "ycpc", "-c", file
end
end
def load_module_data
Dir["#{DATA_DIR}/*.yml"].sort.each do |file|
name = File.basename(file, ".yml")
$yast_modules[name] = YastModule.new(
name,
YAML.load_file(file) || {},
data_dir
)
end
end
def data_dir
# XDG Base Directory Specification, version 0.7:
#
# $XDG_DATA_HOME defines the base directory relative to which user
# specific data files should be stored. If $XDG_DATA_HOME is either not
# set or empty, a default equal to $HOME/.local/share should be used.
if ENV["XDG_DATA_HOME"] && !ENV["XDG_DATA_HOME"].empty?
data_dir = ENV["XDG_DATA_HOME"]
elsif ENV["HOME"] && !ENV["HOME"].empty?
data_dir = "#{ENV["HOME"]}/.local/share"
else
raise "Don't know where to store data, both $XDG_DATA_HOME and $HOME are not set or empty."
end
yast_dir = "#{data_dir}/ycp-killer"
end
def module_from_cwd
cwd = Dir.pwd
# .../work/foo
package = cwd[/\/#{Regexp.escape(YastModule::WORK_DIR)}\/([^\/]+)\Z/, 1]
raise "No modules specified and current directory is not a working directory" unless package
package
end
def modules_from_names(module_names)
module_names = [ module_from_cwd ] if module_names.empty?
module_names = $yast_modules.keys if module_names == ["all"]
modules = TSorter.sort($yast_modules.values_at(*module_names)) { |m| m.ybc_deps }
# TSorter.sort includes all dependencies in the result, even if they weren't
# in the original array. This may or may not be what we want.
if options[:with_deps]
modules
else
modules.select { |m| module_names.include? m.name }
end
end
def with_modules(modules) # :yields: mod
max_counter_size = 3 + ((Math.log10(modules.size).to_i + 1) * 2)
modules.each_index do |i|
message = sprintf(
"%-#{max_counter_size}s %s",
"[#{i + 1}/#{modules.size}]",
"Processing #{modules[i].name}:"
)
Messages.header message do
yield modules[i]
end
end
end
end
begin
CLI.start(ARGV)
rescue Interrupt
puts # ^C is printed already
rescue SystemExit
raise
rescue Exception => e
$stderr.print print_exception(e).red
exit 1
end