Friday, February 27, 2015

Python: Copy Folder or Files using password (sudo)

Following code can be used to copy Folder or Files using sudo with password.

   def copyFolderOrFile(self, sourcePath, destinationPath):  
     if(os.path.isdir(sourcePath)==True):  
       os.chdir("/some_root_location/")  
       sudoPassword = 'password_of_user_having_sudo_rights'  
       try:  
         command="cp -R "+sourcePath+" "+destinationPath  
         os.system('echo %s|sudo -S %s' % (sudoPassword, command))  
         print "Directory Copied Sucessfully"  
       except OSError as exc:  
         if exc.errno == errno.ENOTDIR:  
           command="cp "+sourcePath+" "+destinationPath  
           os.system('echo %s|sudo -S %s' % (sudoPassword, command))  
           print "File Copied Sucessfully"  
         else:  
           print('Directory/File not copied. Error: %s' % exc)  

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.

Monday, January 5, 2015

Java: Example of Wait Method in Java

Following is an example to implement Wait Method for predefined seconds in Java. Useful in Selenium, when you don't have control to wait for some element to load in Rich-faces, etc.

 package selenium;  
 import java.util.concurrent.TimeUnit;  
 import org.apache.log4j.Logger;  
 public class WaitMethod  
 {  
   protected void waitForAFewSecond(int timeS)  
   {  
     try  
     {  
       long timeInMS, timeInSec, timeOutMS, timeOutSec;  
       timeInMS = System.currentTimeMillis();  
       timeInSec = TimeUnit.MILLISECONDS.toSeconds(timeInMS);  
       do  
       {  
         timeOutMS = System.currentTimeMillis();  
         timeOutSec = TimeUnit.MILLISECONDS.toSeconds(timeOutMS);  
       }  
       while (timeOutSec - timeInSec < timeS);  
     }  
     catch (Exception e)  
     {  
       System.out.println(e.getMessage());  
     }  
   }  
 }