Skip to content

Instantly share code, notes, and snippets.

@Intey
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Intey/04f8800bb56401a16424 to your computer and use it in GitHub Desktop.
Save Intey/04f8800bb56401a16424 to your computer and use it in GitHub Desktop.
crossplatform Include filenames fixer for C++ projects.
""" 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment