Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,18 +20,41 @@ async def execute_command(websocket, project_name, command):
|
|
| 20 |
base_path = os.path.join(os.getcwd(), 'projects', project_name)
|
| 21 |
|
| 22 |
try:
|
| 23 |
-
process =
|
| 24 |
command,
|
| 25 |
cwd=base_path,
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Send the command output to the client
|
| 32 |
-
output, error = process.communicate()
|
| 33 |
-
response = f"Command executed:\n\nOutput:\n{output.decode('utf-8')}\nError:\n{error.decode('utf-8')}"
|
| 34 |
-
await websocket.send(response)
|
| 35 |
except Exception as e:
|
| 36 |
await websocket.send(e)
|
| 37 |
|
|
|
|
| 20 |
base_path = os.path.join(os.getcwd(), 'projects', project_name)
|
| 21 |
|
| 22 |
try:
|
| 23 |
+
process = await asyncio.create_subprocess_shell(
|
| 24 |
command,
|
| 25 |
cwd=base_path,
|
| 26 |
+
stdin=asyncio.subprocess.PIPE,
|
| 27 |
+
stdout=asyncio.subprocess.PIPE,
|
| 28 |
+
stderr=asyncio.subprocess.PIPE,
|
| 29 |
+
# text=True,
|
| 30 |
)
|
| 31 |
|
| 32 |
+
async def send_message(message):
|
| 33 |
+
print('sending msg')
|
| 34 |
+
await websocket.send(f'data: {message}')
|
| 35 |
+
|
| 36 |
+
async for line in process.stdout:
|
| 37 |
+
print('sending line')
|
| 38 |
+
print(line.strip())
|
| 39 |
+
# await process_input_request(line)
|
| 40 |
+
await send_message(line.strip())
|
| 41 |
+
print(line.strip())
|
| 42 |
+
|
| 43 |
+
async for line in process.stderr:
|
| 44 |
+
print(f'error:{line.strip()}')
|
| 45 |
+
await send_message(f'error:{line.strip()}')
|
| 46 |
+
|
| 47 |
+
return_code = await process.wait()
|
| 48 |
+
if return_code == 0:
|
| 49 |
+
await send_message('Code executed successfully')
|
| 50 |
+
# pass
|
| 51 |
+
else:
|
| 52 |
+
await send_message(f'error:Execution failed with return code {return_code}')
|
| 53 |
+
|
| 54 |
# Send the command output to the client
|
| 55 |
+
# output, error = process.communicate()
|
| 56 |
+
# response = f"Command executed:\n\nOutput:\n{output.decode('utf-8')}\nError:\n{error.decode('utf-8')}"
|
| 57 |
+
# await websocket.send(response)
|
| 58 |
except Exception as e:
|
| 59 |
await websocket.send(e)
|
| 60 |
|