Bug 1739680 [wpt PR 31525] - Move some tests to external/wpt/web-animations/responsiv...
[gecko.git] / testing / tps / create_venv.py
blob0114dea1ff944290cab1da1e4363610490f3c6f6
1 #!/usr/bin/env python
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/.
6 """
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.
10 """
12 from __future__ import absolute_import, print_function
14 import six
15 import optparse
16 import os
17 import shutil
18 import subprocess
19 import sys
20 import zipfile
23 here = os.path.dirname(os.path.abspath(__file__))
24 usage_message = """
25 ***********************************************************************
27 To run TPS, activate the virtualenv using:
28 source {TARGET}/{BIN_NAME}
30 To change your TPS config, please edit the file:
31 {TARGET}/config.json
33 To execute tps use:
34 runtps --binary=/path/to/firefox
36 See runtps --help for all options
38 ***********************************************************************
39 """
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")
50 else:
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())
62 return target
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))
71 zip_path = download(
72 URL_VIRTUALENV + VERSION_VIRTUALENV, os.path.join(here, "virtualenv.zip")
75 try:
76 with zipfile.ZipFile(zip_path, "r") as f:
77 f.extractall(here)
79 print("Creating new virtual environment")
80 cmd_args = [sys.executable, script_path, target]
82 if python_bin:
83 cmd_args.extend(["-p", python_bin])
85 subprocess.check_call(cmd_args)
86 finally:
87 try:
88 os.remove(zip_path)
89 except OSError:
90 pass
92 shutil.rmtree(os.path.dirname(script_path), ignore_errors=True)
95 def update_configfile(source, target, replacements):
96 lines = []
98 with open(source) as config:
99 for line in config:
100 for source_string, target_string in six.iteritems(replacements):
101 if target_string:
102 line = line.replace(source_string, target_string)
103 lines.append(line)
105 with open(target, "w") as config:
106 for line in lines:
107 config.write(line)
110 def main():
111 parser = optparse.OptionParser("Usage: %prog [options] path_to_venv")
112 parser.add_option(
113 "--keep-config",
114 dest="keep_config",
115 action="store_true",
116 help="Keep the existing config file.",
118 parser.add_option(
119 "--password",
120 type="string",
121 dest="password",
122 metavar="FX_ACCOUNT_PASSWORD",
123 default=None,
124 help="The Firefox Account password.",
126 parser.add_option(
127 "-p",
128 "--python",
129 type="string",
130 dest="python",
131 metavar="PYTHON_BIN",
132 default=None,
133 help="The Python interpreter to use.",
135 parser.add_option(
136 "--sync-passphrase",
137 type="string",
138 dest="sync_passphrase",
139 metavar="SYNC_ACCOUNT_PASSPHRASE",
140 default=None,
141 help="The old Firefox Sync account passphrase.",
143 parser.add_option(
144 "--sync-password",
145 type="string",
146 dest="sync_password",
147 metavar="SYNC_ACCOUNT_PASSWORD",
148 default=None,
149 help="The old Firefox Sync account password.",
151 parser.add_option(
152 "--sync-username",
153 type="string",
154 dest="sync_username",
155 metavar="SYNC_ACCOUNT_USERNAME",
156 default=None,
157 help="The old Firefox Sync account username.",
159 parser.add_option(
160 "--username",
161 type="string",
162 dest="username",
163 metavar="FX_ACCOUNT_USERNAME",
164 default=None,
165 help="The Firefox Account username.",
168 (options, args) = parser.parse_args(args=None, values=None)
170 if len(args) != 1:
171 parser.error("Path to the environment has to be specified")
172 target = args[0]
173 assert target
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")
192 else:
193 testdir = os.path.join(here, "tests")
194 extdir = os.path.join(here, "extensions")
196 if not options.keep_config:
197 update_configfile(
198 os.path.join(here, "config", "config.json.in"),
199 os.path.join(target, "config.json"),
200 replacements={
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__":
221 main()