3 Walk the directory tree and replace the '20XX' copyright dates in lines with 4 'Copyright 2003-20XX' with '2016'. Usage: 6 ./bump_copyright.py # use "." as the start directory 7 ./bump_copyright.py dirname # use dirname as the start directory 10 from __future__
import print_function
16 search_string =
r'Copyright \(c\) 2003-20\d\d' 17 replace_string =
r'Copyright (c) 2003-2020' 22 Check if the directory path has any directories leading with a '.' (accept 23 the current working directory as okay). 25 dirs = dirpath.split(
"/")
28 m = re.match(
r'^\.(.+)', dir)
36 Get all the files NOT in hidden directories 39 for dirpath, dirnames, filenames
in os.walk(root_dir):
42 for filename
in filenames:
44 if not re.search(sys.argv[0].
split(
"/")[-1], filename):
45 fullpath = os.path.join(dirpath, filename)
46 path_collection.append(fullpath)
47 return path_collection
52 Look for the copyright string and sub the date 54 for path
in path_collection:
55 with
open(path,
'r+')
as p:
57 pattern = re.compile(search)
58 contents = pattern.sub(replace, contents)
64 if __name__ ==
'__main__':
66 if '-h' in sys.argv
or '--help' in sys.argv:
75 do_the_subs(path_collection, search_string, replace_string)
int open(const char *, int)
Opens a file descriptor.
def get_the_paths(root_dir)
def has_hidden_dirs(dirpath)
def do_the_subs(path_collection, search, replace)
void split(std::string const &s, char c, OutIter dest)