-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyright.py
42 lines (36 loc) · 1.7 KB
/
copyright.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- 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('''\
// ****************************************************************************
// File "{0}"
// Copyright © Dmitry Morozov 2022
// If you want to use this file please contact me by [email protected].
// ****************************************************************************
''').format(os.path.basename(filepath)) + match.group(1)
with open(filepath, 'w') as f:
f.write(contents)
print "Wrote new: {0}".format(filepath)