3
\$\begingroup\$
#!/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 user@10.0.0.100:/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 user@xxxxxxxx.com/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)
\$\endgroup\$
  • \$\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 Dec 14 '13 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 Dec 14 '13 at 18:28
2
\$\begingroup\$

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': 'user@10.0.0.100:/media/Hitachi/',
        'e': 'user@xxxxxxxx.com/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
    
\$\endgroup\$
  • 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 Dec 16 '13 at 6:35
  • \$\begingroup\$ Yikes! Forgot a very important break. Good catch! \$\endgroup\$ – 200_success Dec 16 '13 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 Dec 16 '13 at 7:05
  • \$\begingroup\$ @Joe exit(0) in your case is the equivalent of break \$\endgroup\$ – Erbureth Jan 15 '14 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.