dotpercent-dirs.py (1036B)
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import os 4 import sys 5 6 def walk(path): 7 #print >> sys.stderr, path 8 emptydir = True 9 for f in os.listdir(path): 10 fpath = os.path.join(path, f) 11 if (not os.path.islink(fpath)) and f[0:2] != ".%": 12 if os.path.isfile(fpath): 13 emptydir = False 14 if os.path.isdir(fpath): 15 emptysubdir = walk(fpath) 16 emptydir = emptydir and emptysubdir 17 if emptysubdir: 18 dest = os.path.join(path, ".%%%s" % f) 19 if not os.path.exists(dest): 20 print "i-have-moved -i -- '%s' '%s'" % (fpath.replace("'", "'\\''"), dest.replace("'", "'\\''")) 21 os.rename(fpath, dest) 22 return emptydir 23 24 def help(): 25 print 'Usage : %s directory > "undo-dotpercent-dirs-$(date).sh"' % sys.argv[0] 26 sys.exit(1) 27 28 if len(sys.argv) != 2: 29 help() 30 for arg in sys.argv[1:]: 31 if arg == '-h' or arg == '--help': 32 help() 33 34 print "#!/bin/sh" 35 print "echo 'Redefine the i-have-moved command at the beginning of this script to undo, e.g.:'" 36 print "echo 'i-have-moved() { mv -i -- \"$4\" \"$3\"; }'" 37 walk(sys.argv[1])