File: generate_bindings.py

package info (click to toggle)
pytsk 20200117-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,188 kB
  • sloc: ansic: 3,090; python: 3,003; sh: 115; makefile: 68
file content (69 lines) | stat: -rwxr-xr-x 2,312 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
#
# Script to generate the Python bindings.
#
# Copyright 2012, Joachim Metz <joachim.metz@gmail.com>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys

import class_parser


def generate_bindings(target, source_files, env=None, initialization="",
                      free="talloc_free"):
    """ Generated the Python bindings """
    module_name = os.path.splitext(os.path.basename(target))[0]
    print("Generating Python bindings for module %s from %s" % (
        module_name, source_files))

    env = env or dict(V=0)

    # Sets the free function
    class_parser.FREE = free
    p = class_parser.HeaderParser(module_name, verbose=env["V"])
    p.module.init_string = initialization
    p.parse_filenames(source_files)

    fd = open(target, "w")
    p.write(fd)
    fd.close()

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: ./generate_bindings.py path_to_source")
        sys.exit(1)

    tsk_source_path = sys.argv[1]
    include_base = "tsk3"

    if not os.path.exists(os.path.join(tsk_source_path, include_base)):
        # sleuthkit 4.1 changed the names of the include headers.
        include_base = "tsk"

    if not os.path.exists(os.path.join(tsk_source_path, include_base)):
        print("Unable to find sleuthkit include headers.")
        sys.exit(1)

    sources = [
        os.path.join(tsk_source_path, include_base, "libtsk.h"),
        os.path.join(tsk_source_path, include_base, "base", "tsk_base.h"),
        os.path.join(tsk_source_path, include_base, "fs", "tsk_fs.h"),
        os.path.join(tsk_source_path, include_base, "img", "tsk_img.h"),
        os.path.join(tsk_source_path, include_base, "vs", "tsk_vs.h"),
        "tsk3.h",
    ]

    generate_bindings("pytsk3.c", sources, initialization="tsk_init();")