3
# This Source Code Form is subject to the terms of the Mozilla Public
4
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
# You can obtain one at http://mozilla.org/MPL/2.0/.
8
Creates and/or modifies a Firefox profile.
9
The profile can be modified by passing in addons to install or preferences to set.
10
If no profile is specified, a new profile is created and the path of the resulting profile is printed.
14
from addons import AddonManager
15
from optparse import OptionParser
16
from prefs import Preferences
17
from profile import Profile
19
__all__ = ['MozProfileCLI', 'cli']
21
class MozProfileCLI(object):
25
def __init__(self, args=sys.argv[1:]):
26
self.parser = OptionParser(description=__doc__)
27
self.add_options(self.parser)
28
(self.options, self.args) = self.parser.parse_args(args)
30
def add_options(self, parser):
32
parser.add_option("-p", "--profile", dest="profile",
33
help="The path to the profile to operate on. If none, creates a new profile in temp directory")
34
parser.add_option("-a", "--addon", dest="addons",
35
action="append", default=[],
36
help="Addon paths to install. Can be a filepath, a directory containing addons, or a url")
37
parser.add_option("--addon-manifests", dest="addon_manifests",
39
help="An addon manifest to install")
40
parser.add_option("--pref", dest="prefs",
41
action='append', default=[],
42
help="A preference to set. Must be a key-value pair separated by a ':'")
43
parser.add_option("--preferences", dest="prefs_files",
44
action='append', default=[],
46
help="read preferences from a JSON or INI file. For INI, use 'file.ini:section' to specify a particular section.")
48
def profile_args(self):
49
"""arguments to instantiate the profile class"""
50
return dict(profile=self.options.profile,
51
addons=self.options.addons,
52
addon_manifests=self.options.addon_manifests,
53
preferences=self.preferences())
55
def preferences(self):
56
"""profile preferences"""
58
# object to hold preferences
61
# add preferences files
62
for prefs_file in self.options.prefs_files:
63
prefs.add_file(prefs_file)
65
# change CLI preferences into 2-tuples
68
for pref in self.options.prefs:
69
if separator not in pref:
70
self.parser.error("Preference must be a key-value pair separated by a ':' (You gave: %s)" % pref)
71
cli_prefs.append(pref.split(separator, 1))
74
prefs.add(cli_prefs, cast=True)
79
def cli(args=sys.argv[1:]):
81
# process the command line
82
cli = MozProfileCLI(args)
85
kwargs = cli.profile_args()
86
kwargs['restore'] = False
87
profile = Profile(**kwargs)
89
# if no profile was passed in print the newly created profile
90
if not cli.options.profile:
93
if __name__ == '__main__':