Created
April 27, 2012 03:09
-
-
Save kladd/2505360 to your computer and use it in GitHub Desktop.
Lua port script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # Automagical Lua Porter | |
| # | |
| # 2012-05-04 | |
| # | |
| import sys | |
| import os | |
| import re | |
| class Controller(object): | |
| PKG_INFO = {} | |
| PKG_INFO_KEYS = [] | |
| INSTALL_PATH = False | |
| PREPEND_PATH = [] | |
| STD_PATH = False | |
| STD_PATHS = [] | |
| SETENV = [] | |
| FLAVORS = [] | |
| FAMILY = "" | |
| EXIT_CONDS = [] | |
| WHATIS = False | |
| METADATA = False | |
| LOAD = [] | |
| def __init__(self, in_file, out_file): | |
| self.parser = Parser(in_file) | |
| self.writer = Writer(out_file) | |
| class Parser(Controller): | |
| FILE = None | |
| def __init__(self, file_object): | |
| self.FILE = file_object | |
| def get_exit_conditions(self, line ): | |
| flag = False | |
| if "if" in line and "__HPCC_is_known_cluster_flavor" in line and "break" in line: | |
| Controller.EXIT_CONDS\ | |
| .append("if __HPCC_is_known_cluster_flavor() == false then os.exit() end") | |
| flag = True | |
| if "if" in line and "__HPCC_req_cpu_arch" in line and "break" in line\ | |
| and "x86_64" in line: | |
| Controller.EXIT_CONDS\ | |
| .append("if __HPCC_req_cpu_arch({0}) == false then os.exit() end\n"\ | |
| .format("{\"x86_64\"}")) | |
| flag = True | |
| if flag: return True | |
| else: return False | |
| def get_pkg_info( self, line ): | |
| key = re.search("(?<=pkg_info\(\")(.*?)(?=\"\))", line) | |
| if key: | |
| if key.group(0) not in Controller.PKG_INFO_KEYS: | |
| Controller.PKG_INFO_KEYS.append(key.group(0)) | |
| if key.group(0) not in Controller.PKG_INFO: Controller.PKG_INFO[key.group(0)] = [] | |
| val = re.search("(?<=[\s]\")(.*?)(?=\")", line) | |
| if val: Controller.PKG_INFO[key.group(0)].append(val.group(0)) | |
| return True | |
| return False | |
| def get_install_path( self, line ): | |
| if "__HPCC_construct_install_path" in line: | |
| m = re.search("(?<=\{)(.*?)(?=\})", line) | |
| if not m: m = re.search("(?<=\])(.*?)(?=])", line) | |
| if not m: return False | |
| for item in m.group(0).split(): | |
| Controller.FLAVORS.append(item.replace("\"", "")) | |
| Controller.INSTALL_PATH = True | |
| return True | |
| elif "set install_path" in line: | |
| line = line.replace("set install_path","").strip() | |
| Controller.INSTALL_PATH = line | |
| return True | |
| return False | |
| #def get_variables(self, line): | |
| # if "set" in line and "setenv" not in line: | |
| # line = line.split() | |
| # Controller.VARS[line[1]] = line[2].replace("\"", "") | |
| def get_standard_paths(self, line): | |
| if "__HPCC_configure_standard_paths" in line: | |
| m = re.search("(?<=\{)(.*?)(?=\})", line) | |
| if not m: return False | |
| for item in m.group(0).split(): | |
| Controller.STD_PATHS.append(item) | |
| Controller.STD_PATH = True | |
| return True | |
| return False | |
| def get_setenv(self, line): | |
| VAR = "" | |
| VAL = "" | |
| if "setenv" in line: | |
| line = line.replace("setenv", "") | |
| VAR = line.split()[0].replace("\"", "") | |
| VAL = line.split()[1].replace("\"", "")\ | |
| .replace("$install_path", "\"..install_path..\"") | |
| Controller.SETENV.append("setenv(\"{0}\", \"{1}\")\n".format(VAR, VAL)) | |
| return True | |
| return False | |
| def get_prepend_path(self, line): | |
| VAR = "" | |
| DELIM = "" | |
| VAL = "" | |
| out = "" | |
| if "prepend-path" in line: | |
| if "-d" in line: | |
| DELIM = re.search("(?<=-d\s\")(.*?)(?=\")", line).group(0) | |
| line = line.replace("-d","") | |
| line = line.replace("\""+DELIM+"\"", "") | |
| line = line.replace("prepend-path", "") | |
| VAR = line.split()[0].replace("\"","") | |
| VAL = line.split()[1].replace("\"","")\ | |
| .replace("$install_path", "\"..install_path..\"") | |
| if DELIM: | |
| out = "prepend_path(\"{0}\", \"{1}\", \"{2}\")\n"\ | |
| .format(VAR, VAL, DELIM) | |
| else: | |
| out = "prepend_path(\"{0}\", \"{1}\")\n"\ | |
| .format(VAR, VAL) | |
| if out: | |
| Controller.PREPEND_PATH.append(out) | |
| return True | |
| else: | |
| return False | |
| return False | |
| def get_whatis( self, line ): | |
| if "__HPCC_generate_whatis" in line: | |
| Controller.WHATIS = True | |
| return True | |
| return False | |
| def get_metadata(self, line): | |
| if "__HPCC_display_metadata" in line: | |
| Controller.METADATA = True | |
| return True | |
| return False | |
| def get_family( self, line ): | |
| m = None | |
| if "family" == line.split()[0]: | |
| m = re.search("(?<=\")(.*?)(?=\")", line) | |
| if m: | |
| Controller.FAMILY = m.group(0) | |
| return True | |
| return False | |
| def get_module_load(self, line): | |
| if "module load" in line: | |
| Controller.LOAD.append(line.split()[2]) | |
| return True | |
| return False | |
| def run(self): | |
| store = "" | |
| for line in file(self.FILE): | |
| flag = False | |
| if store: | |
| line = store+line | |
| store = "" | |
| if "\\" in line: | |
| store = line.replace("\\", "") | |
| continue | |
| if not line or not line.strip() or line[0] == "#": flag = True | |
| if not flag: flag = self.get_exit_conditions(line) | |
| if not flag: flag = self.get_pkg_info(line) | |
| if not flag: flag = self.get_install_path(line) | |
| if not flag: flag = self.get_setenv(line) | |
| if not flag: flag = self.get_standard_paths(line) | |
| if not flag: flag = self.get_prepend_path(line) | |
| if not flag: flag = self.get_family(line) | |
| if not flag: flag = self.get_module_load(line) | |
| if not flag: flag = self.get_whatis(line) | |
| if not flag: flag = self.get_metadata(line) | |
| if not flag: print line | |
| class Writer(Controller): | |
| FILE = None | |
| def __init__(self, in_file): | |
| self.FILE = in_file | |
| def write_lib_link(self): | |
| out = "-- "+os.path.basename(os.getcwd())+"\n\n" | |
| out += "package.path = pathJoin(os.getenv(\"HPCC_MODULES_PATH\")," | |
| out += "\"hpcc_library.lua\")\n" | |
| out += "require \"hpcc_library\"\n\n" | |
| self.FILE.write(out) | |
| def write_exit_conditions(self): | |
| for line in Controller.EXIT_CONDS: self.FILE.write(line+"\n") | |
| def write_setenv(self): | |
| for item in Controller.SETENV: | |
| self.FILE.write(item) | |
| self.FILE.write("\n") | |
| def write_pkg_info(self): | |
| self.FILE.write("local pkg_info = {}\n\n") | |
| for key in Controller.PKG_INFO_KEYS: | |
| out = "pkg_info[\"{0}\"] = ".format(key) | |
| if len(Controller.PKG_INFO[key]) > 1: | |
| out += "{\n" | |
| for item in Controller.PKG_INFO[key]: | |
| out += "\t\"{0}\"".format(item) | |
| if not item == Controller.PKG_INFO[key]\ | |
| [len(Controller.PKG_INFO[key])-1]: | |
| out += ",\n" | |
| out += "\n}\n" | |
| else: | |
| out += "\"{0}\"\n".format(Controller.PKG_INFO[key][0]) | |
| self.FILE.write(out) | |
| self.FILE.write("\n") | |
| def write_install_path(self): | |
| if type(Controller.INSTALL_PATH) == type(True) and Controller.INSTALL_PATH != False: | |
| out = "local install_path = __HPCC_construct_install_path(pkg_info" | |
| out += ", {" | |
| for item in Controller.FLAVORS: | |
| out += "\"{0}\"".format(item) | |
| if item != Controller.FLAVORS[len(Controller.FLAVORS) -1]: | |
| out += ", " | |
| out += "}" | |
| out += ")\n" | |
| else: | |
| out = "local install_path = "+Controller.INSTALL_PATH+"\n" | |
| self.FILE.write(out) | |
| def write_standard_paths(self): | |
| stp = "{" | |
| for item in Controller.STD_PATHS: | |
| stp += item | |
| if item != Controller.STD_PATHS[len(Controller.STD_PATHS) -1]: | |
| stp += ", " | |
| stp += "}" | |
| out = "__HPCC_configure_standard_paths(install_path, {0})\n\n".format(stp) | |
| self.FILE.write(out) | |
| def write_prepend_path(self): | |
| for line in Controller.PREPEND_PATH: | |
| self.FILE.write(line) | |
| self.FILE.write("\n") | |
| def write_family(self): | |
| out = "family(\"{0}\")\n".format(Controller.FAMILY) | |
| self.FILE.write(out) | |
| def write_module_load(self): | |
| modules = "" | |
| for item in Controller.LOAD: | |
| modules += "\"{0}\"".format(item) | |
| if item != Controller.LOAD[len(Controller.LOAD)-1]: | |
| modules += ", " | |
| out = "load({0})\n\n".format(modules) | |
| self.FILE.write(out) | |
| def write_whatis(self): | |
| self.FILE.write("__HPCC_generate_whatis(pkg_info)\n") | |
| def write_metadata(self): | |
| self.FILE.write("__HPCC_display_metadata(pkg_info)\n") | |
| def write_help(self): | |
| self.FILE.write("help([[]])\n") | |
| def write(self): | |
| self.FILE = open(self.FILE, "w") | |
| self.write_lib_link() | |
| if Controller.EXIT_CONDS: self.write_exit_conditions() | |
| if Controller.LOAD: self.write_module_load() | |
| if Controller.PKG_INFO: self.write_pkg_info() | |
| if Controller.INSTALL_PATH: self.write_install_path() | |
| if Controller.STD_PATH: self.write_standard_paths() | |
| if Controller.SETENV: self.write_setenv() | |
| if Controller.PREPEND_PATH: self.write_prepend_path() | |
| if Controller.FAMILY: self.write_family() | |
| if Controller.WHATIS: self.write_whatis() | |
| if Controller.METADATA: self.write_metadata() | |
| self.write_help() | |
| self.FILE.close() | |
| def main(): | |
| if len(sys.argv) <= 1: | |
| print "Usage: port <tcl_file> [<tcl_file>.lua]" | |
| in_file = sys.argv[1] | |
| if len(sys.argv) == 2: | |
| out_file = in_file+".lua" | |
| else: | |
| out_file = sys.argv[2] | |
| C = Controller( in_file, out_file ) | |
| C.parser.run() | |
| C.writer.write() | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment