def prepare_appversion():
    env = common.prepare_env()

    CI_SETTINGS = {}
    manifest_path = os.path.join(".", "resources", "app", "meta", "manifests",
                                 "ci.json")
    if (not os.path.isfile(manifest_path)):
        raise AssertionError("Manifest not found: " + manifest_path)
    with (open(manifest_path)) as ci_settings_file:
        CI_SETTINGS = json.load(ci_settings_file)

    # set tag to app_version.txt
    if not env["GITHUB_TAG"] == "":
        with open(
                os.path.join(
                    ".", *CI_SETTINGS["common"]["prepare_appversion"]
                    ["app_version"]), "w+") as f:
            _ = f.read()
            f.seek(0)
            f.write(env["GITHUB_TAG"])
            f.truncate()
        with open(os.path.join("scripts", "ver.lua"), "w+") as ver:
            _ = ver.read()
            ver.seek(0)
            ver.write("print(\"Package Version: " + env["GITHUB_TAG"] + "\")")
            ver.truncate()

    if not os.path.isdir(os.path.join("..", "build")):
        os.mkdir(os.path.join("..", "build"))
    copy(
        os.path.join(
            ".", *CI_SETTINGS["common"]["prepare_appversion"]["app_version"]),
        os.path.join("..", "build", "app_version.txt"))
示例#2
0
def prepare_binary():
    env = common.prepare_env()

    # make dir to put the binary in
    if not os.path.isdir(os.path.join("..", "artifact")):
        os.mkdir(os.path.join("..", "artifact"))

    BUILD_FILENAME = ""

    if isinstance(BUILD_FILENAME, str):
        BUILD_FILENAME = list(BUILD_FILENAME)

    BUILD_FILENAMES = BUILD_FILENAME

    for BUILD_FILENAME in BUILD_FILENAMES:
        DEST_FILENAME = common.prepare_filename(BUILD_FILENAME)

        print(f"OS Name:        {env['OS_NAME']}")
        print(f"OS Version:     {env['OS_VERSION']}")
        print(f"Build Filename: {BUILD_FILENAME}")
        print(f"Dest Filename:  {DEST_FILENAME}")
        if not BUILD_FILENAME == "":
            print("Build Filesize: " + common.file_size(BUILD_FILENAME))
        else:
            exit(1)

        if not BUILD_FILENAME == "":
            move(os.path.join(".", BUILD_FILENAME),
                 os.path.join("..", "artifact", BUILD_FILENAME))
示例#3
0
def get_upx():
  CI_SETTINGS = {}
  manifest_path = os.path.join("resources","app","meta","manifests","ci.json")
  if (not os.path.isfile(manifest_path)):
    raise AssertionError("Manifest not found: " + manifest_path)
  with(open(manifest_path)) as ci_settings_file:
    CI_SETTINGS = json.load(ci_settings_file)

  if not os.path.isdir("./upx"):
      # get env vars
      env = common.prepare_env()
      # set up download url
      UPX_VERSION = os.getenv("UPX_VERSION") or str(CI_SETTINGS["common"]["get_upx"]["version"])
      UPX_SLUG = ""
      UPX_FILE = ""
      if "windows" in env["OS_NAME"]:
          UPX_SLUG = "upx-" + UPX_VERSION + "-win64"
          UPX_FILE = UPX_SLUG + ".zip"
      else:
          UPX_SLUG = "upx-" + UPX_VERSION + "-amd64_linux"
          UPX_FILE = UPX_SLUG + ".tar.xz"
      UPX_URL = "https://github.com/upx/upx/releases/download/v" + \
          UPX_VERSION + '/' + UPX_FILE

      if "osx" not in env["OS_NAME"]:
          print(f"Getting UPX: {UPX_FILE}")
          with open("./" + UPX_FILE, "wb") as upx:
              UPX_REQ = urllib.request.Request(
                  UPX_URL,
                  data=None
              )
              UPX_REQ = urllib.request.urlopen(UPX_REQ)
              UPX_DATA = UPX_REQ.read()
              upx.write(UPX_DATA)

          unpack_archive(UPX_FILE, "./")

          os.rename("./" + UPX_SLUG, "./upx")
          os.remove("./" + UPX_FILE)

  print("UPX should " +
      ("not " if not os.path.isdir("./upx") else "") +
      "be available.")
import common
import distutils.dir_util  # for copying trees
import os  # for env vars
import stat  # for file stats
import subprocess  # do stuff at the shell level
from git_clean import git_clean
from shutil import copy, make_archive, move, rmtree  # file manipulation

env = common.prepare_env()  # get env vars

dirs = [
    os.path.join("..", "artifact"),  # temp dir for binary
    os.path.join("..", "build"),  # temp dir for other stuff
    os.path.join("..", "deploy")  # dir for archive
]
for dirname in dirs:
    if not os.path.isdir(dirname):
        os.makedirs(dirname)

# make dirs for each os
for dirname in ["linux", "macos", "windows"]:
    if not os.path.isdir(os.path.join("..", "deploy", dirname)):
        os.mkdir(os.path.join("..", "deploy", dirname))

# sanity check permissions for working_dirs.json
dirpath = "."
for dirname in ["resources", "user", "meta", "manifests"]:
    dirpath += os.path.join(dirpath, dirname)
    if os.path.isdir(dirpath):
        os.chmod(dirpath, 0o755)
示例#5
0
import common
import os               # for env vars
import sys              # for path
import urllib.request   # for downloads
from shutil import unpack_archive

# only do stuff if we don't have a UPX folder

if not os.path.isdir("./upx"):
    # get env vars
    env = common.prepare_env()
    # set up download url
    UPX_VERSION = os.getenv("UPX_VERSION") or "3.96"
    UPX_SLUG = ""
    UPX_FILE = ""
    if "windows" in env["OS_NAME"]:
        UPX_SLUG = "upx-" + UPX_VERSION + "-win64"
        UPX_FILE = UPX_SLUG + ".zip"
    else:
        UPX_SLUG = "upx-" + UPX_VERSION + "-amd64_linux"
        UPX_FILE = UPX_SLUG + ".tar.xz"
    UPX_URL = "https://github.com/upx/upx/releases/download/v" + \
        UPX_VERSION + '/' + UPX_FILE

    if "osx" not in env["OS_NAME"]:
        print("Getting UPX: " + UPX_FILE)
        with open("./" + UPX_FILE, "wb") as upx:
            UPX_REQ = urllib.request.Request(
                UPX_URL,
                data=None
            )
示例#6
0
def prepare_release():
    env = common.prepare_env()  # get env vars

    dirs = [
        os.path.join("..", "artifact"),  # temp dir for binary
        os.path.join("..", "build"),  # temp dir for other stuff
        os.path.join("..", "deploy")  # dir for archive
    ]
    for dirname in dirs:
        if not os.path.isdir(dirname):
            os.makedirs(dirname)

    # make dirs for each os
    for dirname in ["linux", "macos", "windows"]:
        if not os.path.isdir(os.path.join("..", "deploy", dirname)):
            os.mkdir(os.path.join("..", "deploy", dirname))

    # sanity check permissions for working_dirs.json
    dirpath = "."
    for dirname in ["resources", "user", "meta", "manifests"]:
        dirpath += os.path.join(dirpath, dirname)
        if os.path.isdir(dirpath):
            os.chmod(dirpath, 0o755)

    # nuke git files
    for git in [
            os.path.join(".", ".gitattrubutes"),
            os.path.join(".", ".gitignore")
    ]:
        if os.path.isfile(git):
            os.remove(git)

    # nuke travis file if it exists
    for travis in [
            os.path.join(".", ".travis.yml"),
            os.path.join(".", ".travis.off")
    ]:
        if os.path.isfile(travis):
            os.remove(travis)

    # nuke test suite if it exists
    if os.path.isdir(os.path.join(".", "tests")):
        distutils.dir_util.remove_tree(os.path.join(".", "tests"))

    BUILD_FILENAME = ""
    ZIP_FILENAME = ""

    # list executables
    BUILD_FILENAME = common.find_binary(os.path.join("."))
    if BUILD_FILENAME == "":
        BUILD_FILENAME = common.find_binary(os.path.join("..", "artifact"))

    if isinstance(BUILD_FILENAME, str):
        BUILD_FILENAME = list(BUILD_FILENAME)

    BUILD_FILENAMES = BUILD_FILENAME

    print(BUILD_FILENAMES)

    if len(BUILD_FILENAMES) > 0:
        for BUILD_FILENAME in BUILD_FILENAMES:
            if not BUILD_FILENAME == "":
                if "artifact" not in BUILD_FILENAME:
                    # move the binary to temp folder
                    move(os.path.join(".", BUILD_FILENAME),
                         os.path.join("..", "artifact", BUILD_FILENAME))

        # clean the git slate
        git_clean()

        # mv dirs from source code
        dirs = [
            os.path.join(".", ".git"),
            os.path.join(".", ".github"),
            os.path.join(".", ".gitattributes"),
            os.path.join(".", ".gitignore"),
            os.path.join(".", "html"),
            os.path.join(".", "resources", "ci")
        ]
        for dirname in dirs:
            if os.path.isdir(dirname):
                move(dirname, os.path.join("..", "build", dirname))

        for BUILD_FILENAME in BUILD_FILENAMES:
            if "artifact" not in BUILD_FILENAME:
                if os.path.isfile(
                        os.path.join("..", "artifact", BUILD_FILENAME)):
                    # move the binary back
                    move(os.path.join("..", "artifact", BUILD_FILENAME),
                         os.path.join(".", BUILD_FILENAME))
                    # Make Linux/Mac binary executable
                    if "linux" in env["OS_NAME"] or \
                        "ubuntu" in env["OS_NAME"] or \
                        "mac" in env["OS_NAME"] or \
                            "osx" in env["OS_NAME"]:
                        os.chmod(os.path.join(".", BUILD_FILENAME), 0o755)

        # .zip if windows
        # .tar.gz otherwise
        if len(BUILD_FILENAMES) > 1:
            ZIP_FILENAME = os.path.join("..", "deploy", env["REPO_NAME"])
        else:
            ZIP_FILENAME = os.path.join("..", "deploy",
                                        os.path.splitext(BUILD_FILENAME)[0])
        if env["OS_NAME"] == "windows":
            make_archive(ZIP_FILENAME, "zip")
            ZIP_FILENAME += ".zip"
        else:
            make_archive(ZIP_FILENAME, "gztar")
            ZIP_FILENAME += ".tar.gz"

        # mv dirs back
        for thisDir in dirs:
            if os.path.isdir(os.path.join("..", "build", thisDir)):
                move(os.path.join("..", "build", thisDir),
                     os.path.join(".", thisDir))

    for BUILD_FILENAME in BUILD_FILENAMES:
        if not BUILD_FILENAME == "":
            print(f"Build Filename: {BUILD_FILENAME}")
            print("Build Filesize: " + common.file_size(BUILD_FILENAME))
        else:
            print(f"No Build to prepare: {BUILD_FILENAME}")

    if not ZIP_FILENAME == "":
        print(f"Zip Filename:   {ZIP_FILENAME}")
        print("Zip Filesize:   " + common.file_size(ZIP_FILENAME))
    else:
        print(f"No Zip to prepare: {ZIP_FILENAME}")

    print(f"Git tag:        {env['GITHUB_TAG']}")

    if (len(BUILD_FILENAMES) == 0) or (ZIP_FILENAME == ""):
        exit(1)
def prepare_release():
    env = common.prepare_env()  # get env vars

    dirs = [
        os.path.join("..", "artifact"),  # temp dir for binary
        os.path.join("..", "build"),  # temp dir for other stuff
        os.path.join("..", "deploy")  # dir for archive
    ]
    for dirname in dirs:
        if not os.path.isdir(dirname):
            os.makedirs(dirname)

    # make dirs for each os
    # for dirname in ["linux", "macos", "windows"]:
    for dirname in ["linux"]:
        if not os.path.isdir(os.path.join("..", "deploy", dirname)):
            os.mkdir(os.path.join("..", "deploy", dirname))

    # sanity check permissions for working_dirs.json
    dirpath = "."
    for dirname in ["resources", "user", "meta", "manifests"]:
        dirpath += os.path.join(dirpath, dirname)
        if os.path.isdir(dirpath):
            os.chmod(dirpath, 0o755)

    # nuke git files
    for git in [
            os.path.join(".", ".gitattrubutes"),
            os.path.join(".", ".gitignore")
    ]:
        if os.path.isfile(git):
            os.remove(git)

    # nuke travis file if it exists
    for travis in [
            os.path.join(".", ".travis.yml"),
            os.path.join(".", ".travis.off")
    ]:
        if os.path.isfile(travis):
            os.remove(travis)

    # nuke test suite if it exists
    if os.path.isdir(os.path.join(".", "tests")):
        distutils.dir_util.remove_tree(os.path.join(".", "tests"))

    BUILD_FILENAME = ""
    ZIP_FILENAME = ""

    # list executables
    BUILD_FILENAME = (os.path.join("."))
    if BUILD_FILENAME == "":
        BUILD_FILENAME = (os.path.join("..", "artifact"))

    if isinstance(BUILD_FILENAME, str):
        BUILD_FILENAME = list(BUILD_FILENAME)

    BUILD_FILENAMES = BUILD_FILENAME

    print(BUILD_FILENAMES)

    if len(BUILD_FILENAMES) > 0:
        # clean the git slate
        git_clean()

        # mv dirs from source code
        dirs = [
            os.path.join(".", ".git"),
            os.path.join(".", ".github"),
            os.path.join(".", ".gitattributes"),
            os.path.join(".", ".gitignore"),
            os.path.join(".", "html"),
            os.path.join(".", "resources"),
            os.path.join(".", "schemas"),
            os.path.join(".", "CODE_OF_CONDUCT.md")
        ]
        for dirname in dirs:
            if os.path.exists(dirname):
                move(dirname, os.path.join("..", "build", dirname))

        # .zip if windows
        # .tar.gz otherwise
        ZIP_FILENAME = os.path.join("..", "deploy", env["REPO_NAME"])
        make_archive(ZIP_FILENAME, "zip")
        ZIP_FILENAME += ".zip"

        # mv dirs back
        for thisDir in dirs:
            if os.path.exists(os.path.join("..", "build", thisDir)):
                move(os.path.join("..", "build", thisDir),
                     os.path.join(".", thisDir))

    if not ZIP_FILENAME == "":
        print(f"Zip Filename:   {ZIP_FILENAME}")
        print("Zip Filesize:   " + common.file_size(ZIP_FILENAME))
    else:
        print(f"No Zip to prepare: {ZIP_FILENAME}")

    print(f"Git tag:        {env['GITHUB_TAG']}")

    if (ZIP_FILENAME == ""):
        exit(1)
示例#8
0
def prepare_discord_notif():
    CI_SETTINGS = {}
    manifest_path = os.path.join(".", "resources", "app", "meta", "manifests",
                                 "ci.json")
    if (not os.path.isfile(manifest_path)):
        raise AssertionError("Manifest not found: " + manifest_path)
    with (open(manifest_path)) as ci_settings_file:
        CI_SETTINGS = json.load(ci_settings_file)

    # default stuff
    DEFAULT_EVENT = "event"
    DEFAULT_REPO_SLUG = '/'.join(CI_SETTINGS["common"]["common"]["repo"])

    # spiffy colors for embed
    colors = {}
    for name, color in CI_SETTINGS["common"]["prepare_discord_notif"][
            "colors"].items():
        colors[name] = (int(color, 16))

    env = common.prepare_env()  # get env vars

    env["COMMIT_AUTHOR"] = CI_SETTINGS["common"]["prepare_discord_notif"][
        "travis"]["author"]
    env["COMMIT_AVATAR"] = CI_SETTINGS["common"]["prepare_discord_notif"][
        "travis"]["avatar"]
    commits = []
    query = ""
    timestamp = ""

    # event log
    event_manifest = {}
    if not env["EVENT_LOG"] == "":
        with (open(env["EVENT_LOG"], "r")) as f:
            event_manifest = json.load(f)

    # branch
    if env["BRANCH"] == "":
        ref = os.getenv("GITHUB_REF", "")
        if not ref == "":
            ref = ref.split('/')
            for varname, check in (("BRANCH", "heads"), ("TAG", "tag")):
                if ref[1] == check:
                    env[varname] = ref[2]
    if env["BRANCH"] == "":
        env["BRANCH"] = "master"

    # author/avatar
    if "sender" in event_manifest:
        if "avatar_url" in event_manifest["sender"]:
            env["COMMIT_AVATAR"] = event_manifest["sender"]["avatar_url"]
        if "login" in event_manifest["sender"]:
            env["COMMIT_AUTHOR"] = event_manifest["sender"]["login"]
    author = {}
    if not env["COMMIT_AUTHOR"] == "":
        author["name"] = env["COMMIT_AUTHOR"]
    if not env["COMMIT_AVATAR"] == "":
        author["icon_url"] = env["COMMIT_AVATAR"]

    # embed color
    color = 0x000000
    if env["COMMIT_AUTHOR"].lower() in colors:
        color = colors[env["COMMIT_AUTHOR"].lower()]

    # commit ID/message
    if "after" in event_manifest:
        if env["COMMIT_ID"] == "":
            env["COMMIT_ID"] = event_manifest["after"]

    if not env["COMMIT_ID"] == "":
        query = "commit/" + env["COMMIT_ID"]

    if not env["COMMIT_COMPARE"] == "":
        query = "compare/" + env["COMMIT_COMPARE"]

    if env["EVENT_MESSAGE"] == "":
        if "commits" in event_manifest:
            if len(event_manifest["commits"]) > 0:
                for commit in event_manifest["commits"]:
                    commit_message = commit["message"]
                    timestamp = commit["timestamp"]
                    if "\n\n" in commit_message:
                        commit_parts = commit_message.split("\n\n")
                        commit_title = commit_parts.pop(0)
                        commit_message = "\n\n".join(commit_parts)
                    else:
                        commit_title = commit_message
                    commit["url"] = commit["url"].replace(
                        "***",
                        CI_SETTINGS["common"]["common"]["repo"]["username"])
                    commits.append("[`" + commit["id"][:7] + "`](" +
                                   commit["url"] + ')' + ' ' + commit_title)
    else:
        commit = {}
        commit_message = env["EVENT_MESSAGE"]
        commit["id"] = env["COMMIT_ID"]
        if "\n\n" in commit_message:
            commit_parts = commit_message.split("\n\n")
            commit_title = commit_parts.pop(0)
            commit_message = "\n\n".join(commit_parts)
        else:
            commit_title = commit_message
        commit["url"] = "http://github.com/" + env["REPO_SLUG"] + '/' + query
        commit["url"] = commit["url"].replace(
            "***", CI_SETTINGS["common"]["common"]["repo"]["username"])
        commits.append("[`" + commit["id"][:7] + "`](" + commit["url"] + ')' +
                       ' ' + commit_title)

    # number of commits
    num_events = len(commits)
    if num_events > 0:
        env["EVENT_MESSAGE"] = ""
        if num_events == 1:
            if not env["COMMIT_COMPARE"] == "":
                num_events = "Many"
        for commit in commits:
            env["EVENT_MESSAGE"] += commit + "\n"

    # event type
    if "pull" in env["EVENT_TYPE"]:
        env["PULL_ID"] = os.getenv("TRAVIS_PULL_REQUEST", "")
        env["PULL_BRANCH"] = os.getenv("TRAVIS_PULL_REQUEST_BRANCH", "")
        env["PULL_SHA"] = os.getenv("TRAVIS_PULL_REQUEST_SHA", "")
        query = "pull/" + env["PULL_ID"]

    if "push" in env["EVENT_TYPE"]:
        env["EVENT_TYPE"] = "commit"

    if "release" in env["EVENT_TYPE"]:
        if "action" in event_manifest:
            env["EVENT_TYPE"] = event_manifest["action"] + ' ' + env[
                "EVENT_TYPE"]
        if "release" in event_manifest:
            if "tag_name" in event_manifest["release"]:
                num_events = 1
                env["EVENT_MESSAGE"] = CI_SETTINGS["common"]["common"]["repo"]["title"] + ' ' + \
                    event_manifest["release"]["tag_name"]
                query = "releases/tag/" + event_manifest["release"]["tag_name"]
                if "assets" in event_manifest["release"]:
                    print(event_manifest["release"]["assets"])

    # timestamp
    if timestamp == "":
        if "repository" in event_manifest:
            if "updated_at" in event_manifest["repository"]:
                timestamp = event_manifest["repository"]["updated_at"]

    if timestamp == "":
        utc_now = pytz.utc.localize(datetime.datetime.utcnow())
        pst_now = utc_now.astimezone(
            pytz.timezone(
                CI_SETTINGS["common"]["prepare_discord_notif"]["timezone"]))
        timestamp = pst_now.isoformat()
        timestamp = timestamp[:timestamp.find('.')]

    if timestamp != "":
        if '+' in timestamp:
            timestamp = timestamp[:timestamp.find('+')]
        elif '-' in timestamp:
            timestamp = timestamp.split('-')
            _ = timestamp.pop()
            timestamp = '-'.join(timestamp)

    # embed title
    embed_title = ""
    embed_title += '[' + env["REPO_NAME"] + ':' + env["BRANCH"] + "] "
    embed_title += str(num_events) + " new " + env["EVENT_TYPE"]
    if isinstance(num_events, str) or (isinstance(num_events, int) and
                                       (not num_events == 1)):
        embed_title += 's'

    # build payload
    payload = {
        "embeds": [{
            "color": color,
            "author": author,
            "title": embed_title,
            "url": "http://github.com/" + env["REPO_SLUG"] + '/' + query,
            "description": env["EVENT_MESSAGE"],
            "timestamp": timestamp
        }]
    }

    # get webhook for MegaMan.EXE
    DISCORD_WEBHOOK = os.environ.get("DISCORD_WEBHOOK")

    # send request
    r = requests.post(DISCORD_WEBHOOK,
                      data=json.dumps(payload),
                      headers={"Content-type": "application/json"})