2018-03-06 21:25:38 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-05-17 09:25:32 +00:00
|
|
|
import glob
|
|
|
|
import re
|
2018-03-06 21:25:38 +00:00
|
|
|
import contextlib
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import tarfile
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
import click
|
2018-05-18 08:37:56 +00:00
|
|
|
import cryptography.fernet
|
2018-03-06 21:25:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2018-05-24 05:10:50 +00:00
|
|
|
def chdir(path: str): # pragma: no cover
|
2018-03-06 21:25:38 +00:00
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(path)
|
|
|
|
yield
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
|
2018-05-23 23:06:05 +00:00
|
|
|
class BuildError(Exception):
|
2018-03-06 21:25:38 +00:00
|
|
|
pass
|
|
|
|
|
2018-03-06 23:39:32 +00:00
|
|
|
|
2018-05-23 23:06:05 +00:00
|
|
|
class BuildEnviron:
|
|
|
|
PLATFORM_TAGS = {
|
|
|
|
"Darwin": "osx",
|
|
|
|
"Windows": "windows",
|
|
|
|
"Linux": "linux",
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
system = "",
|
|
|
|
root_dir = "",
|
|
|
|
|
|
|
|
travis_tag = "",
|
|
|
|
travis_branch = "",
|
|
|
|
travis_pull_request = "",
|
|
|
|
|
|
|
|
appveyor_repo_tag_name = "",
|
|
|
|
appveyor_repo_branch = "",
|
|
|
|
appveyor_pull_request_number = "",
|
|
|
|
|
2018-05-24 10:15:55 +00:00
|
|
|
should_build_wheel = False,
|
|
|
|
should_build_docker = False,
|
|
|
|
should_build_pyinstaller = False,
|
2018-05-23 23:06:05 +00:00
|
|
|
|
|
|
|
has_aws_creds = False,
|
|
|
|
has_twine_creds = False,
|
|
|
|
|
|
|
|
docker_username = "",
|
|
|
|
docker_password = "",
|
|
|
|
):
|
|
|
|
self.system = system
|
|
|
|
self.root_dir = root_dir
|
|
|
|
|
|
|
|
self.travis_tag = travis_tag
|
|
|
|
self.travis_branch = travis_branch
|
|
|
|
self.travis_pull_request = travis_pull_request
|
|
|
|
|
2018-05-24 10:15:55 +00:00
|
|
|
self.should_build_wheel = should_build_wheel
|
|
|
|
self.should_build_docker = should_build_docker
|
|
|
|
self.should_build_pyinstaller = should_build_pyinstaller
|
2018-05-24 05:10:50 +00:00
|
|
|
|
2018-05-23 23:06:05 +00:00
|
|
|
self.appveyor_repo_tag_name = appveyor_repo_tag_name
|
|
|
|
self.appveyor_repo_branch = appveyor_repo_branch
|
|
|
|
self.appveyor_pull_request_number = appveyor_pull_request_number
|
|
|
|
|
|
|
|
self.has_aws_creds = has_aws_creds
|
|
|
|
self.has_twine_creds = has_twine_creds
|
|
|
|
self.docker_username = docker_username
|
|
|
|
self.docker_password = docker_password
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_env(klass):
|
|
|
|
return klass(
|
2018-05-24 05:10:50 +00:00
|
|
|
system = platform.system(),
|
|
|
|
root_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), "..")),
|
2018-05-23 23:06:05 +00:00
|
|
|
|
|
|
|
travis_tag = os.environ.get("TRAVIS_TAG", ""),
|
|
|
|
travis_branch = os.environ.get("TRAVIS_BRANCH", ""),
|
|
|
|
travis_pull_request = os.environ.get("TRAVIS_PULL_REQUEST"),
|
|
|
|
|
|
|
|
appveyor_repo_tag_name = os.environ.get("APPVEYOR_REPO_TAG_NAME", ""),
|
|
|
|
appveyor_repo_branch = os.environ.get("APPVEYOR_REPO_BRANCH", ""),
|
|
|
|
appveyor_pull_request_number = os.environ.get("APPVEYOR_PULL_REQUEST_NUMBER"),
|
|
|
|
|
2018-05-24 10:15:55 +00:00
|
|
|
should_build_wheel = "WHEEL" in os.environ,
|
|
|
|
should_build_pyinstaller = "PYINSTALLER" in os.environ,
|
|
|
|
should_build_docker = "DOCKER" in os.environ,
|
2018-05-23 23:06:05 +00:00
|
|
|
|
|
|
|
has_aws_creds = "AWS_ACCESS_KEY_ID" in os.environ,
|
|
|
|
has_twine_creds= (
|
|
|
|
"TWINE_USERNAME" in os.environ and
|
|
|
|
"TWINE_PASSWORD" in os.environ
|
|
|
|
),
|
|
|
|
|
|
|
|
docker_username = os.environ.get("DOCKER_USERNAME"),
|
|
|
|
docker_password = os.environ.get("DOCKER_PASSWORD"),
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_docker_creds(self) -> bool:
|
|
|
|
return self.docker_username and self.docker_password
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_pull_request(self) -> bool:
|
|
|
|
if self.appveyor_pull_request_number:
|
|
|
|
return True
|
|
|
|
if self.travis_pull_request and self.travis_pull_request != "false":
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tag(self):
|
|
|
|
return self.travis_tag or self.appveyor_repo_tag_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def branch(self):
|
|
|
|
return self.travis_branch or self.appveyor_repo_branch
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self):
|
|
|
|
name = self.tag or self.branch
|
|
|
|
if not name:
|
2018-05-24 05:10:50 +00:00
|
|
|
raise BuildError("We're on neither a tag nor a branch - could not establish version")
|
2018-05-23 23:06:05 +00:00
|
|
|
return re.sub('^v', "", name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def upload_dir(self):
|
|
|
|
if self.tag:
|
|
|
|
return self.version
|
|
|
|
else:
|
|
|
|
return "branches/%s" % self.version
|
|
|
|
|
|
|
|
@property
|
|
|
|
def platform_tag(self):
|
|
|
|
if self.system in self.PLATFORM_TAGS:
|
|
|
|
return self.PLATFORM_TAGS[self.system]
|
|
|
|
raise BuildError("Unsupported platform: %s" % self.system)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def release_dir(self):
|
|
|
|
return os.path.join(self.root_dir, "release")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def build_dir(self):
|
|
|
|
return os.path.join(self.release_dir, "build")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dist_dir(self):
|
|
|
|
return os.path.join(self.release_dir, "dist")
|
|
|
|
|
2018-05-24 09:59:45 +00:00
|
|
|
@property
|
|
|
|
def docker_tag(self):
|
|
|
|
if self.branch == "master":
|
|
|
|
return "dev"
|
|
|
|
return self.version
|
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
def archive(self, path):
|
2018-05-23 23:06:05 +00:00
|
|
|
# ZipFile and tarfile have slightly different APIs. Fix that.
|
|
|
|
if self.system == "Windows":
|
2018-05-24 05:10:50 +00:00
|
|
|
a = zipfile.ZipFile(path, "w")
|
2018-05-23 23:06:05 +00:00
|
|
|
a.add = a.write
|
|
|
|
return a
|
|
|
|
else:
|
2018-05-24 05:10:50 +00:00
|
|
|
return tarfile.open(path, "w:gz")
|
2018-05-23 23:06:05 +00:00
|
|
|
|
|
|
|
def archive_name(self, bdist: str) -> str:
|
|
|
|
if self.system == "Windows":
|
|
|
|
ext = "zip"
|
|
|
|
else:
|
|
|
|
ext = "tar.gz"
|
|
|
|
return "{project}-{version}-{platform}.{ext}".format(
|
|
|
|
project=bdist,
|
|
|
|
version=self.version,
|
|
|
|
platform=self.platform_tag,
|
|
|
|
ext=ext
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def bdists(self):
|
|
|
|
ret = {
|
|
|
|
"mitmproxy": ["mitmproxy", "mitmdump", "mitmweb"],
|
|
|
|
"pathod": ["pathoc", "pathod"]
|
|
|
|
}
|
|
|
|
if self.system == "Windows":
|
|
|
|
ret["mitmproxy"].remove("mitmproxy")
|
|
|
|
return ret
|
|
|
|
|
2018-05-24 10:15:55 +00:00
|
|
|
@property
|
|
|
|
def should_upload_docker(self) -> bool:
|
2018-05-24 10:32:31 +00:00
|
|
|
return all([
|
|
|
|
(self.tag or self.branch == "master"),
|
2018-05-24 10:15:55 +00:00
|
|
|
self.should_build_docker,
|
|
|
|
self.has_docker_creds,
|
2018-05-24 10:32:31 +00:00
|
|
|
])
|
2018-05-24 10:15:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_upload_pypi(self) -> bool:
|
|
|
|
if self.tag and self.should_build_wheel and self.has_twine_creds:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-05-23 23:06:05 +00:00
|
|
|
def dump_info(self, fp=sys.stdout):
|
|
|
|
print("BUILD PLATFORM_TAG=%s" % self.platform_tag, file=fp)
|
|
|
|
print("BUILD ROOT_DIR=%s" % self.root_dir, file=fp)
|
|
|
|
print("BUILD RELEASE_DIR=%s" % self.release_dir, file=fp)
|
|
|
|
print("BUILD BUILD_DIR=%s" % self.build_dir, file=fp)
|
|
|
|
print("BUILD DIST_DIR=%s" % self.dist_dir, file=fp)
|
|
|
|
print("BUILD BDISTS=%s" % self.bdists, file=fp)
|
|
|
|
print("BUILD TAG=%s" % self.tag, file=fp)
|
|
|
|
print("BUILD BRANCH=%s" % self.branch, file=fp)
|
|
|
|
print("BUILD VERSION=%s" % self.version, file=fp)
|
|
|
|
print("BUILD UPLOAD_DIR=%s" % self.upload_dir, file=fp)
|
|
|
|
|
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
def build_wheel(be: BuildEnviron): # pragma: no cover
|
2018-05-17 09:25:32 +00:00
|
|
|
click.echo("Building wheel...")
|
|
|
|
subprocess.check_call([
|
|
|
|
"python",
|
|
|
|
"setup.py",
|
|
|
|
"-q",
|
|
|
|
"bdist_wheel",
|
2018-05-23 23:06:05 +00:00
|
|
|
"--dist-dir", be.dist_dir,
|
2018-05-17 09:25:32 +00:00
|
|
|
])
|
2018-05-24 05:10:50 +00:00
|
|
|
whl = glob.glob(os.path.join(be.dist_dir, 'mitmproxy-*-py3-none-any.whl'))[0]
|
2018-05-17 09:25:32 +00:00
|
|
|
click.echo("Found wheel package: {}".format(whl))
|
2018-05-23 23:06:05 +00:00
|
|
|
subprocess.check_call(["tox", "-e", "wheeltest", "--", whl])
|
2018-05-18 08:37:56 +00:00
|
|
|
return whl
|
|
|
|
|
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
def build_docker_image(be: BuildEnviron, whl: str): # pragma: no cover
|
2018-05-18 08:37:56 +00:00
|
|
|
click.echo("Building Docker image...")
|
|
|
|
subprocess.check_call([
|
|
|
|
"docker",
|
|
|
|
"build",
|
2018-05-24 10:15:55 +00:00
|
|
|
"--tag", "mitmproxy/mitmproxy/{}".format(be.docker_tag),
|
2018-05-24 05:10:50 +00:00
|
|
|
"--build-arg", "WHEEL_MITMPROXY={}".format(whl),
|
|
|
|
"--build-arg", "WHEEL_BASENAME_MITMPROXY={}".format(os.path.basename(whl)),
|
2018-05-18 08:37:56 +00:00
|
|
|
"--file", "docker/Dockerfile",
|
|
|
|
"."
|
|
|
|
])
|
|
|
|
|
2018-05-17 09:25:32 +00:00
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
def build_pyinstaller(be: BuildEnviron): # pragma: no cover
|
2018-05-23 23:06:05 +00:00
|
|
|
click.echo("Building pyinstaller package...")
|
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
PYINSTALLER_SPEC = os.path.join(be.release_dir, "specs")
|
2018-05-18 08:37:56 +00:00
|
|
|
# PyInstaller 3.2 does not bundle pydivert's Windivert binaries
|
2018-05-24 05:10:50 +00:00
|
|
|
PYINSTALLER_HOOKS = os.path.abspath(os.path.join(be.release_dir, "hooks"))
|
|
|
|
PYINSTALLER_TEMP = os.path.abspath(os.path.join(be.build_dir, "pyinstaller"))
|
|
|
|
PYINSTALLER_DIST = os.path.abspath(os.path.join(be.build_dir, "binaries", be.platform_tag))
|
2018-05-18 08:37:56 +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":
|
|
|
|
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",
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
PYINSTALLER_ARGS = []
|
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
if os.path.exists(PYINSTALLER_TEMP):
|
2018-03-06 21:25:38 +00:00
|
|
|
shutil.rmtree(PYINSTALLER_TEMP)
|
2018-05-24 05:10:50 +00:00
|
|
|
if os.path.exists(PYINSTALLER_DIST):
|
2018-03-06 21:25:38 +00:00
|
|
|
shutil.rmtree(PYINSTALLER_DIST)
|
|
|
|
|
2018-05-23 23:06:05 +00:00
|
|
|
for bdist, tools in sorted(be.bdists.items()):
|
2018-05-24 05:10:50 +00:00
|
|
|
with be.archive(os.path.join(be.dist_dir, be.archive_name(bdist))) as archive:
|
2018-03-06 21:25:38 +00:00
|
|
|
for tool in tools:
|
|
|
|
# We can't have a folder and a file with the same name.
|
|
|
|
if tool == "mitmproxy":
|
|
|
|
tool = "mitmproxy_main"
|
|
|
|
# This is PyInstaller, so it messes up paths.
|
|
|
|
# We need to make sure that we are in the spec folder.
|
|
|
|
with chdir(PYINSTALLER_SPEC):
|
2018-05-18 08:37:56 +00:00
|
|
|
click.echo("Building PyInstaller %s binary..." % tool)
|
2018-03-06 21:25:38 +00:00
|
|
|
excludes = []
|
|
|
|
if tool != "mitmweb":
|
|
|
|
excludes.append("mitmproxy.tools.web")
|
|
|
|
if tool != "mitmproxy_main":
|
|
|
|
excludes.append("mitmproxy.tools.console")
|
|
|
|
|
|
|
|
subprocess.check_call(
|
|
|
|
[
|
|
|
|
"pyinstaller",
|
|
|
|
"--clean",
|
|
|
|
"--workpath", PYINSTALLER_TEMP,
|
|
|
|
"--distpath", PYINSTALLER_DIST,
|
|
|
|
"--additional-hooks-dir", PYINSTALLER_HOOKS,
|
|
|
|
"--onefile",
|
|
|
|
"--console",
|
|
|
|
"--icon", "icon.ico",
|
|
|
|
# This is PyInstaller, so setting a
|
|
|
|
# different log level obviously breaks it :-)
|
|
|
|
# "--log-level", "WARN",
|
|
|
|
]
|
|
|
|
+ [x for e in excludes for x in ["--exclude-module", e]]
|
|
|
|
+ PYINSTALLER_ARGS
|
|
|
|
+ [tool]
|
|
|
|
)
|
|
|
|
# Delete the spec file - we're good without.
|
|
|
|
os.remove("{}.spec".format(tool))
|
|
|
|
|
|
|
|
# Test if it works at all O:-)
|
2018-05-24 05:10:50 +00:00
|
|
|
executable = os.path.join(PYINSTALLER_DIST, tool)
|
2018-03-06 21:25:38 +00:00
|
|
|
if platform.system() == "Windows":
|
|
|
|
executable += ".exe"
|
|
|
|
|
|
|
|
# Remove _main suffix from mitmproxy executable
|
|
|
|
if "_main" in executable:
|
|
|
|
shutil.move(
|
|
|
|
executable,
|
|
|
|
executable.replace("_main", "")
|
|
|
|
)
|
|
|
|
executable = executable.replace("_main", "")
|
|
|
|
|
2018-05-17 09:25:32 +00:00
|
|
|
click.echo("> %s --version" % executable)
|
|
|
|
click.echo(subprocess.check_output([executable, "--version"]).decode())
|
2018-03-06 21:25:38 +00:00
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
archive.add(executable, os.path.basename(executable))
|
2018-05-23 23:06:05 +00:00
|
|
|
click.echo("Packed {}.".format(be.archive_name(bdist)))
|
|
|
|
|
|
|
|
|
|
|
|
@click.group(chain=True)
|
2018-05-24 05:10:50 +00:00
|
|
|
def cli(): # pragma: no cover
|
2018-05-23 23:06:05 +00:00
|
|
|
"""
|
|
|
|
mitmproxy build tool
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("build")
|
2018-05-24 05:10:50 +00:00
|
|
|
def build(): # pragma: no cover
|
2018-05-23 23:06:05 +00:00
|
|
|
"""
|
|
|
|
Build a binary distribution
|
|
|
|
"""
|
|
|
|
be = BuildEnviron.from_env()
|
|
|
|
be.dump_info()
|
|
|
|
|
|
|
|
os.makedirs(be.dist_dir, exist_ok=True)
|
|
|
|
|
2018-05-24 10:15:55 +00:00
|
|
|
if be.should_build_wheel:
|
2018-05-23 23:06:05 +00:00
|
|
|
whl = build_wheel(be)
|
|
|
|
# Docker image requires wheels
|
2018-05-24 10:15:55 +00:00
|
|
|
if be.should_build_docker:
|
2018-05-24 05:10:50 +00:00
|
|
|
build_docker_image(be, whl)
|
2018-05-24 10:15:55 +00:00
|
|
|
if be.should_build_pyinstaller:
|
2018-05-24 05:10:50 +00:00
|
|
|
build_pyinstaller(be)
|
2018-03-06 21:25:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("upload")
|
2018-05-24 05:10:50 +00:00
|
|
|
def upload(): # pragma: no cover
|
2018-03-06 21:25:38 +00:00
|
|
|
"""
|
2018-05-18 08:37:56 +00:00
|
|
|
Upload build artifacts
|
|
|
|
|
|
|
|
Uploads the wheels package to PyPi.
|
|
|
|
Uploads the Pyinstaller and wheels packages to the snapshot server.
|
|
|
|
Pushes the Docker image to Docker Hub.
|
2018-03-06 21:25:38 +00:00
|
|
|
"""
|
2018-05-23 23:06:05 +00:00
|
|
|
be = BuildEnviron.from_env()
|
2018-05-18 08:37:56 +00:00
|
|
|
|
2018-05-23 23:06:05 +00:00
|
|
|
if be.is_pull_request:
|
2018-05-18 08:37:56 +00:00
|
|
|
click.echo("Refusing to upload artifacts from a pull request!")
|
2018-03-07 18:55:52 +00:00
|
|
|
return
|
2018-05-17 09:25:32 +00:00
|
|
|
|
2018-05-23 23:06:05 +00:00
|
|
|
if be.has_aws_creds:
|
2018-05-17 09:25:32 +00:00
|
|
|
subprocess.check_call([
|
|
|
|
"aws", "s3", "cp",
|
|
|
|
"--acl", "public-read",
|
2018-05-23 23:06:05 +00:00
|
|
|
be.dist_dir + "/",
|
|
|
|
"s3://snapshots.mitmproxy.org/{}/".format(be.upload_dir),
|
2018-05-17 09:25:32 +00:00
|
|
|
"--recursive",
|
|
|
|
])
|
|
|
|
|
2018-05-24 10:15:55 +00:00
|
|
|
if be.should_upload_pypi:
|
2018-05-24 05:10:50 +00:00
|
|
|
whl = glob.glob(os.path.join(be.dist_dir, 'mitmproxy-*-py3-none-any.whl'))[0]
|
2018-05-18 08:37:56 +00:00
|
|
|
click.echo("Uploading {} to PyPi...".format(whl))
|
2018-05-23 23:06:05 +00:00
|
|
|
subprocess.check_call(["twine", "upload", whl])
|
2018-03-07 01:05:26 +00:00
|
|
|
|
2018-05-24 10:15:55 +00:00
|
|
|
if be.should_upload_docker:
|
2018-05-24 09:59:45 +00:00
|
|
|
click.echo("Uploading Docker image to tag={}...".format(be.docker_tag))
|
2018-05-18 08:37:56 +00:00
|
|
|
subprocess.check_call([
|
|
|
|
"docker",
|
|
|
|
"login",
|
2018-05-23 23:06:05 +00:00
|
|
|
"-u", be.docker_username,
|
|
|
|
"-p", be.docker_password,
|
2018-05-18 08:37:56 +00:00
|
|
|
])
|
|
|
|
subprocess.check_call([
|
|
|
|
"docker",
|
|
|
|
"push",
|
2018-05-24 09:59:45 +00:00
|
|
|
"mitmproxy/mitmproxy:{}".format(be.docker_tag),
|
2018-05-18 08:37:56 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("decrypt")
|
|
|
|
@click.argument('infile', type=click.File('rb'))
|
|
|
|
@click.argument('outfile', type=click.File('wb'))
|
|
|
|
@click.argument('key', envvar='RTOOL_KEY')
|
2018-05-24 05:10:50 +00:00
|
|
|
def decrypt(infile, outfile, key): # pragma: no cover
|
2018-05-18 08:37:56 +00:00
|
|
|
f = cryptography.fernet.Fernet(key.encode())
|
|
|
|
outfile.write(f.decrypt(infile.read()))
|
|
|
|
|
2018-03-07 01:05:26 +00:00
|
|
|
|
2018-05-24 05:10:50 +00:00
|
|
|
if __name__ == "__main__": # pragma: no cover
|
2018-03-06 21:25:38 +00:00
|
|
|
cli()
|