2014-06-03 20:10:01 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
"""
|
|
|
|
This scripts sets up a virtualenv and installs TPS into it.
|
|
|
|
It's probably best to specify a path NOT inside the repo, otherwise
|
|
|
|
all the virtualenv files will show up in e.g. hg status.
|
|
|
|
"""
|
|
|
|
|
2019-07-16 20:49:05 +03:00
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
2020-12-09 12:02:22 +03:00
|
|
|
import six
|
2014-06-03 20:10:01 +04:00
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2014-07-08 00:58:17 +04:00
|
|
|
import zipfile
|
|
|
|
|
2014-06-03 20:10:01 +04:00
|
|
|
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
usage_message = """
|
|
|
|
***********************************************************************
|
|
|
|
|
|
|
|
To run TPS, activate the virtualenv using:
|
|
|
|
source {TARGET}/{BIN_NAME}
|
|
|
|
|
|
|
|
To change your TPS config, please edit the file:
|
|
|
|
{TARGET}/config.json
|
|
|
|
|
|
|
|
To execute tps use:
|
|
|
|
runtps --binary=/path/to/firefox
|
|
|
|
|
|
|
|
See runtps --help for all options
|
|
|
|
|
|
|
|
***********************************************************************
|
|
|
|
"""
|
2014-07-08 00:58:17 +04:00
|
|
|
|
|
|
|
# Link to the folder, which contains the zip archives of virtualenv
|
|
|
|
URL_VIRTUALENV = "https://codeload.github.com/pypa/virtualenv/zip/"
|
2018-05-04 01:46:26 +03:00
|
|
|
VERSION_VIRTUALENV = "15.0.0"
|
2014-07-08 00:58:17 +04:00
|
|
|
|
2014-06-03 20:10:01 +04:00
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
bin_name = os.path.join("Scripts", "activate.bat")
|
|
|
|
activate_env = os.path.join("Scripts", "activate_this.py")
|
2014-06-06 10:31:32 +04:00
|
|
|
python_env = os.path.join("Scripts", "python.exe")
|
2014-06-03 20:10:01 +04:00
|
|
|
else:
|
|
|
|
bin_name = os.path.join("bin", "activate")
|
|
|
|
activate_env = os.path.join("bin", "activate_this.py")
|
2014-06-06 10:31:32 +04:00
|
|
|
python_env = os.path.join("bin", "python")
|
2014-06-03 20:10:01 +04:00
|
|
|
|
2014-07-08 00:58:17 +04:00
|
|
|
|
|
|
|
def download(url, target):
|
|
|
|
"""Downloads the specified url to the given target."""
|
2020-12-09 02:20:08 +03:00
|
|
|
response = six.moves.urllib.request.urlopen(url)
|
2014-07-08 00:58:17 +04:00
|
|
|
with open(target, "wb") as f:
|
|
|
|
f.write(response.read())
|
|
|
|
|
|
|
|
return target
|
|
|
|
|
|
|
|
|
|
|
|
def setup_virtualenv(target, python_bin=None):
|
|
|
|
script_path = os.path.join(
|
|
|
|
here, "virtualenv-%s" % VERSION_VIRTUALENV, "virtualenv.py"
|
|
|
|
)
|
|
|
|
|
2019-07-02 21:04:40 +03:00
|
|
|
print("Downloading virtualenv {}".format(VERSION_VIRTUALENV))
|
2014-07-08 00:58:17 +04:00
|
|
|
zip_path = download(
|
|
|
|
URL_VIRTUALENV + VERSION_VIRTUALENV, os.path.join(here, "virtualenv.zip")
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with zipfile.ZipFile(zip_path, "r") as f:
|
|
|
|
f.extractall(here)
|
|
|
|
|
2019-07-02 21:04:40 +03:00
|
|
|
print("Creating new virtual environment")
|
2020-10-10 02:39:01 +03:00
|
|
|
cmd_args = [sys.executable, script_path, target]
|
2014-07-08 00:58:17 +04:00
|
|
|
|
|
|
|
if python_bin:
|
|
|
|
cmd_args.extend(["-p", python_bin])
|
|
|
|
|
|
|
|
subprocess.check_call(cmd_args)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.remove(zip_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
shutil.rmtree(os.path.dirname(script_path), ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
def update_configfile(source, target, replacements):
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
with open(source) as config:
|
|
|
|
for line in config:
|
2020-12-09 12:02:22 +03:00
|
|
|
for source_string, target_string in six.iteritems(replacements):
|
2014-07-08 00:58:17 +04:00
|
|
|
if target_string:
|
|
|
|
line = line.replace(source_string, target_string)
|
|
|
|
lines.append(line)
|
|
|
|
|
|
|
|
with open(target, "w") as config:
|
|
|
|
for line in lines:
|
|
|
|
config.write(line)
|
|
|
|
|
|
|
|
|
2014-06-03 20:10:01 +04:00
|
|
|
def main():
|
|
|
|
parser = optparse.OptionParser("Usage: %prog [options] path_to_venv")
|
2018-10-16 01:42:22 +03:00
|
|
|
parser.add_option(
|
|
|
|
"--keep-config",
|
|
|
|
dest="keep_config",
|
|
|
|
action="store_true",
|
|
|
|
help="Keep the existing config file.",
|
|
|
|
)
|
2014-06-25 17:11:42 +04:00
|
|
|
parser.add_option(
|
|
|
|
"--password",
|
|
|
|
type="string",
|
|
|
|
dest="password",
|
|
|
|
metavar="FX_ACCOUNT_PASSWORD",
|
|
|
|
default=None,
|
|
|
|
help="The Firefox Account password.",
|
|
|
|
)
|
2014-06-03 20:10:01 +04:00
|
|
|
parser.add_option(
|
|
|
|
"-p",
|
|
|
|
"--python",
|
|
|
|
type="string",
|
|
|
|
dest="python",
|
|
|
|
metavar="PYTHON_BIN",
|
|
|
|
default=None,
|
|
|
|
help="The Python interpreter to use.",
|
|
|
|
)
|
2014-06-25 17:11:42 +04:00
|
|
|
parser.add_option(
|
|
|
|
"--sync-passphrase",
|
|
|
|
type="string",
|
|
|
|
dest="sync_passphrase",
|
|
|
|
metavar="SYNC_ACCOUNT_PASSPHRASE",
|
|
|
|
default=None,
|
|
|
|
help="The old Firefox Sync account passphrase.",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"--sync-password",
|
|
|
|
type="string",
|
|
|
|
dest="sync_password",
|
|
|
|
metavar="SYNC_ACCOUNT_PASSWORD",
|
|
|
|
default=None,
|
|
|
|
help="The old Firefox Sync account password.",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"--sync-username",
|
|
|
|
type="string",
|
|
|
|
dest="sync_username",
|
|
|
|
metavar="SYNC_ACCOUNT_USERNAME",
|
|
|
|
default=None,
|
|
|
|
help="The old Firefox Sync account username.",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"--username",
|
|
|
|
type="string",
|
|
|
|
dest="username",
|
|
|
|
metavar="FX_ACCOUNT_USERNAME",
|
|
|
|
default=None,
|
|
|
|
help="The Firefox Account username.",
|
|
|
|
)
|
|
|
|
|
2014-06-03 20:10:01 +04:00
|
|
|
(options, args) = parser.parse_args(args=None, values=None)
|
|
|
|
|
|
|
|
if len(args) != 1:
|
2019-01-15 18:58:52 +03:00
|
|
|
parser.error("Path to the environment has to be specified")
|
2014-06-03 20:10:01 +04:00
|
|
|
target = args[0]
|
|
|
|
assert target
|
|
|
|
|
2014-07-08 00:58:17 +04:00
|
|
|
setup_virtualenv(target, python_bin=options.python)
|
2014-06-03 20:10:01 +04:00
|
|
|
|
|
|
|
# Activate tps environment
|
|
|
|
tps_env = os.path.join(target, activate_env)
|
2019-09-24 17:44:01 +03:00
|
|
|
exec(open(tps_env).read(), dict(__file__=tps_env))
|
2014-06-03 20:10:01 +04:00
|
|
|
|
|
|
|
# Install TPS in environment
|
2014-06-06 10:31:32 +04:00
|
|
|
subprocess.check_call(
|
|
|
|
[os.path.join(target, python_env), os.path.join(here, "setup.py"), "install"]
|
|
|
|
)
|
2014-06-03 20:10:01 +04:00
|
|
|
|
|
|
|
# Get the path to tests and extensions directory by checking check where
|
|
|
|
# the tests and extensions directories are located
|
|
|
|
sync_dir = os.path.abspath(os.path.join(here, "..", "..", "services", "sync"))
|
|
|
|
if os.path.exists(sync_dir):
|
|
|
|
testdir = os.path.join(sync_dir, "tests", "tps")
|
|
|
|
extdir = os.path.join(sync_dir, "tps", "extensions")
|
|
|
|
else:
|
|
|
|
testdir = os.path.join(here, "tests")
|
|
|
|
extdir = os.path.join(here, "extensions")
|
|
|
|
|
2018-10-16 01:42:22 +03:00
|
|
|
if not options.keep_config:
|
|
|
|
update_configfile(
|
|
|
|
os.path.join(here, "config", "config.json.in"),
|
|
|
|
os.path.join(target, "config.json"),
|
|
|
|
replacements={
|
2019-01-15 18:58:52 +03:00
|
|
|
"__TESTDIR__": testdir.replace("\\", "/"),
|
|
|
|
"__EXTENSIONDIR__": extdir.replace("\\", "/"),
|
2018-10-16 01:42:22 +03:00
|
|
|
"__FX_ACCOUNT_USERNAME__": options.username,
|
|
|
|
"__FX_ACCOUNT_PASSWORD__": options.password,
|
|
|
|
"__SYNC_ACCOUNT_USERNAME__": options.sync_username,
|
|
|
|
"__SYNC_ACCOUNT_PASSWORD__": options.sync_password,
|
|
|
|
"__SYNC_ACCOUNT_PASSPHRASE__": options.sync_passphrase,
|
2020-10-26 21:34:53 +03:00
|
|
|
},
|
2018-10-16 01:42:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if not (options.username and options.password):
|
2019-07-02 21:04:40 +03:00
|
|
|
print("\nFirefox Account credentials not specified.")
|
2018-10-16 01:42:22 +03:00
|
|
|
if not (options.sync_username and options.sync_password and options.passphrase):
|
2019-07-02 21:04:40 +03:00
|
|
|
print("\nFirefox Sync account credentials not specified.")
|
2014-06-03 20:10:01 +04:00
|
|
|
|
|
|
|
# Print the user instructions
|
2019-07-02 21:04:40 +03:00
|
|
|
print(usage_message.format(TARGET=target, BIN_NAME=bin_name))
|
2014-06-03 20:10:01 +04:00
|
|
|
|
2019-01-15 18:58:52 +03:00
|
|
|
|
2014-06-03 20:10:01 +04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|