Class: Autoproj::CLI::Status

Inherits:
InspectionTool show all
Defined in:
lib/autoproj/cli/status.rb

Defined Under Namespace

Classes: PackageStatus, StatusResult

Instance Attribute Summary

Attributes inherited from Base

#ws

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InspectionTool

#finalize_setup, #initialize_and_load

Methods inherited from Base

#export_env_sh, #initialize, #normalize_command_line_package_selection, #notify_env_sh_updated, #resolve_selection, #resolve_user_selection, validate_options, #validate_user_selection

Methods included from Ops::Tools

#common_options, #create_autobuild_package, #load_autoprojrc, #load_main_initrb

Constructor Details

This class inherits a constructor from Autoproj::CLI::Base

Class Method Details

.report_exception(package_status, msg, e) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/autoproj/cli/status.rb', line 75

def self.report_exception(package_status, msg, e)
    package_status.msg << Autoproj.color("  #{msg} (#{e})", :red)
    if Autobuild.debug
        package_status.msg.concat(e.backtrace.map do |line|
            Autoproj.color("    #{line}", :red)
        end)
    end
end

.status_of_package(package_description, only_local: false, snapshot: false) ⇒ Object



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
# File 'lib/autoproj/cli/status.rb', line 85

def self.status_of_package(package_description, only_local: false, snapshot: false)
    pkg = package_description.autobuild
    importer = pkg.importer
    package_status = PackageStatus.new(Array.new, false, false, false, false)
    if !importer
        package_status.msg << Autoproj.color("  is a local-only package (no VCS)", :bold, :red)
    elsif !importer.respond_to?(:status)
        package_status.msg << Autoproj.color("  the #{importer.class.name.gsub(/.*::/, '')} importer does not support status display", :bold, :red)
    elsif !File.directory?(pkg.srcdir)
        package_status.msg << Autoproj.color("  is not imported yet", :magenta)
    else
        begin status = importer.status(pkg, only_local: only_local)
        rescue StandardError => e
            report_exception(package_status, "failed to fetch status information", e)
            return package_status
        end

        snapshot_useful = [Autobuild::Importer::Status::ADVANCED, Autobuild::Importer::Status::NEEDS_MERGE]
                          .include?(status.status)
        if snapshot && snapshot_useful && importer.respond_to?(:snapshot)
            snapshot_version =
                begin importer.snapshot(pkg, nil, exact_state: false, only_local: only_local)
                rescue Autobuild::PackageException
                    Hash.new
                rescue StandardError => e
                    report_exception(package_status, "failed to fetch snapshotting information", e)
                    return package_status
                end
            if snapshot_overrides_vcs?(importer, package_description.vcs, snapshot_version)
                non_nil_values = snapshot_version.delete_if { |k, v| !v }
                package_status.msg << Autoproj.color("  found configuration that contains all local changes: #{non_nil_values.sort_by(&:first).map { |k, v| "#{k}: #{v}" }.join(', ')}", :bright_green)
                package_status.msg << Autoproj.color("  consider adding this to your overrides, or use autoproj versions to do it for you", :bright_green)
                if snapshot
                    importer.relocate(importer.repository, snapshot_version)
                end
            end
        end

        status.unexpected_working_copy_state.each do |msg|
            package_status.unexpected = true
            package_status.msg << Autoproj.color("  #{msg}", :red, :bold)
        end

        if status.uncommitted_code
            package_status.msg << Autoproj.color("  contains uncommitted modifications", :red)
            package_status.uncommitted = true
        end

        case status.status
        when Autobuild::Importer::Status::UP_TO_DATE
            package_status.sync = true
        when Autobuild::Importer::Status::ADVANCED
            package_status.local = true
            package_status.msg << Autoproj.color("  local contains #{status.local_commits.size} commit that remote does not have:", :blue)
            status.local_commits.each do |line|
                package_status.msg << Autoproj.color("    #{line}", :blue)
            end
        when Autobuild::Importer::Status::SIMPLE_UPDATE
            package_status.remote = true
            package_status.msg << Autoproj.color("  remote contains #{status.remote_commits.size} commit that local does not have:", :magenta)
            status.remote_commits.each do |line|
                package_status.msg << Autoproj.color("    #{line}", :magenta)
            end
        when Autobuild::Importer::Status::NEEDS_MERGE
            package_status.local  = true
            package_status.remote = true
            package_status.msg << "  local and remote have diverged with respectively #{status.local_commits.size} and #{status.remote_commits.size} commits each"
            package_status.msg << Autoproj.color("  -- local commits --", :blue)
            status.local_commits.each do |line|
                package_status.msg << Autoproj.color("   #{line}", :blue)
            end
            package_status.msg << Autoproj.color("  -- remote commits --", :magenta)
            status.remote_commits.each do |line|
                package_status.msg << Autoproj.color("   #{line}", :magenta)
            end
        end
    end
    package_status
end

Instance Method Details

#display_status(packages, parallel: ws.config.parallel_import_level, snapshot: false, only_local: false, progress: true) ⇒ Object



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
# File 'lib/autoproj/cli/status.rb', line 234

def display_status(packages, parallel: ws.config.parallel_import_level, snapshot: false, only_local: false, progress: true)
    sync_packages = ""
    spinner = nil

    if progress
        progress = lambda do |pkg|
            unless spinner
                unless sync_packages.empty?
                    Autoproj.message("#{sync_packages}: #{Autoproj.color('local and remote are in sync', :green)}")
                    sync_packages = ""
                end

                spinner = TTY::Spinner.new("[:spinner] #{pkg.name}", clear: true)
            end
            spinner.spin
        end
    end

    result = each_package_status(packages, only_local: only_local, parallel: parallel, progress: progress) do |pkg, status|
        if spinner
            spinner.stop
            spinner = nil
        end

        pkg_name = pkg.name
        if status.sync && status.msg.empty?
            if sync_packages.size > 80
                Autoproj.message "#{sync_packages},"
                sync_packages = ""
            end
            msg = if sync_packages.empty?
                      pkg_name
                  else
                      ", #{pkg_name}"
                  end
            STDERR.print msg
            sync_packages = "#{sync_packages}#{msg}"
            next
        end

        unless sync_packages.empty?
            Autoproj.message("#{sync_packages}: #{Autoproj.color('local and remote are in sync', :green)}")
            sync_packages = ""
        end

        STDERR.print

        if status.msg.size == 1
            Autoproj.message "#{pkg_name}: #{status.msg.first}"
        else
            Autoproj.message "#{pkg_name}:"
            status.msg.each do |l|
                Autoproj.message l
            end
        end
    end
    unless sync_packages.empty?
        Autoproj.message("#{sync_packages}: #{Autoproj.color('local and remote are in sync', :green)}")
    end
    result
end

#each_package_status(packages, parallel: ws.config.parallel_import_level, snapshot: false, only_local: false, progress: nil) ⇒ Object



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
# File 'lib/autoproj/cli/status.rb', line 165

def each_package_status(
    packages,
    parallel: ws.config.parallel_import_level,
    snapshot: false, only_local: false, progress: nil
)
    unless block_given?
        return enum_for(
            __method__, packages,
            parallel: parallel, snapshot: snapshot, only_local: only_local,
            progress: progress
        )
    end

    result = StatusResult.new

    executor = Concurrent::FixedThreadPool.new(parallel, max_length: 0)
    interactive, noninteractive =
        packages.partition { |pkg| pkg.autobuild.importer&.interactive? }
    noninteractive = noninteractive.map do |pkg|
        future = Concurrent::Promises.future_on(executor) do
            Status.status_of_package(
                pkg, snapshot: snapshot, only_local: only_local
            )
        end
        [pkg, future]
    end

    (noninteractive + interactive).each do |pkg, future|
        if future
            if progress
                wait_timeout = 1
                loop do
                    future.wait!(wait_timeout)
                    if future.resolved?
                        break
                    else
                        wait_timeout = 0.2
                        progress.call(pkg)
                    end
                end
            end

            unless (status = future.value)
                raise future.reason
            end
        else
            status = Status.status_of_package(
                pkg, snapshot: snapshot, only_local: only_local
            )
        end

        result.uncommitted ||= status.uncommitted
        result.local       ||= status.local
        result.remote      ||= status.remote
        yield(pkg, status)
    end
    result
rescue Interrupt
    Autoproj.warn "Interrupted, waiting for pending jobs to finish"
    raise
rescue Exception => e
    Autoproj.error "internal error (#{e.class}): #{e}, waiting for pending jobs to finish"
    raise
ensure
    executor.shutdown
    executor.wait_for_termination
end

#run(user_selection, options = Hash.new) ⇒ Object



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
# File 'lib/autoproj/cli/status.rb', line 15

def run(user_selection, options = Hash.new)
    initialize_and_load(mainline: options[:mainline])
    packages, *, config_selected = finalize_setup(
        user_selection,
        recursive: options[:deps]
    )

    if options[:config].nil?
        options[:config] = user_selection.empty? || config_selected
    end

    if packages.empty?
        Autoproj.error "no packages or OS packages match #{user_selection.join(' ')}"
        return
    end

    if !options.has_key?(:parallel) && options[:only_local]
        options[:parallel] = 1
    else
        options[:parallel] ||= ws.config.parallel_import_level
    end

    if options[:config]
        pkg_sets = ws.manifest.each_package_set.to_a
        unless pkg_sets.empty?
            Autoproj.message("autoproj: displaying status of configuration", :bold)
            display_status(
                pkg_sets,
                parallel: options[:parallel],
                snapshot: options[:snapshot],
                only_local: options[:only_local],
                progress: options[:progress]
            )

            STDERR.puts
        end
    end

    Autoproj.message("autoproj: displaying status of packages", :bold)
    packages = packages.sort.map do |pkg_name|
        ws.manifest.find_package_definition(pkg_name)
    end
    display_status(
        packages,
        parallel: options[:parallel],
        snapshot: options[:snapshot],
        only_local: options[:only_local],
        progress: options[:progress]
    )
end

#snapshot_overrides_vcs?(importer, vcs, snapshot) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/autoproj/cli/status.rb', line 66

def snapshot_overrides_vcs?(importer, vcs, snapshot)
    if importer.respond_to?(:snapshot_overrides?)
        importer.snapshot_overrides?(snapshot)
    else
        vcs = vcs.to_hash
        snapshot.any? { |k, v| vcs[k] != v }
    end
end

#validate_options(packages, options) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/autoproj/cli/status.rb', line 7

def validate_options(packages, options)
    packages, options = super
    options[:progress] = Autobuild.progress_display_enabled?
    options[:deps] = false if options[:no_deps_shortcut]
    options[:deps] = true if options[:deps].nil? && packages.empty?
    [packages, options]
end