@@ -31,7 +31,7 @@ def generate_ttl_file(collection, translation_key):
3131 """Generate a TTL file for a given collection and translation_key."""
3232 if not translation_key :
3333 print (f"Skipping: No translation_key found." )
34- return
34+ return True # Indicate that processing continued
3535
3636 layout = COLLECTIONS [collection ]
3737 collection_name = collection .lstrip ("_" ) # Remove leading underscore
@@ -50,46 +50,78 @@ def generate_ttl_file(collection, translation_key):
5050
5151 # Write the TTL file
5252 ttl_path = os .path .join (collection_ttl_dir , f"{ translation_key } .ttl" )
53- with open (ttl_path , "w" , encoding = "utf-8" ) as f :
54- f .write (ttl_content )
55- print (f"Generated: { ttl_path } " )
53+
54+ # Check if the content has changed
55+ content_changed = False
56+ if os .path .exists (ttl_path ):
57+ with open (ttl_path , "r" , encoding = "utf-8" ) as f :
58+ existing_content = f .read ()
59+ if existing_content != ttl_content :
60+ content_changed = True
61+ else :
62+ content_changed = True
63+
64+ if content_changed :
65+ with open (ttl_path , "w" , encoding = "utf-8" ) as f :
66+ f .write (ttl_content )
67+ print (f"Generated/Updated: { ttl_path } " )
68+ return True
69+ else :
70+ print (f"No changes needed: { ttl_path } " )
71+ return False
5672
5773def process_collection (collection ):
5874 """Process all markdown files in the 'de/' subdirectory of a collection."""
5975 de_dir = os .path .join (BASE_DIR , collection , "de" )
6076 if not os .path .exists (de_dir ):
6177 print (f"Directory not found: { de_dir } " )
62- return
78+ return False
6379
80+ changes_made = False
6481 print (f"Processing collection: { collection } /de/" )
6582 for filename in os .listdir (de_dir ):
6683 if filename .endswith (".md" ):
6784 filepath = os .path .join (de_dir , filename )
6885 translation_key = extract_translation_key (filepath )
6986 if translation_key :
7087 print (f"Found translation_key: { translation_key } in { filename } " )
71- generate_ttl_file (collection , translation_key )
88+ if generate_ttl_file (collection , translation_key ):
89+ changes_made = True
7290 else :
7391 print (f"No translation_key in { filename } " )
92+ return changes_made
7493
7594def commit_and_push ():
7695 """Commit and push the generated TTL files to Git using a fine-grained PAT."""
7796 try :
7897 os .chdir (BASE_DIR )
7998
99+ # Check if there are any changes to commit
100+ result = subprocess .run (
101+ ["git" , "status" , "--porcelain" , "_ttl/" ],
102+ capture_output = True ,
103+ text = True
104+ )
105+ if not result .stdout .strip ():
106+ print ("No changes in TTL files to commit." )
107+ return
108+
80109 # Add TTL files to Git
81110 subprocess .run (["git" , "add" , "_ttl/" ], check = True )
82111
83112 # Commit changes
84- result = subprocess .run (
113+ commit_result = subprocess .run (
85114 ["git" , "commit" , "-m" , "Update TTL files [automated]" ],
86- check = False ,
87115 capture_output = True ,
88116 text = True
89117 )
90- if result .returncode != 0 and "nothing to commit" not in result .stderr :
91- print (f"Error committing: { result .stderr } " )
92- return
118+ if commit_result .returncode != 0 :
119+ if "nothing to commit" in commit_result .stderr :
120+ print ("No changes to commit." )
121+ return
122+ else :
123+ print (f"Error committing: { commit_result .stderr } " )
124+ return
93125
94126 # Push using the fine-grained PAT in the remote URL
95127 pat = os .getenv ("GITHUB_PAT" )
@@ -99,14 +131,13 @@ def commit_and_push():
99131 remote_url = f"https://oauth2:{ pat } @github.com/nfdi4objects/NFDI4Objects-Website.git"
100132 push_result = subprocess .run (
101133 ["git" , "push" , remote_url ],
102- check = False ,
103134 capture_output = True ,
104135 text = True
105136 )
106137 if push_result .returncode != 0 :
107138 print (f"Error pushing: { push_result .stderr } " )
108139 else :
109- print ("Committed and pushed TTL files to Git." )
140+ print ("Successfully pushed TTL files to Git." )
110141 except ValueError as e :
111142 print (f"Error: { str (e )} " )
112143 except Exception as e :
@@ -118,11 +149,15 @@ def main():
118149 print (f"TTL files will be saved to: { TTL_DIR } " )
119150
120151 # Process each collection
152+ changes_made = False
121153 for collection in COLLECTIONS :
122- process_collection (collection )
154+ if process_collection (collection ):
155+ changes_made = True
123156
124- # Commit and push the changes
125- commit_and_push ()
157+ if changes_made :
158+ commit_and_push ()
159+ else :
160+ print ("No changes detected in any TTL files." )
126161
127162if __name__ == "__main__" :
128163 main ()
0 commit comments