mirror of
https://github.com/github/codeql.git
synced 2026-05-02 04:05:14 +02:00
Python: Copy Python extractor to codeql repo
This commit is contained in:
34
python/extractor/semmle/thrift/__init__.py
Normal file
34
python/extractor/semmle/thrift/__init__.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import os.path
|
||||
|
||||
from .parse import Parser
|
||||
from .emit import Emitter
|
||||
|
||||
class Extractor(object):
|
||||
|
||||
def __init__(self, trap_folder, src_archive=None):
|
||||
self.parser = Parser()
|
||||
self.emitter = Emitter(trap_folder)
|
||||
self.src_archive = src_archive
|
||||
|
||||
def _walk(self, path):
|
||||
for dirpath, _, filenames in os.walk(path):
|
||||
for filename in filenames:
|
||||
if filename.endswith(".thrift"):
|
||||
yield os.path.join(dirpath, filename)
|
||||
|
||||
def extract_files(self, files):
|
||||
for file in files:
|
||||
self.extract_file(file)
|
||||
|
||||
def extract_folder(self, path):
|
||||
for file in self._walk(path):
|
||||
self.extract_file(file)
|
||||
|
||||
def extract_file(self, file):
|
||||
with open(file, "rb") as fd:
|
||||
bytes_source = fd.read()
|
||||
src = bytes_source.decode('utf-8')
|
||||
tree = self.parser.parse(src)
|
||||
self.emitter.emit(file, tree)
|
||||
if self.src_archive:
|
||||
self.src_archive.write(file, bytes_source)
|
||||
Reference in New Issue
Block a user