Annotate by default if there is an existing annotation.
[cython.git] / Cython / Compiler / Main.py
index c20d7a7c5af931c0177545d47e5aa11d6ab8ec3c..993d8d9985a612896c18d817069deba72598ffe3 100644 (file)
@@ -2,7 +2,7 @@
 #   Cython Top Level
 #
 
-import os, sys, re
+import os, sys, re, codecs
 if sys.version_info[:2] < (2, 3):
     sys.stderr.write("Sorry, Cython requires Python 2.3 or later\n")
     sys.exit(1)
@@ -607,6 +607,14 @@ def run_pipeline(source, options, full_module_name = None):
     # Set up result object
     result = create_default_resultobj(source, options)
 
+    if options.annotate is None:
+        # By default, decide based on whether an html file already exists.
+        html_filename = os.path.splitext(result.c_file)[0] + ".html"
+        if os.path.exists(html_filename):
+            line = codecs.open(html_filename, "r", encoding="UTF-8").readline()
+            if line.startswith(u'<!-- Generated by Cython'):
+                options.annotate = True            
+
     # Get pipeline
     if source_ext.lower() == '.py':
         pipeline = context.create_py_pipeline(options, result)
@@ -826,7 +834,7 @@ default_options = dict(
     errors_to_stderr = 1,
     cplus = 0,
     output_file = None,
-    annotate = False,
+    annotate = None,
     generate_pxi = 0,
     working_path = "",
     recursive = 0,