Skip to content

Commit b2bc466

Browse files
committed
Add python server
1 parent c60541b commit b2bc466

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

Dockerfile

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
# Start from an Ubuntu base
22
FROM ubuntu:22.04
33

4+
# --- SRCML SETUP ---
5+
46
# Set non-interactive mode for apt
57
ENV DEBIAN_FRONTEND=noninteractive
68

7-
# Install required packages
9+
# Install required packages (incl. Python for server)
810
RUN apt-get update && \
911
apt-get install -y \
1012
curl zip g++ make ninja-build antlr libantlr-dev \
1113
libxml2-dev libxml2-utils libxslt1-dev \
1214
libarchive-dev libssl-dev libcurl4-openssl-dev \
1315
cpio man file dpkg-dev \
14-
cmake && \
16+
cmake python3 python3-pip && \
1517
rm -rf /var/lib/apt/lists/*
1618

1719
# Add srcML include files
1820
RUN curl -L http://www.sdml.cs.kent.edu/build/srcML-1.0.0-Boost.tar.gz | \
1921
tar xz -C /usr/local/include
2022

21-
# Create build directory
2223
RUN mkdir -p /srcml/build
2324

24-
# Copy your source code into the container (update `.` as needed)
2525
COPY ./srcml /srcml
2626

27-
# Set working directory
2827
WORKDIR /srcml/build
2928

30-
# Configure and build using CMake and Ninja
3129
RUN cmake .. -G Ninja && \
3230
cmake --build . --config Release --target install
3331

34-
# Set ldconfig so installed libs are available
32+
# Set ldconfig so installed libs are linked and available
3533
RUN ldconfig
3634

37-
# Validate srcML works (can be used in entrypoint or test phase)
38-
RUN srcml --version && \
39-
srcml --text="int a;" -l C++
35+
# --- SERVER SETUP ---
36+
37+
# Install Python packages for server
38+
RUN pip3 install fastapi uvicorn python-multipart
39+
40+
# Add FastAPI server app
41+
COPY ./server/server.py /app/server.py
42+
WORKDIR /app
43+
44+
# Expose port
45+
EXPOSE 8000
4046

41-
# Default command (can be overridden)
42-
ENTRYPOINT [ "srcml" ]
47+
# Start FastAPI server
48+
CMD ["uvicorn", "server:app", "--port", "8000"]
4349

server/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fastapi
2+
uvicorn
3+
python-multipart

server/server.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from fastapi import FastAPI, HTTPException
2+
from pydantic import BaseModel
3+
from fastapi.responses import PlainTextResponse
4+
import subprocess
5+
import tempfile
6+
import os
7+
8+
app = FastAPI()
9+
10+
class CodeInput(BaseModel):
11+
code: str
12+
language: str = "C++"
13+
14+
@app.post("/parse")
15+
async def convert_to_srcml(input_data: CodeInput):
16+
# Create a temporary file to write the source code into
17+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp:
18+
tmp.write(input_data.code.encode("utf-8"))
19+
tmp_path = tmp.name
20+
21+
try:
22+
# Run srcml with the temporary file
23+
result = subprocess.run(
24+
["srcml", tmp_path, "--language", input_data.language],
25+
capture_output=True,
26+
text=True,
27+
check=True
28+
)
29+
return PlainTextResponse(result.stdout, media_type="application/xml")
30+
31+
except subprocess.CalledProcessError as e:
32+
raise HTTPException(status_code=500, detail=f"srcml error: {e.stderr.strip()}")
33+
34+
finally:
35+
os.remove(tmp_path)

0 commit comments

Comments
 (0)