55__version__ = "0.9.6.99"
66__version__bak = __version__ # This version is backup for QlibConfig.reset_qlib_version
77import os
8+ import re
89from typing import Union
910from ruamel .yaml import YAML
1011import logging
@@ -80,34 +81,41 @@ def _mount_nfs_uri(provider_uri, mount_path, auto_mount: bool = False):
8081 LOG = get_module_logger ("mount nfs" , level = logging .INFO )
8182 if mount_path is None :
8283 raise ValueError (f"Invalid mount path: { mount_path } !" )
84+ if not re .match (r"^[a-zA-Z0-9.:/\-_]+$" , provider_uri ):
85+ raise ValueError (f"Invalid provider_uri format: { provider_uri } " )
8386 # FIXME: the C["provider_uri"] is modified in this function
8487 # If it is not modified, we can pass only provider_uri or mount_path instead of C
85- mount_command = "sudo mount.nfs %s %s" % ( provider_uri , mount_path )
88+ mount_command = [ "sudo" , " mount.nfs" , provider_uri , mount_path ]
8689 # If the provider uri looks like this 172.23.233.89//data/csdesign'
8790 # It will be a nfs path. The client provider will be used
8891 if not auto_mount : # pylint: disable=R1702
8992 if not Path (mount_path ).exists ():
9093 raise FileNotFoundError (
91- f"Invalid mount path: { mount_path } ! Please mount manually: { mount_command } or Set init parameter `auto_mount=True`"
94+ f"Invalid mount path: { mount_path } ! Please mount manually: { ' ' . join ( mount_command ) } or Set init parameter `auto_mount=True`"
9295 )
9396 else :
9497 # Judging system type
9598 sys_type = platform .system ()
9699 if "windows" in sys_type .lower ():
97100 # system: window
98- exec_result = os .popen (f"mount -o anon { provider_uri } { mount_path } " )
99- result = exec_result .read ()
100- if "85" in result :
101- LOG .warning (f"{ provider_uri } on Windows:{ mount_path } is already mounted" )
102- elif "53" in result :
103- raise OSError ("not find network path" )
104- elif "error" in result or "错误" in result :
105- raise OSError ("Invalid mount path" )
106- elif provider_uri in result :
107- LOG .info ("window success mount.." )
108- else :
109- raise OSError (f"unknown error: { result } " )
110-
101+ try :
102+ subprocess .run (
103+ ["mount" , "-o" , "anon" , provider_uri , mount_path ],
104+ capture_output = True ,
105+ text = True ,
106+ check = True ,
107+ )
108+ LOG .info ("Mount finished." )
109+ except subprocess .CalledProcessError as e :
110+ error_output = (e .stdout or "" ) + (e .stderr or "" )
111+ if e .returncode == 85 :
112+ LOG .warning (f"{ provider_uri } already mounted at { mount_path } " )
113+ elif e .returncode == 53 :
114+ raise OSError ("Network path not found" ) from e
115+ elif "error" in error_output .lower () or "错误" in error_output :
116+ raise OSError ("Invalid mount path" ) from e
117+ else :
118+ raise OSError (f"Unknown mount error: { error_output .strip ()} " ) from e
111119 else :
112120 # system: linux/Unix/Mac
113121 # check mount
@@ -119,12 +127,13 @@ def _mount_nfs_uri(provider_uri, mount_path, auto_mount: bool = False):
119127 _is_mount = False
120128 while _check_level_num :
121129 with subprocess .Popen (
122- ' mount | grep "{}"' . format ( _remote_uri ) ,
123- shell = True ,
130+ [ " mount" ] ,
131+ text = True ,
124132 stdout = subprocess .PIPE ,
125133 stderr = subprocess .STDOUT ,
126134 ) as shell_r :
127135 _command_log = shell_r .stdout .readlines ()
136+ _command_log = [line for line in _command_log if _remote_uri in line ]
128137 if len (_command_log ) > 0 :
129138 for _c in _command_log :
130139 _temp_mount = _c .decode ("utf-8" ).split (" " )[2 ]
@@ -152,16 +161,16 @@ def _mount_nfs_uri(provider_uri, mount_path, auto_mount: bool = False):
152161 if not command_res :
153162 raise OSError ("nfs-common is not found, please install it by execute: sudo apt install nfs-common" )
154163 # manually mount
155- command_status = os . system ( mount_command )
156- if command_status == 256 :
157- raise OSError (
158- f"mount { provider_uri } on { mount_path } error! Needs SUDO! Please mount manually: { mount_command } "
159- )
160- elif command_status == 32512 :
161- # LOG.error("Command error")
162- raise OSError (f"mount { provider_uri } on { mount_path } error! Command error" )
163- elif command_status == 0 :
164- LOG . info ( "Mount finished" )
164+ try :
165+ subprocess . run ( mount_command , check = True , capture_output = True , text = True )
166+ LOG . info ( "Mount finished." )
167+ except subprocess . CalledProcessError as e :
168+ if e . returncode == 256 :
169+ raise OSError ( "Mount failed: requires sudo or permission denied" ) from e
170+ elif e . returncode == 32512 :
171+ raise OSError (f"mount { provider_uri } on { mount_path } error! Command error" ) from e
172+ else :
173+ raise OSError ( f "Mount failed: { e . stderr } " ) from e
165174 else :
166175 LOG .warning (f"{ _remote_uri } on { _mount_path } is already mounted" )
167176
0 commit comments