From 4e971e9d6d413955b71d445b544b3077cbfc4d7a Mon Sep 17 00:00:00 2001 From: "Magomed Kostoev (mkostoevr)" Date: Fri, 3 Dec 2021 16:22:06 +0000 Subject: [PATCH] [WS] Move simple functions into lib git-svn-id: svn://kolibrios.org@9370 a494cfbc-eb01-0410-851d-a64ba20cac60 --- _tools/get_started.py | 17 ++++++++--------- _tools/lib/logging.py | 4 ++++ _tools/lib/network.py | 8 ++++++++ _tools/lib/platform.py | 16 ++++++++++++++++ _tools/workspace/build.py | 10 +++++----- _tools/workspace/run.py | 35 ++++++----------------------------- 6 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 _tools/lib/logging.py create mode 100644 _tools/lib/network.py create mode 100644 _tools/lib/platform.py diff --git a/_tools/get_started.py b/_tools/get_started.py index 3e2a505648..7d7b520581 100644 --- a/_tools/get_started.py +++ b/_tools/get_started.py @@ -1,4 +1,4 @@ -#!/bin/python3 +#!/usr/bin/python3 # Copyright Magomed Kostoev # Published under MIT license @@ -7,14 +7,14 @@ import os def log(s, end = "\n"): print(s, end = end, flush = True) -def install_python_script(src, dst, tools_lib): +def install_python_script(src, dst, tools): log(f"Copying {src}... ", end = "") with open(src) as src_file: script = src_file.read() - tools_lib_escaped = tools_lib.replace("\\", "\\\\") - repl_from = "path_to_lib = '../lib'" - repl_to = f"path_to_lib ='{tools_lib_escaped}'" + tools = tools.replace("\\", "\\\\") + repl_from = "path_to_tools = '..'" + repl_to = f"path_to_tools ='{tools}'" script = script.replace(repl_from, repl_to, 1) with open(dst, "w") as dst_file: dst_file.write(script) @@ -24,11 +24,10 @@ def install_python_script(src, dst, tools_lib): if __name__ == "__main__": tools_get_started_py = os.path.abspath(__file__) tools = os.sep.join(tools_get_started_py.split(os.sep)[:-1]) - tools_lib = os.sep.join([tools, "lib"]) tools_workspace = os.sep.join([tools, "workspace"]) # Copy scripts from _tools/workspace to current folder, but let them know - # where the _tools/lib is (change their value of tools_lib variable) + # where the _tools/lib is (change their value of tools variable) tools_workspace_run_py = os.sep.join([tools_workspace, "run.py"]) tools_workspace_build_py = os.sep.join([tools_workspace, "build.py"]) - install_python_script(tools_workspace_run_py, "run.py", tools_lib) - install_python_script(tools_workspace_build_py, "build.py", tools_lib) + install_python_script(tools_workspace_run_py, "run.py", tools) + install_python_script(tools_workspace_build_py, "build.py", tools) diff --git a/_tools/lib/logging.py b/_tools/lib/logging.py new file mode 100644 index 0000000000..3576f2c158 --- /dev/null +++ b/_tools/lib/logging.py @@ -0,0 +1,4 @@ +def log(s, end = "\n"): + print(s, end = end, flush = True) + + diff --git a/_tools/lib/network.py b/_tools/lib/network.py new file mode 100644 index 0000000000..201e198aba --- /dev/null +++ b/_tools/lib/network.py @@ -0,0 +1,8 @@ +import urllib.request + +def download(link, path): + log(f"Downloading {path}... ", end = "") + urllib.request.urlretrieve(link, path) + log("Done.") + + diff --git a/_tools/lib/platform.py b/_tools/lib/platform.py new file mode 100644 index 0000000000..dfe88d04ba --- /dev/null +++ b/_tools/lib/platform.py @@ -0,0 +1,16 @@ +import os +import sys + +def path(*args): + return os.sep.join(args) + +def is_win32(): + return True if sys.platform == "win32" else False + +def is_linux(): + return True if sys.platform == "linux" or sys.platform == "linux2" else False + +def is_osx(): + return True if sys.platform == "darwin" else False + + diff --git a/_tools/workspace/build.py b/_tools/workspace/build.py index 06cfbdca53..42fe552125 100644 --- a/_tools/workspace/build.py +++ b/_tools/workspace/build.py @@ -1,17 +1,17 @@ import sys import os -path_to_lib = '../lib' -sys.path.append(path_to_lib) +path_to_tools = '..' +sys.path.append(path_to_tools) -import tupfile_parser +from lib.tupfile_parser import parse as parse_tupfile def build(): if not os.path.exists("Tupfile.lua"): print("No Tupfile.lua, can't build anything") exit() - tup_rules = tupfile_parser.parse("Tupfile.lua") + tup_rules = parse_tupfile("Tupfile.lua") program_files = [] for rule in tup_rules: # TODO: Manage source dependencies @@ -21,4 +21,4 @@ def build(): return program_files if __name__ == "__main__": - build() \ No newline at end of file + build() diff --git a/_tools/workspace/run.py b/_tools/workspace/run.py index f25afba483..9d6d23247d 100644 --- a/_tools/workspace/run.py +++ b/_tools/workspace/run.py @@ -7,10 +7,13 @@ import subprocess import build -path_to_lib = '../lib' -sys.path.append(path_to_lib) +path_to_tools = '..' +sys.path.append(path_to_tools) -from makeflop import Floppy +from lib.makeflop import Floppy +from lib.platform import is_win32, path +from lib.logging import log +from lib.network import download # TODO: Move into _tools/lib def get_file_directory(path): @@ -23,32 +26,6 @@ def get_file_directory(path): else: return "." # Just a filename, let's return current folder -# TODO: Move into _tools/lib -def is_win32(): - return True if sys.platform == "win32" else False - -# TODO: Move into _tools/lib -def is_linux(): - return True if sys.platform == "linux" or sys.platform == "linux2" else False - -# TODO: Move into _tools/lib -def is_osx(): - return True if sys.platform == "darwin" else False - -# TODO: Move into _tools/lib -def log(s, end = "\n"): - print(s, end = end, flush = True) - -# TODO: Move into _tools/lib -def download(link, path): - log(f"Downloading {path}... ", end = "") - urllib.request.urlretrieve(link, path) - log("Done.") - -# TODO: Move into _tools/lib -def path(*args): - return os.sep.join(args) - # TODO: Move into _tools/lib def run_qemu(start_dir = "workspace"): qemu_command = f"qemu-system-i386"