1515
1616
1717def process_file (file : Path , out_file : Path | None = None , execute : bool = False ):
18- """Converts a python file to markdown using jupytext and nbconvert."""
18+ """Converts a python file to markdown using jupytext and nbconvert.
19+
20+ Raises:
21+ subprocess.CalledProcessError: If the conversion fails.
22+ """
1923
2024 out_dir = out_file .parent
2125 command = f"cd { out_dir .as_posix ()} && "
@@ -30,7 +34,11 @@ def process_file(file: Path, out_file: Path | None = None, execute: bool = False
3034 else :
3135 command += f"jupytext --to markdown { file } --output { out_file } "
3236
33- subprocess .run (command , shell = True , check = False )
37+ result = subprocess .run (command , shell = True , check = False , capture_output = True , text = True )
38+ if result .returncode != 0 :
39+ error_msg = f"Failed to process { file .name } : { result .stderr } "
40+ print (error_msg )
41+ raise subprocess .CalledProcessError (result .returncode , command , output = result .stdout , stderr = result .stderr )
3442
3543
3644def is_modified (file : Path , out_file : Path ):
@@ -63,30 +71,51 @@ def main(args):
6371
6472 print (files )
6573
74+ # Track failures
75+ failures = []
76+
6677 # process files in parallel
6778 if args .parallel :
6879 with ThreadPoolExecutor (max_workers = args .max_workers ) as executor :
69- futures = []
80+ futures = {}
7081 for file in files :
7182 out_file = out_dir / f"{ file .stem } .md"
72- futures .append (
73- executor .submit (
74- process_file , file , out_file = out_file , execute = args .execute
75- )
83+ future = executor .submit (
84+ process_file , file , out_file = out_file , execute = args .execute
7685 )
86+ futures [future ] = file
7787
7888 for future in as_completed (futures ):
89+ file = futures [future ]
7990 try :
8091 future .result ()
92+ print (f"Successfully processed: { file .name } " )
8193 except Exception as e :
82- print (f"Error processing file: { e } " )
94+ print (f"Error processing { file .name } : { e } " )
95+ failures .append ((file , e ))
8396 else :
8497 for file in files :
8598 out_file = out_dir / f"{ file .stem } .md"
86- process_file (file , out_file = out_file , execute = args .execute )
99+ try :
100+ process_file (file , out_file = out_file , execute = args .execute )
101+ print (f"Successfully processed: { file .name } " )
102+ except Exception as e :
103+ print (f"Error processing { file .name } : { e } " )
104+ failures .append ((file , e ))
105+
106+ # Report failures and exit with error code if any failed
107+ if failures :
108+ print (f"\n { len (failures )} file(s) failed to process:" )
109+ for file , error in failures :
110+ print (f" - { file .name } " )
111+ return 1 # Return non-zero exit code
112+ else :
113+ print (f"\n All { len (files )} file(s) processed successfully!" )
114+ return 0
87115
88116
89117if __name__ == "__main__" :
118+ import sys
90119 project_root = Path (__file__ ).parents [2 ]
91120
92121 parser = ArgumentParser ()
@@ -99,4 +128,5 @@ def main(args):
99128 parser .add_argument ("--parallel" , type = bool , default = False )
100129 args = parser .parse_args ()
101130
102- main (args )
131+ exit_code = main (args )
132+ sys .exit (exit_code )
0 commit comments