玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python工具火狐扩展打包

python 培训

这是一个关于火狐扩展打包工具的python源码。
需要做的是:读取配置文件的扩展名、版本号并且过滤版本工具、开发工具产生的目录和文件。最后一项是,要压缩打包成一个符合规范的xpi文件。

python Firefox打包

需要的环境是:ubuntu、python2.7 测试通过 。window应该也行,没做测试。

下面是执行chmod +x filename 命令变成可执行文件,Python工具火狐扩展打包源码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# packaging of firefox add-on and studing python
#
# usage: put this file to addon folder. 
#		 double click or running in the shell 
# last edited: Dec. 2011

from shutil import ignore_patterns, rmtree ,copytree
import os, tempfile, os.path as osp,re, zipfile

def packaging(src):
	"""
		reading install.rdf and packaging a xpi file. 
		for example: 
			xxx-0.1.xpi
	"""
	work_copy = osp.dirname(src)
	
	addon_info = "".join(open(work_copy + osp.sep + "install.rdf"))
	addon_name = re.search("(?<=em\:name\=\").*(?=\")",addon_info).group(0)
	addon_version =  re.search("(?<=em\:version\=\").*(?=\")",addon_info).group(0)

	temp_copy_base = tempfile.mkdtemp()
	temp_copy = osp.join(temp_copy_base,addon_name)
	
	xpi_name = "%s-%s.xpi" % (addon_name,addon_version)
	xpi_fullpath = osp.join(work_copy,xpi_name);
	
	print """
	Add-on    : %s
	Version   : %s
	Work Copy : %s
	Temp Copy : %s
	XPI File  : %s
	""" % (addon_name,addon_version,work_copy,temp_copy, xpi_name)

	print "copying work to temp dir..."
	copytree(work_copy,temp_copy,ignore=ignore_patterns('*.xpi','.*','*.bat','*.py','*LOG','*~','*.swp'))

	print "packaging xpi..."
	compress(temp_copy,xpi_fullpath);

	print "cleaning..."
	rmtree(temp_copy_base)

#www.iplaypython.com
def compress(src,dstfile):
	"""
		compressing src to dstfile
	"""
	afile = zipfile.ZipFile(dstfile,"w",zipfile.ZIP_DEFLATED)
	for root,dirs,files in os.walk(src):
		for filename in files:
			abspath = osp.join(root,filename)
			relpath = osp.relpath(abspath,src)
			afile.write(abspath, relpath)


if __name__ == "__main__":
	packaging(__file__)

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2367.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-19 09:50 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部