Created
August 4, 2014 05:13
-
-
Save meoow/e4cb2eec29b6f6ff2a9b to your computer and use it in GitHub Desktop.
Clean retina resources and multi-language files (keeping English and Chinese-Simplified)
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
#!/usr/bin/env python2.7 | |
import os, re, sys | |
from itertools import chain | |
import shutil as sh | |
def main(walkingPath=None, cleanPic=True, cleanLang=True, dryrun=False): | |
matchPatternlang = re.compile(r'^(?!(?:english|base|en(?=\.)|chinese|zh(?=\.)|zh[-_](?!tw))).+\.lproj$', flags=re.I) | |
matchPattern2xp = re.compile(r'^(.+)(@2x)(\.(?:png|bmp|tiff|tif|icns|gif))$', flags=re.I) | |
for path, dirs, files in os.walk(walkingPath): | |
if cleanLang: | |
for d in dirs: | |
if matchPatternlang.match(d): | |
langdir=os.path.join(path, d) | |
try: | |
if not dryrun: | |
sh.rmtree(langdir) | |
except OSError: | |
sys.stderr.write('Cannot remove: {0}\n'.format(langdir)) | |
else: | |
print langdir | |
if cleanPic: | |
for f in files: | |
matched = matchPattern2xp.search(f) | |
if matched: | |
x1p=''.join(chain([path, os.path.sep], (i for c,i in enumerate(matched.groups()) if c in (0, 2)))) | |
if os.path.exists(x1p): | |
removedFile = os.path.join(path, f) | |
try: | |
if not dryrun: | |
os.remove(removedFile) | |
except OSError: | |
sys.stderr.write('Cannot remove: {0}\n'.format(langdir)) | |
else: | |
print removedFile | |
if __name__=='__main__': | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument('-p', action='store_true', default=False, dest='pic', help='clean pic') | |
parser.add_argument('-l', action='store_true', default=False, dest='lang', help='clean lang') | |
parser.add_argument('-d', action='store_true', default=False, dest='dry', help='dry-run') | |
parser.add_argument('DIR', default='.', nargs=1, type=str, help='searching dir') | |
if sys.argv[1:] == []: | |
parser.print_help() | |
raise SystemExit | |
args = parser.parse_args() | |
if not os.path.isdir(args.DIR[0]): | |
raise SystemExit('Invalid Directory!') | |
if args.pic is False and args.lang is False: | |
raise SystemExit('You have to choose one of cleaning types!') | |
main(args.DIR[0], args.pic, args.lang, args.dry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment