Rewrote some tools in python, so they are safer, and work faster.

This commit is contained in:
Georges Dupéron 2012-04-07 12:52:01 +02:00
parent ddd3cd2421
commit 56c735c248
5 changed files with 69 additions and 42 deletions

35
dotpercent-dirs.py Executable file
View File

@ -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])

View File

@ -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 .

31
unhide-dotpercent.py Executable file
View File

@ -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])

View File

@ -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':

View File

@ -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