2016-07-30 01:37:37 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2015-11-29 01:46:08 +00:00
|
|
|
import contextlib
|
|
|
|
import os
|
2018-02-25 17:02:39 +00:00
|
|
|
import sys
|
2016-07-30 01:37:37 +00:00
|
|
|
import platform
|
|
|
|
import runpy
|
|
|
|
import shlex
|
2015-11-29 01:46:08 +00:00
|
|
|
import subprocess
|
2015-11-29 02:38:23 +00:00
|
|
|
import tarfile
|
2016-07-30 01:37:37 +00:00
|
|
|
import zipfile
|
2018-03-06 21:25:38 +00:00
|
|
|
from os.path import join, abspath, dirname
|
2016-07-30 01:37:37 +00:00
|
|
|
|
2018-03-07 01:05:26 +00:00
|
|
|
import cryptography.fernet
|
2015-11-29 01:46:08 +00:00
|
|
|
import click
|
|
|
|
|
|
|
|
# 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-12-11 22:07:47 +00:00
|
|
|
PLATFORM_TAG = {
|
|
|
|
"Darwin": "osx",
|
|
|
|
"Windows": "windows",
|
|
|
|
"Linux": "linux",
|
|
|
|
}.get(platform.system(), platform.system())
|
|
|
|
|
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")
|
2016-12-11 22:07:47 +00:00
|
|
|
PYINSTALLER_DIST = join(BUILD_DIR, "binaries", PLATFORM_TAG)
|
2015-11-29 01:46:08 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2017-12-26 20:53:16 +00:00
|
|
|
def get_version(dev: bool = False, build: bool = False) -> str:
|
2017-12-27 20:46:52 +00:00
|
|
|
x = runpy.run_path(VERSION_FILE)
|
|
|
|
return x["get_version"](dev, build, True)
|
2017-12-26 20:53:16 +00:00
|
|
|
|
|
|
|
|
2016-07-30 01:37:37 +00:00
|
|
|
def archive_name(bdist: str) -> str:
|
2016-02-11 17:51:47 +00:00
|
|
|
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-12-11 22:07:47 +00:00
|
|
|
platform=PLATFORM_TAG,
|
2016-02-11 17:51:47 +00:00
|
|
|
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(
|
2017-12-26 20:53:16 +00:00
|
|
|
version=get_version(True),
|
2015-11-29 18:05:58 +00:00
|
|
|
)
|
2015-11-29 01:46:08 +00:00
|
|
|
|
2015-12-03 16:55:42 +00:00
|
|
|
|
2016-12-11 22:07:47 +00:00
|
|
|
def installer_name() -> str:
|
|
|
|
ext = {
|
|
|
|
"Windows": "exe",
|
|
|
|
"Darwin": "dmg",
|
|
|
|
"Linux": "run"
|
|
|
|
}[platform.system()]
|
|
|
|
return "mitmproxy-{version}-{platform}-installer.{ext}".format(
|
|
|
|
version=get_version(),
|
|
|
|
platform=PLATFORM_TAG,
|
|
|
|
ext=ext,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
2018-02-25 16:49:54 +00:00
|
|
|
@cli.command("homebrew-pr")
|
|
|
|
def homebrew_pr():
|
|
|
|
"""
|
|
|
|
Create a new Homebrew PR
|
|
|
|
"""
|
2018-02-25 17:02:39 +00:00
|
|
|
if platform.system() != "Darwin":
|
|
|
|
print("You need to run this on macOS to create a new Homebrew PR. Sorry.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2018-02-25 16:49:54 +00:00
|
|
|
print("Creating a new PR with Homebrew...")
|
|
|
|
subprocess.check_call([
|
|
|
|
"brew",
|
|
|
|
"bump-formula-pr",
|
|
|
|
"--url", "https://github.com/mitmproxy/mitmproxy/archive/v{}".format(get_version()),
|
|
|
|
"mitmproxy",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2018-03-07 01:05:26 +00:00
|
|
|
@cli.command("encrypt")
|
|
|
|
@click.argument('infile', type=click.File('rb'))
|
|
|
|
@click.argument('outfile', type=click.File('wb'))
|
|
|
|
@click.argument('key', envvar='RTOOL_KEY')
|
|
|
|
def encrypt(infile, outfile, key):
|
|
|
|
f = cryptography.fernet.Fernet(key.encode())
|
|
|
|
outfile.write(f.encrypt(infile.read()))
|
|
|
|
|
|
|
|
|
2015-11-29 01:46:08 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|