Last active
August 26, 2021 12:03
-
-
Save pakdev/f5a21d108329ebeb8e72 to your computer and use it in GitHub Desktop.
Adds/updates a copyright header on *.cs files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import re | |
import fnmatch | |
import argparse | |
from textwrap import dedent | |
parser = argparse.ArgumentParser(description='Add/update copyright on C# files') | |
parser.add_argument('root', nargs=1, help='Path to the root of the C# project') | |
args = parser.parse_args() | |
# Descend into the 'root' directory and find all *.cs files | |
files = [] | |
for root, dirnames, filenames in os.walk(args.root[0]): | |
for filename in fnmatch.filter(filenames, "*.cs"): | |
files.append(os.path.join(root, filename)) | |
print "Found {0} *.cs files".format(len(files)) | |
for filepath in files: | |
with open(filepath) as f: | |
contents = f.read() | |
# This regex will separate the contents of a *.cs file into two parts. | |
# The first part is any text that appears before either 'using' or | |
# 'namespace' - perhaps an old copyright. The second part *should* be | |
# the actual code beginning with 'using' or 'namespace'. | |
match = re.search(r"^.*?((using|namespace|/\*|#).+)$", contents, re.DOTALL) | |
if match: | |
# Make the file's now contain the user defined copyright (below) | |
# followed by a blank line followed by the actual code. | |
contents = dedent('''\ | |
// **************************************************************************** | |
// <copyright file="{0}" company="Insert Company Here"> | |
// Copyright © Insert Company Here 2014 | |
// </copyright> | |
// **************************************************************************** | |
''').format(os.path.basename(filepath)) + match.group(1) | |
with open(filepath, 'w') as f: | |
f.write(contents) | |
print "Wrote new: {0}".format(filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment