-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackaging.py
executable file
·65 lines (53 loc) · 1.65 KB
/
packaging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python2
# -*- 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
#
# author: mattmonkey
# website: mattmonkey.github.com
# 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('scriptdemo','*.xpi','.*','*.bat','*.py','*LOG','*~','*.swp'))
print "packaging xpi..."
compress(temp_copy,xpi_fullpath);
print "cleaning..."
rmtree(temp_copy_base)
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)
afile.close();
if __name__ == "__main__":
packaging(__file__)