14
14
15
15
16
16
def mv_file (origin , destination , use_sudo = False , sudo_password = "" ):
17
+ # Fast fail if invalid origin/destinations passed in
18
+ if origin is None or destination is None :
19
+ raise RuntimeError (f"File Moving: Invalid file paths passed in [origin={ origin } , destination={ destination } ]" )
20
+
21
+ # Fast return if there is no need for the operation
22
+ if is_moved (origin , destination ):
23
+ return
24
+
25
+ # Fail fast if a destination file already exists
26
+ if os .path .isfile (destination ) or os .path .islink (destination ):
27
+ raise RuntimeError (f"File Moving: Destination file already exists [origin={ origin } , destination={ destination } ]" )
28
+
17
29
if use_sudo :
18
30
command = f"mv { origin } { destination } "
19
31
process = subprocess .Popen (['sudo' , '-S' ] + command .split (), stdin = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True )
@@ -34,6 +46,10 @@ def mv_file(origin, destination, use_sudo=False, sudo_password=""):
34
46
35
47
36
48
def rm_file (file , use_sudo = False , sudo_password = "" ):
49
+ # Fast fail if invalid file is passed in
50
+ if file is None :
51
+ raise RuntimeError (f"File Removing: Invalid file passed in [file={ file } ]" )
52
+
37
53
# Fast return if there is no need for the operation
38
54
if not os .path .isfile (file ) and not is_broken_link (file ):
39
55
return
@@ -55,10 +71,18 @@ def rm_file(file, use_sudo=False, sudo_password=""):
55
71
56
72
57
73
def copy_file (origin , destination , use_sudo = False , sudo_password = "" ):
74
+ # Fast fail if invalid origin/destinations passed in
75
+ if origin is None or destination is None :
76
+ raise RuntimeError (f"File Copying: Invalid symlinking file paths passed in [origin={ origin } , destination={ destination } ]" )
77
+
58
78
# Fast return if there is no need for the operation
59
79
if is_copied (origin , destination ):
60
80
return
61
81
82
+ # Fast fail if the file already exists
83
+ if os .path .isfile (destination ) or os .path .islink (destination ):
84
+ raise RuntimeError (f"File Copying: Destination file already exists [origin={ origin } , destination={ destination } ]" )
85
+
62
86
if use_sudo :
63
87
command = f"cp { origin } { destination } "
64
88
process = subprocess .Popen (['sudo' , '-S' ] + command .split (), stdin = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True )
@@ -79,10 +103,18 @@ def copy_file(origin, destination, use_sudo=False, sudo_password=""):
79
103
80
104
81
105
def symlink_file (origin , destination , use_sudo = False , sudo_password = "" ):
106
+ # Fast fail if invalid origin/destinations passed in
107
+ if origin is None or destination is None :
108
+ raise RuntimeError (f"File Symlinking: Invalid symlinking file paths passed in [origin={ origin } , destination={ destination } ]" )
109
+
82
110
# Fast return if there is no need for the operation
83
111
if is_linked (origin , destination ):
84
112
return
85
113
114
+ # Fast fail if the file already exists
115
+ if os .path .isfile (destination ) or os .path .islink (destination ):
116
+ raise RuntimeError (f"File Symlinking: Destination file already exists [origin={ origin } , destination={ destination } ]" )
117
+
86
118
if use_sudo :
87
119
command = f"ln -s { origin } { destination } "
88
120
process = subprocess .Popen (['sudo' , '-S' ] + command .split (), stdin = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True )
@@ -102,38 +134,34 @@ def symlink_file(origin, destination, use_sudo=False, sudo_password=""):
102
134
os .symlink (origin , destination )
103
135
104
136
105
- def unsymlink_file (origin , destination , use_sudo = False , sudo_password = "" ):
106
- # Fast return if there is no need for the operation
107
- if not is_linked ( origin , destination ):
108
- return
137
+ def unsymlink_file (file , use_sudo = False , sudo_password = "" ):
138
+ # Fast fail if invalid file name or type passed in
139
+ if file is None or not os . path . islink ( file ):
140
+ raise RuntimeError ( f"File Unsymlinking: File does not exist or is a symlink [file= { file } ]" )
109
141
110
142
if use_sudo :
111
- command = f"unlink { destination } "
143
+ command = f"unlink { file } "
112
144
process = subprocess .Popen (['sudo' , '-S' ] + command .split (), stdin = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True )
113
145
114
146
try :
115
147
stdout , stderr = process .communicate (sudo_password + '\n ' , timeout = 3 )
116
148
117
149
if "File exists" in stderr :
118
- raise FileExistsError (f"The file { destination } already exists" )
150
+ raise FileExistsError (f"The file { file } already exists" )
119
151
120
152
if process .returncode != 0 :
121
153
raise RuntimeError (stderr )
122
154
except subprocess .TimeoutExpired :
123
155
process .kill ()
124
156
raise
125
157
else :
126
- os .unlink (destination )
158
+ os .unlink (file )
127
159
128
160
129
161
def run_file (file , use_sudo = False , sudo_password = "" ):
130
- # Fast return if there is no file
131
- if file is None :
132
- return
133
-
134
- # Fast fail if the file can't be executed
135
- if not is_executable (file ):
136
- raise RuntimeError (f"File Execution: File does not have execution permissions [file={ file } ]" )
162
+ # Fast fail if there is no file or the file can't be executed
163
+ if file is None or not is_executable (file ):
164
+ raise RuntimeError (f"File Execution: File does not exist or have execution permissions [file={ file } ]" )
137
165
138
166
if use_sudo :
139
167
command = f"{ file } "
@@ -159,6 +187,17 @@ def run_file(file, use_sudo=False, sudo_password=""):
159
187
"""
160
188
161
189
190
+ def is_moved (origin , destination ):
191
+ # Enables fast-failing based on existence
192
+ if not os .path .isfile (destination ):
193
+ return False
194
+
195
+ if os .path .isfile (origin ):
196
+ return False
197
+
198
+ return True
199
+
200
+
162
201
def is_broken_link (file ):
163
202
return os .path .islink (file ) and not os .path .exists (file )
164
203
@@ -168,6 +207,10 @@ def is_linked(origin, destination):
168
207
169
208
170
209
def is_copied (origin , destination ):
210
+ # Enables fast-failing based on type
211
+ if os .path .islink (destination ):
212
+ return False
213
+
171
214
# Enables fast-failing based on existence
172
215
if not os .path .isfile (destination ):
173
216
return False
0 commit comments