From 90d44101f409c88d3a979e09bd51593b114a20cf Mon Sep 17 00:00:00 2001 From: Kedar Navare Date: Mon, 16 Sep 2024 13:37:06 +0530 Subject: [PATCH] lobster-python now uses counter logic. The identifier now uses counter if there exists multiple functions with same name. Issue #58 --- CHANGELOG.md | 2 +- lobster/tools/python/python.py | 31 ++++++++- test-system/lobster-python/hello.output | 87 +++++++++++++++++++++++++ test-system/lobster-python/hello.py | 18 +++++ 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 test-system/lobster-python/hello.output create mode 100644 test-system/lobster-python/hello.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4254c93a..b9c11fc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ ### 0.9.17 -* The `lobster-python` tool now adds the line number to the function +* The `lobster-python` tool now adds the counter logic to the function identifier. This supports situations where different functions have the same name diff --git a/lobster/tools/python/python.py b/lobster/tools/python/python.py index 1e545f4c..4fd9e5d5 100755 --- a/lobster/tools/python/python.py +++ b/lobster/tools/python/python.py @@ -22,6 +22,7 @@ import os.path import multiprocessing import functools +import re from libcst.metadata import PositionProvider import libcst as cst @@ -32,6 +33,26 @@ LOBSTER_TRACE_PREFIX = "# lobster-trace: " LOBSTER_JUST_PREFIX = "# lobster-exclude: " +func_name = [] + + +def count_occurrence_of_last_function_from_function_name_list(function_names): + """ + Function returns a function name(last function from the list of function names) + and its occurrence from the given list of function names + Example: function_names = ['hello.add:2', 'hello.sub:5', 'hello.add:8'] + returns : hello.add-2 + """ + function_and_file_name = re.split(r"[.:]", function_names[-1]) + filename = function_and_file_name[0] + last_function = function_and_file_name[1] + count = 0 + for element in range(0, len(function_names)-1): + if re.split(r"[.:]", function_names[element])[1] == last_function: + count += 1 + function_name = filename + "." + last_function + ("-" + str(count) if count > 0 else '') + + return function_name def parse_value(val): @@ -197,13 +218,19 @@ def to_lobster(self, schema, items): assert schema is Implementation or schema is Activity assert isinstance(items, list) + func_name.append(self.fqn()) + tagname = count_occurrence_of_last_function_from_function_name_list(func_name) + pattern = r"[-]" + val = re.split(pattern, tagname) + name_value = val[0] + if schema is Implementation: l_item = Implementation(tag = Tracing_Tag("python", - self.fqn()), + tagname), location = self.location, language = "Python", kind = self.kind, - name = self.fqn()) + name = name_value) else: if not self.name.startswith("test"): return diff --git a/test-system/lobster-python/hello.output b/test-system/lobster-python/hello.output new file mode 100644 index 00000000..eb6f3e74 --- /dev/null +++ b/test-system/lobster-python/hello.output @@ -0,0 +1,87 @@ +{ + "data": [ + { + "tag": "python hello.hello_world", + "location": { + "kind": "file", + "file": "./demoPython/hello.py", + "line": 2, + "column": null + }, + "name": "hello.hello_world", + "messages": [], + "just_up": [], + "just_down": [], + "just_global": [], + "language": "Python", + "kind": "Function" + }, + { + "tag": "python hello.hello_world-2", + "location": { + "kind": "file", + "file": "./demoPython/hello.py", + "line": 5, + "column": null + }, + "name": "hello.hello_world", + "messages": [], + "just_up": [], + "just_down": [], + "just_global": [], + "language": "Python", + "kind": "Function" + }, + { + "tag": "python hello.hello_universe", + "location": { + "kind": "file", + "file": "./demoPython/hello.py", + "line": 8, + "column": null + }, + "name": "hello.hello_universe", + "messages": [], + "just_up": [], + "just_down": [], + "just_global": [], + "language": "Python", + "kind": "Function" + }, + { + "tag": "python hello.hello_world-3", + "location": { + "kind": "file", + "file": "./demoPython/hello.py", + "line": 11, + "column": null + }, + "name": "hello.hello_world", + "messages": [], + "just_up": [], + "just_down": [], + "just_global": [], + "language": "Python", + "kind": "Function" + }, + { + "tag": "python hello.hello_universe-2", + "location": { + "kind": "file", + "file": "./demoPython/hello.py", + "line": 14, + "column": null + }, + "name": "hello.hello_universe", + "messages": [], + "just_up": [], + "just_down": [], + "just_global": [], + "language": "Python", + "kind": "Function" + } + ], + "generator": "lobster_python", + "schema": "lobster-imp-trace", + "version": 3 +} \ No newline at end of file diff --git a/test-system/lobster-python/hello.py b/test-system/lobster-python/hello.py new file mode 100644 index 00000000..2c917f20 --- /dev/null +++ b/test-system/lobster-python/hello.py @@ -0,0 +1,18 @@ +if True: + def hello_world(): + return 1 +else: + def hello_world(): + return 2 + +def hello_universe(): + return 3 + +def hello_world(): + return 10 + +def hello_universe(): + return 12 + + +print(hello_world()) \ No newline at end of file