2016-07-30 01:37:37 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2015-11-29 01:46:08 +00:00
|
|
|
import contextlib
|
2016-07-30 01:37:37 +00:00
|
|
|
import fnmatch
|
2015-11-29 01:46:08 +00:00
|
|
|
import os
|
2016-07-30 01:37:37 +00:00
|
|
|
import platform
|
|
|
|
import runpy
|
|
|
|
import shlex
|
2015-11-29 01:46:08 +00:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
2015-11-29 02:38:23 +00:00
|
|
|
import tarfile
|
2016-07-30 01:37:37 +00:00
|
|
|
import zipfile
|
2016-11-27 23:51:52 +00:00
|
|
|
from os.path import join, abspath, dirname, exists, basename
|
2016-07-30 01:37:37 +00:00
|
|
|
|
2015-11-29 01:46:08 +00:00
|
|
|
import click
|
2016-02-11 17:51:47 +00:00
|
|
|
import pysftp
|
2015-11-29 01:46:08 +00:00
|
|
|
|
|
|
|
# https://virtualenv.pypa.io/en/latest/userguide.html#windows-notes
|
|
|
|
# scripts and executables on Windows go in ENV\Scripts\ instead of ENV/bin/
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
VENV_BIN = "Scripts"
|
2016-11-27 23:51:52 +00:00
|
|
|
PYINSTALLER_ARGS = [
|
|
|
|
# PyInstaller < 3.2 does not handle Python 3.5's ucrt correctly.
|
|
|
|
"-p", r"C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs\x86",
|
|
|
|
]
|
2015-11-29 01:46:08 +00:00
|
|
|
else:
|
|
|
|
VENV_BIN = "bin"
|
2016-11-27 23:51:52 +00:00
|
|
|
PYINSTALLER_ARGS = []
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
# ZipFile and tarfile have slightly different APIs. Fix that.
|
2015-11-29 01:46:08 +00:00
|
|
|
if platform.system() == "Windows":
|
|
|
|
def Archive(name):
|
2016-02-11 17:51:47 +00:00
|
|
|
a = zipfile.ZipFile(name, "w")
|
2015-11-29 01:46:08 +00:00
|
|
|
a.add = a.write
|
|
|
|
return a
|
|
|
|
else:
|
|
|
|
def Archive(name):
|
2016-02-11 17:51:47 +00:00
|
|
|
return tarfile.open(name, "w:gz")
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2016-07-30 02:07:48 +00:00
|
|
|
ROOT_DIR = abspath(join(dirname(__file__), ".."))
|
2016-07-30 01:37:37 +00:00
|
|
|
RELEASE_DIR = join(ROOT_DIR, "release")
|
2015-11-29 01:46:08 +00:00
|
|
|
|
|
|
|
BUILD_DIR = join(RELEASE_DIR, "build")
|
2016-07-30 01:37:37 +00:00
|
|
|
DIST_DIR = join(RELEASE_DIR, "dist")
|
|
|
|
|
|
|
|
PYINSTALLER_SPEC = join(RELEASE_DIR, "specs")
|
2016-11-25 15:46:49 +00:00
|
|
|
# PyInstaller 3.2 does not bundle pydivert's Windivert binaries
|
|
|
|
PYINSTALLER_HOOKS = join(RELEASE_DIR, "hooks")
|
2015-11-29 01:46:08 +00:00
|
|
|
PYINSTALLER_TEMP = join(BUILD_DIR, "pyinstaller")
|
|
|
|
PYINSTALLER_DIST = join(BUILD_DIR, "binaries")
|
|
|
|
|
|
|
|
VENV_DIR = join(BUILD_DIR, "venv")
|
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
# Project Configuration
|
2016-10-19 22:56:38 +00:00
|
|
|
VERSION_FILE = join(ROOT_DIR, "mitmproxy", "version.py")
|
2016-07-30 01:37:37 +00:00
|
|
|
BDISTS = {
|
|
|
|
"mitmproxy": ["mitmproxy", "mitmdump", "mitmweb"],
|
|
|
|
"pathod": ["pathoc", "pathod"]
|
2015-11-29 01:46:08 +00:00
|
|
|
}
|
|
|
|
if platform.system() == "Windows":
|
2016-07-30 01:37:37 +00:00
|
|
|
BDISTS["mitmproxy"].remove("mitmproxy")
|
|
|
|
|
|
|
|
TOOLS = [
|
|
|
|
tool
|
2016-11-27 23:51:52 +00:00
|
|
|
for tools in sorted(BDISTS.values())
|
2016-07-30 01:37:37 +00:00
|
|
|
for tool in tools
|
|
|
|
]
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2015-12-03 16:55:42 +00:00
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
def git(args: str) -> str:
|
|
|
|
with chdir(ROOT_DIR):
|
|
|
|
return subprocess.check_output(["git"] + shlex.split(args)).decode()
|
|
|
|
|
|
|
|
|
2016-11-27 23:51:52 +00:00
|
|
|
def get_version() -> str:
|
|
|
|
return runpy.run_path(VERSION_FILE)["VERSION"]
|
|
|
|
|
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
def get_snapshot_version() -> str:
|
|
|
|
last_tag, tag_dist, commit = git("describe --tags --long").strip().rsplit("-", 2)
|
2016-02-11 17:51:47 +00:00
|
|
|
tag_dist = int(tag_dist)
|
|
|
|
if tag_dist == 0:
|
2016-02-15 23:22:38 +00:00
|
|
|
return get_version()
|
2016-02-11 17:51:47 +00:00
|
|
|
else:
|
2016-03-31 16:07:47 +00:00
|
|
|
# The wheel build tag (we use the commit) must start with a digit, so we include "0x"
|
|
|
|
return "{version}dev{tag_dist:04}-0x{commit}".format(
|
2016-02-15 23:22:38 +00:00
|
|
|
version=get_version(), # this should already be the next version
|
2016-02-11 17:51:47 +00:00
|
|
|
tag_dist=tag_dist,
|
2016-07-30 01:37:37 +00:00
|
|
|
commit=commit
|
2016-02-11 17:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
def archive_name(bdist: str) -> str:
|
2016-02-11 17:51:47 +00:00
|
|
|
platform_tag = {
|
|
|
|
"Darwin": "osx",
|
|
|
|
"Windows": "win32",
|
|
|
|
"Linux": "linux"
|
|
|
|
}.get(platform.system(), platform.system())
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
ext = "zip"
|
|
|
|
else:
|
|
|
|
ext = "tar.gz"
|
|
|
|
return "{project}-{version}-{platform}.{ext}".format(
|
2016-07-30 01:37:37 +00:00
|
|
|
project=bdist,
|
2016-02-15 23:22:38 +00:00
|
|
|
version=get_version(),
|
2016-02-11 17:51:47 +00:00
|
|
|
platform=platform_tag,
|
|
|
|
ext=ext
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
def wheel_name() -> str:
|
2016-11-27 23:51:52 +00:00
|
|
|
return "mitmproxy-{version}-py3-none-any.whl".format(
|
2016-02-15 23:22:38 +00:00
|
|
|
version=get_version(),
|
2015-11-29 18:05:58 +00:00
|
|
|
)
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2015-12-03 16:55:42 +00:00
|
|
|
|
2015-11-29 01:46:08 +00:00
|
|
|
@contextlib.contextmanager
|
2016-07-30 01:37:37 +00:00
|
|
|
def chdir(path: str):
|
2015-11-29 01:46:08 +00:00
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(path)
|
|
|
|
yield
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
|
|
|
|
@click.group(chain=True)
|
2016-02-19 03:43:26 +00:00
|
|
|
def cli():
|
2015-11-29 01:46:08 +00:00
|
|
|
"""
|
|
|
|
mitmproxy build tool
|
|
|
|
"""
|
2016-02-19 03:43:26 +00:00
|
|
|
pass
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2015-12-03 16:55:42 +00:00
|
|
|
|
2015-11-29 01:46:08 +00:00
|
|
|
@cli.command("contributors")
|
|
|
|
def contributors():
|
|
|
|
"""
|
|
|
|
Update CONTRIBUTORS.md
|
|
|
|
"""
|
2016-02-15 23:22:38 +00:00
|
|
|
with chdir(ROOT_DIR):
|
|
|
|
print("Updating CONTRIBUTORS...")
|
|
|
|
contributors_data = git("shortlog -n -s")
|
2016-07-30 01:37:37 +00:00
|
|
|
with open("CONTRIBUTORS", "wb") as f:
|
|
|
|
f.write(contributors_data.encode())
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2015-12-03 16:55:42 +00:00
|
|
|
|
2015-11-29 01:46:08 +00:00
|
|
|
@cli.command("bdist")
|
2016-11-27 23:51:52 +00:00
|
|
|
def make_bdist():
|
2015-11-29 01:46:08 +00:00
|
|
|
"""
|
|
|
|
Build a binary distribution
|
|
|
|
"""
|
2016-07-30 01:37:37 +00:00
|
|
|
if exists(PYINSTALLER_TEMP):
|
2015-11-29 01:46:08 +00:00
|
|
|
shutil.rmtree(PYINSTALLER_TEMP)
|
2016-07-30 01:37:37 +00:00
|
|
|
if exists(PYINSTALLER_DIST):
|
2015-11-29 01:46:08 +00:00
|
|
|
shutil.rmtree(PYINSTALLER_DIST)
|
|
|
|
|
2016-11-27 23:51:52 +00:00
|
|
|
os.makedirs(DIST_DIR, exist_ok=True)
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2016-11-27 23:51:52 +00:00
|
|
|
for bdist, tools in sorted(BDISTS.items()):
|
2016-07-30 01:37:37 +00:00
|
|
|
with Archive(join(DIST_DIR, archive_name(bdist))) as archive:
|
2016-02-27 13:26:54 +00:00
|
|
|
for tool in tools:
|
2016-11-27 23:51:52 +00:00
|
|
|
# We can't have a folder and a file with the same name.
|
|
|
|
if tool == "mitmproxy":
|
|
|
|
tool = "mitmproxy_main"
|
2016-03-12 17:55:25 +00:00
|
|
|
# This is PyInstaller, so it messes up paths.
|
|
|
|
# We need to make sure that we are in the spec folder.
|
2016-07-30 01:37:37 +00:00
|
|
|
with chdir(PYINSTALLER_SPEC):
|
2016-03-12 17:55:25 +00:00
|
|
|
print("Building %s binary..." % tool)
|
2016-11-27 23:51:52 +00:00
|
|
|
excludes = []
|
|
|
|
if tool != "mitmweb":
|
|
|
|
excludes.append("mitmproxy.tools.web")
|
|
|
|
if tool != "mitmproxy_main":
|
|
|
|
excludes.append("mitmproxy.tools.console")
|
2016-03-12 17:55:25 +00:00
|
|
|
subprocess.check_call(
|
|
|
|
[
|
2016-11-27 23:51:52 +00:00
|
|
|
"pyinstaller",
|
2016-03-12 17:55:25 +00:00
|
|
|
"--clean",
|
|
|
|
"--workpath", PYINSTALLER_TEMP,
|
|
|
|
"--distpath", PYINSTALLER_DIST,
|
2016-11-25 15:46:49 +00:00
|
|
|
"--additional-hooks-dir", PYINSTALLER_HOOKS,
|
|
|
|
"--onefile",
|
|
|
|
"--console",
|
|
|
|
"--icon", "icon.ico",
|
2016-03-12 17:55:25 +00:00
|
|
|
# This is PyInstaller, so setting a
|
|
|
|
# different log level obviously breaks it :-)
|
|
|
|
# "--log-level", "WARN",
|
|
|
|
]
|
2016-11-27 23:51:52 +00:00
|
|
|
+ [x for e in excludes for x in ["--exclude-module", e]]
|
|
|
|
+ PYINSTALLER_ARGS
|
|
|
|
+ [tool]
|
2016-03-12 17:55:25 +00:00
|
|
|
)
|
2016-11-25 15:46:49 +00:00
|
|
|
# Delete the spec file - we're good without.
|
|
|
|
os.remove("{}.spec".format(tool))
|
2016-02-19 03:43:26 +00:00
|
|
|
|
|
|
|
# Test if it works at all O:-)
|
|
|
|
executable = join(PYINSTALLER_DIST, tool)
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
executable += ".exe"
|
2016-11-27 23:51:52 +00:00
|
|
|
|
|
|
|
# Remove _main suffix from mitmproxy executable
|
|
|
|
if executable.startswith("mitmproxy_main"):
|
|
|
|
shutil.move(
|
|
|
|
executable,
|
|
|
|
executable.replace("_main", "")
|
|
|
|
)
|
|
|
|
executable = executable.replace("_main", "")
|
|
|
|
|
2016-02-19 03:43:26 +00:00
|
|
|
print("> %s --version" % executable)
|
2016-07-30 01:37:37 +00:00
|
|
|
print(subprocess.check_output([executable, "--version"]).decode())
|
2016-02-19 03:43:26 +00:00
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
archive.add(executable, basename(executable))
|
|
|
|
print("Packed {}.".format(archive_name(bdist)))
|
2015-11-29 01:46:08 +00:00
|
|
|
|
|
|
|
|
2016-02-11 17:51:47 +00:00
|
|
|
@cli.command("upload-release")
|
2015-11-29 01:46:08 +00:00
|
|
|
@click.option('--username', prompt=True)
|
|
|
|
@click.password_option(confirmation_prompt=False)
|
|
|
|
@click.option('--repository', default="pypi")
|
2016-02-15 23:39:07 +00:00
|
|
|
def upload_release(username, password, repository):
|
2015-11-29 01:46:08 +00:00
|
|
|
"""
|
2016-02-15 23:39:07 +00:00
|
|
|
Upload wheels to PyPI
|
2015-11-29 01:46:08 +00:00
|
|
|
"""
|
2016-02-27 13:26:54 +00:00
|
|
|
filename = wheel_name()
|
|
|
|
print("Uploading {} to {}...".format(filename, repository))
|
|
|
|
subprocess.check_call([
|
|
|
|
"twine",
|
|
|
|
"upload",
|
|
|
|
"-u", username,
|
|
|
|
"-p", password,
|
|
|
|
"-r", repository,
|
|
|
|
join(DIST_DIR, filename)
|
|
|
|
])
|
2015-11-29 01:46:08 +00:00
|
|
|
|
|
|
|
|
2016-02-11 17:51:47 +00:00
|
|
|
@cli.command("upload-snapshot")
|
|
|
|
@click.option("--host", envvar="SNAPSHOT_HOST", prompt=True)
|
|
|
|
@click.option("--port", envvar="SNAPSHOT_PORT", type=int, default=22)
|
2016-02-11 18:34:29 +00:00
|
|
|
@click.option("--user", envvar="SNAPSHOT_USER", prompt=True)
|
|
|
|
@click.option("--private-key", default=join(RELEASE_DIR, "rtool.pem"))
|
|
|
|
@click.option("--private-key-password", envvar="SNAPSHOT_PASS", prompt=True, hide_input=True)
|
2016-02-11 17:51:47 +00:00
|
|
|
@click.option("--wheel/--no-wheel", default=False)
|
|
|
|
@click.option("--bdist/--no-bdist", default=False)
|
2016-02-15 23:39:07 +00:00
|
|
|
def upload_snapshot(host, port, user, private_key, private_key_password, wheel, bdist):
|
2016-02-11 17:51:47 +00:00
|
|
|
"""
|
|
|
|
Upload snapshot to snapshot server
|
|
|
|
"""
|
2016-02-11 18:34:29 +00:00
|
|
|
with pysftp.Connection(host=host,
|
|
|
|
port=port,
|
|
|
|
username=user,
|
|
|
|
private_key=private_key,
|
|
|
|
private_key_pass=private_key_password) as sftp:
|
2016-07-30 01:37:37 +00:00
|
|
|
dir_name = "snapshots/v{}".format(get_version())
|
|
|
|
sftp.makedirs(dir_name)
|
|
|
|
with sftp.cd(dir_name):
|
|
|
|
files = []
|
|
|
|
if wheel:
|
|
|
|
files.append(wheel_name())
|
|
|
|
if bdist:
|
2016-11-27 23:51:52 +00:00
|
|
|
for bdist in sorted(BDISTS.keys()):
|
2016-02-27 13:26:54 +00:00
|
|
|
files.append(archive_name(bdist))
|
2016-02-11 17:51:47 +00:00
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
for f in files:
|
|
|
|
local_path = join(DIST_DIR, f)
|
|
|
|
remote_filename = f.replace(get_version(), get_snapshot_version())
|
|
|
|
symlink_path = "../{}".format(f.replace(get_version(), "latest"))
|
|
|
|
|
|
|
|
# Upload new version
|
|
|
|
print("Uploading {} as {}...".format(f, remote_filename))
|
|
|
|
with click.progressbar(length=os.stat(local_path).st_size) as bar:
|
|
|
|
# We hide the file during upload
|
|
|
|
sftp.put(
|
|
|
|
local_path,
|
|
|
|
"." + remote_filename,
|
|
|
|
callback=lambda done, total: bar.update(done - bar.pos)
|
|
|
|
)
|
2016-02-11 17:51:47 +00:00
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
# Delete old versions
|
|
|
|
old_version = f.replace(get_version(), "*")
|
|
|
|
for f_old in sftp.listdir():
|
|
|
|
if fnmatch.fnmatch(f_old, old_version):
|
|
|
|
print("Removing {}...".format(f_old))
|
|
|
|
sftp.remove(f_old)
|
|
|
|
|
|
|
|
# Show new version
|
|
|
|
sftp.rename("." + remote_filename, remote_filename)
|
|
|
|
|
|
|
|
# update symlink for the latest release
|
|
|
|
if sftp.lexists(symlink_path):
|
|
|
|
print("Removing {}...".format(symlink_path))
|
|
|
|
sftp.remove(symlink_path)
|
|
|
|
if f != wheel_name():
|
|
|
|
# "latest" isn't a proper wheel version, so this could not be installed.
|
|
|
|
# https://github.com/mitmproxy/mitmproxy/issues/1065
|
|
|
|
sftp.symlink("v{}/{}".format(get_version(), remote_filename), symlink_path)
|
2015-11-29 01:46:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|