diff --git a/reprozip-core/reprozip_core/common.py b/reprozip-core/reprozip_core/common.py index 426172d68..c5cc95931 100644 --- a/reprozip-core/reprozip_core/common.py +++ b/reprozip-core/reprozip_core/common.py @@ -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. """ diff --git a/reprozip/reprozip/pack.py b/reprozip/reprozip/pack.py index 75f36b2f5..e32eeb9cf 100644 --- a/reprozip/reprozip/pack.py +++ b/reprozip/reprozip/pack.py @@ -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') diff --git a/reprozip/reprozip/traceutils.py b/reprozip/reprozip/traceutils.py index 63cdad9fb..7c8972822 100644 --- a/reprozip/reprozip/traceutils.py +++ b/reprozip/reprozip/traceutils.py @@ -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. """ @@ -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( diff --git a/tests/common.py b/tests/common.py index 2564b5f9d..216a5b05f 100644 --- a/tests/common.py +++ b/tests/common.py @@ -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): @@ -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): diff --git a/tests/test_reprozip.py b/tests/test_reprozip.py index 724428428..6a3dfc60c 100644 --- a/tests/test_reprozip.py +++ b/tests/test_reprozip.py @@ -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 @@ -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;')