Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e89388

Browse files
committed
Python: CG trace: Don't abuse example dir
1 parent ad2e336 commit 1e89388

16 files changed

Lines changed: 35 additions & 25 deletions

File tree

python/tools/recorded-call-graph-metrics/.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Example DB
22
cg-trace-example-db/
33

4-
# Examples traces should be ignored in general
5-
example-traces/
4+
# Tests artifacts
5+
tests/python-traces/
6+
tests/cg-trace-test-db
67

78
# Artifact from building `pip install -e .`
89
src/cg_trace.egg-info/

python/tools/recorded-call-graph-metrics/README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ also known as _call graph tracing_.
44

55
Execute a python program and for each call being made, record the call and callee. This allows us to compare call graph resolution from static analysis with actual data -- that is, can we statically determine the target of each actual call correctly.
66

7-
This is still in the early stages, and currently only supports a very minimal working example (to show that this approach might work).
8-
9-
The next hurdle is being able to handle multiple calls on the same line, such as
7+
## How do I give it a spin?
108

11-
- `foo(); bar()`
12-
- `foo(bar())`
13-
- `foo().bar()`
9+
After following setup instructions below, you should be able to reproduce the example trace by running
1410

15-
## How do I give it a spin?
11+
```
12+
cg-trace --xml example/simple.xml example/simple.py
13+
```
1614

17-
After following setup instructions below, run the `recreate-db.sh` script to create the database `cg-trace-example-db`. Then run the queries inside the `ql/` directory.
15+
You can also run traces for all tests and build a database by running `tests/create-test-db.sh`. Then run the queries inside the `ql/` directory.
1816

1917

2018
## Setup

python/tools/recorded-call-graph-metrics/example-traces/simple.xml renamed to python/tools/recorded-call-graph-metrics/example/simple.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<root>
22
<info>
33
<cg_trace_version>0.0.2</cg_trace_version>
4-
<args>--xml example-traces/simple.xml example/simple.py</args>
4+
<args>--xml example/simple.xml example/simple.py</args>
55
<exit_status>completed</exit_status>
66
<elapsed>0.00 seconds</elapsed>
7-
<utctimestamp>2020-07-20T12:02:56</utctimestamp>
7+
<utctimestamp>2020-07-22T12:14:02</utctimestamp>
88
</info>
99
<recorded_calls>
1010
<recorded_call>

python/tools/recorded-call-graph-metrics/recreate-db.sh renamed to python/tools/recorded-call-graph-metrics/tests/create-test-db.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
#!/bin/bash
22

3-
set -e
4-
set -x
3+
set -Eeuo pipefail # see https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
54

6-
if ! pip show cg_trace; then
5+
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
7+
if ! pip show cg_trace &>/dev/null; then
78
echo "You need to follow setup instructions in README"
89
exit 1
910
fi
1011

11-
DB="cg-trace-example-db"
12-
SRC="example/"
13-
XMLDIR="example-traces"
12+
DB="$SCRIPTDIR/cg-trace-test-db"
13+
SRC="$SCRIPTDIR/python-src/"
14+
XMLDIR="$SCRIPTDIR/python-traces/"
1415
PYTHON_EXTRACTOR=$(codeql resolve extractor --language=python)
1516

17+
rm -rf "$DB"
18+
rm -rf "$XMLDIR"
1619

17-
cg-trace --xml "$XMLDIR"/simple.xml example/simple.py
18-
cg-trace --xml "$XMLDIR"/builtins.xml example/builtins.py
19-
cg-trace --xml "$XMLDIR"/multiple-on-one-line.xml example/multiple-on-one-line.py
20-
cg-trace --xml "$XMLDIR"/class-simple.xml example/class-simple.py
21-
20+
mkdir -p "$XMLDIR"
2221

23-
rm -rf "$DB"
22+
for f in $(ls $SRC); do
23+
echo "Tracing $f"
24+
cg-trace --xml "$XMLDIR/${f%.py}.xml" "$SRC/$f"
25+
done
2426

2527
codeql database init --source-root="$SRC" --language=python "$DB"
2628
codeql database trace-command --working-dir="$SRC" "$DB" "$PYTHON_EXTRACTOR/tools/autobuild.sh"
2729
codeql database index-files --language xml --include-extension .xml --working-dir="$XMLDIR" "$DB"
2830
codeql database finalize "$DB"
2931

30-
set +x
3132
echo "Created database '$DB'"

python/tools/recorded-call-graph-metrics/example/BUILD_LIST.py renamed to python/tools/recorded-call-graph-metrics/tests/python-src/BUILD_LIST.py

File renamed without changes.

python/tools/recorded-call-graph-metrics/example/BUILD_TUPLE.py renamed to python/tools/recorded-call-graph-metrics/tests/python-src/BUILD_TUPLE.py

File renamed without changes.

python/tools/recorded-call-graph-metrics/example/CALL_FUNCTION_EX.py renamed to python/tools/recorded-call-graph-metrics/tests/python-src/CALL_FUNCTION_EX.py

File renamed without changes.

python/tools/recorded-call-graph-metrics/example/__getitem__.py renamed to python/tools/recorded-call-graph-metrics/tests/python-src/__getitem__.py

File renamed without changes.

python/tools/recorded-call-graph-metrics/example/builtins.py renamed to python/tools/recorded-call-graph-metrics/tests/python-src/builtins.py

File renamed without changes.

python/tools/recorded-call-graph-metrics/example/class-simple.py renamed to python/tools/recorded-call-graph-metrics/tests/python-src/class-simple.py

File renamed without changes.

0 commit comments

Comments
 (0)