Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. __author__ = 'SakuraCrowd'
  3.  
  4. """
  5. RaspberryPi2(Raspbian) から Windows 7 64 の共有フォルダにマウントする GUI です。
  6. 設定後、 Mount トグルボタンでマウントします。
  7. トグルボタンをOFFにするかウィンドウを閉じるとマウントは解除されます。
  8.  
  9. 次のコマンドで起動できます。
  10. python MountGUI.py
  11. 4つのコマンド引数を与えるとウィンドウの既定値を指定して起動できます。
  12. 4つのコマンド引数は順番に、ローカルマウントフォルダ、リモートフォルダ、ログインユーザ名、パスワードです。
  13. python MountGUI.py ./mount-point //192.168.1.20/shared UserName Password
  14. """
  15.  
  16. import sys
  17. from PySide import QtGui
  18. import subprocess
  19. from subprocess import PIPE, CalledProcessError, check_call, Popen
  20.  
  21. def bash_command(cmd):
  22. """
  23. @brief sh コマンドを実行します。
  24. @param cmd コマンド文字列
  25. @return (成否(True=成功, False=失敗), メッセージ)
  26. """
  27. try:
  28. output = subprocess.check_output(cmd, shell=True, executable='/bin/sh', stderr=subprocess.STDOUT)
  29. except CalledProcessError as e:
  30. return (False, e.output)
  31. return (True, output)
  32.  
  33. class MyWindow(QtGui.QWidget):
  34. def __init__(self,
  35. strMountLocal = './mount-point',
  36. strMountRemote = '//192.168.1.20/shared',
  37. strUserName = 'username',
  38. strPassword = 'password',
  39. strMountOption = '-t cifs '):
  40. QtGui.QWidget.__init__(self)
  41.  
  42. layout1 = QtGui.QVBoxLayout()
  43. self.setLayout(layout1)
  44.  
  45. # ローカルマウントフォルダ
  46. self.labelMountLocal = QtGui.QLabel(self)
  47. self.labelMountLocal.setText('Mount Local Path')
  48. layout1.addWidget(self.labelMountLocal)
  49.  
  50. layout1_1 = QtGui.QHBoxLayout()
  51. layout1.addLayout(layout1_1)
  52.  
  53. self.lineMountLocal = QtGui.QLineEdit(self)
  54. self.lineMountLocal.setText(strMountLocal)
  55. layout1_1.addWidget(self.lineMountLocal)
  56.  
  57. self.buttonOpenMountLocal = QtGui.QPushButton('Open', self)
  58. self.buttonOpenMountLocal.clicked.connect(self.openMountLocal)
  59. layout1_1.addWidget(self.buttonOpenMountLocal)
  60.  
  61. # マウント先のパス入力
  62. self.labelMountRemote = QtGui.QLabel(self)
  63. self.labelMountRemote.setText('Mount Remote Path')
  64. layout1.addWidget(self.labelMountRemote)
  65.  
  66. self.lineMount = QtGui.QLineEdit(self)
  67. self.lineMount.setText(strMountRemote)
  68. layout1.addWidget(self.lineMount)
  69.  
  70. # ユーザ名
  71. self.labelUserName = QtGui.QLabel(self)
  72. self.labelUserName.setText('UserName (Remote PC Login)')
  73. layout1.addWidget(self.labelUserName)
  74.  
  75. self.lineUserName = QtGui.QLineEdit(self)
  76. self.lineUserName.setText(strUserName)
  77. layout1.addWidget(self.lineUserName)
  78.  
  79. # パスワード
  80. self.labelPassword = QtGui.QLabel(self)
  81. self.labelPassword.setText('Password (Remote PC Login)')
  82. layout1.addWidget(self.labelPassword)
  83.  
  84. self.linePassword = QtGui.QLineEdit(self)
  85. self.linePassword.setText(strPassword)
  86. layout1.addWidget(self.linePassword)
  87.  
  88. # オプション
  89. self.labelMountOption = QtGui.QLabel(self)
  90. self.labelMountOption.setText('Option (mount command)')
  91. layout1.addWidget(self.labelMountOption)
  92.  
  93. self.lineOption = QtGui.QLineEdit(self)
  94. self.lineOption.setText(strMountOption)
  95. layout1.addWidget(self.lineOption)
  96.  
  97. # Mount トグルボタン
  98. self.buttonMount = QtGui.QPushButton('Mount', self)
  99. self.buttonMount.setCheckable(True)
  100. self.buttonMount.clicked[bool].connect(self.mountTargetFolder)
  101. layout1.addWidget(self.buttonMount)
  102.  
  103. # mount で使うフォルダがなければ作成する。
  104. cmdMkdir = "sudo mkdir -p " + self.lineMountLocal.text()
  105. print cmdMkdir
  106. print bash_command(cmdMkdir)
  107.  
  108. return
  109.  
  110.  
  111.  
  112. def closeEvent(self, e):
  113. # マウントしていればそれを解除する
  114. print "Destructor called"
  115. if self.buttonMount.isChecked() == True:
  116. print "mountTargetFolder call"
  117. self.mountTargetFolder(False)
  118. return
  119.  
  120. def mountTargetFolder(self, flgMount):
  121. """
  122. @param flgMount True ならマウントする。 False なら解除する。
  123. """
  124. if flgMount == True :
  125. cmdMount ="sudo mount " + self.lineOption.text() + " -o " + "username=" + self.lineUserName.text() + ",password=" + self.linePassword.text() + " " + self.lineMount.text() + " " + self.lineMountLocal.text()
  126. print cmdMount
  127. print bash_command(cmdMount)
  128. else:
  129. cmdUmount = "sudo umount " + self.lineMountLocal.text()
  130. print cmdUmount
  131. print bash_command(cmdUmount)
  132. return
  133.  
  134. def openMountLocal(self):
  135. """
  136. ファイルマネージャのウィンドウを開く
  137. """
  138. cmd = "pcmanfm " + self.lineMountLocal.text()
  139. print cmd
  140. print bash_command(cmd)
  141. return
  142.  
  143.  
  144. app = QtGui.QApplication(sys.argv)
  145. argc = len(sys.argv)
  146.  
  147. if argc == 5:
  148. win = MyWindow(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
  149. else:
  150. win = MyWindow()
  151.  
  152. win.resize(320, 240)
  153. win.setWindowTitle("MountGUI")
  154.  
  155. win.show()
  156.  
  157. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement