Created
May 6, 2018 22:21
-
-
Save junjuew/3db8e526e1317a40b2c904a21a57e85d to your computer and use it in GitHub Desktop.
A bare-minimal script to add and remove licenses on top of python 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
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import fire | |
LICENSE_CONTENT = {} | |
LICENSE_CONTENT['apache'] = """# Copyright (C) 2018 Carnegie Mellon University. All Rights Reserved. | |
# | |
# 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. | |
# ============================================================================== | |
""" | |
def remove(file_path): | |
"""Remove existing license in the file.""" | |
with open(file_path) as f: | |
content = f.read().splitlines() | |
end_of_license_line_number = -1 | |
for line_number, line in enumerate(content): | |
if line.startswith("#"): | |
end_of_license_line_number = line_number | |
else: | |
break | |
print("{}: end of license line number: {}".format(file_path, end_of_license_line_number)) | |
with open(file_path, 'w') as f: | |
no_license_content = content[end_of_license_line_number + 1:] | |
f.write('\n'.join(no_license_content)) | |
f.write('\n') | |
def add(file_path, license='apache'): | |
"""Add license to file.""" | |
with open(file_path, 'r+') as f: | |
content = f.read() | |
f.seek(0, 0) | |
f.write(LICENSE_CONTENT[license] + content) | |
if __name__ == '__main__': | |
fire.Fire() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment