Browse Source

make-server and make-client are now in python

master
Thomas FORGIONE 2 years ago
parent
commit
bd42fefea4
No account linked to committer's email address
6 changed files with 189 additions and 82 deletions
  1. 1
    0
      .gitignore
  2. 16
    0
      make-client.py
  3. 1
    22
      make-client.sh
  4. 81
    0
      make-server.py
  5. 1
    60
      make-server.sh
  6. 89
    0
      make.py

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
data
__pycache__

+ 16
- 0
make-client.py View File

@@ -0,0 +1,16 @@
#!/usr/bin/env python3

import urllib
import urllib.request
import make
import os

def main():
try:
response = urllib.request.urlopen('http://localhost:1500/make' + os.getcwd()).read()
except urllib.error.URLError:
print('Server not responding, building manually', file=sys.stderr)
make.main()

if __name__ == '__main__':
main()

+ 1
- 22
make-client.sh View File

@@ -1,23 +1,2 @@
#!/usr/bin/env bash

# A client that corresponds to the make-server.sh
# Simply curls the pwd


curl "localhost:1500/$PWD" > /dev/null 2>&1

if [ $? -ne 0 ]; then

# Connecting to make-server failed, launch make manually

command -v make.sh 2>&1 >/dev/null

if [ $? -eq 0 ]; then
MAKE_COMMAND=make.sh
else
MAKE_COMMAND=make
fi

$MAKE_COMMAND

fi
make-client.py

+ 81
- 0
make-server.py View File

@@ -0,0 +1,81 @@
#!/usr/bin/env python3

import urllib
import urllib.request
from make import *
from http.server import BaseHTTPRequestHandler, HTTPServer

COLOR_GREEN = '\033[32m\033[1m'
COLOR_RED = '\033[31m\033[1m'
COLOR_DEFAULT = '\033[0m'

HELLO_RESPONSE = 'Hello from make-server !'

class CustomRequestHandler(BaseHTTPRequestHandler):

def do_GET(self):

split = self.path[1:].split('/')
action = split[0]
split.pop(0)

if action == 'hello':
self.send_response(200)
self.send_header('Content-type','text/plain')
self.end_headers()
self.wfile.write(bytes(HELLO_RESPONSE, 'utf8'))

elif action == 'make':
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()

path_to_build = '/' + '/'.join(split)
print(COLOR_GREEN + '---- STARTING BUILD ---- from ' + path_to_build + COLOR_DEFAULT)
return_code = build_in_directory(find_build_directory(path_to_build))
notify_from_error_code(return_code)

if return_code == 0:
print(COLOR_GREEN + '------ SUCCESSFUL ------' + COLOR_DEFAULT + '\n')
else:
print(COLOR_RED + '--------- FAIL ---------' + COLOR_DEFAULT + '\n')

else:
self.send_response(404)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(bytes('Action not found', 'utf8'))

return

def log_message(self, format, *args):
return

def main():
# Before starting the server, try to see if it's not already running
try:
response = urllib.request.urlopen('http://localhost:1500/hello').read()
if response.decode('utf-8') == HELLO_RESPONSE:
print('Server is already running...', file=sys.stderr)
sys.exit(1)
else:
print('There is already a HTTP server running on port 1500...', file=sys.stderr)
except urllib.error.URLError:
pass

server_address = ('127.0.0.1', 1500)
try:
httpd = HTTPServer(server_address, CustomRequestHandler)
except OSError:
print('Could not start server...', file=sys.stderr)
sys.exit(1)

print('Listening...')
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('\b\bBye')


if __name__ == '__main__':
main()

+ 1
- 60
make-server.sh View File

@@ -1,61 +1,2 @@
#!/usr/bin/env bash

# Starts a simple HTTP server that waits for compilation request.
# When it receveives "GET //home/johndoe/Programs/MyCppProject", it will go in
# the /home/johndoe/Programs/MyCppProject and run make.sh (or make if make.sh
# does not exist)

# Analyse to see what type of netcat we are using
nc -h 2>&1 | head -n 1 | grep '^GNU' > /dev/null 2>&1

if [ $? -eq 0 ]; then

# We are using GNU netcat, the "close on EOF" option is -c
close_option="-c"
close_value=""

fi

nc -h 2>&1 | head -n 1 | grep '^\[' > /dev/null 2>&1

if [ $? -eq 0 ]; then

# I don't know what we're using, but it seems like the close option is -q 0
close_option="-q"
close_value="0"

fi

command -v make.sh 2>&1 >/dev/null

if [ $? -eq 0 ]; then
make_command=make.sh
else
make_command=make
fi

echo Listening...
while true; do

result=$(echo "HTTP/1.1 200 OK\n\n `date`" | nc -l -p 1500 $close_option $close_value )
dir=$(echo "$result" | grep GET | cut -d ' ' -f 2- | rev | cut -d ' ' -f 2- | rev | tail -c +2)

if [ $? -ne 0 ]; then
echo Server crashed...
exit 1
fi

echo -e "\n\033[32m\033[1m"---- STARTING BUILD ---- from $dir"\033[0m"

cd "$dir" && $make_command

ERROR_CODE=$?
if [ "$ERROR_CODE" -eq "0" ]; then
echo -e "\033[32m\033[1m"------ SUCCESSFUL ------"\033[0m"
else
echo -e "\033[31m\033[1m"--------- FAIL ---------"\033[0m"
echo "$result"
fi

done

make-server.py

+ 89
- 0
make.py View File

@@ -0,0 +1,89 @@
#!/usr/bin/env python3

import os
import sys
from enum import Enum
import subprocess as sp

dir_path = os.path.dirname(os.path.realpath(__file__))
success_icon = os.path.join(dir_path, 'assets/success.png')
failure_icon = os.path.join(dir_path, 'assets/failure.png')

def notify(title='', text=None, icon=None):
args = ['notify-send', title]

if text is not None:
args.append(text)

if icon is not None:
args.append('-i')
args.append(icon)

p = sp.Popen(args)
p.wait()

def notify_success():
notify('Success', 'Make finished successfully', success_icon)

def notify_failure(error_code, message=None):
full_message = 'Make finished with code {}'.format(error_code)
if message is not None:
full_message += ': ' + message

notify('Failure', full_message, failure_icon)

def find_build_directory(path=None):
accepted_makefiles = ['Makefile', 'build/Makefile', 'Cargo.toml']

if path is None:
path = os.getcwd()

while path != '/':
for makefile in accepted_makefiles:
if os.path.isfile(os.path.join(path, makefile)):
return path

path = os.path.dirname(path)

def build_in_directory(path):
if path is None:
return

if os.path.isfile(os.path.join(path, 'Makefile')):
process = sp.Popen(['make'], cwd=path)
elif os.path.isfile(os.path.join(path, 'build/Makefile')):
process = sp.Popen(['make'], cwd=os.path.join(path, 'build'))
elif os.path.isfile(os.path.join(path, 'Cargo.toml')):
process = sp.Popen(['cargo', 'build'], cwd=path)
else:
return

process.wait()
return process.returncode

def notify_from_error_code(error_code):
if error_code is None:
notify_failure(error_code, 'No makefile found')
elif error_code == 0:
notify_success()
else:
notify_failure(error_code)

def main():
return_code = 0
build_dir = find_build_directory()
if build_dir is not None:
return_code = build_in_directory(build_dir)
if return_code == 0:
notify_success()
else:
notify_failure(return_code)
else:
return_code = 2
notify_failure(return_code, 'No makefile found')

return return_code

if __name__ == '__main__':
sys.exit(main())


Loading…
Cancel
Save