astyle: Added client/server

This commit is contained in:
Daniel Marjamäki 2019-06-26 20:26:53 +02:00
parent 9a7ecd622c
commit 76542509a8
2 changed files with 106 additions and 0 deletions

62
tools/astyle_client.py Normal file
View File

@ -0,0 +1,62 @@
import glob
import os
import socket
import time
def receive_data(conn):
data = ''
for t in range(1000):
d = conn.recv(8196)
if d:
data += d
if data.endswith('\nDONE'):
return data[:-5]
time.sleep(0.01)
return ''
def astyle(server_address, code):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(server_address)
sock.sendall(code + '\nDONE')
return receive_data(sock)
except socket.error as err:
print('Network error: ' + str(err))
sock.close()
return None
def get_source_files(path):
files = []
for g in glob.glob(path + '*'):
if g.startswith('.'):
continue
if os.path.isdir(g):
files += get_source_files(g + '/')
if os.path.isfile(g) and (g.endswith('.cpp') or g.endswith('.h')):
files.append(g)
return files
if __name__ == "__main__":
server_address = ('localhost', 18000)
source_files = []
for d in ['cli', 'gui', 'lib', 'test', 'tools']:
source_files += get_source_files(d + '/')
for filename in source_files:
f = open(filename, 'rt')
code = f.read()
f.close()
formatted_code = astyle(server_address, code)
if formatted_code is None:
break
if code != formatted_code:
print('Changed: ' + filename)
f = open(filename, 'wt')
f.write(formatted_code)
f.close()
else:
print('Unchanged: ' + filename)

44
tools/astyle_server.py Normal file
View File

@ -0,0 +1,44 @@
import logging
import socket
import subprocess
from astyle_client import receive_data
logger = logging.getLogger()
def server(port):
socket.setdefaulttimeout(30)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ('', port)
sock.bind(server_address)
sock.listen(1)
while True:
# Wait for a connection
connection, client_address = sock.accept()
# Read data from client
try:
data = receive_data(connection)
except socket.error:
connection.close()
continue
# Format
process = subprocess.Popen(['astyle', '--options=../.astylerc'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
comm = process.communicate(input=data)
connection.sendall(comm[0] + '\nDONE')
connection.close()
if __name__ == "__main__":
server(port=18000)