Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 146 lines (114 sloc) 4.42 KB
#!/usr/bin/env python
import base64
import collections
import httplib
import json
import mimetypes
import os
import sys
import subprocess
this_dir = os.path.dirname(os.path.abspath(__file__))
dist_dir = os.path.join(this_dir, "dist")
setup_py = os.path.join(this_dir, "")
def ordered_hook(pairs):
ordered = collections.OrderedDict()
for pair in pairs:
ordered[pair[0]] = pair[1]
return ordered
class ReleaseCommand(object):
def __init__(self):
self.package = json.load(open("setup.json", "r"),
object_pairs_hook = ordered_hook)
self.tarball_name = "%s-%s.tar.gz" % \
(self.package["name"], self.package["version"])
self.tarball = os.path.join(dist_dir, self.tarball_name)
def run(self, *args):
print "-> %s" % " ".join(args)
return subprocess.check_output(args)
def read_access_token(self):
access_token ="git", "config", "github.files.accesstoken")
self.access_token = access_token.strip()
def github_api(self, path, data):
body = json.dumps(data)
connection = httplib.HTTPSConnection("")
auth = base64.encodestring('%s:%s' % (self.access_token, 'x-oauth-basic')).replace('\n', '')
connection.request("POST", path, body, {
"Authorization": "Basic %s" % auth,
response = connection.getresponse()
response_body =
data = json.loads(response_body)
if "errors" in data:
print >>sys.stderr, "Github Response Failed: %s" % data["message"]
for error in data["errors"]:
print >>sys.stderr, " Error: %s\n" % error["code"]
return data
def github_upload(self):
description = "%s version %s" % \
(self.package["name"], self.package["version"])
data = self.github_api("/repos/marshall/logcat-color/releases", {
"tag_name": "v%s" % self.package["version"],
"name": self.package["version"],
"body": description,
print data
upload_url = data["upload_url"].replace("{?name}", "?name=%s" % self.tarball_name)"curl",
"-u", "%s:x-oauth-basic" % self.access_token,
"-F", "Content-Type=%s" % mimetypes.guess_type(self.tarball)[0],
"-F", "file=@%s" % self.tarball,
def help(self):
print """
Usage: %s <command> [args]
Supported commands:
help view this help message
build build source distribution tarball
push push the release tarball to github and pypi, and push git tags
bump [version] bump to [version] in setup.json, stage, and prepare a commit message
""" % sys.argv[0]
def build(self):
# build sdist, setup_py, "sdist")
print "%s succesfully built. to tag, use %s tag\n" % \
(self.tarball_name, sys.argv[0])
def push(self):
# upload source tarball->github, and upload for pypi
self.github_upload(), setup_py, "sdist", "upload")
print "%s successfully uploaded, and v%s tag pushed. to bump, use %s bump\n" % \
(self.tarball_name, self.package["version"], sys.argv[0])
def bump(self):
if len(sys.argv) < 3:
print >>sys.stderr, "Error: bump requires a version to bump to"
bump_version = sys.argv[2]
self.package["version"] = bump_version
setup_json = json.dumps(self.package,
separators = (',', ': '),
indent = 4)
open("setup.json", "w").write(setup_json)
message = "bump to version %s" % bump_version"git", "add", "setup.json")
# TODO -- full path is needed for execv, detect this
git = "/usr/bin/git"
os.execv(git, (git, "commit", "-v", "-m", message, "-e"))
def main(self):
command = "help"
if len(sys.argv) > 1:
command = sys.argv[1]
if command == "help":
elif command == "build":
elif command == "tag":
elif command == "push":
elif command == "bump":
if __name__ == "__main__":