2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 This scripts sets up a virtualenv and installs TPS into it.
8 It's probably best to specify a path NOT inside the repo, otherwise
9 all the virtualenv files will show up in e.g. hg status.
18 here
= os
.path
.dirname(os
.path
.abspath(__file__
))
20 ***********************************************************************
22 To run TPS, activate the virtualenv using:
23 source {TARGET}/{BIN_NAME}
25 To change your TPS config, please edit the file:
29 runtps --binary=/path/to/firefox
31 See runtps --help for all options
33 ***********************************************************************
36 if sys
.platform
== "win32":
37 bin_name
= os
.path
.join("Scripts", "activate.bat")
38 python_env
= os
.path
.join("Scripts", "python.exe")
40 bin_name
= os
.path
.join("bin", "activate")
41 python_env
= os
.path
.join("bin", "python")
44 def setup_virtualenv(target
):
45 print("Creating new virtual environment:", os
.path
.abspath(target
))
46 # system_site_packages=True so we have access to setuptools.
47 venv
.create(target
, system_site_packages
=True)
50 def update_configfile(source
, target
, replacements
):
53 with
open(source
) as config
:
55 for source_string
, target_string
in replacements
.items():
57 line
= line
.replace(source_string
, target_string
)
60 with
open(target
, "w") as config
:
66 parser
= optparse
.OptionParser("Usage: %prog [options] path_to_venv")
71 help="Keep the existing config file.",
77 metavar
="FX_ACCOUNT_PASSWORD",
79 help="The Firefox Account password.",
88 help="The Python interpreter to use.",
93 dest
="sync_passphrase",
94 metavar
="SYNC_ACCOUNT_PASSPHRASE",
96 help="The old Firefox Sync account passphrase.",
101 dest
="sync_password",
102 metavar
="SYNC_ACCOUNT_PASSWORD",
104 help="The old Firefox Sync account password.",
109 dest
="sync_username",
110 metavar
="SYNC_ACCOUNT_USERNAME",
112 help="The old Firefox Sync account username.",
118 metavar
="FX_ACCOUNT_USERNAME",
120 help="The Firefox Account username.",
123 (options
, args
) = parser
.parse_args(args
=None, values
=None)
126 parser
.error("Path to the environment has to be specified")
130 setup_virtualenv(target
)
132 # Activate tps environment
135 # Install TPS in environment
136 subprocess
.check_call(
137 [os
.path
.join(target
, python_env
), os
.path
.join(here
, "setup.py"), "install"]
140 # Get the path to tests and extensions directory by checking check where
141 # the tests and extensions directories are located
142 sync_dir
= os
.path
.abspath(os
.path
.join(here
, "..", "..", "services", "sync"))
143 if os
.path
.exists(sync_dir
):
144 testdir
= os
.path
.join(sync_dir
, "tests", "tps")
145 extdir
= os
.path
.join(sync_dir
, "tps", "extensions")
147 testdir
= os
.path
.join(here
, "tests")
148 extdir
= os
.path
.join(here
, "extensions")
150 if not options
.keep_config
:
152 os
.path
.join(here
, "config", "config.json.in"),
153 os
.path
.join(target
, "config.json"),
155 "__TESTDIR__": testdir
.replace("\\", "/"),
156 "__EXTENSIONDIR__": extdir
.replace("\\", "/"),
157 "__FX_ACCOUNT_USERNAME__": options
.username
,
158 "__FX_ACCOUNT_PASSWORD__": options
.password
,
159 "__SYNC_ACCOUNT_USERNAME__": options
.sync_username
,
160 "__SYNC_ACCOUNT_PASSWORD__": options
.sync_password
,
161 "__SYNC_ACCOUNT_PASSPHRASE__": options
.sync_passphrase
,
165 if not (options
.username
and options
.password
):
166 print("\nFirefox Account credentials not specified.")
167 if not (options
.sync_username
and options
.sync_password
and options
.passphrase
):
168 print("\nFirefox Sync account credentials not specified.")
170 # Print the user instructions
171 print(usage_message
.format(TARGET
=target
, BIN_NAME
=bin_name
))
174 def activate(target
):
175 # This is a lightly modified copy of `activate_this.py`, which existed when
176 # venv was an external package, but doesn't come with the builtin venv support.
177 old_os_path
= os
.environ
.get("PATH", "")
178 os
.environ
["PATH"] = (
179 os
.path
.dirname(os
.path
.abspath(__file__
)) + os
.pathsep
+ old_os_path
181 base
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
182 if sys
.platform
== "win32":
183 site_packages
= os
.path
.join(base
, "Lib", "site-packages")
185 site_packages
= os
.path
.join(
186 base
, "lib", "python%s" % sys
.version
[:3], "site-packages"
188 prev_sys_path
= list(sys
.path
)
191 site
.addsitedir(site_packages
)
192 sys
.real_prefix
= sys
.prefix
194 # Move the added items to the front of the path:
196 for item
in list(sys
.path
):
197 if item
not in prev_sys_path
:
198 new_sys_path
.append(item
)
199 sys
.path
.remove(item
)
200 sys
.path
[:0] = new_sys_path
203 if __name__
== "__main__":