Github user kaknikhil commented on a diff in the pull request: https://github.com/apache/madlib/pull/271#discussion_r191603261 --- 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) + raise ValueError("Missing module SQL file (%s)" % sqlfile) -def _run_sql_file(schema, maddir_mod_py, module, sqlfile, - tmpfile, logfile, pre_sql, upgrade=False, - sc=None): + # Prepare the file using M4 + try: + # Add the before SQL + if pre_sql: + output_filehandle.writelines([pre_sql, '\n\n']) + # Find the madpack dir (platform specific or generic) + if os.path.isdir(maddir + "/ports/" + portid + "/" + dbver + "/madpack"): + maddir_madpack = maddir + "/ports/" + portid + "/" + dbver + "/madpack" + else: + maddir_madpack = maddir + "/madpack" + maddir_ext_py = maddir + "/lib/python" + + m4args = ['m4', + '-P', + '-DMADLIB_SCHEMA=' + schema, + '-DPLPYTHON_LIBDIR=' + maddir_mod_py, + '-DEXT_PYTHON_LIBDIR=' + maddir_ext_py, + '-DMODULE_PATHNAME=' + maddir_lib, + '-DMODULE_NAME=' + module, + '-I' + maddir_madpack, + sqlfile] + + info_(this, "> ... parsing: " + " ".join(m4args), verbose) + output_filehandle.flush() + subprocess.call(m4args, stdout=output_filehandle) + except: + error_(this, "Failed executing m4 on %s" % sqlfile, False) + raise Exception + +def _run_sql_file_install_check(schema, maddir_mod_py, module, sqlfile, + tmpfile, logfile, pre_sql, upgrade=False, + sc=None): --- End diff -- we don't really need the two optional params since `_run_sql_file_install_check` is only called once.
---