Advertisement
Guest User

Untitled

a guest
Oct 24th, 2010
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.24 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. ### Pacman database informer 2.1
  5. ### by mangus
  6. ### mangus@deprecated.it
  7. ###The ARCHLINUX name and logo are recognized trademarks
  8. ###See http://www.archlinux.org for acceptable use and restrictions.
  9.  
  10. import sys
  11. import os
  12. import subprocess
  13. from PyQt4 import QtGui, QtCore
  14. from mainform import Ui_MainWindow
  15.  
  16. class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
  17.         def __init__(self,parent=None):
  18.                 super(MainWindow, self).__init__(parent)
  19.                 self.setupUi(self)
  20.                 self.statusbar.showMessage('Pacman database Informer')
  21.                 self.lineEdit.backspace()
  22.                 self.lineEdit.setPlaceholderText('Insert complete PATH - pacman -Qo')
  23.                 list=self.findRepos()
  24.                 self.comboBox.addItems(list)
  25.                 self.comboBox.insertSeparator((self.comboBox.count())-3)
  26.                 self.style()
  27.                 #signals
  28.                 self.pushButton_5.clicked.connect(self.clearAll)
  29.                 self.pushButton.clicked.connect(self.searchPkg)
  30.                 self.actionRefresh.triggered.connect(self.clearAll)
  31.                 self.comboBox.activated[str].connect(self.listRepo)
  32.                 self.connect(self.actionExit, QtCore.SIGNAL("triggered()"), self.quit)
  33.                 self.connect(self.lineEdit,QtCore.SIGNAL("returnPressed ()"),self.searchPkg)
  34.  
  35.         def quit(self):
  36.                 QtGui.QApplication.quit()
  37.  
  38.         def closeEvent(self, event):  ##reimplemented qt function
  39.                 self.hide()  ##always hide the mainwindow
  40.                 event.ignore()
  41.  
  42.         def style(self):
  43.                 self.textEdit.setStyleSheet ("background-image:url('pixmaps/sticker-arch2.png'); \
  44.                                              background-repeat: no repeat; background-position:center; \
  45.                                             ")
  46.                 self.textEdit_2.setStyleSheet ("background-image:url('pixmaps/sticker-arch2.png'); \
  47.                                                background-repeat: no repeat; background-position:center; \
  48.                                               ")
  49.                 self.textEdit_3.setStyleSheet ("background-image:url('pixmaps/sticker-arch2.png'); \
  50.                                                background-repeat: no repeat; background-position:center; \
  51.                                               ")
  52.                 self.textEdit_4.setStyleSheet ("background-image:url('pixmaps/sticker-arch2.png'); \
  53.                                                background-repeat: no repeat; background-position:center; \
  54.                                               ")
  55.  
  56.         def removeStyle(self):
  57.                 self.textEdit.setStyleSheet ("background-image:url('');")
  58.                 self.textEdit_2.setStyleSheet ("background-image:url('');")
  59.                 self.textEdit_3.setStyleSheet ("background-image:url('');")
  60.                 self.textEdit_4.setStyleSheet ("background-image:url('');")
  61.  
  62.         def clearAll(self):
  63.                 list=self.findRepos()
  64.                 self.comboBox.addItems(list)
  65.                 self.comboBox.insertSeparator((self.comboBox.count())-3)
  66.                 self.listWidget.clear()
  67.                 self.textEdit.clear()
  68.                 self.textEdit_2.clear()
  69.                 self.textEdit_3.clear()
  70.                 self.textEdit_4.clear()
  71.                 self.lineEdit.clear()
  72.                 self.comboBox.setCurrentIndex(0)
  73.                 self.tabWidget.setCurrentWidget(self.tab_3)
  74.                 self.style()
  75.  
  76.         def listRepo(self,repo):
  77.                 self.listWidget.clear()
  78.                 self.textEdit.clear()
  79.                 self.style()
  80.                 self.textEdit_2.clear()
  81.                 self.textEdit_3.clear()
  82.                 self.textEdit_4.clear()
  83.  
  84.                 if repo == 'Orphans - pacman -Qdt':
  85.                   cmd='pacman -Qdt'
  86.                   barstring=' orphans packages'
  87.                   self.listPackages(cmd,barstring)
  88.                 elif repo == 'Foreign - pacman -Qm':
  89.                   cmd='pacman -Qm'
  90.                   barstring=' foreign packages'
  91.                   self.listPackages(cmd,barstring)
  92.                 else:
  93.                   if repo == 'Stable - All But Testing':
  94.                       repos = [i for i in self.findRepos()[1:-3] if 'testing' not in i]
  95.                       print repos
  96.                   elif repo == 'All':
  97.                       repos = self.findRepos()[1:-3]
  98.                       print repos
  99.                   else:
  100.                     repos = [repo]
  101.                     print repos
  102.                   for repo in repos:
  103.                     cmd='paclist '+ str(repo)
  104.                    # barstring=str(repo) +' packages'
  105.                     barstring=' packages'
  106.                     self.listPackages(cmd,barstring)
  107.  
  108.         def listPackages(self,cmd,barstring):
  109.                 for i in os.popen(cmd,"r"):
  110.                     item = QtGui.QListWidgetItem(i.strip())
  111.                     item.setIcon(QtGui.QIcon('pixmaps/tgz.png'))
  112.                     self.listWidget.addItem(item)
  113.                 c=self.listWidget.count()
  114.                 self.statusbar.showMessage(str(c) + ' ' + barstring)
  115.  
  116.         def findRepos(self):
  117.                 list= ['All']
  118.                 f = open('/etc/pacman.conf','r')
  119.                 for line in f.readlines():
  120.                     if line[0] == '[':  ## this may be ugly....
  121.                         a=line.split(' ')[0].rstrip("\n")[1:-1]
  122.                         list.append(a)
  123.                 f.close()
  124.                 del list[1]  ###removes '[options]' from pacman.conf parsed with the repos
  125.                 list.append('Orphans - pacman -Qdt')
  126.                 list.append('Foreign - pacman -Qm')
  127.                 list.append('Stable - All But Testing')
  128.                 return list
  129.  
  130.         def on_listWidget_itemClicked(self, item):
  131.                 ### first tab info
  132.                 # clean and set first tab
  133.                 self.removeStyle()
  134.                 self.textEdit.clear()
  135.                 self.textEdit_2.clear()
  136.                 self.textEdit_3.clear()
  137.                 self.textEdit_4.clear()
  138.                 self.tabWidget.setCurrentWidget(self.tab_3)
  139.                 p=str(item.text()).split(" ")[0]
  140.                 cmd="pacman -Qi " + p
  141.                 a=os.popen(cmd).read().decode("utf-8")
  142.                 self.textEdit.setText(a)
  143.  
  144.                 ### second tab files
  145.                 self.textEdit_2.clear()
  146.                 a=self.listWidget.selectedItems()[0]
  147.                 p=str(a.text()).split(" ")[0]
  148.                 cmd="pacman -Ql " + p
  149.                 l=os.popen(cmd).read()
  150.                 self.textEdit_2.setText(l)
  151.  
  152.                 ### third tab scripts
  153.                 self.textEdit_3.clear()
  154.                 a=self.listWidget.selectedItems()[0]
  155.                 p=str(a.text()).split(" ")[0]
  156.                 cmd="pacscripts " + p
  157.                 r = subprocess.Popen(cmd, shell=True ,stdout=subprocess.PIPE, stdin=subprocess.PIPE,  stderr=subprocess.PIPE)
  158.                 o=r.stdout.read().decode( "utf-8")
  159.                 u=r.stderr.read().decode( "utf-8")
  160.                 self.textEdit_3.setText(str(o)+str(u))
  161.  
  162.                 ### fourth tab deps tree
  163.                 self.textEdit_4.clear()
  164.                 a=self.listWidget.selectedItems()[0]
  165.                 p=str(a.text()).split(" ")[0]
  166.                 app.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
  167.                 self.statusbar.showMessage('Loading deps tree')
  168.                 cmd="pactree " + p
  169.                 l=os.popen(cmd).read()
  170.                 self.textEdit_4.setText(l)
  171.                 self.statusbar.showMessage('Done')
  172.                 app.restoreOverrideCursor()
  173.  
  174.         def searchPkg(self):
  175.                 self.listWidget.clear()
  176.                 self.tabWidget.setCurrentWidget(self.tab_3)
  177.                 self.textEdit.clear()
  178.                 a=self.lineEdit.displayText().toUtf8()
  179.                 b=str(a)
  180.                 if not b:
  181.                     self.textEdit.clear()
  182.                     self.removeStyle()
  183.                     self.textEdit.setText('No file specified.')
  184.                     return
  185.                 cmd="pacman -Qo " + str(a)
  186.                 app.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
  187.                 a=os.popen(cmd).readlines()
  188.                 if not a:
  189.                     self.textEdit.clear()
  190.                     self.removeStyle()
  191.                     self.textEdit.setText('No such file or directory.')
  192.                     app.restoreOverrideCursor()
  193.                     return
  194.  
  195.                 for l in os.popen(cmd):
  196.                     r=l.decode( "utf-8")
  197.                     item = QtGui.QListWidgetItem(r.strip().split(" ")[4] + ' ' +r.strip().split(" ")[5])
  198.                     item.setIcon(QtGui.QIcon('pixmaps/tgz.png'))
  199.                     self.listWidget.addItem(item)
  200.                 self.statusbar.showMessage('Done.')
  201.                 app.restoreOverrideCursor()
  202.  
  203.         @QtCore.pyqtSlot()
  204.         def on_actionAbout_triggered(self):
  205.               QtGui.QMessageBox.about(self ,"Pacman database Informer" , 'Pacman database Informer 2.1 by mangus \n \n'
  206.                                             'A little tool for little queries to the pacman database \n'
  207.                                             'mangus@deprecated.it \n \n The ARCHLINUX name and'
  208.                                             'logo are recognized trademarks \n'
  209.                                             'See http://www.archlinux.org/ for acceptable use and restrictions.')
  210.  
  211.         @QtCore.pyqtSlot()
  212.         def on_actionAbout_Qt_triggered(self):
  213.               QtGui.QMessageBox.aboutQt(self)
  214.  
  215.         def on_actionArchlinux_org_triggered(self):
  216.               QtGui.QDesktopServices().openUrl(QtCore.QUrl('http://www.archlinux.org'))
  217.  
  218. class SystemTrayIcon(QtGui.QSystemTrayIcon):
  219.     def __init__(self, icon, parent=None):
  220.         QtGui.QSystemTrayIcon.__init__(self, icon, parent)
  221.         menu = QtGui.QMenu(parent)
  222.         # Actions
  223.         self.actionQuit= QtGui.QAction(QtGui.QIcon('pixmaps/application-exit.png'),"Exit", self)
  224.         menu.addAction(self.actionQuit)
  225.         # Connect menu with signals
  226.         self.connect(self.actionQuit, QtCore.SIGNAL("triggered()"), self.quit)
  227.         # Other signals
  228.         traySignal = "activated(QSystemTrayIcon::ActivationReason)"
  229.         QtCore.QObject.connect(self, QtCore.SIGNAL(traySignal), self.icon_activated)
  230.         # Create Menu
  231.         self.setContextMenu(menu)
  232.  
  233.     def quit(self):
  234.         QtGui.QApplication.quit()
  235.  
  236.     def icon_activated(self, reason):
  237.         if reason == QtGui.QSystemTrayIcon.Trigger:
  238.             mainwindow.show()
  239.  
  240. def main():
  241.       global app
  242.       app = QtGui.QApplication(sys.argv)
  243.       # TrayIcon
  244.       if QtGui.QSystemTrayIcon.isSystemTrayAvailable():
  245.           w = QtGui.QWidget()
  246.           icon = QtGui.QIcon("pixmaps/archlinux-icon-crystal-64.svg")
  247.           trayIcon = SystemTrayIcon(icon, w)
  248.           trayIcon.show()
  249.           trayIcon.setToolTip("pdi - pacman database informer")
  250.       # Main Window
  251.       global mainwindow
  252.       mainwindow=MainWindow()
  253.       mainwindow.show()
  254.       sys.exit(app.exec_())
  255.  
  256. if __name__ == "__main__":
  257.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement