用 cttfiles 轻松的创建测试文件

有一个小众的需求: 创建大量的某种特性的文件, 用于测试程序.

个人实现了一个简单的程序, cttfiles, 可以:

  • 设置目录和文件的数量, 目录层级;

  • 设置文件的内容来源为其他文件, 目录或随机生成;

  • 设置文件的大小范围;

  • 设置文件名和目录名的生成规则;

使用说明如下

Usage:
        cttfiles.py [options] directory

    Options:
        -d    create directories, default false, Create directories recursivly, 
                depth of directry is set by argument 'depth'.
        -f    create files, default true
        -h    show help message
        -m    mix mode, default false. Create directories recursivly, then create files in each directory.
              
      Directory
        --depth=DEPTH   directory depth, default 1
        --dnum=DNUM     num of directories, default 10
        --dprefix=DPRE  prefix of directory name
        --dsuffix=DSUF  suffix of directory name

      File
        --exts=EXTS                 file extensions, separated by ','
        --from_dirs=DIRECTORIES     A test file's content is one of file in DIRECTORIES, which is randomly selected.
        --from_files=FILES          A test file's content is one of FILES, which is randomly selected.
        --fprefix=FPRE              prefix of file name
        --fsuffix=FSUF              suffix of file name
        --fnum=FNUM                 num of files, default 10
        --fminsize=FMINSIZE         min size of file when file's content is created randomly. default 1.
        --fmaxsize=FMAXSIZE         max size of file when file's content is created randomly. default 1024.

示例:

$ python cttfiles.py -m --depth=2 --dnum=3 --dprefix='dp' --dsuffix='ds' --exts='php,jsp' --from_files="1.jsp,1.php" --fprefix="fp" --fsuffix="fs" --fnum=2 tmp_dir