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.
12 from __future__
import absolute_import
, print_function
23 here
= os
.path
.dirname(os
.path
.abspath(__file__
))
25 ***********************************************************************
27 To run TPS, activate the virtualenv using:
28 source {TARGET}/{BIN_NAME}
30 To change your TPS config, please edit the file:
34 runtps --binary=/path/to/firefox
36 See runtps --help for all options
38 ***********************************************************************
41 # Link to the folder, which contains the zip archives of virtualenv
42 URL_VIRTUALENV
= "https://codeload.github.com/pypa/virtualenv/zip/"
43 VERSION_VIRTUALENV
= "15.0.0"
46 if sys
.platform
== "win32":
47 bin_name
= os
.path
.join("Scripts", "activate.bat")
48 activate_env
= os
.path
.join("Scripts", "activate_this.py")
49 python_env
= os
.path
.join("Scripts", "python.exe")
51 bin_name
= os
.path
.join("bin", "activate")
52 activate_env
= os
.path
.join("bin", "activate_this.py")
53 python_env
= os
.path
.join("bin", "python")
56 def download(url
, target
):
57 """Downloads the specified url to the given target."""
58 response
= six
.moves
.urllib
.request
.urlopen(url
)
59 with
open(target
, "wb") as f
:
60 f
.write(response
.read())
65 def setup_virtualenv(target
, python_bin
=None):
66 script_path
= os
.path
.join(
67 here
, "virtualenv-%s" % VERSION_VIRTUALENV
, "virtualenv.py"
70 print("Downloading virtualenv {}".format(VERSION_VIRTUALENV
))
72 URL_VIRTUALENV
+ VERSION_VIRTUALENV
, os
.path
.join(here
, "virtualenv.zip")
76 with zipfile
.ZipFile(zip_path
, "r") as f
:
79 print("Creating new virtual environment")
80 cmd_args
= [sys
.executable
, script_path
, target
]
83 cmd_args
.extend(["-p", python_bin
])
85 subprocess
.check_call(cmd_args
)
92 shutil
.rmtree(os
.path
.dirname(script_path
), ignore_errors
=True)
95 def update_configfile(source
, target
, replacements
):
98 with
open(source
) as config
:
100 for source_string
, target_string
in six
.iteritems(replacements
):
102 line
= line
.replace(source_string
, target_string
)
105 with
open(target
, "w") as config
:
111 parser
= optparse
.OptionParser("Usage: %prog [options] path_to_venv")
116 help="Keep the existing config file.",
122 metavar
="FX_ACCOUNT_PASSWORD",
124 help="The Firefox Account password.",
131 metavar
="PYTHON_BIN",
133 help="The Python interpreter to use.",
138 dest
="sync_passphrase",
139 metavar
="SYNC_ACCOUNT_PASSPHRASE",
141 help="The old Firefox Sync account passphrase.",
146 dest
="sync_password",
147 metavar
="SYNC_ACCOUNT_PASSWORD",
149 help="The old Firefox Sync account password.",
154 dest
="sync_username",
155 metavar
="SYNC_ACCOUNT_USERNAME",
157 help="The old Firefox Sync account username.",
163 metavar
="FX_ACCOUNT_USERNAME",
165 help="The Firefox Account username.",
168 (options
, args
) = parser
.parse_args(args
=None, values
=None)
171 parser
.error("Path to the environment has to be specified")
175 setup_virtualenv(target
, python_bin
=options
.python
)
177 # Activate tps environment
178 tps_env
= os
.path
.join(target
, activate_env
)
179 exec(open(tps_env
).read(), dict(__file__
=tps_env
))
181 # Install TPS in environment
182 subprocess
.check_call(
183 [os
.path
.join(target
, python_env
), os
.path
.join(here
, "setup.py"), "install"]
186 # Get the path to tests and extensions directory by checking check where
187 # the tests and extensions directories are located
188 sync_dir
= os
.path
.abspath(os
.path
.join(here
, "..", "..", "services", "sync"))
189 if os
.path
.exists(sync_dir
):
190 testdir
= os
.path
.join(sync_dir
, "tests", "tps")
191 extdir
= os
.path
.join(sync_dir
, "tps", "extensions")
193 testdir
= os
.path
.join(here
, "tests")
194 extdir
= os
.path
.join(here
, "extensions")
196 if not options
.keep_config
:
198 os
.path
.join(here
, "config", "config.json.in"),
199 os
.path
.join(target
, "config.json"),
201 "__TESTDIR__": testdir
.replace("\\", "/"),
202 "__EXTENSIONDIR__": extdir
.replace("\\", "/"),
203 "__FX_ACCOUNT_USERNAME__": options
.username
,
204 "__FX_ACCOUNT_PASSWORD__": options
.password
,
205 "__SYNC_ACCOUNT_USERNAME__": options
.sync_username
,
206 "__SYNC_ACCOUNT_PASSWORD__": options
.sync_password
,
207 "__SYNC_ACCOUNT_PASSPHRASE__": options
.sync_passphrase
,
211 if not (options
.username
and options
.password
):
212 print("\nFirefox Account credentials not specified.")
213 if not (options
.sync_username
and options
.sync_password
and options
.passphrase
):
214 print("\nFirefox Sync account credentials not specified.")
216 # Print the user instructions
217 print(usage_message
.format(TARGET
=target
, BIN_NAME
=bin_name
))
220 if __name__
== "__main__":