\$\begingroup\$
\$\endgroup\$
2
#!/usr/bin/python3
# Description: Python script that will mount FTP-Server via SSHFS protocol
import sys
import os
import subprocess
def mount():
#Function for mounting the networked drive
if os.path.exists('/home/user/MNT/MEDIA/'):
print('Network drive is mounted')
un = input("To unmount type U/u \n")
if un == "u" or un == "U":
subprocess.call(['sudo fusermount -u /home/user/MNT/'], shell=True)
else:
pass
exit(0)
if not os.path.exists('/home/user/MNT/MEDIA/'):
IP = input("Type I/i for local connection, E/e for external\n")
if IP == "i" or IP == "I":
subprocess.call(['sudo sshfs -o idmap=user -o uid=1000 -o gid=1000 -o allow_other -o default_permissions -p 100 [email protected]:/media/Hitachi/ /home/user/MNT'], shell=True)
if IP == "e" or IP == "E":
subprocess.call(['sudo sshfs -o idmap=user -o uid=1000 -o gid=1000 -o allow_other -o default_permissions -p 100 [email protected]/media/Hitachi/ /home/user/MNT'], shell=True)
elif IP == "": #returns if null value
return mount()
def makedir():
#Function to check and see if directory is created and if not create it
if not os.path.exists('/home/user/MNT/'):
subprocess.call(['sudo mkdir /home/user/MNT/'], shell=True)
if os.path.isdir('/home/user/MNT/'):
mount()
makedir()
EDITED:
def sshfs():
#function to mount network drive
while True:
user = input("Type 'i' for internal, 'e' for external connection\n")
fs = File_System.get(user.lower()) #Get value from the dict called File_System
if fs is None:
print("Invalid option, enter either 'i' for internal or 'e' for external\n")
else:
subprocess.call(["sudo sshfs -o uid=1000 -o gid=1000 -o idmap=user -o default_permissions -o allow_other -p 100 %s %s" % (fs, Local_Mount)], shell=True)
exit(0)
asked Dec 14, 2013 at 15:31
-
\$\begingroup\$ Please provide some more context. Looking at the code tells us one what you are actually doing, but having a general description of what you want to do may give some opportunity to provide alternative suggestions too. \$\endgroup\$rolfl– rolfl2013年12月14日 16:40:15 +00:00Commented Dec 14, 2013 at 16:40
-
\$\begingroup\$ Just trying to make a script to use with all my linux machines to mount to a centralized server \$\endgroup\$Joseph– Joseph2013年12月14日 18:28:01 +00:00Commented Dec 14, 2013 at 18:28
1 Answer 1
\$\begingroup\$
\$\endgroup\$
4
Efficiency is just not an issue in this kind of task. As "glue" code, it's not computationally intensive. There are, however, some maintainability issues.
In
mount()
, the basic outline is:def mount(): if os.path.exists('/home/user/MNT/MEDIA/'): ... exit(0) if not os.path.exists('/home/user/MNT/MEDIA/'): ...
... which should be expressed as:
def mount_or_unmount(): if os.path.exists('/home/user/MNT/MEDIA/'): ... else: ...
- Using recursion for error handling is weird. That would normally be handled using a while-loop. It's also slightly odd that the empty string triggers a retry, but any other invalid input would exit the script.
The two
sudo sshfs
commands differ only slightly. Your code should be structured accordingly.REMOTE_FS = { 'i': '[email protected]:/media/Hitachi/', 'e': '[email protected]/media/Hitachi/', } ... while True: ip = input("Type I/i for local connection, E/e for external\n") fs = REMOTE_FS.get(ip.lower()) if fs is None: # Input was not 'i' or 'e' continue subprocess.call(['sudo sshfs -o idmap=user -o uid=1000 -o gid=1000 -o allow_other -o default_permissions -p 100 %s /home/user/MNT' % (fs)], shell=True) break
answered Dec 15, 2013 at 10:30
-
1\$\begingroup\$ Awesome, I reedited my code and I learned a lot, especially with reducing 'glue' code. However I have one question and I need it dumbed down while True: Till what is true? Why/How does the loop end? \$\endgroup\$Joseph– Joseph2013年12月16日 06:35:16 +00:00Commented Dec 16, 2013 at 6:35
-
\$\begingroup\$ Yikes! Forgot a very important
break
. Good catch! \$\endgroup\$200_success– 200_success2013年12月16日 06:52:39 +00:00Commented Dec 16, 2013 at 6:52 -
\$\begingroup\$ I did it slightly different. I'm still confused as to why the loop breaks even without the break statement?: \$\endgroup\$Joseph– Joseph2013年12月16日 07:05:22 +00:00Commented Dec 16, 2013 at 7:05
-
\$\begingroup\$ @Joe
exit(0)
in your case is the equivalent ofbreak
\$\endgroup\$Erbureth– Erbureth2014年01月15日 09:09:13 +00:00Commented Jan 15, 2014 at 9:09
lang-py