Last active
September 29, 2016 07:54
-
-
Save bobismijnnaam/84a112f18abdf7fe7967a36d4615061b to your computer and use it in GitHub Desktop.
Syncs all RoboTeamTwente packages in catkin workspace.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import getpass | |
from git import Repo | |
from git.exc import * | |
from github import Github, GithubException | |
import os | |
import sys | |
description = """ | |
Version: 29/9/2016 09:51 | |
Script to sync the roboteam twente repos. Only works when executed in a catkin workspace. | |
Run with either "./rts.py insert-args-here" or "python3 rts.py insert-args-here". | |
Needed: | |
- python 3 | |
- gitpython (>= 2.0.8) | |
- pygithub (>= 1.28) | |
Python 3 should ship with Ubuntu. Other dependencies can be installed with "pip3 install [insert package here]". | |
Command line usage: | |
- rts.py all (master|devel) | |
Switches all packages to master or devel. | |
- rts.py all (master|devel) except [package] [package] ... | |
Switches all packages to master or devel, except the specified packages | |
- rts.py switch master [package] [package] ... devel [package] [package] ... | |
Switches specified packages to master, and specified package to devel. | |
Use "any" after master or devel to select any package that | |
does not appear in the other category. Unspecified repos remain untouched. Only | |
one (either master or devel) can contain an "any". The order of master and devel is | |
significant (so don't put devel first). | |
- rts.py sync | |
For each repo, switch to devel, pull, switch to master, pull. | |
Leaves each repo at master. | |
- rts.py show | |
Shows all the current branches and which repos contain uncomitted changes. | |
- rts.py get | |
Downloads all roboteam_* repos that are not yet downloaded from organization RoboTeamTwente. | |
Repos which are dirty (i.e. have changed files) will be printed and unaffected by the specified operation. | |
Revision history: | |
- 29/9/2016 09:51 | |
Added get command that pulls all roboteam_* not yet pulled from organization RoboTeamTwente. | |
- 28/9/2016 15:43 | |
Added show command that shows every packages state. Added --ff-only to pull s.t. packages can only be pulled | |
if a pull is a fast-forward. Not really tested though. | |
- 28/9/2016 01:01 | |
Initial version. No comments yet. | |
""" | |
def displayHelp(): | |
print("\n-- Help --\n" + description) | |
def abort(reason): | |
print(reason, "Aborting.") | |
displayHelp() | |
exit(1) | |
def getAllRepos(): | |
files = [os.path.join(".", f) for f in os.listdir(".")] | |
files = [f for f in files if os.path.isdir(f)] | |
repos = [] | |
for f in files: | |
try: | |
repos += [Repo(f)] | |
except (InvalidGitRepositoryError, NoSuchPathError): | |
print("Skipping \"" + f + "\", not a git repo") | |
return repos | |
def getRepoName(repo): | |
return os.path.basename(repo.working_dir) | |
def pullBranch(repo, branch): | |
if repo.is_dirty(): | |
print("Repo", getRepoName(repo), "has uncomitted changes; skipping.") | |
return | |
if branch in repo.heads: | |
repo.heads[branch].checkout() | |
try: | |
repo.remotes.origin.pull(**{"ff-only":True}) | |
except GitCommandError: | |
print("Pulling", getRepoName(repo), "/", branch, "failed") | |
def tryCheckoutBranch(repo, branch): | |
if branch in repo.heads: | |
try: | |
repo.heads[branch].checkout() | |
print("Checked out " + getRepoName(repo) + "/" + branch) | |
except GitCommandError: | |
print("It seems you still have uncommitted changes on", | |
getRepoName(repo) + "/" + branch + "; skipping.") | |
else: | |
print("Cannot checkout " + getRepoName(repo) + "/" + branch + "; skipping.") | |
def syncAllRepos(): | |
repos = getAllRepos() | |
for repo in repos: | |
print("Syncing", getRepoName(repo)) | |
pullBranch(repo, "devel") | |
pullBranch(repo, "master") | |
def setAllToExcept(targetBranch, ignoredRepos): | |
repos = getAllRepos() | |
for repo in repos: | |
if not getRepoName(repo) in ignoredRepos: | |
tryCheckoutBranch(repo, targetBranch) | |
def switchRepos(masters, devels): | |
masterContainsAny = "any" in masters | |
develContainsAny = "any" in devels | |
if masterContainsAny and develContainsAny: | |
abort("Not both master and devel can contain \"any\".") | |
for repo in getAllRepos(): | |
isInMaster = getRepoName(repo) in masters or masterContainsAny | |
isInDevel = getRepoName(repo) in devels or develContainsAny | |
if isInMaster: | |
tryCheckoutBranch(repo, "master") | |
elif isInDevel: | |
tryCheckoutBranch(repo, "devel") | |
def showInfo(): | |
for repo in getAllRepos(): | |
msg = "" | |
if repo.is_dirty(): | |
msg = "contains changes" | |
else: | |
msg = "no changes" | |
print("{:>17} {:>1} {:>8} {:>10}".format(getRepoName(repo), "/", str(repo.active_branch), msg)) | |
def downloadRepos(): | |
username = input("GitHub username: ") | |
pw = getpass.getpass() | |
g = Github(username, pw) | |
try: | |
org = g.get_organization("RoboTeamTwente") | |
except GithubException: | |
print("Could not find organization RoboTeamTwente") | |
return | |
repoNames = [repo.name for repo in list(org.get_repos())] | |
repoNames = [repo for repo in repoNames if repo.startswith("roboteam_")] | |
currentRepos = [getRepoName(repo) for repo in getAllRepos()] | |
for repoName in repoNames: | |
if not repoName in currentRepos: | |
print("Cloning repo:", repoName) | |
repo = Repo.clone_from("https://github.com/RoboTeamTwente/" + repoName + ".git", os.path.join(".", repoName)) | |
if __name__ == "__main__": | |
files = os.listdir(".") | |
if not ".catkin_workspace" in files: | |
abort("I'm not being executed in a catkin workspace") | |
try: | |
os.chdir("src") | |
except FileNotFoundError: | |
abort("Catkin workspace does not contain directory \"src\"") | |
args = sys.argv | |
if len(args) == 1: | |
abort("Cannot run rts.py without arguments.") | |
if args[1] == "help": | |
displayHelp() | |
exit() | |
elif args[1] == "sync": | |
print("Syncing") | |
syncAllRepos() | |
elif args[1] == "switch": | |
masterStart = args.index("master") | |
develStart = args.index("devel") | |
if develStart != -1 and masterStart >= develStart: | |
abort("Master and devel arguments are ordered the wrong way.") | |
masterRepos = [] | |
if masterRepos != -1: | |
masterRepos = args[masterStart + 1:develStart] | |
develRepos = [] | |
if develStart != -1: | |
develRepos = args[develStart + 1:] | |
print("Switching") | |
switchRepos(masters=masterRepos, devels=develRepos) | |
elif args[1] == "all": | |
if len(args) < 3: | |
abort("Insufficient arguments.") | |
print("Switching all") | |
if len(args) == 3: | |
if args[2] == "master": | |
switchRepos(masters=["any"], devels=[]) | |
elif args[2] == "devel": | |
switchRepos(masters=[], devels=["any"]) | |
else: | |
abort("Unknown directive \"" + args[2] + "\".") | |
displayHelp() | |
exit() | |
elif args[3] == "except": | |
setAllToExcept(args[2], args[4:]) | |
else: | |
abort("Unknown combination of arguments.") | |
displayHelp() | |
exit() | |
elif args[1] == "show": | |
print("Showing info") | |
showInfo() | |
elif args[1] == "get": | |
print("Getting repos") | |
downloadRepos() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment