treediff/treediff.nim
2016-01-25 09:13:34 -06:00

52 lines
1.3 KiB
Nim

import os, docopt, tables, md5, iterutils, re
type
FileEntry* = tuple[relPath: string, checksum: string]
DirAnalysis* = tuple[allEntries: seq[FileEntry],
byRelPath: Table[string, FileEntry],
byChecksum: Table[string, FileEntry]]
proc analyzeDir(root: string): DirAnalysis =
let fileCount = countFiles(root)
proc countFiles(root: string): int =
# TODO
proc studyDir(root: string, ignore: Iterable[string]): TableRef[string, string] =
result = newTable[string, string]()
for path in walkDirRec(root):
var relPath = substr(path, len(root))
if foldl(ignore, proc (acc: bool, it: string): bool = acc and match(relPath, re(it)), true): continue
var fileInfo = getFileInfo(path)
if fileInfo.kind == pcFile:
result.add(relPath, $(toMD5(readFile(path))))
elif fileInfo.kind == pcDir:
result.add(relPath, "directory")
when isMainModule:
let doc = """
treediff
Usage:
treediff [-i <regex>]... [<path>]...
treediff (-h | --help)
treediff (-v | --version)
Options:
-h --help Show this usage information.
-v --version Show the program version.
"""
let args = docopt(doc, version = "treediff 0.1")
for root in @(args["<path>"]):
echo "Looking at ", root
echo studyDir(root, @(args["<regex>"]))
echo ""