Skip to content

Instantly share code, notes, and snippets.

@choffmeister
Created May 23, 2014 13:26
Show Gist options
  • Save choffmeister/bd2e2c2d012547ca6eb7 to your computer and use it in GitHub Desktop.
Save choffmeister/bd2e2c2d012547ca6eb7 to your computer and use it in GitHub Desktop.
Mass converts file encodings. Allows include filters and exclude filters.
#!/usr/bin/python
# The MIT License (MIT)
# Copyright (c) 2014 Christian Hoffmeister
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import subprocess
exclude_dir = ['.git', '.svn']
include_ext = ['.java', '.xml']
source_enc = 'ISO-8859-1'
target_enc = 'UTF-8'
def get_root_dir():
if len(sys.argv) >= 2:
return os.path.realpath(sys.argv[1])
else:
return os.path.dirname(os.path.realpath(__file__))
def list_files(current_dir, exclude):
current_dir = os.path.realpath(current_dir)
children_names = os.listdir(current_dir)
result = []
for child_path in map(lambda name: os.path.join(current_dir, name), children_names):
if os.path.isdir(child_path):
if not os.path.basename(child_path) in exclude:
result += list_files(child_path, exclude)
else:
result += [child_path]
return result
def execute(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return out
def write(path, content):
f = open(path, 'w')
f.write(content)
f.close()
root_dir = get_root_dir()
for file_path in filter(lambda file_path: os.path.splitext(file_path)[1] in include_ext, list_files(root_dir, exclude_dir)):
print os.path.relpath(file_path, root_dir)
converted = execute(['iconv', '-f', source_enc, '-t', target_enc, file_path])
# withouth this hack \r\n line endings are converted to \r\r\n line endings
converted = converted.replace('\r\r\n', '\r\n')
write(file_path, converted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment