Skip to content

Commit

Permalink
Windows fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz committed Apr 3, 2024
1 parent 9e66e40 commit fc8f533
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
Binary file added relenv/build/.common.py.swp
Binary file not shown.
7 changes: 7 additions & 0 deletions relenv/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import random
import codecs
import signal

from . import linux, darwin, windows
from .common import builds, CHECK_VERSIONS_SUPPORT
Expand Down Expand Up @@ -166,6 +167,12 @@ def main(args):
show_ui = False
else:
show_ui = True

def signal_handler(signal, frame):
sys.exit(1)

signal.signal(signal.SIGINT, signal_handler)

build(
steps=steps,
arch=args.arch,
Expand Down
38 changes: 22 additions & 16 deletions relenv/build/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import tarfile
import tempfile
import time
import traceback
import subprocess
import random
import sys
Expand Down Expand Up @@ -607,6 +606,7 @@ def __call__(self, force_download=False, show_ui=False, exit_on_failure=False):
:rtype: bool
"""
os.makedirs(self.filepath.parent, exist_ok=True)

downloaded = False
if force_download:
_, downloaded = self.fetch_file()
Expand Down Expand Up @@ -897,7 +897,9 @@ def add(self, name, build_func=None, wait_on=None, download=None):
"download": download,
}

def run(self, name, event, build_func, download):
def run(
self, name, event, build_func, download, show_ui=False, log_level="WARNING"
):
"""
Run a build step.
Expand All @@ -912,8 +914,14 @@ def run(self, name, event, build_func, download):
:return: The output of the build function
"""
log = logging.getLogger(None)
for handler in log.handlers:
root_log = logging.getLogger(None)
if sys.platform == "win32":
if not show_ui:
handler = logging.StreamHandler()
handler.setLevel(logging.getLevelName(log_level))
root_log.addHandler(handler)

for handler in root_log.handlers:
if isinstance(handler, logging.StreamHandler):
handler.setFormatter(
logging.Formatter(f"%(asctime)s {name} %(message)s")
Expand All @@ -932,8 +940,8 @@ def run(self, name, event, build_func, download):

logfp = io.open(os.path.join(dirs.logs, "{}.log".format(name)), "w")
handler = logging.FileHandler(dirs.logs / f"{name}.log")
log.addHandler(handler)
log.setLevel(logging.NOTSET)
root_log.addHandler(handler)
root_log.setLevel(logging.NOTSET)

# DEBUG: Uncomment to debug
# logfp = sys.stdout
Expand Down Expand Up @@ -967,20 +975,15 @@ def run(self, name, event, build_func, download):
env["RELENV_NATIVE_PY"] = str(native_root / "bin" / "python3")

self.populate_env(env, dirs)

logfp.write("*" * 80 + "\n")
_ = dirs.to_dict()
for k in _:
logfp.write("{} {}\n".format(k, _[k]))
logfp.write("*" * 80 + "\n")
log.info("Directory %s %s", k, _[k])
for k in env:
logfp.write("{} {}\n".format(k, env[k]))
logfp.write("*" * 80 + "\n")
logfp.flush()
log.info("Environment %s %s", k, env[k])
try:
return build_func(env, dirs, logfp)
except Exception:
logfp.write(traceback.format_exc() + "\n")
log.exception("Build failure")
sys.exit(1)
finally:
os.chdir(cwd)
Expand Down Expand Up @@ -1073,7 +1076,7 @@ def download_files(self, steps=None, force_download=False, show_ui=False):
sys.stderr.flush()
sys.exit(1)

def build(self, steps=None, cleanup=True, show_ui=False):
def build(self, steps=None, cleanup=True, show_ui=False, log_level="WARNING"):
"""
Build!
Expand All @@ -1097,6 +1100,8 @@ def build(self, steps=None, cleanup=True, show_ui=False):
event = multiprocessing.Event()
events[name] = event
kwargs = dict(self.recipies[name])
kwargs["show_ui"] = show_ui
kwargs["log_level"] = log_level

# Determine needed dependency recipies.
wait_on = kwargs.pop("wait_on", [])
Expand Down Expand Up @@ -1218,6 +1223,7 @@ def __call__(
:type force_download: bool, optional
"""
log = logging.getLogger(None)
log.setLevel(logging.NOTSET)

if not show_ui:
handler = logging.StreamHandler()
Expand Down Expand Up @@ -1259,7 +1265,7 @@ def __call__(
# Start a process for each build passing it an event used to notify each
# process if it's dependencies have finished.
self.download_files(steps, force_download=force_download, show_ui=show_ui)
self.build(steps, cleanup, show_ui=show_ui)
self.build(steps, cleanup, show_ui=show_ui, log_level=log_level)

def check_versions(self):
success = True
Expand Down
4 changes: 4 additions & 0 deletions relenv/build/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import os
import pathlib
import tarfile
import logging


log = logging.getLogger(__name__)


from .common import runcmd, create_archive, MODULE_DIR, builds, install_runtime
Expand Down
31 changes: 18 additions & 13 deletions relenv/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import os
import pathlib
import platform
import queue
import selectors
import subprocess
import sys
import queue
import tarfile
import textwrap
import time
import threading
import time

# relenv package version
__version__ = "0.15.1"
Expand Down Expand Up @@ -85,7 +85,7 @@
SHEBANG_TPL = SHEBANG_TPL_MACOS


log = logging.getLogger()
log = logging.getLogger(__name__)


class RelenvException(Exception):
Expand Down Expand Up @@ -468,16 +468,20 @@ def runcmd(*args, **kwargs):
log.error(line)

else:

def enqueue_stream(stream, queue, type):
for line in iter(stream.readline, b''):
NOOP = object()
for line in iter(stream.readline, NOOP):
if line is NOOP or line == "":
break
if line:
queue.put((type, line))
queue.put((type, line))
log.debug("stream close %r %r", type, line)
stream.close()


def enqueue_process(process, queue):
process.wait()
queue.put(('x', 'x'))
queue.put(("x", "x"))

p = subprocess.Popen(*args, **kwargs)
q = queue.Queue()
Expand All @@ -490,23 +494,24 @@ def enqueue_process(process, queue):

while True:
kind, line = q.get()
if kind == 'x':
break
if kind == 2: # stderr
log.error(line[:-1])
else:
if kind == 1: # stdout
log.info(line[:-1])
elif kind == 2:
log.error(line[:-1])
elif kind == "x":
log.debug("process queue end")
break

tp.join()
to.join()
te.join()


p.wait()
if p.returncode != 0:
raise RelenvException("Build cmd '{}' failed".format(" ".join(args[0])))
return p


def relative_interpreter(root_dir, scripts_dir, interpreter):
"""
Return a relativized path to the given scripts_dir and interpreter.
Expand Down

0 comments on commit fc8f533

Please sign in to comment.