From 56c735c248bbebb5b96f9e27b87cecee79a1e94b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Sat, 7 Apr 2012 12:52:01 +0200 Subject: [PATCH] Rewrote some tools in python, so they are safer, and work faster. --- dotpercent-dirs.py | 35 +++++++++++++++++++++++++++++++++++ pseudo-empty-dirs.sh | 39 --------------------------------------- unhide-dotpercent.py | 31 +++++++++++++++++++++++++++++++ updatehash.py | 4 ++-- updatehash.sql | 2 +- 5 files changed, 69 insertions(+), 42 deletions(-) create mode 100755 dotpercent-dirs.py delete mode 100755 pseudo-empty-dirs.sh create mode 100755 unhide-dotpercent.py diff --git a/dotpercent-dirs.py b/dotpercent-dirs.py new file mode 100755 index 0000000..15bdc14 --- /dev/null +++ b/dotpercent-dirs.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os +import sys + +def walk(path): + #print >> sys.stderr, path + emptydir = True + for f in os.listdir(path): + fpath = os.path.join(path, f) + if (not os.path.islink(fpath)) and f[0:2] != ".%": + if os.path.isfile(fpath): + emptydir = False + if os.path.isdir(fpath): + emptysubdir = walk(fpath) + emptydir = emptydir and emptysubdir + if emptysubdir: + dest = os.path.join(path, ".%%%s" % f) + if not os.path.exists(dest): + print "mv -i '%s' '%s'" % (fpath.replace("'", "'\\''"), dest.replace("'", "'\\''")) + os.rename(fpath, dest) + return emptydir + +def help(): + print 'Usage : %s directory' % sys.argv[0] + sys.exit(1) + +if len(sys.argv) != 2: + help() +for arg in sys.argv[1:]: + if arg == '-h' or arg == '--help': + help() + +print "#!/bin/sh" +walk(sys.argv[1]) diff --git a/pseudo-empty-dirs.sh b/pseudo-empty-dirs.sh deleted file mode 100755 index 54f7a46..0000000 --- a/pseudo-empty-dirs.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/zsh - -# masque les dossiers qui ne contiennent que des fichiers dont le nom est -# .%fichier, récursivement (les dossiers sont masqués en préfixant ".%" à -# leur nom, donc les dossiers ne contenant que des fichiers / dossiers -# masqués le seront eux aussi. -# Un dossier n'est pas masqué ssi il contient au moins un fichier -# (regular file) ou dossier non masqué. - -setopt dotglob - -dohide() { - local x pseudo_empty subdirs si - echo "[$1] $PWD" - cd "./$1" - pseudo_empty="1" - si=1 - subdirs=() - for x in *(N/); do - if [ "${x[0,2]}" != ".%" ]; then - subdirs[si]="$x" - si=$(($si+1)) - fi - done - while [ $si -gt 1 ]; do - si=$(($si-1)) - dohide "${subdirs[si]}" - done - for x in *(/,.NoN); do # N = pas d'erreur quand vide, oN = order none, / = dossiers, . = fichiers - if [ "${x[0,2]}" != ".%" ]; then - pseudo_empty="0" - break; - fi - done - cd .. - [ "$pseudo_empty" = "1" ] && [ "${1[0,2]}" != ".%" ] && mv -i -- "$1" ".%$1" -} - -dohide . diff --git a/unhide-dotpercent.py b/unhide-dotpercent.py new file mode 100755 index 0000000..dfe63df --- /dev/null +++ b/unhide-dotpercent.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os +import sys + +def walk(path): + #print >> sys.stderr, path + for f in os.listdir(path): + fpath = os.path.join(path, f) + if f[0:2] == ".%": + ff = f + while ff[0:2] == ".%": + ff = ff[2:] + dest = os.path.join(path, ff) + if not os.path.exists(dest): + print "i-have-moved -i '%s' '%s'" % (fpath.replace("'", "'\\''"), dest.replace("'", "'\\''")) + os.rename(fpath, dest) + if os.path.isdir(fpath) and not os.path.islink(fpath): + walk(fpath) +def help(): + print 'Usage : %s directory' % sys.argv[0] + sys.exit(1) + +if len(sys.argv) != 2: + help() +for arg in sys.argv[1:]: + if arg == '-h' or arg == '--help': + help() + +print "#!/bin/sh" +walk(sys.argv[1]) diff --git a/updatehash.py b/updatehash.py index bbeee57..d73cd4f 100755 --- a/updatehash.py +++ b/updatehash.py @@ -59,7 +59,7 @@ def update(connection,cursor,path): cursor.execute("insert or replace into files(tag,timestamp,path,md5,sha1,mtime,size) values(?,?,?,?,?,?,?)", values) currentTime = time.clock() - if abs(lastTime-currentTime) >= 0.1: + if abs(lastTime-currentTime) >= 10: lastTime = currentTime connection.commit() print "commit!" @@ -82,7 +82,7 @@ def help(): print 'Usage : %s database-file directory' % sys.argv[0] sys.exit(1) -if len(sys.argv) < 3: +if len(sys.argv) != 3: help() for arg in sys.argv[1:]: if arg == '-h' or arg == '--help': diff --git a/updatehash.sql b/updatehash.sql index fb63a6f..f463976 100644 --- a/updatehash.sql +++ b/updatehash.sql @@ -21,5 +21,5 @@ select (select path from files where rowid == hashesother.id) from hashesother w select (select path from files where rowid == hashesother.id),(select (select path from files where rowid == hashesA.id) from hashesA where hashesA.hash == hashesother.hash) from hashesother where hashesother.hash in (select hash from hashesA); # Rename (prepend ".% to file name) files not in folder A which have a duplicate in folder A. -[ -e hashes.db ] && sqlite3 hashes.db "select (select path from files where rowid == hashesother.id) from hashesother where hashesother.hash in (select hash from hashesA);" > dup.lst +[ -e hashes.db ] && sqlite3 hashes.db "select (select path from files where rowid == hashesother.id) from hashesother where hashesother.hash in (select hash from hashesA);" | sort > dup.lst pv -l dup.lst | while read ab; do file="${ab##*/}"; dir="${ab%/*}"; dest="${dir}/.%${file}"; if [ -e "$ab" ]; then [ "$file" != "${file#.%}" ] || [ -e "$dest" ] || mv -i -- "$ab" "$dest"; fi; done