Skip to content

Instantly share code, notes, and snippets.

@waruqi
Created January 9, 2017 15:12
Show Gist options
  • Save waruqi/561248b97f6864eeba3d70e3371c177f to your computer and use it in GitHub Desktop.
Save waruqi/561248b97f6864eeba3d70e3371c177f to your computer and use it in GitHub Desktop.
replace multiline text
#!/usr/bin/python
#
##The Text Replacer
#
# @author ruki
# @file replace.py
#
# ######################################################################################
# imports
#
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from optparse import OptionParser
import os
import re
import sys
import shutil
import zipfile
import subprocess
import ConfigParser
# ######################################################################################
# implementation
#
# replace text in the root directory
def replace(rootdir, oldtext, newtext):
# replace all
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
(basename, extension) = os.path.splitext(filename)
if basename.find(".") == 0:
continue
print(os.path.join(parent, filename))
# load old content
content = None
with open(os.path.join(parent, filename), 'r') as file:
content = file.read()
print(content.find(oldtext))
# replace content
content = content.replace(oldtext, newtext)
# save new content
with open(os.path.join(parent, filename), 'w') as file:
file.write(content)
# ######################################################################################
# main
#
def main():
# set utf8 for sys
reload(sys)
sys.setdefaultencoding('utf8')
# init options
parser = OptionParser("usage: %prog [options]")
parser.add_option("-r", "--rootdir", dest = "rootdir", help = "The root directory")
(options, args) = parser.parse_args()
oldtext = """/*!The Graphic 2D Box Library
*
* GBox2 is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* GBox2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GBox2;
* If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
*
* Copyright (C) 2009 - 2012, ruki All rights reserved.
"""
newtext = """/*!The Graphic Box Library
*
* 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.
*
* Copyright (C) 2009 - 2017, TBOOX Open Source Group.
"""
# replace it
replace(options.rootdir, oldtext, newtext)
# ok
return 0
# entry
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment