def test_command_reset(self): self.parser.parse(["reset"]) db = Database() types = db.get_types() assert types == []
def test_command_config_with_route(self): self.parser.parse(["route", "/usr/include"]) self.parser.parse(["config"]) db = Database() types = db.get_types() assert "FILE" in types
def test_command_flush_types(self): try: self.parser.parse(["flush", "types"]) db = Database() types = db.get_types() assert "t_stream" not in types finally: db.close_connection()
def test_command_embed(self): try: self.parser.parse(["embed", "t_prueba", "prueba.h"]) db = Database() types = db.get_types() assert "t_prueba" in types finally: db.close_connection()
def test_command_flush_route(self): try: self.parser.parse(["route", "/usr/include"]) self.parser.parse(["flush", "routes"]) db = Database() routes = db.get_routes() assert routes == [] finally: db.close_connection()
def setUp(self): db = Database() self.configurer = Configure() self.configurer.initialize_directories() self.configurer.create_db() self.configurer.create_headers() self.configurer.gather_types()
def _get_source(self, tipo): """ Gets the source file of the type received :param tipo: str type to find source :return: None """ if self._match_array(tipo, self.c_array_types): tipo = tipo.strip()[:-4] db = Database() query = "SELECT type_source FROM types WHERE type_name = '" + tipo + "' ORDER BY type_id" self.source_file = list(db.execute_query(query)) if self.source_file: # Validacion por si la query no encontro valores self.source_file = self.source_file[0][0] db.close_connection()
def do_execute(self, parser, *args): self.filer = Filer() self.db = Database() if len(args[0]) == 0: msg = "Faltan parametros\n" msg += print_helpers(parser, "create") raise CreateError(msg) else: parameters = list(args)[0] tipo = parameters.pop(0) try: if tipo.lower() == "model": self._create_model(parameters) elif tipo.lower() == "socket": self._create_socket(parameters) else: msg = "Opcion invalida para el comando create" raise CreateError(msg) except FileError as exc: raise CreateError(exc)
class CreateCommand(Command): def __init__(self): self.filer = None self.db = None def do_execute(self, parser, *args): self.filer = Filer() self.db = Database() if len(args[0]) == 0: msg = "Faltan parametros\n" msg += print_helpers(parser, "create") raise CreateError(msg) else: parameters = list(args)[0] tipo = parameters.pop(0) try: if tipo.lower() == "model": self._create_model(parameters) elif tipo.lower() == "socket": self._create_socket(parameters) else: msg = "Opcion invalida para el comando create" raise CreateError(msg) except FileError as exc: raise CreateError(exc) def _create_model(self, *args): print("Escribiendo estructuras y funciones asociadas al modelo") model = self.filer.write_model(*args) print("Insertando modelo en base de datos") self.db.insert_type(model, "modelos.h") def _create_socket(self, *args): self.filer.copy_templates() def __str__(self): msg = "El comando create permite tanto inicializar la estructura de directorios necesario para el " msg += "uso de socketpy, así como crear modelos de estructuras utilizadas para el envío de datos por sockets\n" return msg
def _get_types(self): """ Gets all types from the database and adds their arrays types for regex :return: None """ db = Database() self.c_built_ins = list(map(lambda tup: tup[0], db.select_built_types())) self.c_built_in_array_types = r'^(' + '|'.join(self.escaped(self.c_built_ins)) + ')\[[0-9]*\]' self.c_types = list(map(lambda tup: tup[0], db.select_types())) self.c_array_types = r'^(' + '|'.join(self.escaped(self.c_types)) + ')\[[0-9]*\]' db.close_connection()
def do_execute(self, parser, *args): if len(args[0]) == 0: msg = "Faltan parametros\n" msg += "Se necesita tener un parámetro que especifique el tipo de dato y " \ "el archivo source al que pertenece" raise EmbedError(msg) else: parameters = list(args)[0] print(parameters) tipo = parameters.pop(0) source = parameters.pop(0) try: db = Database() db.insert_type(tipo, source) db.close_connection() except Exception as exc: raise EmbedError(exc)
class Route: def __init__(self): self.working_directory = os.getcwd() self.database = Database() # Public Interface # def load_route(self, directorios): """ Formats the route and inserts it in the database :param directorios: str with the route to add :return: None """ route = os.sep.join(directorios) print("Ruta a insertar: ", route) self._insert_route(route) def flush_routes(self): """ Deletes the routes in the database :return: None """ self.database.flush_routes() def close_connection(self): """ Closes the connection to the database :return: None """ self.database.close_connection() def _insert_route(self, route): """ Inserts the route in the database :param route: str with the route :return: None """ self.database.insert_route(route)
def do_execute(self, parser, *args): if len(args[0]) == 0: msg = "Faltan parametros\n" msg += "Las opciones para el comando flush son: -" for opcion in parser.helpers["flush"]: msg += opcion + " " raise FlushError(msg) else: parameters = list(args)[0] tipo = parameters.pop(0) try: if tipo.lower() == "types": db = Database() db.flush_types() db.close_connection() elif tipo.lower() == "routes": route = Route() route.flush_routes() route.close_connection() else: msg = "Opcion invalida para el comando create" raise FlushError(msg) except FileError as exc: raise FlushError(exc)
def __init__(self): self.working_directory = os.getcwd() self.headers = os.path.join(os.path.dirname(os.path.abspath(__file__)), "headers") self.database = Database() self.analyzed_files = []
def do_execute(self, parser, *args): db = Database() db.destroy_tables() db.create_tables() db.close_connection()
def do_execute(self, parser, *args): db = Database() print("Eliminando configuracion") db.destroy_database()
def __init__(self): self.working_directory = os.getcwd() self.database = Database()
class Configure: def __init__(self): self.working_directory = os.getcwd() self.headers = os.path.join(os.path.dirname(os.path.abspath(__file__)), "headers") self.database = Database() self.analyzed_files = [] # Public Interface # def initialize_directories(self): """ Creates base directories for socketpy :return: None """ self._create_directory("database") self._create_directory("headers") print("\tGenerados directorios database y headers") def create_db(self): """ Creates the database, its tables and loads basic types :return: None """ print("\tInicializando db") self.database.create_tables() self._load_basic_types() self._load_basic_routes() def close_connection(self): """ Closes the connection to the database :return: None """ self.database.close_connection() def create_headers(self): """ Creates the base templates to be copied by socketpy :return: None """ self._create_file("modelos.h", MODEL) self._create_file("paquetes.h", PACKH) self._create_file("paquetes.c", PACKC) self._create_file("socket.h", SOCKH) self._create_file("socket.c", SOCKC) print("\tGenerados templates de sources a utilizar") def gather_types(self): """ Walks through the working directory and gets the types from '.c' or '.h' files :return: None """ directories = list(os.walk(self.working_directory)) for direc in directories: root, subdirs, files = direc for fd in files: if fd.endswith(".c") or fd.endswith(".h"): self._analyze_file(root, fd) print("\tNo hay más tipos de datos en el archivo\n") # Private Methods # def _analyze_file(self, root, source): """ Makes the abs path with root and source, opens the file and explores its lines looking for types :param root: str root directory :param source: str file :return: None """ file = os.path.join(root, source) print("Procesando archivo: ", source) fd = FileLineWrapper(open(file)) self._explore_lines(fd, source) # Lines processing # def _explore_lines(self, fd, source): """ Explores the lines of a file and gathers possible types :param fd: FileDescriptor :param source: str file name :return: None """ struct_body = 0 for line in fd.f: line = line.lstrip() if line.startswith( "#include") and source in SOCKET_FILES: # Linea #include self._inspect_include(line) elif line.startswith("typedef") and line.endswith( ";\n"): # Linea typedef simple self._get_type_from_typedef_sentence(line, source) elif line.startswith("typedef") and ( line.endswith("{") or line.endswith("\n")): # Linea typedef compuesta struct_body += 1 elif line.endswith("{\n"): struct_body += 1 elif line.startswith("}"): # Fin typedef compuesta struct_body -= 1 if struct_body == 0: self._get_type_from_typedef_end_sentence(line, source) # Line Analyzing # def _get_type_from_typedef_end_sentence(self, line, source): """ Gets the type from the end of a typedef block :param line: str of the line analyzed :param source: str of the file name :return: None """ tipo = re.sub('[;}\ \n]', '', line) if tipo.startswith("__"): tipo = tipo.split("))")[1] if tipo != "": self.database.insert_type(tipo, source) def _get_type_from_typedef_sentence(self, line, source): """ Gets the type from a typedef expression without block :param line: str of the line analyzed :param source: str of the file name :return: None """ analyzer = Analyzer() # TODO: Separar en diferentes analizadores de linea (punteros a funciones, structs, etc) print("Linea typedef simple: ", line) # typedef struct ptw32_cleanup_t ptw32_cleanup_t; if re.match(r'(typedef) ' + analyzer.all() + ' (\()', line): print("Es un puntero") self._get_function_ptr(line, source) elif re.match('typedef struct', line): print("Es un struct") self._get_struct(line, source) elif re.match('typedef', line): print("Es un tipo basico") self._get_basic_type(line, source) def _get_struct(self, line, source): """ Gets type from a struct in the typedef expression :param line: str of the line analyzed :param source: str of the file name :return: None """ linea = line.split(" ") tipo = linea[-1] tipo = re.sub('[\(\n;]', '', tipo) self.database.insert_type(tipo, source) def _get_function_ptr(self, line, source): """ Gets type from a function pointer :param line: str of the line analyzed :param source: str of the file name :return: """ linea = line.split(" ") tipo = linea[2] tipo = tipo.split(")")[0] print("Tipo: ", tipo) tipo = re.sub('[\(\*\n;]', '', tipo) self.database.insert_type(tipo, source) def _get_basic_type(self, line, source): """ Gets type from a basic typedef expression :param line: str of the line analyzed :param source: str of the file name :return: None """ linea = line.replace("\t", " ") linea = linea.split(" ") tipo = linea[-1] tipo = re.sub('[\(\n;]', '', tipo) print("Tipo: ", tipo) self.database.insert_type(tipo, source) def _inspect_include(self, line): """ Inspects an include and tries to find it in the routes of the database to keep looking for types :param line: str of the line analyzed :return: None """ print("Explorando include ", line) if "<" in line: file = line.split("<")[-1] file = re.sub('[>\n]', '', file) if "/" in file: file = file.split("/")[-1] print("Archivo {}".format(file)) if "\"" in line: file = line.split("\"")[-2] for root in self.database.get_routes(): for dir, subdirs, files in os.walk(root): if file in files and file not in self.analyzed_files: self._analyze_file(dir, file) print("\tNo hay más tipos de dato en el archivo") self.analyzed_files.append(file) break elif file in self.analyzed_files: print("El archivo {} ya fue analizado".format(file)) break else: print( "\tEl archivo incluido no se encuentra en las rutas definidas\n", end='') # Db initialization # def _load_basic_types(self): """ Inserts basic types into the database :return: None """ types = [ ("int", "builtin"), ("float", "builtin"), ("double", "builtin"), ("void", "builtin"), ("char", "builtin"), ("int*", "builtin"), ("float*", "builtin"), ("double*", "builtin"), ("void*", "builtin"), ("char*", "builtin"), ] self.database.insert_types(types) def _load_basic_routes(self): """ Inserts basic routes into the database for the search of types :return: None """ # TODO: Add more routes route = "/usr/include/commons/" self.database.insert_route(route) # Directories initialization # @staticmethod def _create_directory(directory): """ Creates a directory if it doesn't exist :param directory: str of the directory to create :return: None """ if not os.path.exists( os.path.join(os.path.dirname(os.path.abspath(__file__)), directory)): os.makedirs( os.path.join(os.path.dirname(os.path.abspath(__file__)), directory)) # Templates Creation # def _create_file(self, file, lines): """ Creates a file and writes the lines received :param file: str file to create :param lines: str lines to write :return: None """ fd = FileLineWrapper(open(os.path.join(self.headers, file), "w+")) fd.f.writelines(lines) fd.close()
def test_command_route(self): self.parser.parse(["route", "/usr/include"]) db = Database() routes = db.get_routes() assert "/usr/include" in routes