1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
14 # The Original Code is standalone Firefox Windows performance test.
16 # The Initial Developer of the Original Code is Google Inc.
17 # Portions created by the Initial Developer are Copyright (C) 2006
18 # the Initial Developer. All Rights Reserved.
21 # Annie Sullivan <annie.sullivan@gmail.com> (original author)
22 # Alice Nodelman <anodelman@mozilla.com>
24 # Alternatively, the contents of this file may be used under the terms of
25 # either the GNU General Public License Version 2 or later (the "GPL"), or
26 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
36 # ***** END LICENSE BLOCK *****
38 """A set of functions to set up a Firefox browser with the correct
39 preferences and extensions in the given directory.
43 __author__
= 'annie.sullivan@gmail.com (Annie Sullivan)'
57 if platform
.system() == "Linux":
58 from ffprofile_unix
import *
59 elif platform
.system() in ("Windows", "Microsoft"):
60 from ffprofile_win32
import *
61 elif platform
.system() == "Darwin":
62 from ffprofile_unix
import *
64 def PrefString(name
, value
, newline
):
65 """Helper function to create a pref string for Firefox profile prefs.js
66 in the form 'user_pref("name", value);<newline>'
69 name: String containing name of pref
70 value: String containing value of pref
71 newline: Line ending to use, i.e. '\n' or '\r\n'
74 String containing 'user_pref("name", value);<newline>'
77 out_value
= str(value
)
78 if type(value
) == bool:
79 # Write bools as "true"/"false", not "True"/"False".
80 out_value
= out_value
.lower()
81 if type(value
) == str:
82 # Write strings with quotes around them.
83 out_value
= '"%s"' % value
84 return 'user_pref("%s", %s);%s' % (name
, out_value
, newline
)
87 def CreateTempProfileDir(source_profile
, prefs
, extensions
):
88 """Creates a temporary profile directory from the source profile directory
89 and adds the given prefs and links to extensions.
92 source_profile: String containing the absolute path of the source profile
93 directory to copy from.
94 prefs: Preferences to set in the prefs.js file of the new profile. Format:
95 {"PrefName1" : "PrefValue1", "PrefName2" : "PrefValue2"}
96 extensions: Guids and paths of extensions to link to. Format:
97 {"{GUID1}" : "c:\\Path\\to\\ext1", "{GUID2}", "c:\\Path\\to\\ext2"}
100 String containing the absolute path of the profile directory.
103 # Create a temporary directory for the profile, and copy the
104 # source profile to it.
105 temp_dir
= tempfile
.mkdtemp()
106 profile_dir
= os
.path
.join(temp_dir
, 'profile')
107 shutil
.copytree(source_profile
, profile_dir
)
108 MakeDirectoryContentsWritable(profile_dir
)
110 # Copy the user-set prefs to user.js
111 user_js_filename
= os
.path
.join(profile_dir
, 'user.js')
112 user_js_file
= open(user_js_filename
, 'w')
114 user_js_file
.write(PrefString(pref
, prefs
[pref
], '\n'))
117 # Add links to all the extensions.
118 extension_dir
= os
.path
.join(profile_dir
, "extensions")
119 if not os
.path
.exists(extension_dir
):
120 os
.makedirs(extension_dir
)
121 for extension
in extensions
:
122 link_file
= open(os
.path
.join(extension_dir
, extension
), 'w')
123 link_file
.write(extensions
[extension
])
126 return temp_dir
, profile_dir
128 def InstallInBrowser(firefox_path
, dir_path
):
130 Take the given directory and copies it to appropriate location in the given
133 # add the provided directory to the given firefox install
134 fromfiles
= glob
.glob(os
.path
.join(dir_path
, '*'))
135 todir
= os
.path
.join(os
.path
.dirname(firefox_path
), os
.path
.basename(os
.path
.normpath(dir_path
)))
136 for fromfile
in fromfiles
:
137 if not os
.path
.isfile(os
.path
.join(todir
, os
.path
.basename(fromfile
))):
138 shutil
.copy(fromfile
, todir
)
139 utils
.debug("installed " + fromfile
)
141 utils
.debug("WARNING: file already installed (" + fromfile
+ ")")
143 def InitializeNewProfile(firefox_path
, profile_dir
, init_url
):
144 """Runs Firefox with the new profile directory, to negate any performance
145 hit that could occur as a result of starting up with a new profile.
146 Also kills the "extra" Firefox that gets spawned the first time Firefox
147 is run with a new profile.
150 firefox_path: String containing the path to the Firefox exe
151 profile_dir: The full path to the profile directory to load
153 PROFILE_REGEX
= re
.compile('__metrics(.*)__metrics', re
.DOTALL|re
.MULTILINE
)
155 cmd
= ffprocess
.GenerateFirefoxCommandLine(firefox_path
, profile_dir
, init_url
)
156 (match
, timed_out
) = ffprocess
.RunProcessAndWaitForOutput(cmd
,