0
|
1 import os
|
|
2 import sys
|
|
3 import xml.dom.minidom
|
|
4 import subprocess
|
|
5 import shutil
|
|
6 import zipfile
|
|
7 from ConfigParser import ConfigParser
|
|
8
|
|
9 # Path to the root of the extension, relative to where this script is
|
|
10 # located.
|
|
11 EXT_SUBDIR = "ambnews"
|
|
12
|
|
13 def find_profile_dir(name):
|
|
14 """
|
|
15 Given the name of a Firefox profile, attempts to find the absolute
|
|
16 path to its directory. If it can't be found, None is returned.
|
|
17 """
|
|
18
|
|
19 base_path = None
|
|
20 if sys.platform == "darwin":
|
|
21 base_path = os.path.expanduser(
|
|
22 "~/Library/Application Support/Firefox/"
|
|
23 )
|
|
24 elif sys.platform.startswith("win"):
|
|
25 # TODO: This only works on 2000/XP/Vista, not 98/Me.
|
|
26 appdata = os.environ["APPDATA"]
|
|
27 base_path = os.path.join(appdata, "Mozilla\\Firefox")
|
|
28 else:
|
|
29 base_path = os.path.expanduser("~/.mozilla/firefox/")
|
|
30 inifile = os.path.join(base_path, "profiles.ini")
|
|
31 config = ConfigParser()
|
|
32 config.read(inifile)
|
|
33 profiles = [section for section in config.sections()
|
|
34 if section.startswith("Profile")]
|
|
35 for profile in profiles:
|
|
36 if config.get(profile, "Name") == name:
|
|
37 # TODO: Look at IsRelative?
|
|
38 path = config.get(profile, "Path")
|
|
39 if not os.path.isabs(path):
|
|
40 path = os.path.join(base_path, path)
|
|
41 print "Found profile '%s' at %s." % (name, path)
|
|
42 return path
|
|
43 print "Couldn't find a profile called '%s'." % name
|
|
44 return None
|
|
45
|
|
46 def get_install_rdf_dom(path_to_extension_root):
|
|
47 rdf_path = os.path.join(path_to_extension_root, "install.rdf")
|
|
48 rdf = xml.dom.minidom.parse(rdf_path)
|
|
49 return rdf
|
|
50
|
|
51 def get_install_rdf_property(path_to_extension_root, property):
|
|
52 rdf = get_install_rdf_dom(path_to_extension_root)
|
|
53 element = rdf.documentElement.getElementsByTagName(property)[0]
|
|
54 return element.firstChild.nodeValue
|
|
55
|
|
56 if __name__ == "__main__":
|
|
57 args = sys.argv[1:]
|
|
58 if not args:
|
|
59 print "usage: %s <command>" % sys.argv[0]
|
|
60 print
|
|
61 print "'command' can be one of the following:"
|
|
62 print
|
|
63 print " test - run unit tests"
|
|
64 print " install - install to the given profile"
|
|
65 print " uninstall - uninstall from the given profile"
|
|
66 print " build-xpi - build an xpi of the addon"
|
|
67 print
|
|
68 sys.exit(1)
|
|
69
|
|
70 main = __import__("__main__")
|
|
71 mydir = os.path.abspath(os.path.split(main.__file__)[0])
|
|
72
|
|
73 path_to_extension_root = os.path.join(mydir, EXT_SUBDIR)
|
|
74
|
|
75 cmd = args[0]
|
|
76
|
|
77 if cmd == "test":
|
|
78 print "No unit tests."
|
|
79 sys.exit(1)
|
|
80 elif cmd in ["install", "uninstall"]:
|
|
81 if len(args) != 2:
|
|
82 print "Attempting to find location of default profile..."
|
|
83
|
|
84 profile_dir = find_profile_dir("default")
|
|
85 else:
|
|
86 profile_dir = args[1]
|
|
87 if not os.path.exists(profile_dir):
|
|
88 print "Attempting to find a profile with the name '%s'." % (
|
|
89 profile_dir
|
|
90 )
|
|
91 profile_dir = find_profile_dir(profile_dir)
|
|
92
|
|
93 if not (profile_dir and os.path.exists(profile_dir) and
|
|
94 os.path.isdir(profile_dir)):
|
|
95 print "Can't resolve profile directory; aborting."
|
|
96 sys.exit(1)
|
|
97
|
|
98 extension_id = get_install_rdf_property(path_to_extension_root,
|
|
99 "em:id")
|
|
100
|
|
101 extension_file = os.path.join(profile_dir,
|
|
102 "extensions",
|
|
103 extension_id)
|
|
104 files_to_remove = ["compreg.dat",
|
|
105 "xpti.dat"]
|
|
106 for filename in files_to_remove:
|
|
107 abspath = os.path.join(profile_dir, filename)
|
|
108 if os.path.exists(abspath):
|
|
109 os.remove(abspath)
|
|
110 if os.path.exists(extension_file):
|
|
111 if os.path.isdir(extension_file):
|
|
112 shutil.rmtree(extension_file)
|
|
113 else:
|
|
114 os.remove(extension_file)
|
|
115 if cmd == "install":
|
|
116 fileobj = open(extension_file, "w")
|
|
117 fileobj.write(path_to_extension_root)
|
|
118 fileobj.close()
|
|
119 print "Extension '%s' installed." % extension_id
|
|
120 else:
|
|
121 print "Extension '%s' uninstalled." % extension_id
|
|
122 elif cmd == "build-xpi":
|
|
123 version = get_install_rdf_property(path_to_extension_root,
|
|
124 "em:version")
|
|
125 extname = get_install_rdf_property(path_to_extension_root,
|
|
126 "em:name").lower()
|
|
127 extname = extname.replace(" ", "")
|
|
128 zfname = "%s-%s.xpi" % (extname, version)
|
|
129 zf = zipfile.ZipFile(zfname,
|
|
130 "w",
|
|
131 zipfile.ZIP_DEFLATED)
|
|
132 for dirpath, dirnames, filenames in os.walk(path_to_extension_root):
|
|
133 for filename in filenames:
|
|
134 abspath = os.path.join(dirpath, filename)
|
|
135 arcpath = abspath[len(path_to_extension_root)+1:]
|
|
136 zf.write(abspath, arcpath)
|
|
137 print "Created %s." % zfname
|
|
138 else:
|
|
139 print "Unknown command '%s'" % cmd
|
|
140 sys.exit(1)
|