Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. """ Replace UPPERCASE chars in filenames to lowercase,
  2. and (in future) fix include directives respectively to new filenames. It's
  3. needed for cross-platform compilation C++ code: on Windows, you can not doubt
  4. about headers name. But, when you try to build it on *nix system case does
  5. matter.
  6.  
  7. Usage:
  8. renamer.py DIRECTORY [--dry-run]
  9. renamer.py (-h | --help)
  10. Options:
  11. DIRECTORY Run rename recursive
  12. FILES rename only given files
  13. --dry-run just display what wanna do
  14. -h, --help Show this help text
  15. -v, --version Stow version
  16. """
  17.  
  18. import os
  19. import string
  20. import shutil
  21.  
  22. from docopt import docopt
  23.  
  24. pattern = string.Template("$dir/$this -> $dir/$result )")
  25.  
  26.  
  27. def inform(f, dp):
  28. print(pattern.substitute(this=f, result=f.lower(), dir=dp))
  29.  
  30.  
  31. def determine_path():
  32. if not os.path.abspath(directoryToProcess):
  33. if not os.path.abspath("/".join([os.path.curdir, directoryToProcess])):
  34. print("can't find directory %s in current directory, also it's not "
  35. "a absolute-path directory. Exit.")
  36. exit()
  37. else:
  38. path = "/".join([os.path.curdir, directoryToProcess])
  39. else:
  40. path = directoryToProcess
  41.  
  42. return path.replace("\\", "/")
  43.  
  44.  
  45. if __name__ == '__main__':
  46. arguments = docopt(__doc__, version='0.1')
  47.  
  48. dryRun = arguments['--dry-run']
  49. directoryToProcess = arguments["DIRECTORY"]
  50.  
  51. path = determine_path()
  52.  
  53. print("Start replacing in %s" % path)
  54. # make reserve
  55. reserve_path = path.rpartition("/")[0] + "/backup"
  56.  
  57. if not dryRun:
  58. shutil.copytree(path, reserve_path)
  59. print("create reserve %s" % reserve_path)
  60.  
  61. for dirpath, dirs, files in os.walk(path):
  62. dp = dirpath.replace('\\', '/') + "/"
  63. for f in files:
  64. src_path, dst_path = dp + f, dp + f.lower()
  65. if not dryRun:
  66. os.rename(src_path, dst_path)
  67. inform(f, dp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement