Skip to content

Instantly share code, notes, and snippets.

@sakura-crowd
Created February 25, 2016 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakura-crowd/8dac6531afcdf1752d3d to your computer and use it in GitHub Desktop.
Save sakura-crowd/8dac6531afcdf1752d3d to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
__author__ = 'SakuraCrowd'
"""
RaspberryPi2(Raspbian) から Windows 7 64 の共有フォルダにマウントする GUI です。
設定後、 Mount トグルボタンでマウントします。
トグルボタンをOFFにするかウィンドウを閉じるとマウントは解除されます。
次のコマンドで起動できます。
python MountGUI.py
4つのコマンド引数を与えるとウィンドウの既定値を指定して起動できます。
4つのコマンド引数は順番に、ローカルマウントフォルダ、リモートフォルダ、ログインユーザ名、パスワードです。
python MountGUI.py ./mount-point //192.168.1.20/shared UserName Password
"""
import sys
from PySide import QtGui
import subprocess
from subprocess import PIPE, CalledProcessError, check_call, Popen
def bash_command(cmd):
"""
@brief sh コマンドを実行します。
@param cmd コマンド文字列
@return (成否(True=成功, False=失敗), メッセージ)
"""
try:
output = subprocess.check_output(cmd, shell=True, executable='/bin/sh', stderr=subprocess.STDOUT)
except CalledProcessError as e:
return (False, e.output)
return (True, output)
class MyWindow(QtGui.QWidget):
def __init__(self,
strMountLocal = './mount-point',
strMountRemote = '//192.168.1.20/shared',
strUserName = 'username',
strPassword = 'password',
strMountOption = '-t cifs '):
QtGui.QWidget.__init__(self)
layout1 = QtGui.QVBoxLayout()
self.setLayout(layout1)
# ローカルマウントフォルダ
self.labelMountLocal = QtGui.QLabel(self)
self.labelMountLocal.setText('Mount Local Path')
layout1.addWidget(self.labelMountLocal)
layout1_1 = QtGui.QHBoxLayout()
layout1.addLayout(layout1_1)
self.lineMountLocal = QtGui.QLineEdit(self)
self.lineMountLocal.setText(strMountLocal)
layout1_1.addWidget(self.lineMountLocal)
self.buttonOpenMountLocal = QtGui.QPushButton('Open', self)
self.buttonOpenMountLocal.clicked.connect(self.openMountLocal)
layout1_1.addWidget(self.buttonOpenMountLocal)
# マウント先のパス入力
self.labelMountRemote = QtGui.QLabel(self)
self.labelMountRemote.setText('Mount Remote Path')
layout1.addWidget(self.labelMountRemote)
self.lineMount = QtGui.QLineEdit(self)
self.lineMount.setText(strMountRemote)
layout1.addWidget(self.lineMount)
# ユーザ名
self.labelUserName = QtGui.QLabel(self)
self.labelUserName.setText('UserName (Remote PC Login)')
layout1.addWidget(self.labelUserName)
self.lineUserName = QtGui.QLineEdit(self)
self.lineUserName.setText(strUserName)
layout1.addWidget(self.lineUserName)
# パスワード
self.labelPassword = QtGui.QLabel(self)
self.labelPassword.setText('Password (Remote PC Login)')
layout1.addWidget(self.labelPassword)
self.linePassword = QtGui.QLineEdit(self)
self.linePassword.setText(strPassword)
layout1.addWidget(self.linePassword)
# オプション
self.labelMountOption = QtGui.QLabel(self)
self.labelMountOption.setText('Option (mount command)')
layout1.addWidget(self.labelMountOption)
self.lineOption = QtGui.QLineEdit(self)
self.lineOption.setText(strMountOption)
layout1.addWidget(self.lineOption)
# Mount トグルボタン
self.buttonMount = QtGui.QPushButton('Mount', self)
self.buttonMount.setCheckable(True)
self.buttonMount.clicked[bool].connect(self.mountTargetFolder)
layout1.addWidget(self.buttonMount)
# mount で使うフォルダがなければ作成する。
cmdMkdir = "sudo mkdir -p " + self.lineMountLocal.text()
print cmdMkdir
print bash_command(cmdMkdir)
return
def closeEvent(self, e):
# マウントしていればそれを解除する
print "Destructor called"
if self.buttonMount.isChecked() == True:
print "mountTargetFolder call"
self.mountTargetFolder(False)
return
def mountTargetFolder(self, flgMount):
"""
@param flgMount True ならマウントする。 False なら解除する。
"""
if flgMount == True :
cmdMount ="sudo mount " + self.lineOption.text() + " -o " + "username=" + self.lineUserName.text() + ",password=" + self.linePassword.text() + " " + self.lineMount.text() + " " + self.lineMountLocal.text()
print cmdMount
print bash_command(cmdMount)
else:
cmdUmount = "sudo umount " + self.lineMountLocal.text()
print cmdUmount
print bash_command(cmdUmount)
return
def openMountLocal(self):
"""
ファイルマネージャのウィンドウを開く
"""
cmd = "pcmanfm " + self.lineMountLocal.text()
print cmd
print bash_command(cmd)
return
app = QtGui.QApplication(sys.argv)
argc = len(sys.argv)
if argc == 5:
win = MyWindow(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
else:
win = MyWindow()
win.resize(320, 240)
win.setWindowTitle("MountGUI")
win.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment