~ubuntu-core-dev/ubuntu-release-upgrader/trunk

« back to all changes in this revision

Viewing changes to SoftwareProperties/dialog_apt_key.py

  • Committer: Michael Vogt
  • Date: 2005-11-15 13:18:07 UTC
  • Revision ID: egon@top-20051115131807-12fada324eb74180
* initial revision (after accidently killing it)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# dialog_apt_key.py.in - edit the apt keys
 
2
#  
 
3
#  Copyright (c) 2004 Canonical
 
4
#  
 
5
#  Author: Michael Vogt <mvo@debian.org>
 
6
 
7
#  This program is free software; you can redistribute it and/or 
 
8
#  modify it under the terms of the GNU General Public License as 
 
9
#  published by the Free Software Foundation; either version 2 of the
 
10
#  License, or (at your option) any later version.
 
11
 
12
#  This program is distributed in the hope that it will be useful,
 
13
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#  GNU General Public License for more details.
 
16
 
17
#  You should have received a copy of the GNU General Public License
 
18
#  along with this program; if not, write to the Free Software
 
19
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
20
#  USA
 
21
 
 
22
import os
 
23
import gobject
 
24
import gtk
 
25
import gtk.glade
 
26
import subprocess
 
27
import gettext
 
28
from utils import *
 
29
from subprocess import PIPE
 
30
 
 
31
# gettext convenient
 
32
_ = gettext.gettext
 
33
def dummy(e): return e
 
34
N_ = dummy
 
35
 
 
36
# some known keys
 
37
N_("Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>")
 
38
N_("Ubuntu CD Image Automatic Signing Key <cdimage@ubuntu.com>")
 
39
 
 
40
class apt_key:
 
41
    def __init__(self):
 
42
        self.gpg = ["/usr/bin/gpg"]
 
43
        self.base_opt = self.gpg + ["--no-options", "--no-default-keyring",
 
44
                                    "--secret-keyring", "/etc/apt/secring.gpg",
 
45
                                    "--trustdb-name", "/etc/apt/trustdb.gpg",
 
46
                                    "--keyring", "/etc/apt/trusted.gpg"]
 
47
        self.list_opt = self.base_opt + ["--with-colons", "--batch",
 
48
                                         "--list-keys"]
 
49
        self.rm_opt = self.base_opt + ["--quiet", "--batch",
 
50
                                       "--delete-key", "--yes"]
 
51
        self.add_opt = self.base_opt + ["--quiet", "--batch",
 
52
                                        "--import"]
 
53
        
 
54
       
 
55
    def list(self):
 
56
        res = []
 
57
        #print self.list_opt
 
58
        p = subprocess.Popen(self.list_opt,stdout=PIPE).stdout
 
59
        for line in p.readlines():
 
60
            fields = line.split(":")
 
61
            if fields[0] == "pub":
 
62
                name = fields[9]
 
63
                res.append("%s %s\n%s" %((fields[4])[-8:],fields[5], _(name)))
 
64
        return res
 
65
 
 
66
    def add(self, filename):
 
67
        #print "request to add " + filename
 
68
        cmd = self.add_opt[:]
 
69
        cmd.append(filename)
 
70
        p = subprocess.Popen(cmd)
 
71
        return (p.wait() == 0)
 
72
        
 
73
    def update(self):
 
74
        cmd = ["/usr/bin/apt-key", "update"]
 
75
        p = subprocess.Popen(cmd)
 
76
        return (p.wait() == 0)
 
77
 
 
78
    def rm(self, key):
 
79
        #print "request to remove " + key
 
80
        cmd = self.rm_opt[:]
 
81
        cmd.append(key)
 
82
        p = subprocess.Popen(cmd)
 
83
        return (p.wait() == 0)
 
84
 
 
85
class dialog_apt_key:
 
86
  def __init__(self, parent, datadir):
 
87
    # gtk stuff
 
88
    if os.path.exists("../data/SoftwarePropertiesDialogs.glade"):
 
89
      self.gladexml = gtk.glade.XML("../data/SoftwarePropertiesDialogs.glade")
 
90
    else:
 
91
      self.gladexml = gtk.glade.XML("%s/SoftwarePropertiesDialogs.glade", datadir)
 
92
    self.main = self.gladexml.get_widget("dialog_apt_key")
 
93
    self.main.set_transient_for(parent)
 
94
 
 
95
    self.gladexml.signal_connect("on_button_key_add_clicked",
 
96
                                 self.on_button_key_add_clicked)
 
97
    self.gladexml.signal_connect("on_button_key_remove_clicked",
 
98
                                 self.on_button_key_remove_clicked)
 
99
    self.gladexml.signal_connect("on_button_apt_key_update_clicked",
 
100
                                 self.on_button_apt_key_update_clicked)
 
101
 
 
102
    # create apt-key object (abstraction for the apt-key command)
 
103
    self.apt_key = apt_key()
 
104
    
 
105
    # get some widgets
 
106
    self.treeview_apt_key = self.gladexml.get_widget("treeview_apt_key")
 
107
    self.liststore_apt_key = gtk.ListStore(str)
 
108
    self.treeview_apt_key.set_model(self.liststore_apt_key)
 
109
    # Create columns and append them.
 
110
    tr = gtk.CellRendererText()
 
111
    tr.set_property("xpad", 10)
 
112
    tr.set_property("ypad", 10)
 
113
    c0 = gtk.TreeViewColumn("Key", tr, text=0)
 
114
    self.treeview_apt_key.append_column(c0)
 
115
    self.update_key_list()
 
116
 
 
117
  def on_button_apt_key_update_clicked(self, widget):
 
118
      self.apt_key.update()
 
119
      self.update_key_list()
 
120
 
 
121
  def on_button_key_add_clicked(self, widget):
 
122
      chooser = gtk.FileChooserDialog(title=_("Choose a key-file"),
 
123
                                      parent=self.main,
 
124
                                      buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_REJECT,
 
125
                                               gtk.STOCK_OK,gtk.RESPONSE_ACCEPT))
 
126
      res = chooser.run()
 
127
      chooser.hide()
 
128
      if res == gtk.RESPONSE_ACCEPT:
 
129
          #print chooser.get_filename()
 
130
          if not self.apt_key.add(chooser.get_filename()):
 
131
              dialog_error(self.main,
 
132
                    _("Error importing selected file"),
 
133
                    _("The selected file may not be a GPG key file "
 
134
                      "or it might be corrupt."))
 
135
          self.update_key_list()
 
136
          
 
137
  def on_button_key_remove_clicked(self, widget):
 
138
      selection = self.treeview_apt_key.get_selection()
 
139
      (model,a_iter) = selection.get_selected()
 
140
      if a_iter == None:
 
141
          return
 
142
      key = model.get_value(a_iter,0)
 
143
      if not self.apt_key.rm(key[:8]):
 
144
          error(self.main,
 
145
                _("Error removing the key"),
 
146
                _("The key you selected could not be removed. "
 
147
                  "Please report this as a bug."))
 
148
      self.update_key_list()
 
149
 
 
150
  def update_key_list(self):
 
151
      self.liststore_apt_key.clear()
 
152
      for key in self.apt_key.list():
 
153
          self.liststore_apt_key.append([key])
 
154
 
 
155
  def run(self):
 
156
      res = self.main.run()
 
157
      self.main.hide()
 
158
 
 
159
 
 
160
if __name__ == "__main__":
 
161
    ui = dialog_apt_key(None)
 
162
    ui.run()
 
163