2024-06-04 16:10:15 +03:00
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
"""
2024-07-03 17:01:03 +03:00
Check out pkg / { distribution } .
With - u , fetch commits , and if changed , commit the latest hash .
2024-06-04 16:10:15 +03:00
"""
import argparse
import json
import shlex
import subprocess
from pathlib import Path
def parse_args ( ) :
p = argparse . ArgumentParser (
description = __doc__ ,
)
p . add_argument (
' distribution ' ,
nargs = ' + ' ,
)
p . add_argument (
' --no-fetch ' ,
dest = ' fetch ' ,
action = ' store_false ' ,
default = True ,
)
2024-07-03 17:01:03 +03:00
p . add_argument (
' --update ' , ' -u ' ,
action = ' store_true ' ,
default = False ,
)
2024-06-04 16:10:15 +03:00
return p . parse_args ( )
def read_config ( distro : str ) :
cmd = [ ' mkosi ' , ' --json ' , ' -d ' , distro , ' summary ' ]
print ( f " + { shlex . join ( cmd ) } " )
text = subprocess . check_output ( cmd , text = True )
data = json . loads ( text )
2024-07-12 16:33:49 +03:00
images = { image [ " Image " ] : image for image in data [ " Images " ] }
return images [ " build " ]
2024-06-04 16:10:15 +03:00
def commit_file ( distro : str , file : Path , commit : str , changes : str ) :
message = ' \n ' . join ( (
f ' mkosi: update { distro } commit reference ' ,
' ' ,
changes ) )
cmd = [ ' git ' , ' commit ' , ' -m ' , message , str ( file ) ]
print ( f " + { shlex . join ( cmd ) } " )
subprocess . check_call ( cmd )
2024-07-03 17:01:03 +03:00
def checkout_distro ( args , distro : str , config : dict ) :
dest = Path ( f ' pkg/ { distro } ' )
if dest . exists ( ) :
print ( f ' { dest } already exists. ' )
return
url = config [ ' Environment ' ] [ ' GIT_URL ' ]
branch = config [ ' Environment ' ] [ ' GIT_BRANCH ' ]
# Only debian uses source-git for now…
reference = [ f ' --reference-if-able=. ' ] if distro == ' debian ' else [ ]
cmd = [
' git ' , ' clone ' , url ,
f ' --branch= { branch } ' ,
dest . as_posix ( ) ,
* reference ,
]
2024-06-04 16:10:15 +03:00
print ( f " + { shlex . join ( cmd ) } " )
subprocess . check_call ( cmd )
2024-07-03 17:01:03 +03:00
args . fetch = False # no need to fetch if we just cloned
def update_distro ( args , distro : str , config : dict ) :
2024-06-04 16:10:15 +03:00
branch = config [ ' Environment ' ] [ ' GIT_BRANCH ' ]
old_commit = config [ ' Environment ' ] [ ' GIT_COMMIT ' ]
2024-07-03 17:11:43 +03:00
cmd = [ ' git ' , ' -C ' , f ' pkg/ { distro } ' , ' switch ' , branch ]
print ( f " + { shlex . join ( cmd ) } " )
subprocess . check_call ( cmd )
2024-07-03 17:10:49 +03:00
cmd = [ ' git ' , ' -C ' , f ' pkg/ { distro } ' , ' fetch ' , ' origin ' , ' -v ' ,
f ' { branch } :remotes/origin/ { branch } ' ]
print ( f " + { shlex . join ( cmd ) } " )
subprocess . check_call ( cmd )
2024-06-04 16:10:15 +03:00
cmd = [ ' git ' , ' -C ' , f ' pkg/ { distro } ' , ' rev-parse ' , f ' refs/remotes/origin/ { branch } ' ]
print ( f " + { shlex . join ( cmd ) } " )
new_commit = subprocess . check_output ( cmd , text = True ) . strip ( )
if old_commit == new_commit :
print ( f ' { distro } : commit { new_commit !s} is still fresh ' )
return
2024-11-13 20:03:35 +03:00
cmd = [ ' git ' , ' -C ' , f ' pkg/ { distro } ' , ' log ' , ' --graph ' , ' --first-parent ' ,
2024-06-04 16:10:15 +03:00
' --pretty=oneline ' , ' --no-decorate ' , ' --abbrev-commit ' , ' --abbrev=10 ' ,
f ' { old_commit } .. { new_commit } ' ]
print ( f " + { shlex . join ( cmd ) } " )
changes = subprocess . check_output ( cmd , text = True ) . strip ( )
2024-07-12 16:33:49 +03:00
conf_dir = Path ( ' mkosi.images/build/mkosi.conf.d ' )
2024-06-04 16:10:15 +03:00
files = conf_dir . glob ( ' */*.conf ' )
for file in files :
s = file . read_text ( )
if old_commit in s :
print ( f ' { distro } : { file } : found old hash, updating… ' )
new = s . replace ( old_commit , new_commit )
assert new != s
file . write_text ( new )
commit_file ( distro , file , new_commit , changes )
break
else :
raise ValueError ( f ' { distro } : hash { new_commit } not found under { conf_dir } ' )
if __name__ == ' __main__ ' :
args = parse_args ( )
2024-07-03 17:01:03 +03:00
2024-06-04 16:10:15 +03:00
for distro in args . distribution :
2024-07-03 17:01:03 +03:00
config = read_config ( distro )
checkout_distro ( args , distro , config )
if args . update :
update_distro ( args , distro , config )