Skip to content

Commit

Permalink
Update libtmux to lastest with new API
Browse files Browse the repository at this point in the history
  • Loading branch information
bubylou committed Apr 16, 2023
1 parent db8c86f commit 6714f89
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 57 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ python = "^3.9"
click = "^8.1.3"
colorama = "^0.4.6"
libtmux = "^0.21.1"
ruamel-yaml = "^0.17.21"
pyyaml = "^6.0"
vdf = "^3.4"

[tool.poetry.group.dev.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions scsm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,9 @@ def steamcmd_install():
message('Status', 'SteamCMD installed')

message('Status', 'SteamCMD updating')
exit_code, text = steamcmd.update()
exit_code = steamcmd.update()

if exit_code == 0:
message('Status', 'SteamCMD updated')
else:
message('Error', text)
message('Error', 'SteamCMD update failed')
14 changes: 6 additions & 8 deletions scsm/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import yaml
import platform
from pathlib import Path
from pkg_resources import resource_filename
from ruamel.yaml import YAML
import scsm


if platform.system() != 'Windows':
Expand Down Expand Up @@ -31,7 +31,7 @@

class Config():
system_wide = False
data_dir = resource_filename(__name__, 'data')
data_dir = Path(scsm.__path__[0], 'data')

if platform.system() != 'Windows':
config_dir = Path('~/.config/scsm').expanduser()
Expand All @@ -41,14 +41,13 @@ class Config():
else:
config_dir = Path(os.getenv('APPDATA'), 'scsm')

_yaml = YAML(typ='safe')
config_f = Path(config_dir, 'config.yaml')

if config_f.exists():
with open(config_f, 'r') as _f:
data = _yaml.load(_f)
data = yaml.safe_load(_f)
else:
data = _yaml.load(DEFAULTS)
data = yaml.safe_load(DEFAULTS)

compression = str(data['general']['compression'])
steam_guard = str(data['general']['steam_guard'])
Expand All @@ -74,8 +73,7 @@ def create(system_wide=False):
Config.config_f = Path(config_dir, 'config.yaml')

with open(Path(config_dir, 'config.yaml'), 'w') as f:
yaml = YAML(typ='safe')
yaml.dump(yaml.load(DEFAULTS), f)
yaml.dump(yaml.safe_load(DEFAULTS), f)

@staticmethod
def remove():
Expand Down
66 changes: 22 additions & 44 deletions scsm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import libtmux
import vdf
from ruamel.yaml import YAML
import yaml

from .config import Config

Expand All @@ -31,8 +31,7 @@ def __init__(self, app, app_dir, backup_dir=None, platform=None):
self.config_is_default = False

with open(self.config_f, 'r') as f:
yaml = YAML(typ='safe')
data = yaml.load(f)
data = yaml.safe_load(f)

self.app_names = list(data['apps'].keys())
if not self.app_name:
Expand Down Expand Up @@ -162,24 +161,18 @@ def restore(self, backup):
'''Restore specified backup file'''
with tarfile.open(Path(self.backup_dir, backup)) as tar:
def is_within_directory(directory, target):

abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)

prefix = os.path.commonprefix([abs_directory, abs_target])

return prefix == abs_directory

def safe_extract(tar, path=".", members=None, *, numeric_owner=False):

for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")

tar.extractall(path, members, numeric_owner=numeric_owner)



safe_extract(tar, self.app_dir.parent)

if self.config_is_default:
Expand Down Expand Up @@ -219,8 +212,7 @@ def list(directory):
return

with open(Index.f, 'r') as f:
yaml = YAML(typ='safe')
data = yaml.load(f)
data = yaml.safe_load(f)

for app_id in directory.iterdir():
if len(data[int(app_id.name)].keys()) > 1:
Expand All @@ -233,8 +225,7 @@ def list(directory):
def list_all():
'''Return generator of all app_id's in index'''
with open(Index.f, 'r') as f:
yaml = YAML(typ='safe')
data = yaml.load(f)
data = yaml.safe_load(f)

for app_id in data.keys():
app_names = data[app_id].keys()
Expand All @@ -254,8 +245,7 @@ def search(app):
pass

with open(Index.f, 'r') as f:
yaml = YAML(typ='safe')
data = yaml.load(f)
data = yaml.safe_load(f)

if app in data.keys():
return app, None, None
Expand All @@ -278,15 +268,13 @@ def update():
for f in d.iterdir():
if Path(f).suffix == '.yaml':
with open(Path(d, f), 'r') as config_f:
yaml = YAML(typ='safe')
data = yaml.load(config_f)
data = yaml.safe_load(config_f)

for app in data['apps'].keys():
app_index[data['app_id']] = {app: list(data['apps'][app]
['servers'].keys())}

with open(Index.f, 'w') as f:
yaml = YAML(typ='safe')
yaml.dump(app_index, f)


Expand All @@ -300,8 +288,8 @@ def __init__(self, app, app_dir, backup_dir=None, platform=None):
self.session_name = f'{self.app_name}-{self.server_name}'

try:
self.session = self.tmux.find_where({'session_name': self.session_name})
except libtmux.exc.LibTmuxException:
self.session = self.tmux.sessions.filter(session_name=self.session_name)[0]
except IndexError:
self.session = None

@property
Expand All @@ -326,27 +314,23 @@ def running_check(app_name, server_name=None):

if server_name:
try:
session = tmux.find_where({'session_name': f'{app_name}-{server_name}'})
session = tmux.sessions.filter(f'{app_name}-{server_name}')[0]
if session:
return True
return False
except libtmux.exc.LibTmuxException:
except IndexError:
return False
else:
# tmux.find_where does not work with partial names
try:
for session in tmux.list_sessions():
if session.name.startswith(f'{app_name}-'):
return True
except libtmux.exc.LibTmuxException:
return False
for session in tmux.sessions:
if session.name.startswith(f'{app_name}-'):
return True
return False

def send(self, command):
'''Send command to tmux session'''
window = self.session.list_windows()[0]
pane = window.list_panes()[0]
pane = self.session.windows[0].panes[0]
# suppress_history and literal must be false for c-c to work
pane.send_keys(command, enter=True, suppress_history=False, literal=False, )
pane.send_keys(command, enter=True, suppress_history=False, literal=False)

def start(self, debug=False):
'''Start server'''
Expand Down Expand Up @@ -405,8 +389,8 @@ def app_update(self, app_id, app_dir, beta=None, beta_password=None,
config=None, platform=None, validate=False,
username='anonymous', password='', steam_guard='',):
'''+app_update wrapper'''
cmd = ['+login', username, password, steam_guard, '+force_install_dir',
app_dir, '+app_update', str(app_id), '+quit']
cmd = ['+force_install_dir', app_dir, '+login', username, password,
steam_guard, '+app_update', str(app_id), '+quit']

if config:
cmd.insert(-3, f'+app_set_config {app_id} {config}')
Expand Down Expand Up @@ -478,24 +462,18 @@ def install(self):
if pf.system() != 'Windows':
with tarfile.open(Path(self.directory, f)) as tar:
def is_within_directory(directory, target):

abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)

prefix = os.path.commonprefix([abs_directory, abs_target])

return prefix == abs_directory

def safe_extract(tar, path=".", members=None, *, numeric_owner=False):

for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")

tar.extractall(path, members, numeric_owner=numeric_owner)



safe_extract(tar, self.directory)
else:
with ZipFile(Path(self.directory, f)) as zipf:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def test_console(self, server):

def test_send(self, server_running):
server_running.send('test')
window = server_running.session.list_windows()[0]
pane = window.list_panes()[0]
pane = server_running.session.windows[0].panes[0]
pane_contents = '\n'.join(pane.cmd('capture-pane', '-p').stdout)
assert 'test' in pane_contents

Expand Down

0 comments on commit 6714f89

Please sign in to comment.