Last active
August 29, 2015 14:07
-
-
Save ChiChou/b55b6df5ecbdf6b347ff to your computer and use it in GitHub Desktop.
Automatic replace iconfont and css content for iconfont.cn project
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 zipfile, sys, os, re, urllib2, tempfile | |
BASE_DIR = 'example/static/' | |
FONT_DIR = os.path.join(BASE_DIR, 'fonts') | |
CSS_DIR = os.path.join(BASE_DIR, 'css') | |
def usage(): | |
print "Invalid param." | |
print "Usage: %s iconfont.zip" % sys.argv[0] | |
sys.exit(1) | |
def error(msg): | |
print msg | |
sys.exit(1) | |
def extract(zip_file): | |
try: | |
zfile = zipfile.ZipFile(zip_file) | |
except (IOError, zipfile.BadZipfile, zipfile.LargeZipFile): | |
error('Unable to read %s, please make sure that you have specified a valid package' % zip_file) | |
name_list = zfile.namelist() | |
for name in name_list: | |
(dirname, file_name) = os.path.split(name) | |
if re.match(r'^iconfont\.(eot|ttf|svg|woff|css)$', file_name): | |
content = zfile.read(name) | |
directory = FONT_DIR | |
if file_name == 'iconfont.css': | |
directory = CSS_DIR | |
# automatically replace path | |
content = content.replace("url('", "url('../fonts/") | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
dest = os.path.join(directory, file_name) | |
print "Updating %s..." % dest, | |
with open(dest, 'w') as f: | |
f.write(content) | |
print "(%s bytes written)" % len(content) | |
print "%s files updated. Done." % len(name_list) | |
if __name__ == "__main__": | |
try: | |
param = sys.argv[1] | |
if os.path.exists(param) and os.path.isfile(param): | |
extract(param) | |
else: | |
raise ValueError('Invalid file %s' % param) | |
except: | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment