Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2016 05:12
Show Gist options
  • Save anonymous/ff71292094362fc5c594 to your computer and use it in GitHub Desktop.
Save anonymous/ff71292094362fc5c594 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import sys
ORIG_LICENSE = """
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
""".strip()
NEW_LICENSE = """
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
""".strip()
def replace_license(re_match):
comment_char = re_match.group(1)
new_lines = [(comment_char + l).rstrip() for l in NEW_LICENSE.splitlines()]
return "\n".join(new_lines)
orig_lines = ORIG_LICENSE.splitlines()
orig_regex = "\n".join([r"((?://|#| \*)\s*)" + re.escape(l) for l in orig_lines])
orig_regex = re.compile(orig_regex)
leading_comment_lines = re.compile("^((//|#)\s*\n)*")
for path in sys.argv[1:]:
orig_text = file(path).read()
new_text = orig_regex.sub(replace_license, orig_text)
new_text = leading_comment_lines.sub("", new_text)
new_file = file(path, "w")
print >>new_file, new_text,
new_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment