11#!/usr/bin/env python3
22
33import os
4- import sys
54import time
65import yaml
76import datetime
87import subprocess
8+ import argparse
99
1010start_dir = os .getcwd ()
1111
1212def main ():
13- if len (sys .argv ) != 2 :
14- print ("Usage: update.py <target_dir>" )
15- sys .exit (1 )
16-
17- target_dir = sys .argv [1 ]
18-
19- with open ("images.yaml" ) as file :
13+ parser = argparse .ArgumentParser (
14+ description = 'Pull and update container images from Docker to Apptainer/Singularity format'
15+ )
16+ parser .add_argument (
17+ 'target_dir' ,
18+ help = 'Target directory where images will be stored'
19+ )
20+ parser .add_argument (
21+ '--config' ,
22+ default = 'images.yaml' ,
23+ help = 'Path to the configuration YAML file (default: images.yaml)'
24+ )
25+ parser .add_argument (
26+ '--keep-days' ,
27+ type = int ,
28+ default = 10 ,
29+ help = 'Number of days to keep old images (default: 10)'
30+ )
31+
32+ args = parser .parse_args ()
33+
34+ with open (args .config ) as file :
2035 conf = yaml .safe_load (file )
2136
2237 now = datetime .datetime .now (datetime .timezone .utc )
@@ -36,13 +51,13 @@ def main():
3651 sing_arch = "arm64" if arch == "aarch64" else "amd64"
3752
3853 os .chdir (start_dir )
39- os .makedirs (os .path .join (target_dir , arch , img ["name" ]), exist_ok = True )
40- os .chdir (os .path .join (target_dir , arch , img ["name" ]))
54+ os .makedirs (os .path .join (args . target_dir , arch , img ["name" ]), exist_ok = True )
55+ os .chdir (os .path .join (args . target_dir , arch , img ["name" ]))
4156
4257 print (f"Working in { os .getcwd ()} " )
4358
4459 # log the build to a file in the same structure under logs/
45- log_dir = os .path .join ("../../.." , target_dir , "logs" , arch )
60+ log_dir = os .path .join ("../../.." , args . target_dir , "logs" , arch )
4661 os .makedirs (log_dir , exist_ok = True )
4762 log_file = os .path .join (log_dir , f"{ img ['name' ]} .txt" )
4863
@@ -65,7 +80,7 @@ def main():
6580 subprocess .run ("ls *.sif | sort | tail -n 1 > latest.txt" , shell = True )
6681
6782 # cleanup old images, keep only latest N
68- subprocess .run ("find . -maxdepth 1 -name \\ *.sif -mtime +10 -exec rm -f {} \\ ;" , shell = True )
83+ subprocess .run (f "find . -maxdepth 1 -name \\ *.sif -mtime +{ args . keep_days } -exec rm -f {{} } \\ ;" , shell = True )
6984
7085 now2 = datetime .datetime .now (datetime .timezone .utc )
7186 now2_human = now2 .strftime ("%Y-%m-%d %H:%M:%S %Z" )
@@ -76,4 +91,3 @@ def main():
7691
7792if __name__ == "__main__" :
7893 main ()
79-
0 commit comments