Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal builder for JupyterLab ecosystem #107

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions hatch_jupyter_builder/debuglog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""A mixin for adding a debug log file.

"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import contextlib
import logging
import os
import sys
import tempfile
import traceback
import warnings

from traitlets import Unicode
from traitlets.config import Configurable


class DebugLogFileMixin(Configurable):
debug_log_path = Unicode("", config=True, help="Path to use for the debug log file")

@contextlib.contextmanager
def debug_logging(self):
log_path = self.debug_log_path
if os.path.isdir(log_path):
log_path = os.path.join(log_path, "jupyterlab-debug.log")
if not log_path:
handle, log_path = tempfile.mkstemp(prefix="jupyterlab-debug-", suffix=".log")
os.close(handle)
log = self.log

# Transfer current log level to the handlers:
for h in log.handlers:
h.setLevel(self.log_level)
log.setLevel("DEBUG")

# Create our debug-level file handler:
_debug_handler = logging.FileHandler(log_path, "w", "utf8", delay=True)
_log_formatter = self._log_formatter_cls(fmt=self.log_format, datefmt=self.log_datefmt)
_debug_handler.setFormatter(_log_formatter)
_debug_handler.setLevel("DEBUG")

log.addHandler(_debug_handler)

try:
yield
except Exception as ex:
_, _, exc_traceback = sys.exc_info()
msg = traceback.format_exception(ex.__class__, ex, exc_traceback)
for line in msg:
self.log.debug(line)
if isinstance(ex, SystemExit):
warnings.warn(f"An error occurred. See the log file for details: {log_path}")
raise
warnings.warn("An error occurred.")
warnings.warn(msg[-1].strip())
warnings.warn(f"See the log file for details: {log_path}")
self.exit(1)
else:
log.removeHandler(_debug_handler)
_debug_handler.flush()
_debug_handler.close()
try:
os.remove(log_path)
except FileNotFoundError:
pass
log.removeHandler(_debug_handler)
110 changes: 110 additions & 0 deletions hatch_jupyter_builder/extension_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import itertools

from .semver import Range, gt, gte, lt, lte


def _test_overlap(spec1, spec2, drop_prerelease1=False, drop_prerelease2=False):
"""Test whether two version specs overlap.

Returns `None` if we cannot determine compatibility,
otherwise whether there is an overlap
"""
cmp = _compare_ranges(
spec1, spec2, drop_prerelease1=drop_prerelease1, drop_prerelease2=drop_prerelease2
)
if cmp is None:
return
return cmp == 0


def _compare_ranges(spec1, spec2, drop_prerelease1=False, drop_prerelease2=False): # noqa
"""Test whether two version specs overlap.

Returns `None` if we cannot determine compatibility,
otherwise return 0 if there is an overlap, 1 if
spec1 is lower/older than spec2, and -1 if spec1
is higher/newer than spec2.
"""
# Test for overlapping semver ranges.
r1 = Range(spec1, True)
r2 = Range(spec2, True)

# If either range is empty, we cannot verify.
if not r1.range or not r2.range:
return

# Set return_value to a sentinel value
return_value = False

# r1.set may be a list of ranges if the range involved an ||, so we need to test for overlaps between each pair.
for r1set, r2set in itertools.product(r1.set, r2.set):
x1 = r1set[0].semver
x2 = r1set[-1].semver
y1 = r2set[0].semver
y2 = r2set[-1].semver

if x1.prerelease and drop_prerelease1:
x1 = x1.inc("patch")

if y1.prerelease and drop_prerelease2:
y1 = y1.inc("patch")

o1 = r1set[0].operator
o2 = r2set[0].operator

# We do not handle (<) specifiers.
if o1.startswith("<") or o2.startswith("<"):
continue

# Handle single value specifiers.
lx = lte if x1 == x2 else lt
ly = lte if y1 == y2 else lt
gx = gte if x1 == x2 else gt
gy = gte if x1 == x2 else gt

# Handle unbounded (>) specifiers.
def noop(x, y, z):
return True

if x1 == x2 and o1.startswith(">"):
lx = noop
if y1 == y2 and o2.startswith(">"):
ly = noop

# Check for overlap.
if (
gte(x1, y1, True)
and ly(x1, y2, True)
or gy(x2, y1, True)
and ly(x2, y2, True)
or gte(y1, x1, True)
and lx(y1, x2, True)
or gx(y2, x1, True)
and lx(y2, x2, True)
):
# if we ever find an overlap, we can return immediately
return 0

if gte(y1, x2, True):
if return_value is False:
# We can possibly return 1
return_value = 1
elif return_value == -1:
# conflicting information, so we must return None
return_value = None
continue

if gte(x1, y2, True):
if return_value is False:
return_value = -1
elif return_value == 1:
# conflicting information, so we must return None
return_value = None
continue

msg = "Unexpected case comparing version ranges"
raise AssertionError(msg)

if return_value is False:
return_value = None
return return_value
Loading