Last active
September 24, 2017 06:27
-
-
Save oglops/2e76637fc79addfb08437d6f73decb8a to your computer and use it in GitHub Desktop.
restore leading spaces/tabs/newlines from original www 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
import os | |
import re | |
cn_dir = '~/github/tomato-gui' | |
en_dir = '~/github/tomato-gui-en' | |
cn_dir = os.path.expanduser(cn_dir) | |
en_dir = os.path.expanduser(en_dir) | |
# loop through files in en_dir | |
file_types = ('.asp', '.js', '.jsx') | |
def getSize(filename): | |
st = os.stat(filename) | |
return st.st_size | |
def fix_file(file): | |
print 'fixing file', file | |
cn_file = os.path.join(cn_dir, file) | |
en_file = os.path.join(en_dir, file) | |
if getSize(cn_file) == getSize(en_file): | |
print 'skipping %s, because file sizes are the same' % file | |
return | |
new_cn_file = '' | |
with open(en_file) as f: | |
with open(cn_file) as g: | |
use_last_cn = False | |
for l_num, en_line in enumerate(f): | |
l_num += 1 | |
if not use_last_cn: | |
cn_line = g.readline() | |
match_en = re.search('^([\s\t\n]+)', en_line) | |
match_cn = re.search('^([\s\t\n]+)', cn_line) | |
if match_en: | |
match_en_prefix = match_en.groups()[0] | |
# if en line is empty | |
if match_en_prefix.endswith('\n'): | |
new_cn_file += match_en_prefix | |
if match_cn and match_cn.groups()[0] == match_en_prefix: | |
pass | |
else: | |
use_last_cn = True | |
continue | |
# if cn line has prefix but does not match en_line | |
if match_cn: | |
if match_en_prefix == match_cn.groups()[0]: | |
new_cn_file += cn_line | |
else: | |
new_cn_file += match_en_prefix+cn_line | |
else: | |
new_cn_file += match_en_prefix+cn_line | |
# check cn file in the same line | |
else: | |
print 'no match enline, use cn_line [%s]' % cn_line | |
new_cn_file += cn_line | |
use_last_cn = False | |
open(cn_file, 'w').write(new_cn_file) | |
files = [] | |
for file in os.listdir(en_dir): | |
name, ext = os.path.splitext(file) | |
if ext in file_types: | |
fix_file(file) | |
# break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment