From 0a759c0d1f9cf256b5df143a36edd779cb6bb766 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Wed, 9 Oct 2019 15:40:50 -0400 Subject: [PATCH] Add python test script for Git --- python/gitloaded.json | 15 +++++++++++ python/gittest.py | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 python/gitloaded.json create mode 100644 python/gittest.py diff --git a/python/gitloaded.json b/python/gitloaded.json new file mode 100644 index 0000000..c5c414c --- /dev/null +++ b/python/gitloaded.json @@ -0,0 +1,15 @@ +[ + { + "git": { + "token": "84014fake588token55b46751for4297u361223", + "id": "shaunrd0", + "name": "Shaun Reed", + "email": "shaunrd0@gmail.com" + }, + + "repos": { + "klips": "/home/kapper/Code/test", + "cmake": "/home/kapper/Code/cmake" + } + } +] diff --git a/python/gittest.py b/python/gittest.py new file mode 100644 index 0000000..3cf158a --- /dev/null +++ b/python/gittest.py @@ -0,0 +1,62 @@ +############################################################################### +## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ## +## ## +## A test script for using Git APIs and gitpython module ## +############################################################################### +# gittest.py + +import git +import requests +import json + + +def local_status(path): + repo = git.Repo(path) + if repo.bare is True: + print("Error loading repository located at %s, check that it is a repository\n" % path) + else: + print("Loaded local repository: %s\n\tOrigin URL: %s" % (repo.description, repo.remotes['origin'])) + return repo + +def remote_status(user, repo): + reply = requests.get('https://api.github.com/repos/%s/%s/commits' % (user, repo)) + commits = reply.json() + remoteCommit = [] + remoteCommit.append(commits[0]['commit']['author']['name']) + remoteCommit.append(commits[0]['commit']['author']['email']) + remoteCommit.append(commits[0]['commit']['author']['date']) + remoteCommit.append(commits[0]['commit']['message']) + print("Fetched most recent commit by %s <%s> on %s \n\tCommit message: %s\n" % (tuple(remoteCommit))) + return remoteCommit + +def load_config(): + global user + global paths + with open(config, 'r') as f: + loadedConfig = json.load(f) + user = loadedConfig[0]['git'] + paths = loadedConfig[0]['repos'] + +def load_repo(path): + global user + local = local_status(paths[path]) + print("\tRepo: ", repo) + remote_status(user["id"], repo) + print(local.active_branch.name) + +global user +global paths +user = [] +paths = [] +config = 'gitloaded.json' + +# Load user JSON config +load_config() +print("Loaded user: %s\n" % user) +print("Loaded paths: %s\n" % paths) + +for repo in paths: + print("Loading path: %s" % paths[repo]) + load_repo(repo) + print() +