Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Replace UPPERCASE chars in filenames to lowercase,
- and (in future) fix include directives respectively to new filenames. It's
- needed for cross-platform compilation C++ code: on Windows, you can not doubt
- about headers name. But, when you try to build it on *nix system case does
- matter.
- Usage:
- renamer.py DIRECTORY [--dry-run]
- renamer.py (-h | --help)
- Options:
- DIRECTORY Run rename recursive
- FILES rename only given files
- --dry-run just display what wanna do
- -h, --help Show this help text
- -v, --version Stow version
- """
- import os
- import string
- import shutil
- from docopt import docopt
- pattern = string.Template("$dir/$this -> $dir/$result )")
- def inform(f, dp):
- print(pattern.substitute(this=f, result=f.lower(), dir=dp))
- def determine_path():
- if not os.path.abspath(directoryToProcess):
- if not os.path.abspath("/".join([os.path.curdir, directoryToProcess])):
- print("can't find directory %s in current directory, also it's not "
- "a absolute-path directory. Exit.")
- exit()
- else:
- path = "/".join([os.path.curdir, directoryToProcess])
- else:
- path = directoryToProcess
- return path.replace("\\", "/")
- if __name__ == '__main__':
- arguments = docopt(__doc__, version='0.1')
- dryRun = arguments['--dry-run']
- directoryToProcess = arguments["DIRECTORY"]
- path = determine_path()
- print("Start replacing in %s" % path)
- # make reserve
- reserve_path = path.rpartition("/")[0] + "/backup"
- if not dryRun:
- shutil.copytree(path, reserve_path)
- print("create reserve %s" % reserve_path)
- for dirpath, dirs, files in os.walk(path):
- dp = dirpath.replace('\\', '/') + "/"
- for f in files:
- src_path, dst_path = dp + f, dp + f.lower()
- if not dryRun:
- os.rename(src_path, dst_path)
- inform(f, dp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement