Skip to content

Commit

Permalink
pkg-cleanup: optimize gen-update by using pygit2 and reading the hist…
Browse files Browse the repository at this point in the history
…ory once
  • Loading branch information
lilydjwg committed Jun 11, 2024
1 parent faa044b commit 03614ef
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
44 changes: 20 additions & 24 deletions pkg-cleanup/gen-update
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
#!/usr/bin/python3

import sys, os
import json
from pathlib import Path
import logging
from collections import defaultdict
from copy import deepcopy
import time
import subprocess

import pygit2

import yamlutils
from lilac2 import lilacyaml
import const

sys.path.append(os.path.join(os.path.dirname(__file__), '../pylib'))
import gitutils

logger = logging.getLogger(__name__)

REPODIR = Path(const.REPODIR)
lilac_mail = '[email protected]'

def check_last_git_update(pkg):
cmd = [
"git", "log", "--format=%at %ae", "--", pkg,
]

p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, universal_newlines=True,
cwd = REPODIR,
)

try:
stdout = p.stdout
while True:
line = stdout.readline()
t, email = line.rstrip().split(None, 1)
if int(t) < time.time() - 30 * 86400:
return False
if email == lilac_mail:
continue
return True
finally:
p.terminate()
def get_last_git_update():
repo = pygit2.Repository(REPODIR)
stop_time = time.time() - 30 * 86400
package_last_update = defaultdict(int)
for commit in gitutils.iter_commits(repo, stop_time, lilac_mail):
if len(commit.parents) == 1:
diff = repo.diff(commit, commit.parents[0])
pkgs = gitutils.get_touched_packages(diff)
for pkg in pkgs:
package_last_update[pkg] = max(package_last_update[pkg], commit.commit_time)

return package_last_update

def main():
with open('/home/lilydjwg/tmpfs/removed.json') as f:
Expand Down Expand Up @@ -73,9 +68,10 @@ def main():
for m in old[name]:
pkgs_to_remove[m].append(name)

package_last_update = get_last_git_update()
for pkgs in pkgs_to_remove.values():
for pkg in pkgs[:]:
if check_last_git_update(pkg):
if package_last_update.get(('archlinuxcn', pkg)):
pkgs.remove(pkg)

with open('/home/lilydjwg/tmpfs/update.yaml', 'w') as f:
Expand Down
29 changes: 29 additions & 0 deletions pylib/gitutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def iter_commits(repo, stop_time, lilac_mail):
commit_hash = repo.head.target
commit = repo[commit_hash]
stack = [commit]
seen_commits = set()

while stack:
commit = stack.pop()
if commit.commit_time < stop_time:
continue
if commit.id.raw in seen_commits:
continue
stack.extend(commit.parents)
seen_commits.add(commit.id.raw)
if commit.author.email == lilac_mail:
continue

yield commit

def get_touched_packages(diff):
ret = set()
for d in diff.deltas:
for a in [d.old_file, d.new_file]:
parts = a.path.split('/', 2)
if len(parts) == 3:
r, pkgbase, _ = parts
ret.add((r, pkgbase))
return ret

0 comments on commit 03614ef

Please sign in to comment.