Add python test script for Git

This commit is contained in:
Shaun Reed 2019-10-09 15:40:50 -04:00
parent 840143eec5
commit 0a759c0d1f
2 changed files with 77 additions and 0 deletions

15
python/gitloaded.json Normal file
View File

@ -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"
}
}
]

62
python/gittest.py Normal file
View File

@ -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()