@@ -334,9 +334,49 @@ def __setattr__(self, name, value):
334334
335335
336336
337+ def copy_file (self , source , destination , server_source = False , server_dest = True , raise_error = False , ** kwargs ):
338+ """
339+ COPY A FILE
340+ ===========
341+
342+ This function copies a file or directory from the source to the destination.
343+ The destination is on the cluster, the source is in the local machine.
344+
345+ It uses scp to perform the copy.
346+ Alternative implementations can be performed by overloading this function.
347+
348+ args and kwargs are passed to the ExecuteCMD function.
349+
350+ The result is the output of the ExecuteCMD function.
351+
352+ Parameters
353+ ----------
354+ source : string
355+ The source file to be copied
356+ destination : string
357+ The destination file
358+ server_source : bool
359+ If true, the source is on the server
360+ server_dest : bool
361+ If true, the destination is on the server
362+ raise_error : bool
363+ If True, raises an error upon failure
364+ """
365+ server_path = "%s:" % self .hostname
366+ source_path = f"{ source } "
367+ dest_path = f"{ destination } "
368+
369+ if server_source :
370+ source_path = server_path + source_path
371+ if server_dest :
372+ dest_path = server_path + dest_path
373+
374+ cmd = self .scpcmd + f" { source_path } { dest_path } "
375+ result = self .ExecuteCMD (cmd , raise_error = raise_error , ** kwargs )
376+ return result
337377
338378
339- def ExecuteCMD (self , cmd , raise_error = False , return_output = False , on_cluster = False ):
379+ def ExecuteCMD (self , cmd , raise_error = False , return_output = False , on_cluster = False , use_active_shell = False ):
340380 """
341381 EXECUTE THE CMD ON THE CLUSTER
342382 ==============================
@@ -355,6 +395,10 @@ def ExecuteCMD(self, cmd, raise_error = False, return_output = False, on_cluster
355395 returned as second value.
356396 on_cluster : bool
357397 If true, the command is executed directly on the cluster through ssh
398+ use_active_shell : bool
399+ If true, the command is executed in a new shell on the cluster
400+ This is usefull if the command is a script that must be executed
401+ in a new shell, or if the command requires .bashrc to be sourced.
358402
359403 Returns
360404 -------
@@ -367,6 +411,14 @@ def ExecuteCMD(self, cmd, raise_error = False, return_output = False, on_cluster
367411
368412 if on_cluster :
369413 cmd = self .sshcmd + " {} '{}'" .format (self .hostname , cmd )
414+ if use_active_shell :
415+ cmd = "{ssh} {host} -t '{shell} --login -c \" echo {string}\" '" .format (ssh = self .sshcmd ,
416+ host = self .hostname ,
417+ string = parse_symbols (string ),
418+ shell = self .terminal )
419+
420+
421+
370422
371423
372424 success = False
@@ -630,7 +682,7 @@ def prepare_input_file(self, structures, calc, labels):
630682
631683Error message:
632684''' .format (label )
633- MSG += str (e )
685+ MSG += str (repr ( e ) )
634686 print (MSG )
635687
636688
@@ -710,8 +762,7 @@ def copy_files(self, list_of_input, list_of_output, to_server):
710762
711763
712764 # Clean eventually input/output file of this very same calculation
713- cmd = self .sshcmd + " %s '%s'" % (self .hostname , rm_cmd )
714- self .ExecuteCMD (cmd , False )
765+ self .ExecuteCMD (rm_cmd , False , on_cluster = True )
715766# cp_res = os.system(cmd + " > /dev/null")
716767# if cp_res != 0:
717768# print "Error while executing:", cmd
@@ -720,8 +771,9 @@ def copy_files(self, list_of_input, list_of_output, to_server):
720771#
721772
722773 # Copy the file into the cluster
723- cmd = self .scpcmd + " %s %s:%s/" % (tar_file , self .hostname , self .workdir )
724- cp_res = self .ExecuteCMD (cmd , False )
774+ cp_res = self .copy_file (tar_file , self .workdir , raise_error = False )
775+ #cmd = self.scpcmd + " %s %s:%s/" % (tar_file, self.hostname, self.workdir)
776+ #cp_res = self.ExecuteCMD(cmd, False)
725777 if not cp_res :
726778 print ("Error while executing:" , cmd )
727779 print ("Return code:" , cp_res )
@@ -734,8 +786,9 @@ def copy_files(self, list_of_input, list_of_output, to_server):
734786
735787 # Unpack the input files and remove the archive
736788 decompress = 'cd {}; tar xf {};' .format (self .workdir , tar_name )
737- cmd = self .sshcmd + " %s '%s'" % (self .hostname , decompress )
738- cp_res = self .ExecuteCMD (cmd , False )
789+ #cmd = self.sshcmd + " %s '%s'" % (self.hostname, decompress)
790+ #cp_res = self.ExecuteCMD(cmd, False)
791+ cp_res = self .ExecuteCMD (decompress , False , on_cluster = True )
739792 if not cp_res :
740793 print ("Error while executing:" , cmd )
741794 print ("Return code:" , cp_res )
@@ -751,16 +804,18 @@ def copy_files(self, list_of_input, list_of_output, to_server):
751804
752805 compress_cmd = 'cd {}; {}' .format (self .workdir , tar_command )
753806
754- cmd = self .sshcmd + " %s '%s'" % (self .hostname , compress_cmd )
755- cp_res = self .ExecuteCMD (cmd , False )
807+ # cmd = self.sshcmd + " %s '%s'" % (self.hostname, compress_cmd)
808+ # cp_res = self.ExecuteCMD(cmd, False)
809+ cp_res = self .ExecuteCMD (compress_cmd , False , on_cluster = True )
756810 if not cp_res :
757811 print ("Error while compressing the outputs:" , cmd , list_of_output , "\n Return code:" , cp_res )
758812 #return cp_res
759813
760814
761- # Copy the tar and unpack
762- cmd = self .scpcmd + "%s:%s %s/" % (self .hostname , os .path .join (self .workdir , tar_name ), self .local_workdir )
763- cp_res = self .ExecuteCMD (cmd , False )
815+ # Copy the tar from the server to the local and unpack
816+ # cmd = self.scpcmd + "%s:%s %s/" % (self.hostname, os.path.join(self.workdir, tar_name), self.local_workdir)
817+ # cp_res = self.ExecuteCMD(cmd, False)
818+ cp_res = self .copy_file (os .path .join (self .workdir , tar_name ), self .local_workdir , raise_error = False , server_source = True , server_dest = False )
764819 if not cp_res :
765820 print ("Error while executing:" , cmd )
766821 print ("Return code:" , cp_res )
@@ -1114,10 +1169,9 @@ def run_atoms(self, ase_calc, ase_atoms, label="ESP",
11141169 submission += mpicmd + " " + binary + "\n "
11151170
11161171 # First of all clean eventually input/output file of this very same calculation
1117- cmd = self .sshcmd + " %s 'rm -f %s/%s%s %s/%s%s'" % (self .hostname ,
1118- self .workdir , label , in_extension ,
1119- self .workdir , label , out_extension )
1120- self .ExecuteCMD (cmd , False )
1172+ cmd = "rm -f %s/%s%s %s/%s%s" % (self .workdir , label , in_extension ,
1173+ self .workdir , label , out_extension )
1174+ self .ExecuteCMD (cmd , False , on_cluster = True )
11211175# cp_res = os.system(cmd)
11221176# if cp_res != 0:
11231177# print "Error while executing:", cmd
@@ -1128,16 +1182,18 @@ def run_atoms(self, ase_calc, ase_atoms, label="ESP",
11281182 f = open ("%s/%s.sh" % (self .local_workdir , label ), "w" )
11291183 f .write (submission )
11301184 f .close ()
1131- cmd = self .scpcmd + " %s/%s.sh %s:%s" % (self .local_workdir , label , self .hostname , self .workdir )
1132- self .ExecuteCMD (cmd , False )
1185+ #cmd = self.scpcmd + " %s/%s.sh %s:%s" % (self.local_workdir, label, self.hostname, self.workdir)
1186+ self .copy_file ("%s/%s.sh" % (self .local_workdir , label ), self .workdir , server_source = False , server_dest = True )
1187+ #self.ExecuteCMD(cmd, False)
11331188# cp_res = os.system(cmd)
11341189# if cp_res != 0:
11351190# print "Error while executing:", cmd
11361191# print "Return code:", cp_res
11371192# sys.stderr.write(cmd + ": exit with code " + str(cp_res))
11381193#
1139- cmd = self .scpcmd + " %s/%s%s %s:%s" % (self .local_workdir , label , in_extension , self .hostname , self .workdir )
1140- cp_res = self .ExecuteCMD (cmd , False )
1194+ cp_res = self .copy_file ("%s/%s%s" % (self .local_workdir , label , in_extension ), self .workdir , server_source = False , server_dest = True , raise_error = False )
1195+ #cmd = self.scpcmd + " %s/%s%s %s:%s" % (self.local_workdir, label, in_extension, self.hostname, self.workdir)
1196+ #cp_res = self.ExecuteCMD(cmd, False)
11411197 #cp_res = os.system(cmd)
11421198 if not cp_res :
11431199 #print "Error while executing:", cmd
@@ -1146,18 +1202,20 @@ def run_atoms(self, ase_calc, ase_atoms, label="ESP",
11461202 return
11471203
11481204 # Run the simulation
1149- cmd = self .sshcmd + " %s '%s %s/%s.sh'" % (self .hostname , self .submit_command , self .workdir , label )
1150- self .ExecuteCMD (cmd , False )
1205+ cmd = "%s %s/%s.sh" % (self .submit_command , self .workdir , label )
1206+ #cmd = self.sshcmd + " %s '%s %s/%s.sh'" % (self.hostname, self.submit_command, self.workdir, label)
1207+ self .ExecuteCMD (cmd , False , on_cluster = True )
11511208# cp_res = os.system(cmd)
11521209# if cp_res != 0:
11531210# print "Error while executing:", cmd
11541211# print "Return code:", cp_res
11551212# sys.stderr.write(cmd + ": exit with code " + str(cp_res))
11561213
11571214 # Get the response
1158- cmd = self .scpcmd + " %s:%s/%s%s %s/" % (self .hostname , self .workdir , label , out_extension ,
1159- self .local_workdir )
1160- cp_res = self .ExecuteCMD (cmd , False )
1215+ #cmd = self.scpcmd + " %s:%s/%s%s %s/" % (self.hostname, self.workdir, label, out_extension,
1216+ #self.local_workdir)
1217+ #cp_res = self.ExecuteCMD(cmd, False)
1218+ cp_res = self .copy_file ("%s/%s%s" % (self .workdir , label , out_extension ), self .local_workdir , server_source = True , server_dest = False )
11611219 #cp_res = os.system(cmd)
11621220 if not cp_res :
11631221 print ("Error while executing:" , cmd )
@@ -1401,10 +1459,11 @@ def setup_workdir(self, verbose = True):
14011459 """
14021460 workdir = self .parse_string (self .workdir )
14031461
1404- sshcmd = self .sshcmd + " %s 'mkdir -p %s'" % (self .hostname ,
1405- workdir )
1462+ cmd = "mkdir -p %s" % workdir
1463+ # sshcmd = self.sshcmd + " %s 'mkdir -p %s'" % (self.hostname,
1464+ # workdir)
14061465
1407- self .ExecuteCMD (sshcmd , raise_error = True )
1466+ self .ExecuteCMD (cmd , raise_error = True , on_cluster = True )
14081467#
14091468# retval = os.system(sshcmd)
14101469# if retval != 0:
@@ -1441,18 +1500,17 @@ def parse_string(self, string):
14411500
14421501 # Open a pipe with the server
14431502 # Use single ' to avoid string parsing by the local terminal
1444- cmd = "%s %s 'echo \" %s\" '" % (self .sshcmd , self .hostname , string )
1503+ #cmd = "%s %s 'echo \"%s\"'" % (self.sshcmd, self.hostname, string)
1504+ cmd = f"echo \" { string } \" "
14451505
1446- if self .use_active_shell_for_parsing :
1447- cmd = "{ssh} {host} -t '{shell} --login -c \" echo {string}\" '" .format (ssh = self .sshcmd ,
1448- host = self .hostname ,
1449- string = parse_symbols (string ),
1450- shell = self .terminal )
1451- #print cmd
1506+ status , output = self .ExecuteCMD (cmd , return_output = True , raise_error = True , use_active_shell = self .use_active_shell_for_parsing , on_cluster = True )
1507+
1508+ #print cmd
14521509
14531510 #print(cmd)
14541511
1455- status , output = self .ExecuteCMD (cmd , return_output = True , raise_error = True )
1512+ #status, output = self.ExecuteCMD(cmd, return_output = True, raise_error= True)
1513+
14561514#
14571515# p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
14581516# output, err = p.communicate()
0 commit comments