~ubuntu-branches/ubuntu/vivid/youker-assistant/vivid

« back to all changes in this revision

Viewing changes to backends/youker-assistant-daemon/src/softwarecenter/apt_daemon.py

  • Committer: Package Import Robot
  • Author(s): Jack Yu
  • Date: 2013-09-02 23:17:45 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130902231745-psyl14qxslg1g3dp
Tags: 0.1.6-0ubuntu1
* Modify SystemDispatcher and SessionDispatcher Classes.
* Add watch file into debian folder.
* Resolve status of onekey button(LP #1216236 ).
* Add monitorball code into systemdbus daemon.
* Optimize the status button.
* Add fcitx configuration function.
* Fix bugs due to bad references to apt lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
### BEGIN LICENSE
 
4
 
 
5
# Copyright (C) 2013 National University of Defense Technology(NUDT) & Kylin Ltd
 
6
# This program is free software: you can redistribute it and/or modify it
 
7
# under the terms of the GNU General Public License version 3, as published
 
8
# by the Free Software Foundation.
 
9
#
 
10
# This program is distributed in the hope that it will be useful, but
 
11
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
13
# PURPOSE.  See the GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along
 
16
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
### END LICENSE
 
18
 
 
19
import apt
 
20
import apt_pkg
 
21
import sys, os
 
22
import apt.progress.base as apb
 
23
 
 
24
class FetchProcess(apb.AcquireProgress):
 
25
        '''Fetch Process'''
 
26
        def __init__(self):
 
27
                apb.AcquireProgress.__init__(self)
 
28
 
 
29
        def done(self, item):
 
30
                print 'done la done la !!!'
 
31
 
 
32
        def fail(self, item):
 
33
                print 'fail la fail la !!!'
 
34
 
 
35
        def fetch(self, item):
 
36
                print 'fetch la fetch la !!!'
 
37
 
 
38
        def ims_hit(self, item):
 
39
                print 'ims_hit la !!!'
 
40
 
 
41
        def media_change(self, media, drive):
 
42
                print 'media change la !!!'
 
43
 
 
44
        def pulse(self, owner):
 
45
#               print 'pulse pulse pluse ...', owner
 
46
                print 'current_bytes: ', self.current_bytes
 
47
                print 'current_cps: ', self.current_cps
 
48
                print 'current_items: ', self.current_items
 
49
                print 'elapsed_time: ', self.elapsed_time
 
50
                print 'fetched_bytes: ', self.fetched_bytes
 
51
                print 'last_bytes: ', self.last_bytes
 
52
                print 'total_bytes: ', self.total_bytes
 
53
                print 'total_items: ', self.total_items
 
54
 
 
55
        def start(self):
 
56
                print 'fetch progress is start ...'
 
57
 
 
58
        def stop(self):
 
59
                print 'fetch progress is stop ...'
 
60
 
 
61
 
 
62
class AptProcess(apb.InstallProgress):
 
63
    '''Install progress'''
 
64
    def __init__(self):
 
65
        apb.InstallProgress.__init__(self)
 
66
 
 
67
    def conffile(self, current, new):
 
68
        # global_event.emit("action-conffile", (current, new))
 
69
 
 
70
        # log("conffile: %s %s" % (current, new))
 
71
        print 'conffile wo ca!'
 
72
        
 
73
    def error(self, errorstr):
 
74
        # global_event.emit("action-error", (self.pkg_name, errorstr))
 
75
        # log("error: %s" % errorstr)
 
76
        print 'ERROR :' + errorstr
 
77
 
 
78
    def start_update(self):
 
79
        '''Start update.'''
 
80
        # log("start action...")
 
81
        print 'kai shi le kaishile !!!'
 
82
        
 
83
    def status_change(self, pkg, percent, status):
 
84
        '''Progress status change.'''
 
85
        # global_event.emit("action-update", (self.pkg_name, self.action_type, int(percent), status))
 
86
        # log((self.pkg_name, self.action_type, int(percent), status))
 
87
        print '\n' + str(int(percent)) + "%  zhuangtai: " + status
 
88
 
 
89
class AptDaemon:
 
90
        def __init__(self):
 
91
                self.ca = apt.Cache()
 
92
#               self.ca.update()
 
93
#               self.ca.open()
 
94
                self.pkgList = []
 
95
                self.pkgNameList = []
 
96
                for pkg in self.ca:
 
97
                        self.pkgList.append(pkg)
 
98
                        self.pkgNameList.append(pkg.name)
 
99
 
 
100
        def get_pkg_by_name(self, pkgname):
 
101
                return self.ca[pkgname]
 
102
 
 
103
        def install_pkg(self, pkgname):
 
104
                self.ca.open()
 
105
                pkg = self.get_pkg_by_name(pkgname)
 
106
                pkg.mark_install()
 
107
                
 
108
                try: 
 
109
                        self.ca.commit(FetchProcess(), AptProcess())
 
110
                except Exception, e:
 
111
                        print e
 
112
                        print "install err"
 
113
 
 
114
        def uninstall_pkg(self, pkgname):
 
115
                self.ca.open()
 
116
                pkg = self.get_pkg_by_name(pkgname)
 
117
                pkg.mark_delete()
 
118
                
 
119
                try:
 
120
                        self.ca.commit(None, AptProcess())
 
121
                except Exception, e:
 
122
                        print e
 
123
                        print "uninstall err"
 
124
 
 
125
        def update_pkg(self, pkgname):
 
126
                self.ca.open()
 
127
                pkg = self.get_pkg_by_name(pkgname)
 
128
                pkg.mark_update()
 
129
                
 
130
                try:
 
131
                        self.ca.commit(FetchProcess(), AptProcess())
 
132
                except Exception, e:
 
133
                        print e
 
134
                        print "update err"
 
135
 
 
136
        def get_pkgs_name_list(self):
 
137
                return self.pkgNameList
 
138
 
 
139
        def search_pkgs_name(self, pkgname):
 
140
                if pkgname in self.pkgNameList:
 
141
                        return pkgname
 
142
                else:
 
143
                        rtns = []
 
144
                        for name in self.pkgNameList:
 
145
                                if name.find(pkgname) >= 0:
 
146
                                        rtns.append(name)
 
147
                        return rtns
 
148
 
 
149
if __name__ == "__main__":
 
150
        ad = AptDaemon()
 
151
        while True:
 
152
                print "\ninput your command: "
 
153
                cmd = raw_input()
 
154
                if cmd == "l":
 
155
                        for name in ad.pkgNameList:
 
156
                                print name + "\n"
 
157
                elif cmd == "i":
 
158
                        print "input pkgName to install: "
 
159
                        pkgName = raw_input()
 
160
                        ad.install_pkg(pkgName)
 
161
                elif cmd == "n":
 
162
                        print "input pkgName to uninstall: "
 
163
                        pkgName = raw_input()
 
164
                        ad.uninstall_pkg(pkgName)
 
165
                elif cmd == "u":
 
166
                        print "input pkgName to update: "
 
167
                        pkgName = raw_input()
 
168
                        ad.update_pkg(pkgName)
 
169
                else:
 
170
                        print "nothing..."
 
171
 
 
172
        print ad.get_pkg_by_name('gedit')
 
173
        # pnl = ad.getpkglist()
 
174
        # print len(pnl)
 
175
#       name1 = ad.search_pkgs_name('wesnoth-1.10-core')
 
176
#       print name1
 
177
        # print 'aaa' + str(1)
 
178
#       ad.install_pkg(name1)
 
179
#       ad.uninstall_pkg(name1)
 
180
        # p = ad.get_pkg_by_name(name1)
 
181
        # print p.id
 
182
        # c = AptCache()
 
183
        # c.hahaha()
 
184
        # print c.hahaha()
 
185
        # pkgs = []
 
186
        # ca = apt.Cache()
 
187
        # i = 0
 
188
        # for a in ca:
 
189
        #       i += 1
 
190
        #       pkgs.append(a.name)
 
191
                # print a.name
 
192
        # print i
 
193
        # nanop = ca['nano']
 
194
        # print nanop
 
195
        # nanop.mark_install()
 
196
        # ca.commit()
 
 
b'\\ No newline at end of file'