better tools

This commit is contained in:
Devaev Maxim
2019-03-24 01:30:33 +03:00
parent cdc9ed54c9
commit 4ee3b18533
4 changed files with 68 additions and 39 deletions

53
tools/common.py Normal file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env python3
#============================================================================#
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
#============================================================================#
import textwrap
# =====
def get_prepend() -> str:
return textwrap.dedent("""
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
""").strip() + "\n"

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env -S python3 -B
#============================================================================# #============================================================================#
# # # #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. # # uStreamer - Lightweight and fast MJPG-HTTP streamer. #
@@ -22,12 +22,13 @@
import sys import sys
import os
import textwrap import textwrap
import common
# ===== # =====
def main(): def main() -> None:
assert len(sys.argv) == 4, "%s <file.html> <file.h> <name>" % (sys.argv[0]) assert len(sys.argv) == 4, "%s <file.html> <file.h> <name>" % (sys.argv[0])
html_path = sys.argv[1] html_path = sys.argv[1]
header_path = sys.argv[2] header_path = sys.argv[2]
@@ -36,9 +37,6 @@ def main():
with open(html_path, "r") as html_file: with open(html_path, "r") as html_file:
text = html_file.read() text = html_file.read()
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "prepend.h")) as prepend_file:
prepend = prepend_file.read()
text = text.strip() text = text.strip()
text = text.replace("\"", "\\\"") text = text.replace("\"", "\\\"")
text = text.replace("%VERSION%", "\" VERSION \"") text = text.replace("%VERSION%", "\" VERSION \"")
@@ -48,7 +46,7 @@ def main():
for line in text.split("\n") for line in text.split("\n")
) )
text = "const char HTML_%s_PAGE[] = \" \\\n%s\n\";\n" % (name, text) text = "const char HTML_%s_PAGE[] = \" \\\n%s\n\";\n" % (name, text)
text = prepend + "\n#include \"../../config.h\"\n\n\n" + text text = common.get_prepend() + "\n#include \"../../config.h\"\n\n\n" + text
with open(header_path, "w") as header_file: with open(header_path, "w") as header_file:
header_file.write(text) header_file.write(text)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env -S python3 -B
#============================================================================# #============================================================================#
# # # #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. # # uStreamer - Lightweight and fast MJPG-HTTP streamer. #
@@ -22,13 +22,17 @@
import sys import sys
import os
import io import io
import struct import struct
from typing import Tuple
from typing import List
import common
# ===== # =====
def _get_jpeg_size(data): def _get_jpeg_size(data: bytes) -> Tuple[int, int]:
# https://sheep.horse/2013/9/finding_the_dimensions_of_a_jpeg_file_in_python.html # https://sheep.horse/2013/9/finding_the_dimensions_of_a_jpeg_file_in_python.html
stream = io.BytesIO(data) stream = io.BytesIO(data)
@@ -55,7 +59,7 @@ def _get_jpeg_size(data):
# ===== # =====
def main(): def main() -> None:
assert len(sys.argv) == 4, "%s <file.jpeg> <file.h> <name>" % (sys.argv[0]) assert len(sys.argv) == 4, "%s <file.jpeg> <file.h> <name>" % (sys.argv[0])
jpeg_path = sys.argv[1] jpeg_path = sys.argv[1]
header_path = sys.argv[2] header_path = sys.argv[2]
@@ -66,10 +70,7 @@ def main():
(width, height) = _get_jpeg_size(jpeg_data) (width, height) = _get_jpeg_size(jpeg_data)
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "prepend.h")) as prepend_file: rows: List[List[str]] = [[]]
prepend = prepend_file.read()
rows = [[]]
for ch in jpeg_data: for ch in jpeg_data:
if len(rows[-1]) > 20: if len(rows[-1]) > 20:
rows.append([]) rows.append([])
@@ -79,7 +80,7 @@ def main():
text = "const unsigned char %s_JPEG_DATA[] = {\n\t%s\n};\n" % (name, text) text = "const unsigned char %s_JPEG_DATA[] = {\n\t%s\n};\n" % (name, text)
text = "const unsigned %s_JPEG_HEIGHT = %d;\n\n" % (name, height) + text text = "const unsigned %s_JPEG_HEIGHT = %d;\n\n" % (name, height) + text
text = "const unsigned %s_JPEG_WIDTH = %d;\n" % (name, width) + text text = "const unsigned %s_JPEG_WIDTH = %d;\n" % (name, width) + text
text = prepend + "\n\n" + text text = common.get_prepend() + "\n\n" + text
with open(header_path, "w") as header_file: with open(header_path, "w") as header_file:
header_file.write(text) header_file.write(text)

View File

@@ -1,23 +0,0 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once