about summary refs log tree commit diff
path: root/shadowbox.py
blob: 93e08e98291ca62af943b6539e09a3af02b49503 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from dataclasses import dataclass
from flask import Flask, render_template
from pathlib import Path
import commonmark
import os
import subprocess

root = Path(os.environ['HOME'])/'devil'
app = Flask(__name__)

@app.route('/')
def list_repos():
	repos = find_repos().values()
	return render_template('repo-list.html', title='Shadowbox', repos=repos)

@app.route('/<path:name>')
def show_repo(name):
	repo = find_repos()[name]

	cmd = '/usr/bin/git log --oneline'
	commits = subprocess.run(cmd, cwd=root/name, shell=True, capture_output=True, text=True).stdout.splitlines()

	readme = get_readme(name)

	return render_template('repo.html', title=f'{repo.name} - {repo.description}', repo=repo, commits=commits, readme=readme)

def get_readme(name):
	path = root/name/'README.md'
	if path.exists():
		with(root/name/'README.md').open() as f:
			content = f.read()
			ast = commonmark.Parser().parse(content)
			readme = commonmark.HtmlRenderer().render(ast)
		return readme
	else:
		return None

@dataclass
class Repo:
	name: str
	description: str
	last_update: str # date

def find_repos() -> dict[Repo]:
	# iterate through all subdirectories, looking for things that look like git repos
	cmd = '/usr/bin/find . -type d -execdir /usr/bin/git -C {} rev-parse ";" -printf "%P\n" -prune'
	repo_folders = subprocess.run(cmd, cwd=root, shell=True, capture_output=True, text=True).stdout.splitlines()

	repos = {}
	for folder in repo_folders:
		# description is at ./description for bare repos, ./.git/description for normal repos
		loc = root/folder/'description' if (root/folder/'description').exists() else root/folder/'.git'/'description'
		with loc.open() as f:
			desc = f.readline()

		cmd = '/usr/bin/git show --no-patch --format="%cr"'
		last = subprocess.run(cmd, cwd=root/folder, shell=True, capture_output=True, text=True).stdout

		repos[folder] = Repo(folder, desc, last)
	return repos