Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -393,7 +393,7 @@ async def read_process_output(process, websocket):
|
|
393 |
# }
|
394 |
# await websocket.send(json.dumps(event))
|
395 |
|
396 |
-
async def handle_user_input(websocket,key, process, connected):
|
397 |
while True:
|
398 |
try:
|
399 |
await asyncio.sleep(0.1)
|
@@ -437,7 +437,8 @@ async def handle_user_input(websocket,key, process, connected):
|
|
437 |
stderr=asyncio.subprocess.PIPE,
|
438 |
# text=True,
|
439 |
)
|
440 |
-
|
|
|
441 |
async def send_message(message):
|
442 |
# print('sending msg')
|
443 |
# await websocket.send(f'data: {message}')
|
@@ -446,7 +447,7 @@ async def handle_user_input(websocket,key, process, connected):
|
|
446 |
|
447 |
|
448 |
await asyncio.gather(
|
449 |
-
handle_user_input(websocket,key, process, connected),
|
450 |
read_process_output(process, websocket)
|
451 |
)
|
452 |
|
@@ -464,7 +465,8 @@ async def handle_user_input(websocket,key, process, connected):
|
|
464 |
|
465 |
}
|
466 |
await websocket.send(json.dumps(event))
|
467 |
-
|
|
|
468 |
|
469 |
except Exception as e:
|
470 |
await error(websocket, str(e))
|
@@ -480,15 +482,43 @@ async def handle_user_input(websocket,key, process, connected):
|
|
480 |
|
481 |
await wirte_file(websocket, key,event["project_name"], event["path"], event["content"], connected)
|
482 |
elif event["command"]["type"]=="curl":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
483 |
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
492 |
elif event["command"]["type"]=="create":
|
493 |
if event["item"]=="folder":
|
494 |
await create_folder(websocket, key,event["project_name"], event["path"],event["name"],event['root'],event['targetElementData'],event["rpath"], connected)
|
@@ -739,7 +769,7 @@ async def error(websocket, message):
|
|
739 |
await websocket.send(json.dumps(event))
|
740 |
|
741 |
|
742 |
-
async def exe(websocket,connected,key):
|
743 |
"""
|
744 |
Receive and process moves from a player.
|
745 |
|
@@ -778,6 +808,8 @@ async def exe(websocket,connected,key):
|
|
778 |
stderr=asyncio.subprocess.PIPE,
|
779 |
# text=True,
|
780 |
)
|
|
|
|
|
781 |
|
782 |
async def send_message(message):
|
783 |
# print('sending msg')
|
@@ -787,7 +819,7 @@ async def exe(websocket,connected,key):
|
|
787 |
|
788 |
|
789 |
await asyncio.gather(
|
790 |
-
handle_user_input(websocket,key, process, connected),
|
791 |
read_process_output(process, websocket)
|
792 |
)
|
793 |
|
@@ -805,6 +837,7 @@ async def exe(websocket,connected,key):
|
|
805 |
|
806 |
}
|
807 |
await websocket.send(json.dumps(event))
|
|
|
808 |
|
809 |
except Exception as e:
|
810 |
await error(websocket, str(e))
|
@@ -1050,6 +1083,7 @@ async def start(websocket,events):
|
|
1050 |
|
1051 |
user = User()
|
1052 |
connected = {websocket}
|
|
|
1053 |
|
1054 |
join_key = secrets.token_urlsafe(12)
|
1055 |
JOIN[join_key] = user, connected
|
@@ -1065,9 +1099,13 @@ async def start(websocket,events):
|
|
1065 |
}
|
1066 |
await websocket.send(json.dumps(event))
|
1067 |
|
1068 |
-
await exe(websocket,connected,join_key)
|
1069 |
finally:
|
1070 |
del JOIN[join_key]
|
|
|
|
|
|
|
|
|
1071 |
|
1072 |
|
1073 |
|
|
|
393 |
# }
|
394 |
# await websocket.send(json.dumps(event))
|
395 |
|
396 |
+
async def handle_user_input(websocket,key, process, connected,process_ids):
|
397 |
while True:
|
398 |
try:
|
399 |
await asyncio.sleep(0.1)
|
|
|
437 |
stderr=asyncio.subprocess.PIPE,
|
438 |
# text=True,
|
439 |
)
|
440 |
+
id=process.pid
|
441 |
+
process_ids.append(id)
|
442 |
async def send_message(message):
|
443 |
# print('sending msg')
|
444 |
# await websocket.send(f'data: {message}')
|
|
|
447 |
|
448 |
|
449 |
await asyncio.gather(
|
450 |
+
handle_user_input(websocket,key, process, connected,process_ids),
|
451 |
read_process_output(process, websocket)
|
452 |
)
|
453 |
|
|
|
465 |
|
466 |
}
|
467 |
await websocket.send(json.dumps(event))
|
468 |
+
|
469 |
+
process_ids.remove(id)
|
470 |
|
471 |
except Exception as e:
|
472 |
await error(websocket, str(e))
|
|
|
482 |
|
483 |
await wirte_file(websocket, key,event["project_name"], event["path"], event["content"], connected)
|
484 |
elif event["command"]["type"]=="curl":
|
485 |
+
method = event.get("method", "GET")
|
486 |
+
url = event["url"]
|
487 |
+
body = event.get("data", "")
|
488 |
+
headers = event.get("headers", {})
|
489 |
+
|
490 |
+
if method.upper() == "GET":
|
491 |
+
response = requests.get(url, headers=headers)
|
492 |
+
elif method.upper() == "POST":
|
493 |
+
response = requests.post(url, data=body, headers=headers)
|
494 |
+
else:
|
495 |
+
response = {"error": "Unsupported method"}
|
496 |
+
# Parse HTML content
|
497 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
498 |
+
# Find and modify CSS links
|
499 |
+
for link in soup.find_all("link", rel="stylesheet", href=True):
|
500 |
+
if link['href'].startswith('/'):
|
501 |
+
css_url = url + link['href']
|
502 |
+
css_response = requests.get(css_url)
|
503 |
+
css_content = css_response.text
|
504 |
+
style_tag = soup.new_tag("style")
|
505 |
+
style_tag.string = css_content
|
506 |
+
link.replace_with(style_tag)
|
507 |
|
508 |
+
# Find and modify JS links
|
509 |
+
for script in soup.find_all("script", src=True):
|
510 |
+
if script['src'].startswith('/'):
|
511 |
+
js_url = url + script['src']
|
512 |
+
js_response = requests.get(js_url)
|
513 |
+
js_content = js_response.text
|
514 |
+
script_tag = soup.new_tag("script")
|
515 |
+
script_tag.string = js_content
|
516 |
+
script.replace_with(script_tag)
|
517 |
+
response_data = {
|
518 |
+
"type": "web-data",
|
519 |
+
"data": str(soup),
|
520 |
+
}
|
521 |
+
await websocket.send(json.dumps(response_data))
|
522 |
elif event["command"]["type"]=="create":
|
523 |
if event["item"]=="folder":
|
524 |
await create_folder(websocket, key,event["project_name"], event["path"],event["name"],event['root'],event['targetElementData'],event["rpath"], connected)
|
|
|
769 |
await websocket.send(json.dumps(event))
|
770 |
|
771 |
|
772 |
+
async def exe(websocket,connected,key,process_ids):
|
773 |
"""
|
774 |
Receive and process moves from a player.
|
775 |
|
|
|
808 |
stderr=asyncio.subprocess.PIPE,
|
809 |
# text=True,
|
810 |
)
|
811 |
+
id=process.pid
|
812 |
+
process_ids.append(id)
|
813 |
|
814 |
async def send_message(message):
|
815 |
# print('sending msg')
|
|
|
819 |
|
820 |
|
821 |
await asyncio.gather(
|
822 |
+
handle_user_input(websocket,key, process, connected,process_ids),
|
823 |
read_process_output(process, websocket)
|
824 |
)
|
825 |
|
|
|
837 |
|
838 |
}
|
839 |
await websocket.send(json.dumps(event))
|
840 |
+
process_ids.remove(id)
|
841 |
|
842 |
except Exception as e:
|
843 |
await error(websocket, str(e))
|
|
|
1083 |
|
1084 |
user = User()
|
1085 |
connected = {websocket}
|
1086 |
+
process_ids = []
|
1087 |
|
1088 |
join_key = secrets.token_urlsafe(12)
|
1089 |
JOIN[join_key] = user, connected
|
|
|
1099 |
}
|
1100 |
await websocket.send(json.dumps(event))
|
1101 |
|
1102 |
+
await exe(websocket,connected,join_key,process_ids)
|
1103 |
finally:
|
1104 |
del JOIN[join_key]
|
1105 |
+
project_path = os.path.join(os.getcwd(), 'projects', JOIN[join_key])
|
1106 |
+
shutil.rmtree(project_path)
|
1107 |
+
for id in process_ids:
|
1108 |
+
os.killpg(os.getpgid(id), signal.SIGTERM)
|
1109 |
|
1110 |
|
1111 |
|