Skip to content

Commit

Permalink
Move create_trace_schema() to reprozip_core
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Jun 29, 2021
1 parent d27bd1e commit 57a5c16
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 74 deletions.
68 changes: 68 additions & 0 deletions reprozip-core/reprozip_core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,74 @@ def save_config(filename, runs, packages, other_files, reprozip_version,
""")


def create_trace_schema(conn):
"""Create the trace database schema on a given SQLite3 connection.
"""
sql = [
'''
CREATE TABLE processes(
id INTEGER NOT NULL PRIMARY KEY,
run_id INTEGER NOT NULL,
parent INTEGER,
timestamp INTEGER NOT NULL,
exit_timestamp INTEGER,
cpu_time INTEGER,
is_thread BOOLEAN NOT NULL,
exitcode INTEGER
);
''',
'''
CREATE INDEX proc_parent_idx ON processes(parent);
''',
'''
CREATE TABLE opened_files(
id INTEGER NOT NULL PRIMARY KEY,
run_id INTEGER NOT NULL,
name TEXT NOT NULL,
timestamp INTEGER NOT NULL,
mode INTEGER NOT NULL,
is_directory BOOLEAN NOT NULL,
process INTEGER NOT NULL
);
''',
'''
CREATE INDEX open_proc_idx ON opened_files(process);
''',
'''
CREATE TABLE executed_files(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
run_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
process INTEGER NOT NULL,
argv TEXT NOT NULL,
envp TEXT NOT NULL,
workingdir TEXT NOT NULL
);
''',
'''
CREATE INDEX exec_proc_idx ON executed_files(process);
''',
'''
CREATE TABLE connections(
id INTEGER NOT NULL PRIMARY KEY,
run_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
process INTEGER NOT NULL,
inbound INTEGER NOT NULL,
family TEXT NULL,
protocol TEXT NULL,
address TEXT NULL
);
''',
'''
CREATE INDEX connections_proc_idx ON connections(process);
''',
]
for stmt in sql:
conn.execute(stmt)


class LoggingDateFormatter(logging.Formatter):
"""Formatter that puts milliseconds in the timestamp.
"""
Expand Down
2 changes: 1 addition & 1 deletion reprozip/reprozip/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from reprozip_core.common import File, load_config, save_config, \
record_usage_package
from reprozip.tracer.linux_pkgs import identify_packages
from reprozip.traceutils import combine_files
from reprozip_core.traceutils import combine_files


logger = logging.getLogger('reprozip')
Expand Down
71 changes: 2 additions & 69 deletions reprozip/reprozip/traceutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,13 @@
from rpaths import Path
import sqlite3

from reprozip_core.common import create_trace_schema
from reprozip.tracer.trace import TracedFile


logger = logging.getLogger('reprozip')


def create_schema(conn):
"""Create the trace database schema on a given SQLite3 connection.
"""
sql = [
'''
CREATE TABLE processes(
id INTEGER NOT NULL PRIMARY KEY,
run_id INTEGER NOT NULL,
parent INTEGER,
timestamp INTEGER NOT NULL,
exit_timestamp INTEGER,
cpu_time INTEGER,
is_thread BOOLEAN NOT NULL,
exitcode INTEGER
);
''',
'''
CREATE INDEX proc_parent_idx ON processes(parent);
''',
'''
CREATE TABLE opened_files(
id INTEGER NOT NULL PRIMARY KEY,
run_id INTEGER NOT NULL,
name TEXT NOT NULL,
timestamp INTEGER NOT NULL,
mode INTEGER NOT NULL,
is_directory BOOLEAN NOT NULL,
process INTEGER NOT NULL
);
''',
'''
CREATE INDEX open_proc_idx ON opened_files(process);
''',
'''
CREATE TABLE executed_files(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
run_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
process INTEGER NOT NULL,
argv TEXT NOT NULL,
envp TEXT NOT NULL,
workingdir TEXT NOT NULL
);
''',
'''
CREATE INDEX exec_proc_idx ON executed_files(process);
''',
'''
CREATE TABLE connections(
id INTEGER NOT NULL PRIMARY KEY,
run_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
process INTEGER NOT NULL,
inbound INTEGER NOT NULL,
family TEXT NULL,
protocol TEXT NULL,
address TEXT NULL
);
''',
'''
CREATE INDEX connections_proc_idx ON connections(process);
''',
]
for stmt in sql:
conn.execute(stmt)


def combine_files(newfiles, newpackages, oldfiles, oldpackages):
"""Merges two sets of packages and files.
"""
Expand Down Expand Up @@ -131,7 +64,7 @@ def combine_traces(traces, target):
conn.row_factory = sqlite3.Row

# Create the schema
create_schema(conn)
create_trace_schema(conn)

# Temporary database with lookup tables
conn.execute(
Expand Down
4 changes: 2 additions & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rpaths import Path
import sqlite3

from reprozip.traceutils import create_schema
from reprozip_core.common import create_trace_schema


def make_database(insert, path=None):
Expand All @@ -16,7 +16,7 @@ def make_database(insert, path=None):
conn = sqlite3.connect('')
conn.row_factory = sqlite3.Row

create_schema(conn)
create_trace_schema(conn)

run = -1
for timestamp, l in enumerate(insert):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_reprozip.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import unittest

from reprozip_core.common import FILE_READ, FILE_WRITE, FILE_WDIR, \
InputOutputFile
InputOutputFile, create_trace_schema
from reprozip.tracer.trace import get_files, compile_inputs_outputs
from reprozip import traceutils
from reprozip_core.utils import UniqueNames, make_dir_writable
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_combine(self):
trace = self.tmpdir / ('trace%d.sqlite3' % i)
conn = sqlite3.connect(str(trace)) # connect() only accepts str
conn.row_factory = sqlite3.Row
traceutils.create_schema(conn)
create_trace_schema(conn)
conn.executescript('PRAGMA foreign_keys=OFF; BEGIN TRANSACTION;' +
dat +
'COMMIT;')
Expand Down

0 comments on commit 57a5c16

Please sign in to comment.