|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +from adabot import requests |
| 24 | +import os |
| 25 | +import subprocess |
| 26 | +import shlex |
| 27 | +from io import StringIO |
| 28 | + |
| 29 | +import sh |
| 30 | +from sh.contrib import git |
| 31 | + |
| 32 | +bundles = ["Adafruit_CircuitPython_Bundle", "CircuitPython_Community_Bundle"] |
| 33 | + |
| 34 | +def fetch_bundle(bundle, directory): |
| 35 | + if not os.path.isdir(bundle_path): |
| 36 | + os.makedirs(directory, exist_ok=True) |
| 37 | + git.clone("-o", "adafruit", "https://github.com/adafruit/" + bundle + ".git", bundle_path) |
| 38 | + working_directory = os.getcwd() |
| 39 | + os.chdir(bundle_path) |
| 40 | + git.pull() |
| 41 | + git.submodule("init") |
| 42 | + git.submodule("update") |
| 43 | + os.chdir(working_directory) |
| 44 | + |
| 45 | + |
| 46 | +class Submodule: |
| 47 | + def __init__(self, directory): |
| 48 | + self.directory = directory |
| 49 | + |
| 50 | + def __enter__(self): |
| 51 | + self.original_directory = os.path.abspath(os.getcwd()) |
| 52 | + os.chdir(self.directory) |
| 53 | + |
| 54 | + def __exit__(self, exc_type, exc_value, traceback): |
| 55 | + os.chdir(self.original_directory) |
| 56 | + |
| 57 | + |
| 58 | +def commit_to_tag(repo_path, commit): |
| 59 | + with Submodule(repo_path): |
| 60 | + try: |
| 61 | + output = StringIO() |
| 62 | + git.describe("--tags", "--exact-match", commit, _out=output) |
| 63 | + commit = output.getvalue().strip() |
| 64 | + except sh.ErrorReturnCode_128: |
| 65 | + pass |
| 66 | + return commit |
| 67 | + |
| 68 | +def repo_version(): |
| 69 | + version = StringIO() |
| 70 | + try: |
| 71 | + git.describe("--tags", "--exact-match", _out=version) |
| 72 | + except sh.ErrorReturnCode_128: |
| 73 | + git.log(pretty="format:%h", n=1, _out=version) |
| 74 | + |
| 75 | + return version.getvalue().strip() |
| 76 | + |
| 77 | + |
| 78 | +def repo_remote_url(repo_path): |
| 79 | + with Submodule(repo_path): |
| 80 | + output = StringIO() |
| 81 | + git.remote("get-url", "origin", _out=output) |
| 82 | + return output.getvalue().strip() |
| 83 | + |
| 84 | +def update_bundle(bundle_path): |
| 85 | + working_directory = os.path.abspath(os.getcwd()) |
| 86 | + os.chdir(bundle_path) |
| 87 | + git.submodule("foreach", "git", "fetch") |
| 88 | + # sh fails to find the subcommand so we use subprocess. |
| 89 | + subprocess.run(shlex.split("git submodule foreach 'git checkout -q `git rev-list --tags --max-count=1`'"), stdout=subprocess.DEVNULL) |
| 90 | + |
| 91 | + # Don't update circuitpython, its going away soon. |
| 92 | + git.submodule("update", "circuitpython") |
| 93 | + |
| 94 | + status = StringIO() |
| 95 | + result = git.status("--short", _out=status) |
| 96 | + updates = [] |
| 97 | + for status_line in status.getvalue().strip().split("\n"): |
| 98 | + action, directory = status_line.split() |
| 99 | + if action != "M" or not directory.startswith("libraries"): |
| 100 | + RuntimeError("Unsupported updates") |
| 101 | + |
| 102 | + # Compute the tag difference. |
| 103 | + diff = StringIO() |
| 104 | + result = git.diff("--submodule=log", directory, _out=diff) |
| 105 | + diff_lines = diff.getvalue().split("\n") |
| 106 | + commit_range = diff_lines[0].split()[2] |
| 107 | + commit_range = commit_range.strip(":").split(".") |
| 108 | + old_commit = commit_to_tag(directory, commit_range[0]) |
| 109 | + new_commit = commit_to_tag(directory, commit_range[-1]) |
| 110 | + url = repo_remote_url(directory) |
| 111 | + summary = "\n".join(diff_lines[1:-1]) |
| 112 | + updates.append((url[:-4], old_commit, new_commit, summary)) |
| 113 | + os.chdir(working_directory) |
| 114 | + return updates |
| 115 | + |
| 116 | +def commit_updates(update_info): |
| 117 | + message = ["Automated update by Adabot (adafruit/adabot@{})" |
| 118 | + .format(repo_version())] |
| 119 | + for url, old_commit, new_commit, summary in update_info: |
| 120 | + url_parts = url.split("/") |
| 121 | + user, repo = url_parts[-2:] |
| 122 | + summary = summary.replace("#", "{}/{}#".format(user, repo)) |
| 123 | + message.append("Updating {} to {} from {}:\n{}".format(url, |
| 124 | + new_commit, |
| 125 | + old_commit, |
| 126 | + summary)) |
| 127 | + message = "\n\n".join(message) |
| 128 | + git.add(".") |
| 129 | + git.commit(message=message) |
| 130 | + pass |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + directory = ".bundles" |
| 134 | + for bundle in bundles[:1]: |
| 135 | + bundle_path = os.path.abspath(os.path.join(directory, bundle)) |
| 136 | + #fetch_bundle(bundle, bundle_path) |
| 137 | + update_info = update_bundle(bundle_path) |
| 138 | + if update_info: |
| 139 | + commit_updates(update_info) |
0 commit comments