Github user kaknikhil commented on a diff in the pull request: https://github.com/apache/madlib/pull/271#discussion_r191588071 --- Diff: src/madpack/madpack.py --- @@ -131,10 +141,73 @@ def _get_relative_maddir(maddir, port): return maddir # ------------------------------------------------------------------------------ +def _cleanup_comments_in_sqlfile(output_filename, upgrade): + """ + @brief Remove comments in the sql script + """ + if not upgrade: + with open(output_filename, 'r+') as output_filehandle: + full_sql = output_filehandle.read() + pattern = re.compile(r"""(/\*(.|[\r\n])*?\*/)|(--(.*|[\r\n]))""") + res = '' + lines = re.split(r'[\r\n]+', full_sql) + for line in lines: + tmp = line + if not tmp.strip().startswith("E'"): + line = re.sub(pattern, '', line) + res += line + '\n' + full_sql = res.strip() + full_sql = re.sub(pattern, '', full_sql).strip() + # Re-write the cleaned-up sql to a new file. Python does not let us + # erase all the content of a file and rewrite the same file again. + cleaned_output_filename = output_filename+'.tmp' + with open(cleaned_output_filename, 'w') as output_filehandle: + _write_to_file(output_filehandle, full_sql) + # Move the cleaned output file to the old one. + os.rename(cleaned_output_filename, output_filename) + +def _run_m4_and_append(schema, maddir_mod_py, module, sqlfile, + output_filehandle, pre_sql=None): + """ + Function to process a sql file with M4. + """ + # Check if the SQL file exists + if not os.path.isfile(sqlfile): + error_(this, "Missing module SQL file (%s)" % sqlfile, False) --- End diff -- why do we need to call `error_`, isn't `ValueError` enough ?
---