mitmproxy/release/build.py

242 lines
7.4 KiB
Python
Raw Normal View History

2015-07-21 15:51:59 +00:00
#!/usr/bin/env python
2015-07-22 11:49:18 +00:00
from __future__ import (absolute_import, print_function, division, unicode_literals)
from contextlib import contextmanager
2015-07-21 17:03:25 +00:00
from os.path import dirname, realpath, join, exists, normpath
2015-07-21 15:51:59 +00:00
import os
import shutil
import subprocess
import glob
2015-07-21 17:03:25 +00:00
import re
2015-07-21 15:51:59 +00:00
from shlex import split
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 os.name == "nt":
venv_bin = "Scripts"
else:
venv_bin = "bin"
2015-07-21 17:03:25 +00:00
root_dir = join(dirname(realpath(__file__)), "..", "..")
mitmproxy_dir = join(root_dir, "mitmproxy")
dist_dir = join(mitmproxy_dir, "dist")
test_venv_dir = join(root_dir, "venv.mitmproxy-release")
2015-07-21 15:51:59 +00:00
2015-07-22 11:49:18 +00:00
all_projects = ("netlib", "pathod", "mitmproxy")
tools = {
"mitmproxy": ["mitmproxy", "mitmdump", "mitmweb"],
"pathod": ["pathod", "pathoc"],
"netlib": []
}
2015-07-22 00:43:45 +00:00
if os.name == "nt":
2015-07-22 11:49:18 +00:00
tools["mitmproxy"].remove("mitmproxy")
version_files = {
"mitmproxy": normpath(join(root_dir, "mitmproxy/libmproxy/version.py")),
"pathod": normpath(join(root_dir, "pathod/libpathod/version.py")),
"netlib": normpath(join(root_dir, "netlib/netlib/version.py")),
}
2015-07-21 15:51:59 +00:00
2015-07-22 11:49:18 +00:00
@contextmanager
def empty_pythonpath():
"""
Make sure that the regular python installation is not on the python path,
which would give us access to modules installed outside of our virtualenv.
"""
pythonpath = os.environ["PYTHONPATH"]
os.environ["PYTHONPATH"] = ""
yield
os.environ["PYTHONPATH"] = pythonpath
@contextmanager
def chdir(path):
old_dir = os.getcwd()
os.chdir(path)
yield
os.chdir(old_dir)
2015-07-21 15:51:59 +00:00
2015-07-22 00:43:45 +00:00
2015-07-21 15:51:59 +00:00
@click.group(chain=True)
def cli():
"""
mitmproxy build tool
"""
pass
@cli.command("contributors")
2015-07-22 00:43:45 +00:00
def contributors():
"""
Update CONTRIBUTORS.md
"""
2015-07-21 15:51:59 +00:00
print("Updating CONTRIBUTORS.md...")
2015-07-22 00:43:45 +00:00
contributors_data = subprocess.check_output(split("git shortlog -n -s"))
2015-07-21 17:03:25 +00:00
with open(join(mitmproxy_dir, "CONTRIBUTORS"), "w") as f:
2015-07-22 00:43:45 +00:00
f.write(contributors_data)
2015-07-21 15:51:59 +00:00
@cli.command("docs")
2015-07-22 00:43:45 +00:00
def docs():
"""
Render the docs
"""
2015-07-21 15:51:59 +00:00
print("Rendering the docs...")
subprocess.check_call([
"cshape",
2015-07-21 17:03:25 +00:00
join(mitmproxy_dir, "doc-src"),
join(mitmproxy_dir, "doc")
2015-07-21 15:51:59 +00:00
])
2015-07-22 00:43:45 +00:00
@cli.command("set-version")
2015-07-22 11:49:18 +00:00
@click.option('--project', '-p', 'projects', multiple=True, type=click.Choice(all_projects), default=all_projects)
2015-07-22 00:43:45 +00:00
@click.argument('version')
2015-07-22 11:49:18 +00:00
def set_version(projects, version):
2015-07-22 00:43:45 +00:00
"""
Update version information
"""
print("Update versions...")
version = ", ".join(version.split("."))
2015-07-22 11:49:18 +00:00
for project, version_file in version_files.items():
if project not in projects:
continue
2015-07-22 00:43:45 +00:00
print("Update %s..." % version_file)
with open(version_file, "rb") as f:
content = f.read()
new_content = re.sub(r"IVERSION\s*=\s*\([\d,\s]+\)", "IVERSION = (%s)" % version, content)
with open(version_file, "wb") as f:
f.write(new_content)
@cli.command("git")
2015-07-22 11:49:18 +00:00
@click.option('--project', '-p', 'projects', multiple=True, type=click.Choice(all_projects), default=all_projects)
2015-07-22 00:43:45 +00:00
@click.argument('args', nargs=-1, required=True)
2015-07-22 11:49:18 +00:00
def git(projects, args):
2015-07-22 00:43:45 +00:00
"""
Run a git command on every project
"""
args = ["git"] + list(args)
for project in projects:
print("%s> %s..." % (project, " ".join(args)))
subprocess.check_call(
args,
cwd=join(root_dir, project)
)
@cli.command("sdist")
2015-07-22 11:49:18 +00:00
@click.option('--project', '-p', 'projects', multiple=True, type=click.Choice(all_projects), default=all_projects)
def sdist(projects):
2015-07-22 00:43:45 +00:00
"""
Build a source distribution
"""
2015-07-22 11:49:18 +00:00
with empty_pythonpath():
print("Building release...")
if exists(dist_dir):
shutil.rmtree(dist_dir)
for project in projects:
print("Creating %s source distribution..." % project)
subprocess.check_call(
["python", "./setup.py", "-q", "sdist", "--dist-dir", dist_dir, "--formats=gztar"],
cwd=join(root_dir, project)
)
2015-07-22 00:43:45 +00:00
2015-07-21 17:03:25 +00:00
@cli.command("test")
2015-07-22 11:49:18 +00:00
@click.option('--project', '-p', 'projects', multiple=True, type=click.Choice(all_projects), default=all_projects)
2015-07-21 17:03:25 +00:00
@click.pass_context
2015-07-22 11:49:18 +00:00
def test(ctx, projects):
2015-07-22 00:43:45 +00:00
"""
Test the source distribution
"""
2015-07-21 17:03:25 +00:00
if not exists(dist_dir):
2015-07-22 00:43:45 +00:00
ctx.invoke(sdist)
2015-07-21 17:03:25 +00:00
2015-07-22 11:49:18 +00:00
with empty_pythonpath():
print("Creating virtualenv for test install...")
if exists(test_venv_dir):
shutil.rmtree(test_venv_dir)
subprocess.check_call(["virtualenv", "-q", test_venv_dir])
2015-07-21 15:51:59 +00:00
2015-07-22 11:49:18 +00:00
pip = join(test_venv_dir, venv_bin, "pip")
with chdir(dist_dir):
for project in projects:
print("Installing %s..." % project)
dist = glob.glob("./%s*" % project)[0]
subprocess.check_call([pip, "install", "-q", dist])
2015-07-21 15:51:59 +00:00
2015-07-22 11:49:18 +00:00
print("Running binaries...")
for project in projects:
for tool in tools[project]:
tool = join(test_venv_dir, venv_bin, tool)
print(tool)
print(subprocess.check_output([tool, "--version"]))
2015-07-21 15:51:59 +00:00
2015-07-22 11:49:18 +00:00
print("Virtualenv available for further testing:")
print("source %s" % normpath(join(test_venv_dir, venv_bin, "activate")))
2015-07-21 15:51:59 +00:00
@cli.command("upload")
@click.option('--username', prompt=True)
@click.password_option(confirmation_prompt=False)
@click.option('--repository', default="pypi")
def upload_release(username, password, repository):
2015-07-22 00:43:45 +00:00
"""
Upload source distributions to PyPI
"""
2015-07-21 15:51:59 +00:00
print("Uploading distributions...")
subprocess.check_call([
"twine",
"upload",
"-u", username,
"-p", password,
"-r", repository,
2015-07-21 17:03:25 +00:00
"%s/*" % dist_dir
2015-07-21 15:51:59 +00:00
])
2015-07-21 17:03:25 +00:00
2015-07-22 11:49:18 +00:00
# TODO: Fully automate build process.
# This wizard is missing OSX builds and updating mitmproxy.org.
2015-07-22 00:43:45 +00:00
@cli.command("wizard")
@click.option('--version', prompt=True)
2015-07-22 11:49:18 +00:00
@click.option('--username', prompt="PyPI Username")
@click.password_option(confirmation_prompt=False, prompt="PyPI Password")
2015-07-22 00:43:45 +00:00
@click.option('--repository', default="pypi")
2015-07-22 11:49:18 +00:00
@click.option('--project', '-p', 'projects', multiple=True, type=click.Choice(all_projects), default=all_projects)
2015-07-22 00:43:45 +00:00
@click.pass_context
2015-07-22 11:49:18 +00:00
def wizard(ctx, version, username, password, repository, projects):
"""
2015-07-22 00:43:45 +00:00
Interactive Release Wizard
2015-07-22 11:49:18 +00:00
"""
2015-07-22 00:43:45 +00:00
for project in projects:
if subprocess.check_output(["git", "status", "--porcelain"], cwd=join(root_dir, project)):
raise RuntimeError("%s repository is not clean." % project)
2015-07-22 11:49:18 +00:00
# Build test release
ctx.invoke(sdist, projects=projects)
ctx.invoke(test, projects=projects)
click.confirm("Please test the release now. Is it ok?", abort=True)
2015-07-22 00:43:45 +00:00
2015-07-22 11:49:18 +00:00
# bump version, update docs and contributors
ctx.invoke(set_version, version=version, projects=projects)
2015-07-22 00:43:45 +00:00
ctx.invoke(docs)
ctx.invoke(contributors)
2015-07-22 11:49:18 +00:00
# version bump commit + tag
ctx.invoke(git, args=["commit", "-a", "-m", "bump version"], projects=projects)
ctx.invoke(git, args=["tag", "v" + version], projects=projects)
ctx.invoke(git, args=["push"], projects=projects)
ctx.invoke(git, args=["push", "--tags"], projects=projects)
# Re-invoke sdist with bumped version
ctx.invoke(sdist, projects=projects)
click.confirm("All good, can upload to PyPI?", abort=True)
2015-07-22 00:43:45 +00:00
ctx.invoke(upload_release, username=username, password=password, repository=repository)
click.echo("All done!")
2015-07-22 11:49:18 +00:00
2015-07-22 00:43:45 +00:00
2015-07-21 15:51:59 +00:00
if __name__ == "__main__":
cli()