<commit_before>/***************************************************************************
*
* Copyright (C) 2014 Baidu.com, Inc. All Rights Reserved
*
***************************************************************************/

/**
 * @file util.js ~ 2014-03-24 16:47
 * @author sekiyika (<EMAIL>)
 * @description
 *  工具包
 */


var fs = require('fs');
var path = require('path');

/**
 * 获取sdk的工作路径
 */
exports.getSDKHome = function() {
    var dir = process.env[
        require( 'os' ).platform() === 'win32' 
        ? 'APPDATA'
        : 'HOME'
    ] + path.sep + '.edpx-zhixin';

    // 如果这个目录不存在，则创建这个目录
    if(!fs.existsSync(dir)) {
        fs.mkdirSync(dir);
    }

    return dir;
};

exports.copyFile = function(source, dest) {
    var content = fs.readFileSync(source);
    fs.writeFileSync(dest, content);
};

/**
 * 得到绝对路径
 * 
 * @param  {string} argsPath 路径
 * @param  {string} relaPath 依照路径（绝对的路径）
 * @return {string}          绝对路径
 */
exports.getAbsPath = function (argsPath, relaPath) {

    if (fs.existsSync(argsPath) && /^(\/\\)/.test(argsPath)) {
        return argsPath;
    }
    else {
        var absPath = path.resolve(relaPath ? relaPath : process.cwd(), argsPath);
        return fs.existsSync(absPath) ? absPath : null;
    }
};

/**
 * 删除文件夹
 * 
 * @param  {string} dirPath 文件夹绝对路径
 */
exports.rmdir = function (dirPath) {
    var files = [];
    var me = this;
    if(fs.existsSync(dirPath)) {
        files = fs.readdirSync(dirPath);
        files.forEach(function (file, index){
            var curPath = path.resolve(dirPath, file);
            if(fs.statSync(curPath).isDirectory()) { // recurse
                me.rmdir(curPath);
            } 
            else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(dirPath);
    }
};

/**
 * 找文件
 * 
 * @param  {string} somePath 文件的依照文件夹
 * @param  {string} filename 要查找的文件名
 * @return {string}          返回文件的所在真实目录
 */
exports.findFile = function (somePath, filename) {
    var pathArr = this.getAbsPath(somePath).split('/');
    while (pathArr.length > 0) {
        var tmpPath = path.resolve(pathArr.join('/'), filename);
        if (fs.existsSync(tmpPath)) {
            return pathArr.join('/');
        }
        else {
            pathArr.pop();
        }
    } <commit_msg>util.js的windows兼容性<commit_after>/***************************************************************************
*
* Copyright (C) 2014 Baidu.com, Inc. All Rights Reserved
*
***************************************************************************/

/**
 * @file util.js ~ 2014-03-24 16:47
 * @author sekiyika (<EMAIL>)
 * @description
 *  工具包
 */


var fs = require('fs');
var path = require('path');

/**
 * 获取sdk的工作路径
 */
exports.getSDKHome = function() {
    var dir = process.env[
        require( 'os' ).platform() === 'win32' 
        ? 'APPDATA'
        : 'HOME'
    ] + path.sep + '.edpx-zhixin';

    // 如果这个目录不存在，则创建这个目录
    if(!fs.existsSync(dir)) {
        fs.mkdirSync(dir);
    }

    return dir;
};

exports.copyFile = function(source, dest) {
    var content = fs.readFileSync(source);
    fs.writeFileSync(dest, content);
};

/**
 * 得到绝对路径
 * 
 * @param  {string} argsPath 路径
 * @param  {string} relaPath 依照路径（绝对的路径）
 * @return {string}          绝对路径
 */
exports.getAbsPath = function (argsPath, relaPath) {

    if (fs.existsSync(argsPath) && /^(\/\\)/.test(argsPath)) {
        return argsPath;
    }
    else {
        var absPath = path.resolve(relaPath ? relaPath : process.cwd(), argsPath);
        return fs.existsSync(absPath) ? absPath : null;
    }
};

/**
 * 删除文件夹
 * 
 * @param  {string} dirPath 文件夹绝对路径
 */
exports.rmdir = function (dirPath) {
    var files = [];
    var me = this;
    if(fs.existsSync(dirPath)) {
        files = fs.readdirSync(dirPath);
        files.forEach(function (file, index){
            var curPath = path.resolve(dirPath, file);
            if(fs.statSync(curPath).isDirectory()) { // recurse
                me.rmdir(curPath);
            } 
            else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(dirPath);
    }
};

/**
 * 找文件
 * 
 * @param  {string} somePath 文件的依照文件夹
 * @param  {string} filename 要查找的文件名
 * @return {string}          返回文件的所在真实目录
 */
exports.findFile = function (somePath, filename) {
    var pathArr = this.getAbsPath(somePath).split(path.sep);
    while (pathArr.length > 0) {
        var tmpPath = path.resolve(pathArr.join(path.sep), filename);
        if (fs.existsSync(tmpPath)) {
            return pathArr.join(path.sep);
        }
        else {
            pathArr.pop();
        }
    } 
{'fraction_non_alphanumeric': 0.21171171171171171, 'fraction_numerical': 0.008151008151008152, 'mean_word_length': 2.374095513748191, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2424716', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1650, 'n_words': 292}