Skip to content

Commit

Permalink
Revert src to 36877c3
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpitt committed Dec 13, 2023
1 parent 13ab1f0 commit 55327ee
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 102 deletions.
13 changes: 1 addition & 12 deletions src/client/cockpit-client
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ def prctl(*args):
raise Exception('prctl() failed')


def get_user_state_dir():
try:
# GLib ≥ 2.72
return GLib.get_user_state_dir()
except AttributeError:
try:
return os.environ["XDG_STATE_HOME"]
except KeyError:
return os.path.expanduser("~/.local/share")


SET_PDEATHSIG = 1


Expand Down Expand Up @@ -233,7 +222,7 @@ class CockpitClient(Gtk.Application):
context.set_sandbox_enabled(enabled=True)
context.set_cache_model(WebKit2.CacheModel.DOCUMENT_VIEWER)

cookiesFile = os.path.join(get_user_state_dir(), "cockpit-client", "cookies.txt")
cookiesFile = os.path.join(GLib.get_user_state_dir(), "cockpit-client", "cookies.txt")
cookies = context.get_cookie_manager()
cookies.set_persistent_storage(cookiesFile, WebKit2.CookiePersistentStorage.TEXT)

Expand Down
27 changes: 2 additions & 25 deletions src/cockpit/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import pwd
import shlex
import socket
import stat
import subprocess
from typing import Iterable, List, Optional, Sequence, Tuple, Type

Expand Down Expand Up @@ -203,33 +202,11 @@ def try_to_receive_stderr():
os.close(fd)


def setup_journald() -> bool:
# If stderr is a socket, prefer systemd-journal logging. This covers the
# case we're already connected to the journal but also the case where we're
# talking to the ferny agent, while leaving logging to file or terminal
# unaffected.
if not stat.S_ISSOCK(os.fstat(2).st_mode):
# not a socket? Don't redirect.
return False

try:
import systemd.journal # type: ignore[import]
except ImportError:
# No python3-systemd? Don't redirect.
return False

logging.root.addHandler(systemd.journal.JournalHandler())
return True


def setup_logging(*, debug: bool) -> None:
def setup_logging(*, debug: bool):
"""Setup our logger with optional filtering of modules if COCKPIT_DEBUG env is set"""

modules = os.getenv('COCKPIT_DEBUG', '')

# Either setup logging via journal or via formatted messages to stderr
if not setup_journald():
logging.basicConfig(format='%(name)s-%(levelname)s: %(message)s')
logging.basicConfig(format='%(name)s-%(levelname)s: %(message)s')

if debug or modules == 'all':
logging.getLogger().setLevel(level=logging.DEBUG)
Expand Down
23 changes: 3 additions & 20 deletions src/cockpit/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import functools
import gzip
import io
import itertools
import json
import logging
import mimetypes
Expand Down Expand Up @@ -266,22 +265,11 @@ def ensure_scanned(self) -> None:
# Accept-Language is case-insensitive and uses '-' to separate variants
lower_locale = locale.lower().replace('_', '-')

logger.debug('Adding translation %r %r -> %r', basename, lower_locale, name)
self.translations[f'{basename}.js'][lower_locale] = name
else:
# strip out trailing '.gz' components
basename = re.sub('.gz$', '', name)
logger.debug('Adding content %r -> %r', basename, name)
basename = name[:-3] if name.endswith('.gz') else name
self.files[basename] = name

# If we see a filename like `x.min.js` we want to also offer it
# at `x.js`, but only if `x.js(.gz)` itself is not present.
# Note: this works for both the case where we found the `x.js`
# first (it's already in the map) and also if we find it second
# (it will be replaced in the map by the line just above).
# See https://github.com/cockpit-project/cockpit/pull/19716
self.files.setdefault(basename.replace('.min.', '.'), name)

# support old cockpit-po-plugin which didn't write po.manifest.??.js
if not self.translations['po.manifest.js']:
self.translations['po.manifest.js'] = self.translations['po.js']
Expand Down Expand Up @@ -502,13 +490,8 @@ def load(self) -> None:
def show(self):
for name in sorted(self.packages):
package = self.packages[name]
menuitems = []
for entry in itertools.chain(
package.manifest.get('menu', {}).values(),
package.manifest.get('tools', {}).values()):
with contextlib.suppress(KeyError):
menuitems.append(entry['label'])
print(f'{name:20} {", ".join(menuitems):40} {package.path}')
menuitems = ''
print(f'{name:20} {menuitems:40} {package.path}')

def get_bridge_configs(self) -> Sequence[BridgeConfig]:
def yield_configs():
Expand Down
14 changes: 14 additions & 0 deletions src/cockpit/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class CockpitProtocol(asyncio.Protocol):
transport: Optional[asyncio.Transport] = None
buffer = b''
_closed: bool = False
_communication_done: Optional[asyncio.Future] = None

def do_ready(self) -> None:
pass
Expand Down Expand Up @@ -172,6 +173,12 @@ def close(self, exc: Optional[Exception] = None) -> None:

self.do_closed(exc)

if self._communication_done is not None:
if exc is None:
self._communication_done.set_result(None)
else:
self._communication_done.set_exception(exc)

def write_channel_data(self, channel, payload):
"""Send a given payload (bytes) on channel (string)"""
# Channel is certainly ascii (as enforced by .encode() below)
Expand Down Expand Up @@ -203,6 +210,13 @@ def data_received(self, data):
def eof_received(self) -> Optional[bool]:
return False

async def communicate(self) -> None:
"""Wait until communication is complete on this protocol."""
assert self._communication_done is None
self._communication_done = asyncio.get_running_loop().create_future()
await self._communication_done
self._communication_done = None


# Helpful functionality for "server"-side protocol implementations
class CockpitProtocolServer(CockpitProtocol):
Expand Down
43 changes: 4 additions & 39 deletions src/cockpit/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# 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 asyncio
import collections
import logging
from typing import Dict, List, Optional
Expand Down Expand Up @@ -62,7 +61,7 @@ class Endpoint:
__endpoint_frozen_queue: Optional[ExecutionQueue] = None

def __init__(self, router: 'Router'):
router.add_endpoint(self)
router.endpoints[self] = set()
self.router = router

def freeze_endpoint(self):
Expand Down Expand Up @@ -134,7 +133,6 @@ class Router(CockpitProtocolServer):
routing_rules: List[RoutingRule]
open_channels: Dict[str, Endpoint]
endpoints: 'dict[Endpoint, set[str]]'
no_endpoints: asyncio.Event # set if endpoints dict is empty
_eof: bool = False

def __init__(self, routing_rules: List[RoutingRule]):
Expand All @@ -143,8 +141,6 @@ def __init__(self, routing_rules: List[RoutingRule]):
self.routing_rules = routing_rules
self.open_channels = {}
self.endpoints = {}
self.no_endpoints = asyncio.Event()
self.no_endpoints.set() # at first there are no endpoints

def check_rules(self, options: JsonObject) -> Endpoint:
for rule in self.routing_rules:
Expand All @@ -164,23 +160,16 @@ def drop_channel(self, channel: str) -> None:
except KeyError:
logger.error('trying to drop non-existent channel %s from %s', channel, self.open_channels)

def add_endpoint(self, endpoint: Endpoint) -> None:
self.endpoints[endpoint] = set()
self.no_endpoints.clear()

def shutdown_endpoint(self, endpoint: Endpoint, _msg: 'JsonObject | None' = None, **kwargs: JsonDocument) -> None:
channels = self.endpoints.pop(endpoint)
logger.debug('shutdown_endpoint(%s, %s) will close %s', endpoint, kwargs, channels)
for channel in channels:
self.write_control(_msg, command='close', channel=channel, **kwargs)
self.drop_channel(channel)

if not self.endpoints:
self.no_endpoints.set()

# were we waiting to exit?
if self._eof:
logger.debug(' endpoints remaining: %r', self.endpoints)
logger.debug(' %d endpoints remaining', len(self.endpoints))
if not self.endpoints and self.transport:
logger.debug(' close transport')
self.transport.close()
Expand Down Expand Up @@ -237,30 +226,6 @@ def eof_received(self) -> bool:
logger.debug(' endpoints remaining: %r', self.endpoints)
return bool(self.endpoints)

_communication_done: Optional[asyncio.Future] = None

def do_closed(self, exc: Optional[Exception]) -> None:
# If we didn't send EOF yet, do it now.
if not self._eof:
self.eof_received()

if self._communication_done is not None:
if exc is None:
self._communication_done.set_result(None)
else:
self._communication_done.set_exception(exc)

async def communicate(self) -> None:
"""Wait until communication is complete on the router and all endpoints are done."""
assert self._communication_done is None
self._communication_done = asyncio.get_running_loop().create_future()
try:
await self._communication_done
except (BrokenPipeError, ConnectionResetError):
pass # these are normal occurrences when closed from the other side
finally:
self._communication_done = None

# In an orderly exit, this is already done, but in case it wasn't
# orderly, we need to make sure the endpoints shut down anyway...
await self.no_endpoints.wait()
for rule in self.routing_rules:
rule.shutdown()
8 changes: 2 additions & 6 deletions src/cockpit/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,8 @@ def sample(self, samples: Samples) -> None:
num_sectors_read = fields[5]
num_sectors_written = fields[9]

# ignore mdraid
if dev_major == '9':
continue

# ignore device-mapper
if dev_name.startswith('dm-'):
# ignore device-mapper and md
if dev_major in ['9', '253']:
continue

# Skip partitions
Expand Down

0 comments on commit 55327ee

Please sign in to comment.