comparison manage.py @ 0:7180966f48bf

Origination.
author Atul Varma <varmaa@toolness.com>
date Thu, 16 Apr 2009 15:23:22 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7180966f48bf
1 import os
2 import sys
3 import xml.dom.minidom
4 import subprocess
5 import shutil
6 import zipfile
7 import shutil
8 import distutils.dir_util
9 from ConfigParser import ConfigParser
10
11 # Path to the root of the extension, relative to where this script is
12 # located.
13 EXT_SUBDIR = "JSWeakRef"
14
15 # Full path to xpcshell; if it's not an absolute path, it's assumed
16 # to be on the user's PATH.
17 g_xpcshell_path = "xpcshell"
18
19 g_mydir = os.path.abspath(os.path.split(__import__("__main__").__file__)[0])
20
21 def clear_dir(dirname):
22 if os.path.exists(dirname) and os.path.isdir(dirname):
23 shutil.rmtree(dirname)
24
25 def find_profile_dir(name):
26 """
27 Given the name of a Firefox profile, attempts to find the absolute
28 path to its directory. If it can't be found, None is returned.
29 """
30
31 base_path = None
32 if sys.platform == "darwin":
33 base_path = os.path.expanduser(
34 "~/Library/Application Support/Firefox/"
35 )
36 elif sys.platform.startswith("win"):
37 # TODO: This only works on 2000/XP/Vista, not 98/Me.
38 appdata = os.environ["APPDATA"]
39 base_path = os.path.join(appdata, "Mozilla\\Firefox")
40 elif sys.platform == "cygwin":
41 appdata = os.environ["APPDATA"]
42 base_path = os.path.join(appdata, "Mozilla\\Firefox")
43 else:
44 base_path = os.path.expanduser("~/.mozilla/firefox/")
45 inifile = os.path.join(base_path, "profiles.ini")
46 config = ConfigParser()
47 config.read(inifile)
48 profiles = [section for section in config.sections()
49 if section.startswith("Profile")]
50 for profile in profiles:
51 if config.get(profile, "Name") == name:
52 # TODO: Look at IsRelative?
53 path = config.get(profile, "Path")
54 if not os.path.isabs(path):
55 path = os.path.join(base_path, path)
56 print "Found profile '%s' at %s." % (name, path)
57 return path
58 print "Couldn't find a profile called '%s'." % name
59 return None
60
61 def get_install_rdf_dom(path_to_extension_root):
62 rdf_path = os.path.join(path_to_extension_root, "install.rdf")
63 rdf = xml.dom.minidom.parse(rdf_path)
64 return rdf
65
66 def get_install_rdf_property(path_to_extension_root, property):
67 rdf = get_install_rdf_dom(path_to_extension_root)
68 element = rdf.documentElement.getElementsByTagName(property)[0]
69 return element.firstChild.nodeValue
70
71 def run_program(args, **kwargs):
72 retval = subprocess.call(args, **kwargs)
73 if retval:
74 print "Process failed with exit code %d." % retval
75 sys.exit(retval)
76
77 def run_python_script(args):
78 run_program([sys.executable] + args)
79
80 def get_xpcom_info():
81 cmdline = [
82 os.path.join(os.path.dirname(g_xpcshell_path),
83 "run-mozilla.sh"),
84 g_xpcshell_path,
85 os.path.join(g_mydir, "get_xpcom_info.js")
86 ]
87 if not os.path.exists(cmdline[0]):
88 cmdline = cmdline[1:]
89 popen = subprocess.Popen(
90 cmdline,
91 stdout = subprocess.PIPE
92 )
93 retval = popen.wait()
94 assert retval == 0
95 os_target, xpcomabi = popen.stdout.read().splitlines()
96 comsd = os.path.join(os.path.dirname(g_xpcshell_path),
97 "components")
98 return dict(comsd = comsd,
99 os_target = os_target,
100 xpcomabi = xpcomabi)
101
102 if __name__ == "__main__":
103 args = sys.argv[1:]
104 if not args:
105 print "usage: %s <command>" % sys.argv[0]
106 print
107 print "'command' can be one of the following:"
108 print
109 print " try - run Firefox w/ new profile and extension installed"
110 print " install - install to the given profile"
111 print " uninstall - uninstall from the given profile"
112 print " build-xpi - build an xpi of the addon"
113 print " build-components - build C++ XPCOM components"
114 print
115 sys.exit(1)
116
117 if os.environ.get("OBJDIR"):
118 g_xpcshell_path = os.path.join(os.environ["OBJDIR"],
119 "dist", "bin", g_xpcshell_path)
120
121 path_to_extension_root = os.path.join(g_mydir, EXT_SUBDIR)
122
123 cmd = args[0]
124
125 if cmd == "try":
126 import systemtests
127 import jsbridge
128 print ("Starting Firefox with a new profile and "
129 "the extension installed...")
130 moz = jsbridge.start_from_settings(systemtests.settings)
131 print "Firefox started, quit it or press CTRL-C to exit."
132 try:
133 moz.wait()
134 except KeyboardInterrupt:
135 moz.stop()
136 print "Farewell."
137 sys.exit(0)
138 if cmd in ["install", "uninstall"]:
139 if len(args) != 2:
140 print "Attempting to find location of default profile..."
141
142 profile_dir = find_profile_dir("default")
143 else:
144 profile_dir = args[1]
145 if not os.path.exists(profile_dir):
146 print "Attempting to find a profile with the name '%s'." % (
147 profile_dir
148 )
149 profile_dir = find_profile_dir(profile_dir)
150
151 if not (profile_dir and os.path.exists(profile_dir) and
152 os.path.isdir(profile_dir)):
153 print "Can't resolve profile directory; aborting."
154 sys.exit(1)
155
156 extension_id = get_install_rdf_property(path_to_extension_root,
157 "em:id")
158
159 extension_file = os.path.join(profile_dir,
160 "extensions",
161 extension_id)
162 files_to_remove = ["compreg.dat",
163 "xpti.dat"]
164 for filename in files_to_remove:
165 abspath = os.path.join(profile_dir, filename)
166 if os.path.exists(abspath):
167 os.remove(abspath)
168 if os.path.exists(extension_file):
169 if os.path.isdir(extension_file):
170 shutil.rmtree(extension_file)
171 else:
172 os.remove(extension_file)
173 if cmd == "install":
174 #if cygwin, change the path to windows format so firefox can understand it
175 if sys.platform == "cygwin":
176 file = 'cygpath.exe -w ' + path_to_extension_root
177 path_to_extension_root = "".join(os.popen(file).readlines()).replace("\n", " ").rstrip()
178
179 extdir = os.path.dirname(extension_file)
180 if not os.path.exists(extdir):
181 distutils.dir_util.mkpath(extdir)
182 fileobj = open(extension_file, "w")
183 fileobj.write(path_to_extension_root)
184 fileobj.close()
185 print "Extension '%s' installed." % extension_id
186 else:
187 print "Extension '%s' uninstalled." % extension_id
188 elif cmd == "build-xpi":
189 version = get_install_rdf_property(path_to_extension_root,
190 "em:version")
191 extname = get_install_rdf_property(path_to_extension_root,
192 "em:name").lower()
193 zfname = "%s-%s.xpi" % (extname, version)
194 zf = zipfile.ZipFile(zfname,
195 "w",
196 zipfile.ZIP_DEFLATED)
197 for dirpath, dirnames, filenames in os.walk(path_to_extension_root):
198 for filename in filenames:
199 abspath = os.path.join(dirpath, filename)
200 arcpath = abspath[len(path_to_extension_root)+1:]
201 zf.write(abspath, arcpath)
202 print "Created %s." % zfname
203 elif cmd == "build-components":
204 if "TOPSRCDIR" not in os.environ:
205 print ("Please set the TOPSRCDIR environment variable "
206 "to the root of your mozilla-central checkout. "
207 "If you're on Windows, this should be a standard "
208 "Windows-style path, NOT a unix-style path.")
209 sys.exit(1)
210 if "OBJDIR" not in os.environ:
211 print ("Please set the OBJDIR envirionment variable "
212 "to the root of your objdir. "
213 "If you're on Windows, this should be a standard "
214 "Windows-style path, NOT a unix-style path.")
215 sys.exit(1)
216 xpcominfo = get_xpcom_info()
217 topsrcdir = os.environ["TOPSRCDIR"]
218 objdir = os.environ["OBJDIR"]
219 comp_src_dir = os.path.join(g_mydir, "components")
220 rel_dest_dir = os.path.join("browser", "components", "JSWeakRef")
221 comp_dest_dir = os.path.join(topsrcdir, rel_dest_dir)
222 comp_xpi_dir = os.path.join(objdir, "dist", "xpi-stage",
223 "JSWeakRef-components", "components")
224 comp_plat_dir = os.path.join(
225 g_mydir, "JSWeakRef", "platform",
226 "%(os_target)s_%(xpcomabi)s" % xpcominfo,
227 "components",
228 )
229
230 clear_dir(comp_dest_dir)
231 clear_dir(comp_xpi_dir)
232 clear_dir(comp_plat_dir)
233
234 shutil.copytree(comp_src_dir, comp_dest_dir)
235
236 # Ensure that these paths are unix-like on Windows.
237 sh_pwd = subprocess.Popen(["sh", "-c", "pwd"],
238 cwd=topsrcdir,
239 stdout=subprocess.PIPE)
240 sh_pwd.wait()
241 unix_topsrcdir = sh_pwd.stdout.read().strip()
242 unix_rel_dest_dir = rel_dest_dir.replace("\\", "/")
243
244 # We're specifying 'perl' here because we have to for this
245 # to work on Windows.
246 run_program(["perl",
247 os.path.join(topsrcdir, "build", "autoconf",
248 "make-makefile"),
249 "-t", unix_topsrcdir,
250 unix_rel_dest_dir],
251 cwd=objdir)
252 run_program(["make"],
253 cwd=os.path.join(objdir, rel_dest_dir))
254
255 shutil.copytree(comp_xpi_dir, comp_plat_dir)
256 for filename in os.listdir(comp_xpi_dir):
257 shutil.copy(os.path.join(comp_xpi_dir, filename),
258 xpcominfo["comsd"])
259
260 for filename in ["compreg.dat", "xpti.dat"]:
261 fullpath = os.path.join(xpcominfo["comsd"], filename)
262 if os.path.exists(fullpath):
263 os.unlink(fullpath)
264 else:
265 print "Unknown command '%s'" % cmd
266 sys.exit(1)