/*
* Author: sambafeng
* date:2011-04-25
*/
/*
* TODO:auto create patches for script language programe,such as shell,ruby,perl,python
*/
package util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AutoCreatePatch{
/*
* TODO:create the patches by the key words:sourcedir,destdir,patchTxtName
*/
@SuppressWarnings("unchecked")
public void createPatches(String pre_sourcedir,String pre_destdir,String patchTxtName) throws FileNotFoundException, IOException{
List num=new ArrayList();
HashMap hm=new HashMap();
String suff_pos_name;
/*empty the destdir */
deletefile(pre_destdir);
System.out.println("Cleaning:"+pre_destdir);
new File(pre_destdir).mkdir();
System.out.println("Mkdir:"+pre_destdir+" finished!");
try{
BufferedReader in = new BufferedReader(new FileReader(patchTxtName));
String str;
int row = 0;
int row_pre=0;
while((str = in.readLine())!= null){
row++;
row_pre=row-1;
hm.put(row,str);
if(str.equals("===================================================================")){
num.add(row_pre);
}
}
for(int i=0;i<num.size();i++){
suff_pos_name=(String) hm.get(num.get(i));//Index: basefunction/base_resources.rb
String fullfilename=suff_pos_name.substring(suff_pos_name.indexOf("Index: ")+7);//ct/fd/
File fullsourceFile=new File(pre_sourcedir+"/"+fullfilename);//source file
File fulldestFile=new File(pre_destdir+"/"+fullfilename);//dest file
// System.out.println(fulldestFile.getParentFile());
String fulldestDir=fulldestFile.getParent().toString();
new File(fulldestDir).mkdirs();
copyFile(fullsourceFile,fulldestFile);
System.out.println("Copy "+fullsourceFile+" to "+fulldestFile);
}
in.close();
System.out.println("Make Patches finished! Count "+num.size()+" Files");
}catch(IOException e){
System.out.println("error");
}
}
/*
* copy the file from sourceFile to destFile
*
*/
public static void copyFile(File sourceFile,File destFile) throws IOException{
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);
FileOutputStream output = new FileOutputStream(destFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b))!=-1){
outBuff.write(b,0,len);
}
outBuff.flush();
inBuff.close();
outBuff.close();
output.close();
input.close();
}
public static boolean deletefile(String destDir)throws FileNotFoundException, IOException {
try {
File file = new File(destDir);
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i=0;i<filelist.length; i++) {
File delfile = new File(destDir+"\\"+filelist[i]);
if (!delfile.isDirectory()){
delfile.delete();
}
else if(delfile.isDirectory()) {
deletefile(destDir+"\\"+filelist[i]);
}
}
file.delete();
}
} catch (FileNotFoundException e) {
System.out.println("Exception:" + e.getMessage());
}
return true;
}
public static void main(String args[]) throws IOException{
AutoCreatePatch ap=new AutoCreatePatch();
ap.createPatches("D:\\workspace\\Ctrip_AutomationTest","E:/patch/Ctrip_AutomationTest","E:\\patch.txt");
}
}
* Author: sambafeng
* date:2011-04-25
*/
/*
* TODO:auto create patches for script language programe,such as shell,ruby,perl,python
*/
package util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AutoCreatePatch{
/*
* TODO:create the patches by the key words:sourcedir,destdir,patchTxtName
*/
@SuppressWarnings("unchecked")
public void createPatches(String pre_sourcedir,String pre_destdir,String patchTxtName) throws FileNotFoundException, IOException{
List num=new ArrayList();
HashMap hm=new HashMap();
String suff_pos_name;
/*empty the destdir */
deletefile(pre_destdir);
System.out.println("Cleaning:"+pre_destdir);
new File(pre_destdir).mkdir();
System.out.println("Mkdir:"+pre_destdir+" finished!");
try{
BufferedReader in = new BufferedReader(new FileReader(patchTxtName));
String str;
int row = 0;
int row_pre=0;
while((str = in.readLine())!= null){
row++;
row_pre=row-1;
hm.put(row,str);
if(str.equals("===================================================================")){
num.add(row_pre);
}
}
for(int i=0;i<num.size();i++){
suff_pos_name=(String) hm.get(num.get(i));//Index: basefunction/base_resources.rb
String fullfilename=suff_pos_name.substring(suff_pos_name.indexOf("Index: ")+7);//ct/fd/
File fullsourceFile=new File(pre_sourcedir+"/"+fullfilename);//source file
File fulldestFile=new File(pre_destdir+"/"+fullfilename);//dest file
// System.out.println(fulldestFile.getParentFile());
String fulldestDir=fulldestFile.getParent().toString();
new File(fulldestDir).mkdirs();
copyFile(fullsourceFile,fulldestFile);
System.out.println("Copy "+fullsourceFile+" to "+fulldestFile);
}
in.close();
System.out.println("Make Patches finished! Count "+num.size()+" Files");
}catch(IOException e){
System.out.println("error");
}
}
/*
* copy the file from sourceFile to destFile
*
*/
public static void copyFile(File sourceFile,File destFile) throws IOException{
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);
FileOutputStream output = new FileOutputStream(destFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b))!=-1){
outBuff.write(b,0,len);
}
outBuff.flush();
inBuff.close();
outBuff.close();
output.close();
input.close();
}
public static boolean deletefile(String destDir)throws FileNotFoundException, IOException {
try {
File file = new File(destDir);
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i=0;i<filelist.length; i++) {
File delfile = new File(destDir+"\\"+filelist[i]);
if (!delfile.isDirectory()){
delfile.delete();
}
else if(delfile.isDirectory()) {
deletefile(destDir+"\\"+filelist[i]);
}
}
file.delete();
}
} catch (FileNotFoundException e) {
System.out.println("Exception:" + e.getMessage());
}
return true;
}
public static void main(String args[]) throws IOException{
AutoCreatePatch ap=new AutoCreatePatch();
ap.createPatches("D:\\workspace\\Ctrip_AutomationTest","E:/patch/Ctrip_AutomationTest","E:\\patch.txt");
}
}