Changeset View
Changeset View
Standalone View
Standalone View
usr/lib/neon_update/kf5_snap.rb
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
1 | #!/usr/bin/env ruby | ||||
---|---|---|---|---|---|
2 | # frozen_string_literal: true | ||||
3 | # | ||||
4 | # Copyright (C) 2019 Harald Sitter <sitter@kde.org> | ||||
5 | # | ||||
6 | # This program is free software; you can redistribute it and/or | ||||
7 | # modify it under the terms of the GNU General Public License as | ||||
8 | # published by the Free Software Foundation; either version 3 of | ||||
9 | # the License or any later version accepted by the membership of | ||||
10 | # KDE e.V. (or its successor approved by the membership of KDE | ||||
11 | # e.V.), which shall act as a proxy defined in Section 14 of | ||||
12 | # version 3 of the license. | ||||
13 | # | ||||
14 | # This program is distributed in the hope that it will be useful, | ||||
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
17 | # GNU General Public License for more details. | ||||
18 | # | ||||
19 | # You should have received a copy of the GNU General Public License | ||||
20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
21 | | ||||
22 | require 'json' | ||||
23 | require 'net/http' | ||||
24 | require 'socket' | ||||
25 | | ||||
26 | SOCKET_PATH = '/run/snapd.socket' | ||||
27 | | ||||
28 | class APIResponse | ||||
29 | attr_reader :type | ||||
30 | attr_reader :status_code | ||||
31 | attr_reader :status | ||||
32 | attr_reader :result | ||||
33 | | ||||
34 | def initialize(json) | ||||
35 | @type = json.fetch('type') | ||||
36 | @status_code = json.fetch('status-code') | ||||
37 | @status = json.fetch('status') | ||||
38 | @result = json.fetch('result') | ||||
39 | end | ||||
40 | end | ||||
41 | | ||||
42 | class Connection | ||||
43 | def initialize | ||||
44 | @socket = Net::BufferedIO.new(UNIXSocket.new(SOCKET_PATH)) | ||||
45 | end | ||||
46 | | ||||
47 | # @return returns the (not-parsed) json reply obj | ||||
48 | def get(path) | ||||
49 | request = Net::HTTP::Get.new(path) | ||||
50 | request['host'] = 'localhost' | ||||
51 | request.exec(@socket, '1.1', path) | ||||
52 | | ||||
53 | response = nil | ||||
54 | loop do | ||||
55 | response = Net::HTTPResponse.read_new(@socket) | ||||
56 | break unless response.is_a?(Net::HTTPContinue) | ||||
57 | end | ||||
58 | response.reading_body(@socket, request.response_body_permitted?) {} | ||||
59 | | ||||
60 | yield response if block_given? | ||||
61 | | ||||
62 | response | ||||
63 | end | ||||
64 | end | ||||
65 | | ||||
66 | unless File.exist?(SOCKET_PATH) | ||||
67 | warn 'snapd not running ➜ no clean-up of kde-frameworks-5 snap' | ||||
68 | exit 0 | ||||
69 | end | ||||
70 | | ||||
71 | # Get a list of all slot interfaces of kf5 and record snaps connected to it. | ||||
72 | # If none are connected that means the snap is unused and can be thrown out | ||||
73 | # under the assumption that the reason it is installed is because it was seeded | ||||
74 | # on the ISO. | ||||
75 | kf5_used_by = [] | ||||
76 | Connection.new.get('/v2/interfaces') do |response| | ||||
77 | response = JSON.parse(response.body) | ||||
78 | response = APIResponse.new(response) | ||||
79 | | ||||
80 | slots = response.result.fetch('slots') | ||||
81 | kf5_slots = slots.find_all { |x| x.fetch('snap') == 'kde-frameworks-5' } | ||||
82 | | ||||
83 | if kf5_slots.empty? | ||||
84 | puts 'kde-frameworks-5 snap not installed; nothing to clean up.' | ||||
85 | exit 0 | ||||
86 | end | ||||
87 | | ||||
88 | kf5_slots.each do |slot| | ||||
89 | connections = slot&.fetch('connections', []) | ||||
90 | kf5_used_by += connections.collect { |x| x.fetch('snap') } | ||||
91 | end | ||||
92 | end | ||||
93 | | ||||
94 | if kf5_used_by.empty? | ||||
95 | puts 'kde-frameworks-5 snap used by nothing ➜ scheduling removal' | ||||
96 | system('snap remove --no-wait kde-frameworks-5') | ||||
97 | else | ||||
98 | puts "kde-frameworks-5 snap used by #{kf5_used_by} ➜ leaving it installed" | ||||
99 | end |