compare local files and remote files.

比较本地与远程文件差异
本文介绍了一个实用工具,用于对比本地与远程服务器上的文件差异,并将结果输出为Excel文件。该工具通过SSH连接远程服务器获取文件列表,然后与本地文件进行比对,最后用Apache POI生成可视化的对比报告。

最近发生了一件怪事,项目在本地run的时候会报一个jar包下找不到某个class的错误,然而同样的project promote到远程服务器上 是ok的,于是就怀疑会不会是远程服务器上的jar包 跟本地的有差别,于是乎写下了这个东西。

 

只是一个basic的版本,可以再加点内容完善的。

 

需要的jar包 

 

连接linux remote server要用的:

ganymed-ssh2.jar

因为我是导出了excel 所以用了POI

poi.jar

 

 

Utils 类

写道
package org.vic.util;

import java.util.Collection;
import java.util.Collections;

public class Utils {

public static <T> Collection<T> ifNullReturnEmpty(Collection<T> collection) {
return collection == null ? Collections.emptyList() : collection;
}

}

 DTO: 数据行结构

 

package org.vic.dto;

public class Row {
	
	private String fileName;
	
	private int fileAmount;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getFileAmount() {
		return fileAmount;
	}

	public void setFileAmount(int fileAmount) {
		this.fileAmount = fileAmount;
	}
	
}

 主功能类:

package org.vic.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;
import org.vic.dto.Row;
import org.vic.util.Utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class Comparator {
	
	/**
	 * 
	 * @param remoteFolderPath 远程服务目标文件夹路径
	 * @param remoteIp         远程服务器IP
	 * @param remoteUsername   远程服务器登陆用户名
	 * @param remotePassword   远程服务器登陆密码
	 * @param localFolderPath  本地目标文件夹路径
	 * @param excelFilePath    导出结果excel文件路径
	 * @throws Exception       抛出一场
	 */
	public void getDifferentFiles(String remoteFolderPath, String remoteIp, String remoteUsername, String remotePassword, String localFolderPath, String excelFilePath) throws Exception {
		System.out.println("start searching local files");
		List<String> localFileNames = getLocalFileNames(localFolderPath);
		System.out.println("start searching remote files");
		List<String> remoteFileNames = getRemoteFileNames(remoteFolderPath, remoteIp, remoteUsername, remotePassword);
		System.out.println("merging data");
		Set<String> totalSet = new HashSet<String>();
		totalSet.addAll(localFileNames);
		totalSet.addAll(remoteFileNames);
		List<Row> localFileInfo = new ArrayList<Row>();
		List<Row> remoteFileInfo = new ArrayList<Row>();
		System.out.println("creating rows");
		for(String fileName : Utils.ifNullReturnEmpty(localFileNames)){
			Row row = fileInfoProcesser(fileName, localFileNames);
			localFileInfo.add(row);
		}
		for(String fileName : Utils.ifNullReturnEmpty(remoteFileNames)){
			Row row = fileInfoProcesser(fileName, remoteFileNames);
			remoteFileInfo.add(row);
		}
		System.out.println("generating excel file...");
		excelCreater(localFileInfo, remoteFileInfo, excelFilePath, totalSet);
		System.out.println("generating finished, please read the file in : " + excelFilePath);
	}
	
	private void excelCreater(List<Row> localList, List<Row> remoteList, String exportFilePath, Set<String> totalSet) {
		HSSFWorkbook wb = new HSSFWorkbook();  
        HSSFSheet sheet = wb.createSheet("comparator"); 
        sheet.setColumnWidth(0, 100 * 180);
        sheet.setColumnWidth(1, 100 * 50);
        sheet.setColumnWidth(2, 100 * 180);
        sheet.setColumnWidth(3, 100 * 50);
        HSSFRow row = sheet.createRow((int) 0);  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        Font titleFont = wb.createFont();
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        style.setFont(titleFont);
        HSSFCellStyle differentStyle = wb.createCellStyle();
        differentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        Font font = wb.createFont();
        font.setColor(Font.COLOR_RED);
        differentStyle.setFont(font);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("localFileName");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("localFileAmount");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("remoteFileName");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("remoteFileAmount");
        cell.setCellStyle(style);
        HSSFCellStyle dataStyle = wb.createCellStyle();  
        dataStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        Font dataFont = wb.createFont();
        dataFont.setColor(HSSFColor.BLUE.index);
        dataStyle.setFont(dataFont);
        List<String> archList = new ArrayList<String>();
        archList.addAll(totalSet);
        for(int i = 0; i < archList.size(); i ++) {
        	row = sheet.createRow(i + 1);  
        	String fileName = archList.get(i);
        	Row localRowData = findRowByFileName(fileName, localList);
        	Row remoteRowData = findRowByFileName(fileName, remoteList);
        	HSSFCell cell0 = row.createCell(0);
        	cell0.setCellStyle(dataStyle);
        	cell0.setCellValue(localRowData.getFileName());
        	HSSFCell cell1 = row.createCell(1);
        	cell1.setCellStyle(dataStyle);
        	cell1.setCellValue(localRowData.getFileAmount());
        	HSSFCell cell2 = row.createCell(2);
        	cell2.setCellStyle(dataStyle);
        	cell2.setCellValue(remoteRowData.getFileName());
        	HSSFCell cell3 = row.createCell(3);
        	cell3.setCellStyle(dataStyle);
        	cell3.setCellValue(remoteRowData.getFileAmount());
            if(!localRowData.getFileName().equals(remoteRowData.getFileName()) || localRowData.getFileAmount() != remoteRowData.getFileAmount()) {
            	cell0.setCellStyle(differentStyle);
            	cell1.setCellStyle(differentStyle);
            	cell2.setCellStyle(differentStyle);
            	cell3.setCellStyle(differentStyle);
            	
            }
        }
        
        try {
        	File file = new File(exportFilePath);
        	if(!file.exists()) {
        		file.createNewFile();
        	}
			FileOutputStream fos = new FileOutputStream(exportFilePath);
			wb.write(fos);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private Row findRowByFileName(String fileName, List<Row> fileList) {
		for(Row row : fileList) {
			if(fileName.equals(row.getFileName())){
				return row;
			}
		}
		Row empty = new Row();
		empty.setFileName("--");
		empty.setFileAmount(0);
		return empty;
	}
	
	private Row fileInfoProcesser(String fileName, List<String> fileList) {
		Row row = new Row();
		int count = 0;
		for(String file : Utils.ifNullReturnEmpty(fileList)) {
			if(file.equals(fileName)) {
				count ++;
			}
		}
		row.setFileName(fileName);
		row.setFileAmount(count);
		return row;
	}
	
	private List<String> getLocalFileNames(String localFolderPath) throws Exception {
		List<String> result = new ArrayList<String>();
		File file = new File(localFolderPath);
		if(file.exists()) {
			if(file.isDirectory()) {
				File[] files = file.listFiles();
				if(file != null && file.length() > 0) {
					for (File subFile : files) {
						String fileName = subFile.getName();
						result.add(fileName);
					}
				}
			} else {
				throw new Exception("path is a file, not a directory!");
			}
		} else {
			throw new Exception("folder is not existing!");
		}
		return result;
	}
	
	private List<String> getRemoteFileNames(String remoteFolderPath, String remoteIp, String remoteUsername, String remotePassword) throws IOException {
		List<String> result = new ArrayList<String>();
		Connection conn = new Connection(remoteIp);
		conn.connect();
        boolean isAuthenticated = conn.authenticateWithPassword(remoteUsername, remotePassword);
        if (isAuthenticated == false) throw new IOException("Authentication failed.");
        Session session = conn.openSession();
        String commands = "cd " + remoteFolderPath + "&&ls";
        session.execCommand(commands);
        InputStream stdout = new StreamGobbler(session.getStdout());
		@SuppressWarnings("resource")
		BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
		String tmp = null;
		while ((tmp = br.readLine()) != null) {
			String fileName = tmp;
			fileName = fileName.replace("Shell Message : ", "");
			result.add(fileName);
		}
        session.close();
        conn.close();
		return result;
	}
	
	/**
	 * Execution Entrance
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Comparator c = new Comparator();
		
		c.getDifferentFiles("/remoteServer/project/lib", "xx.xx.xx.xx", "username", "password", "local/project/lib", "excel/result.xls");
	}
}

 

 

bitbake --help usage: bitbake [-s] [-e] [-g] [-u UI] [--version] [-h] [-f] [-c CMD] [-C INVALIDATE_STAMP] [--runall RUNALL] [--runonly RUNONLY] [--no-setscene] [--skip-setscene] [--setscene-only] [-n] [-p] [-k] [-P] [-S SIGNATURE_HANDLER] [--revisions-changed] [-b BUILDFILE] [-D] [-l DEBUG_DOMAINS] [-v] [-q] [-w WRITEEVENTLOG] [-B BIND] [-T SERVER_TIMEOUT] [--remote-server REMOTE_SERVER] [-m] [--token XMLRPCTOKEN] [--observe-only] [--status-only] [--server-only] [-r PREFILE] [-R POSTFILE] [-I EXTRA_ASSUME_PROVIDED] [recipename/target [recipename/target ...]] It is assumed there is a conf/bblayers.conf available in cwd or in BBPATH which will provide the layer, BBFILES and other configuration information. General options: recipename/target Execute the specified task (default is 'build') for these target recipes (.bb files). -s, --show-versions Show current and preferred versions of all recipes. -e, --environment Show the global or per-recipe environment complete with information about where variables were set/changed. -g, --graphviz Save dependency tree information for the specified targets in the dot syntax. -u UI, --ui UI The user interface to use (knotty, ncurses, taskexp, taskexp_ncurses or teamcity - default knotty). --version Show programs version and exit. -h, --help Show this help message and exit. Task control options: -f, --force Force the specified targets/task to run (invalidating any existing stamp file). -c CMD, --cmd CMD Specify the task to execute. The exact options available depend on the metadata. Some examples might be 'compile' or 'populate_sysroot' or 'listtasks' may give a list of the tasks available. -C INVALIDATE_STAMP, --clear-stamp INVALIDATE_STAMP Invalidate the stamp for the specified task such as 'compile' and then run the default task for the specified target(s). --runall RUNALL Run the specified task for any recipe in the taskgraph of the specified target (even if it wouldn't otherwise have run). --runonly RUNONLY Run only the specified task within the taskgraph of the specified targets (and any task dependencies those tasks may have). --no-setscene Do not run any setscene tasks. sstate will be ignored and everything needed, built. --skip-setscene Skip setscene tasks if they would be executed. Tasks previously restored from sstate will be kept, unlike --no-setscene. --setscene-only Only run setscene tasks, don't run any real tasks. Execution control options: -n, --dry-run Don't execute, just go through the motions. -p, --parse-only Quit after parsing the BB recipes. -k, --continue Continue as much as possible after an error. While the target that failed and anything depending on it cannot be built, as much as possible will be built before stopping. -P, --profile Profile the command and save reports. -S SIGNATURE_HANDLER, --dump-signatures SIGNATURE_HANDLER Dump out the signature construction information, with no task execution. The SIGNATURE_HANDLER parameter is passed to the handler. Two common values are none and printdiff but the handler may define more/less. none means only dump the signature, printdiff means recursively compare the dumped signature with the most recent one in a local build or sstate cache (can be used to find out why tasks re-run when that is not expected) --revisions-changed Set the exit code depending on whether upstream floating revisions have changed or not. -b BUILDFILE, --buildfile BUILDFILE Execute tasks from a specific .bb recipe directly. WARNING: Does not handle any dependencies from other recipes. Logging/output control options: -D, --debug Increase the debug level. You can specify this more than once. -D sets the debug level to 1, where only bb.debug(1, ...) messages are printed to stdout; -DD sets the debug level to 2, where both bb.debug(1, ...) and bb.debug(2, ...) messages are printed; etc. Without -D, no debug messages are printed. Note that -D only affects output to stdout. All debug messages are written to ${T}/log.do_taskname, regardless of the debug level. -l DEBUG_DOMAINS, --log-domains DEBUG_DOMAINS Show debug logging for the specified logging domains. -v, --verbose Enable tracing of shell tasks (with 'set -x'). Also print bb.note(...) messages to stdout (in addition to writing them to ${T}/log.do_<task>). -q, --quiet Output less log message data to the terminal. You can specify this more than once. -w WRITEEVENTLOG, --write-log WRITEEVENTLOG Writes the event log of the build to a bitbake event json file. Use '' (empty string) to assign the name automatically. Server options: -B BIND, --bind BIND The name/address for the bitbake xmlrpc server to bind to. -T SERVER_TIMEOUT, --idle-timeout SERVER_TIMEOUT Set timeout to unload bitbake server due to inactivity, set to -1 means no unload, default: Environment variable BB_SERVER_TIMEOUT. --remote-server REMOTE_SERVER Connect to the specified server. -m, --kill-server Terminate any running bitbake server. --token XMLRPCTOKEN Specify the connection token to be used when connecting to a remote server. --observe-only Connect to a server as an observing-only client. --status-only Check the status of the remote bitbake server. --server-only Run bitbake without a UI, only starting a server (cooker) process. Configuration options: -r PREFILE, --read PREFILE Read the specified file before bitbake.conf. -R POSTFILE, --postread POSTFILE Read the specified file after bitbake.conf. -I EXTRA_ASSUME_PROVIDED, --ignore-deps EXTRA_ASSUME_PROVIDED Assume these dependencies don't exist and are already provided (equivalent to ASSUME_PROVIDED). Useful to make dependency graphs more appealing.
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值