2022-10-06 21:48:52 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-04-12 20:14:00 +02:00
|
|
|
from subprocess import Popen, PIPE, TimeoutExpired, run
|
2022-10-06 21:48:52 +02:00
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import argparse
|
2022-11-08 19:57:56 +01:00
|
|
|
from pathlib import Path, PosixPath
|
2022-10-06 21:48:52 +02:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2022-10-21 20:51:13 +02:00
|
|
|
from typing import Union
|
2022-10-28 18:02:42 +02:00
|
|
|
from urllib.parse import urlparse
|
2022-11-09 15:23:25 +01:00
|
|
|
from urllib.request import urlopen
|
2022-11-09 15:28:15 +01:00
|
|
|
import logging
|
2023-01-17 20:00:52 +01:00
|
|
|
import copy
|
2022-11-09 15:28:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.DEBUG,
|
|
|
|
format='[%(asctime)s] %(levelname)s: %(message)s',
|
|
|
|
handlers=[logging.StreamHandler(stream=sys.stdout)],
|
|
|
|
)
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
repos = ['https://github.com/lightningd/plugins']
|
|
|
|
|
|
|
|
|
2023-04-07 00:04:44 +02:00
|
|
|
def py_entry_guesses(name) -> list:
|
2022-10-06 21:48:52 +02:00
|
|
|
return [name, f'{name}.py', '__init__.py']
|
|
|
|
|
|
|
|
|
2023-04-07 00:04:44 +02:00
|
|
|
def unsupported_entry(name) -> list:
|
2022-10-06 21:48:52 +02:00
|
|
|
return [f'{name}.go', f'{name}.sh']
|
|
|
|
|
|
|
|
|
2023-04-07 00:04:44 +02:00
|
|
|
def entry_guesses(name: str) -> list:
|
2023-02-28 19:51:56 +01:00
|
|
|
guesses = []
|
|
|
|
global INSTALLERS
|
|
|
|
for iname, inst in INSTALLERS.items():
|
|
|
|
for entry in inst.entries:
|
|
|
|
guesses.append(entry.format(name=name))
|
|
|
|
return guesses
|
|
|
|
|
|
|
|
|
2023-01-17 20:00:52 +01:00
|
|
|
class Installer:
|
|
|
|
'''
|
|
|
|
The identification of a plugin language, compiler or interpreter
|
|
|
|
availability, and the install procedures.
|
|
|
|
'''
|
2023-04-07 00:04:44 +02:00
|
|
|
def __init__(self, name: str, mimetype: str,
|
|
|
|
exe: Union[str, None] = None,
|
|
|
|
compiler: Union[str, None] = None,
|
|
|
|
manager: Union[str, None] = None,
|
|
|
|
entry: Union[str, None] = None):
|
2023-01-17 20:00:52 +01:00
|
|
|
self.name = name
|
|
|
|
self.mimetype = mimetype
|
2023-02-28 19:51:56 +01:00
|
|
|
self.entries = []
|
|
|
|
if entry:
|
|
|
|
self.entries.append(entry)
|
2023-01-17 20:00:52 +01:00
|
|
|
self.exe = exe # interpreter (if required)
|
2023-02-28 19:51:56 +01:00
|
|
|
self.compiler = compiler # compiler bin (if required)
|
2023-01-17 20:00:52 +01:00
|
|
|
self.manager = manager # dependency manager (if required)
|
|
|
|
self.dependency_file = None
|
|
|
|
self.dependency_call = None
|
|
|
|
if self.executable():
|
|
|
|
global INSTALLERS
|
|
|
|
if not INSTALLERS:
|
|
|
|
INSTALLERS = {}
|
|
|
|
INSTALLERS[self.name] = self
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return (f'<Installer {self.name}: mimetype: {self.mimetype}, '
|
|
|
|
f'exe: {self.exe}, manager: {self.manager}>')
|
|
|
|
|
2023-04-07 00:04:44 +02:00
|
|
|
def executable(self) -> bool:
|
2023-02-28 19:51:56 +01:00
|
|
|
'''Validate the necessary bins are available to execute the plugin.'''
|
2023-01-17 20:00:52 +01:00
|
|
|
if self.exe:
|
|
|
|
if shutil.which(self.exe):
|
|
|
|
# This should arguably not be checked here.
|
|
|
|
if self.manager:
|
|
|
|
if shutil.which(self.manager):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return False
|
2023-04-07 00:04:44 +02:00
|
|
|
return True
|
2023-01-17 20:00:52 +01:00
|
|
|
|
2023-04-07 00:04:44 +02:00
|
|
|
def installable(self) -> bool:
|
2023-02-28 19:51:56 +01:00
|
|
|
'''Validate the necessary compiler and package manager executables are
|
|
|
|
available to install. If these are defined, they are considered
|
|
|
|
mandatory even though the user may have the requisite packages already
|
|
|
|
installed.'''
|
2023-01-17 20:00:52 +01:00
|
|
|
if self.compiler and not shutil.which(self.compiler):
|
|
|
|
return False
|
|
|
|
if self.manager and not shutil.which(self.manager):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-02-28 19:51:56 +01:00
|
|
|
def add_entrypoint(self, entry: str):
|
|
|
|
self.entries.append(entry)
|
|
|
|
|
|
|
|
def add_dependency_file(self, dep: str):
|
|
|
|
self.dependency_file = dep
|
|
|
|
|
|
|
|
def add_dependency_call(self, call: list):
|
|
|
|
if self.dependency_call is None:
|
|
|
|
self.dependency_call = []
|
|
|
|
self.dependency_call.append(call)
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
return copy.deepcopy(self)
|
|
|
|
|
2023-01-17 20:00:52 +01:00
|
|
|
|
2022-10-06 21:48:52 +02:00
|
|
|
class InstInfo:
|
|
|
|
def __init__(self, name, url, git_url):
|
|
|
|
self.name = name
|
|
|
|
self.repo = url # Used for 'git clone'
|
|
|
|
self.git_url = git_url # API access for github repos
|
|
|
|
self.entry = None
|
|
|
|
self.deps = None
|
|
|
|
self.subdir = None
|
|
|
|
self.commit = None
|
|
|
|
|
|
|
|
def __repr__(self):
|
2023-04-12 21:27:34 +02:00
|
|
|
return (f'InstInfo({self.name}, {self.repo}, {self.git_url}, '
|
2022-10-25 18:07:23 +02:00
|
|
|
f'{self.entry}, {self.deps})')
|
2022-10-06 21:48:52 +02:00
|
|
|
|
2023-04-07 00:04:44 +02:00
|
|
|
def get_inst_details(self) -> bool:
|
2022-10-06 21:48:52 +02:00
|
|
|
"""
|
|
|
|
Populate installation details from a github repo url.
|
|
|
|
Return True if all data is found.
|
|
|
|
"""
|
2023-04-06 02:28:17 +02:00
|
|
|
if "api.github.com" in self.git_url:
|
|
|
|
# This lets us redirect to handle blackbox testing
|
2023-04-12 21:27:34 +02:00
|
|
|
redir_addr = (API_GITHUB_COM +
|
|
|
|
self.git_url.split("api.github.com")[-1])
|
2023-04-06 02:28:17 +02:00
|
|
|
r = urlopen(redir_addr, timeout=5)
|
|
|
|
else:
|
|
|
|
r = urlopen(self.git_url, timeout=5)
|
2022-11-08 16:56:32 +01:00
|
|
|
if r.status != 200:
|
2022-10-06 21:48:52 +02:00
|
|
|
return False
|
|
|
|
if 'git/tree' in self.git_url:
|
2022-11-09 15:23:25 +01:00
|
|
|
tree = json.loads(r.read().decode())['tree']
|
2022-10-06 21:48:52 +02:00
|
|
|
else:
|
2022-11-09 15:23:25 +01:00
|
|
|
tree = json.loads(r.read().decode())
|
2023-02-28 19:51:56 +01:00
|
|
|
for g in entry_guesses(self.name):
|
2022-10-06 21:48:52 +02:00
|
|
|
for f in tree:
|
2023-04-12 20:14:00 +02:00
|
|
|
if f['path'].lower() == g.lower():
|
|
|
|
self.entry = f['path']
|
2022-10-06 21:48:52 +02:00
|
|
|
break
|
|
|
|
if self.entry is not None:
|
|
|
|
break
|
|
|
|
if self.entry is None:
|
|
|
|
for g in unsupported_entry(self.name):
|
|
|
|
for f in tree:
|
|
|
|
if f['path'] == g:
|
|
|
|
# FIXME: This should be easier to implement
|
|
|
|
print(f'entrypoint {g} is not yet supported')
|
|
|
|
return False
|
2023-02-28 19:51:56 +01:00
|
|
|
for name, inst in INSTALLERS.items():
|
|
|
|
# FIXME: Allow multiple depencencies
|
2022-10-06 21:48:52 +02:00
|
|
|
for f in tree:
|
2023-02-28 19:51:56 +01:00
|
|
|
if f['path'] == inst.dependency_file:
|
|
|
|
return True
|
2022-10-06 21:48:52 +02:00
|
|
|
if not self.entry:
|
|
|
|
return False
|
|
|
|
if not self.deps:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-11-08 19:57:56 +01:00
|
|
|
def create_dir(r: int, directory: PosixPath) -> bool:
|
2022-10-06 21:48:52 +02:00
|
|
|
"""Creation of a directory at path `d` with a maximum new dir depth `r`"""
|
2022-11-08 19:57:56 +01:00
|
|
|
if directory.exists():
|
2022-10-06 21:48:52 +02:00
|
|
|
return True
|
2022-11-08 19:57:56 +01:00
|
|
|
if r <= 0:
|
2022-10-06 21:48:52 +02:00
|
|
|
return False
|
2022-11-08 19:57:56 +01:00
|
|
|
if create_dir(r-1, directory.parent):
|
2022-10-06 21:48:52 +02:00
|
|
|
os.mkdir(directory, 0o777)
|
|
|
|
print(f'created directory {directory}')
|
2022-11-08 19:57:56 +01:00
|
|
|
assert directory.exists()
|
2022-10-21 20:51:13 +02:00
|
|
|
return True
|
2023-04-12 21:27:34 +02:00
|
|
|
return False
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def remove_dir(target: str) -> bool:
|
2022-10-06 21:48:52 +02:00
|
|
|
try:
|
|
|
|
shutil.rmtree(target)
|
|
|
|
return True
|
|
|
|
except NotADirectoryError:
|
|
|
|
print(f"Tried to remove directory {target} that does not exist.")
|
|
|
|
except PermissionError:
|
|
|
|
print(f"Permission denied removing dir: {target}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class Config():
|
|
|
|
"""A generic class for procuring, reading and editing config files"""
|
2022-10-21 20:51:13 +02:00
|
|
|
def obtain_config(self,
|
|
|
|
config_path: str,
|
|
|
|
default_text: str,
|
|
|
|
warn: bool = False) -> str:
|
2022-10-06 21:48:52 +02:00
|
|
|
"""Return a config file from the desired location. Create one with
|
|
|
|
default_text if it cannot be found."""
|
|
|
|
if isinstance(config_path, type(None)):
|
|
|
|
raise Exception("Generic config must be passed a config_path.")
|
|
|
|
assert isinstance(config_path, str)
|
|
|
|
# FIXME: warn if reckless dir exists, but conf not found
|
2022-10-24 23:00:26 +02:00
|
|
|
if Path(config_path).exists():
|
2022-10-06 21:48:52 +02:00
|
|
|
with open(config_path, 'r+') as f:
|
|
|
|
config_content = f.readlines()
|
|
|
|
return config_content
|
|
|
|
print(f'config file not found: {config_path}')
|
2022-10-06 23:02:01 +02:00
|
|
|
if warn:
|
|
|
|
confirm = input('press [Y] to create one now.\n').upper() == 'Y'
|
|
|
|
else:
|
|
|
|
confirm = True
|
|
|
|
if not confirm:
|
2022-10-06 21:48:52 +02:00
|
|
|
sys.exit(1)
|
2022-10-24 23:00:26 +02:00
|
|
|
parent_path = Path(config_path).parent
|
2022-10-06 21:48:52 +02:00
|
|
|
# Create up to one parent in the directory tree.
|
|
|
|
if create_dir(1, parent_path):
|
|
|
|
with open(self.conf_fp, 'w') as f:
|
|
|
|
f.write(default_text)
|
|
|
|
# FIXME: Handle write failure
|
|
|
|
return default_text
|
|
|
|
else:
|
2023-04-12 21:27:34 +02:00
|
|
|
logging.debug('could not create the parent directory ' +
|
|
|
|
parent_path)
|
2022-10-21 16:35:07 +02:00
|
|
|
raise FileNotFoundError('invalid parent directory')
|
2022-10-06 21:48:52 +02:00
|
|
|
|
2022-12-08 20:10:34 +01:00
|
|
|
def editConfigFile(self, addline: Union[str, None],
|
|
|
|
removeline: Union[str, None]):
|
|
|
|
"""Idempotent function to add and/or remove a single line each."""
|
2022-10-06 21:48:52 +02:00
|
|
|
remove_these_lines = []
|
|
|
|
with open(self.conf_fp, 'r') as reckless_conf:
|
|
|
|
original = reckless_conf.readlines()
|
|
|
|
empty_lines = []
|
2022-12-08 20:10:34 +01:00
|
|
|
write_required = False
|
2022-10-06 21:48:52 +02:00
|
|
|
for n, l in enumerate(original):
|
2022-12-08 20:10:34 +01:00
|
|
|
if removeline and l.strip() == removeline.strip():
|
|
|
|
write_required = True
|
2022-10-06 21:48:52 +02:00
|
|
|
remove_these_lines.append(n)
|
|
|
|
continue
|
|
|
|
if l.strip() == '':
|
|
|
|
empty_lines.append(n)
|
|
|
|
if n-1 in empty_lines:
|
|
|
|
# The white space is getting excessive.
|
|
|
|
remove_these_lines.append(n)
|
|
|
|
continue
|
2022-12-08 20:10:34 +01:00
|
|
|
if not addline:
|
|
|
|
return
|
|
|
|
# No write necessary if addline is already in config.
|
|
|
|
if not write_required:
|
|
|
|
for line in original:
|
|
|
|
if line.strip() == addline.strip():
|
|
|
|
return
|
2022-10-06 21:48:52 +02:00
|
|
|
with open(self.conf_fp, 'w') as conf_write:
|
|
|
|
# no need to write if passed 'None'
|
|
|
|
line_exists = not bool(addline)
|
|
|
|
for n, l in enumerate(original):
|
|
|
|
if n not in remove_these_lines:
|
|
|
|
if n > 0:
|
|
|
|
conf_write.write(f'\n{l.strip()}')
|
|
|
|
else:
|
|
|
|
conf_write.write(l.strip())
|
2022-12-05 19:16:40 +01:00
|
|
|
if addline.strip() == l.strip():
|
2022-10-06 21:48:52 +02:00
|
|
|
# addline is idempotent
|
|
|
|
line_exists = True
|
|
|
|
if not line_exists:
|
|
|
|
conf_write.write(f'\n{addline}')
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def __init__(self, path: Union[str, None] = None,
|
|
|
|
default_text: Union[str, None] = None,
|
|
|
|
warn: bool = False):
|
2022-10-06 21:48:52 +02:00
|
|
|
assert path is not None
|
|
|
|
assert default_text is not None
|
|
|
|
self.conf_fp = path
|
2022-10-06 23:02:01 +02:00
|
|
|
self.content = self.obtain_config(self.conf_fp, default_text,
|
|
|
|
warn=warn)
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RecklessConfig(Config):
|
|
|
|
"""Reckless config (by default, specific to the bitcoin network only.)
|
|
|
|
This is inherited by the lightningd config and contains all reckless
|
|
|
|
maintained plugins."""
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def enable_plugin(self, plugin_path: str):
|
2022-10-06 21:48:52 +02:00
|
|
|
"""Handle persistent plugin loading via config"""
|
|
|
|
self.editConfigFile(f'plugin={plugin_path}',
|
|
|
|
f'disable-plugin={plugin_path}')
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def disable_plugin(self, plugin_path: str):
|
2022-10-06 21:48:52 +02:00
|
|
|
"""Handle persistent plugin disabling via config"""
|
|
|
|
self.editConfigFile(f'disable-plugin={plugin_path}',
|
|
|
|
f'plugin={plugin_path}')
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def __init__(self, path: Union[str, None] = None,
|
|
|
|
default_text: Union[str, None] = None):
|
2022-10-06 21:48:52 +02:00
|
|
|
if path is None:
|
2022-11-08 19:57:56 +01:00
|
|
|
path = Path(LIGHTNING_DIR) / 'reckless' / 'bitcoin-reckless.conf'
|
2022-10-06 21:48:52 +02:00
|
|
|
if default_text is None:
|
2022-10-25 18:07:23 +02:00
|
|
|
default_text = (
|
|
|
|
'# This configuration file is managed by reckless to activate '
|
|
|
|
'and disable\n# reckless-installed plugins\n\n'
|
|
|
|
)
|
2022-10-06 21:48:52 +02:00
|
|
|
Config.__init__(self, path=str(path), default_text=default_text)
|
2022-10-24 23:00:26 +02:00
|
|
|
self.reckless_dir = Path(path).parent
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LightningBitcoinConfig(Config):
|
|
|
|
"""lightningd config specific to the bitcoin network. This is inherited by
|
|
|
|
the main lightningd config and in turn, inherits bitcoin-reckless.conf."""
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def __init__(self, path: Union[str, None] = None,
|
|
|
|
default_text: Union[str, None] = None,
|
|
|
|
warn: bool = True):
|
2022-10-06 21:48:52 +02:00
|
|
|
if path is None:
|
2022-10-24 23:00:26 +02:00
|
|
|
path = Path(LIGHTNING_DIR).joinpath('bitcoin', 'config')
|
2022-10-06 21:48:52 +02:00
|
|
|
if default_text is None:
|
|
|
|
default_text = "# This config was autopopulated by reckless\n\n"
|
|
|
|
Config.__init__(self, path=str(path),
|
2022-10-06 23:02:01 +02:00
|
|
|
default_text=default_text, warn=warn)
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class InferInstall():
|
|
|
|
"""Once a plugin is installed, we may need its directory and entrypoint"""
|
2022-10-21 20:51:13 +02:00
|
|
|
def __init__(self, name: str):
|
2022-10-06 21:48:52 +02:00
|
|
|
reck_contents = os.listdir(RECKLESS_CONFIG.reckless_dir)
|
2023-04-12 20:14:00 +02:00
|
|
|
reck_contents_lower = {}
|
|
|
|
for f in reck_contents:
|
|
|
|
reck_contents_lower.update({f.lower(): f})
|
2023-04-06 21:37:24 +02:00
|
|
|
|
|
|
|
def match_name(name) -> str:
|
|
|
|
for tier in range(0, 10):
|
|
|
|
# Look for each installers preferred entrypoint format first
|
|
|
|
for n, inst in INSTALLERS.items():
|
|
|
|
fmt = inst.entries[tier]
|
|
|
|
if '{name}' in fmt:
|
|
|
|
pre = fmt.split('{name}')[0]
|
|
|
|
post = fmt.split('{name}')[-1]
|
|
|
|
if name.startswith(pre) and name.endswith(post):
|
|
|
|
return name.lstrip(pre).rstrip(post)
|
|
|
|
else:
|
|
|
|
if fmt == name:
|
|
|
|
return name
|
|
|
|
return name
|
|
|
|
|
|
|
|
name = match_name(name)
|
2023-04-12 20:14:00 +02:00
|
|
|
if name.lower() in reck_contents_lower.keys():
|
|
|
|
actual_name = reck_contents_lower[name.lower()]
|
|
|
|
self.dir = Path(RECKLESS_CONFIG.reckless_dir).joinpath(actual_name)
|
2022-10-06 21:48:52 +02:00
|
|
|
else:
|
|
|
|
raise Exception(f"Could not find a reckless directory for {name}")
|
2023-04-12 20:14:00 +02:00
|
|
|
plug_dir = Path(RECKLESS_CONFIG.reckless_dir).joinpath(actual_name)
|
|
|
|
for guess in entry_guesses(actual_name):
|
2022-10-24 23:00:26 +02:00
|
|
|
for content in plug_dir.iterdir():
|
|
|
|
if content.name == guess:
|
|
|
|
self.entry = str(content)
|
2023-04-12 20:14:00 +02:00
|
|
|
self.name = actual_name
|
2022-10-24 23:00:26 +02:00
|
|
|
return
|
2022-10-06 21:48:52 +02:00
|
|
|
raise Exception(f'plugin entrypoint not found in {self.dir}')
|
|
|
|
|
|
|
|
|
2023-01-17 20:00:52 +01:00
|
|
|
INSTALLERS = {}
|
|
|
|
Installer('python3pip', 'text/x-python', exe='python3',
|
|
|
|
manager='pip', entry='{name}.py')
|
|
|
|
INSTALLERS['python3pip'].add_entrypoint('{name}')
|
|
|
|
INSTALLERS['python3pip'].add_entrypoint('__init__.py')
|
|
|
|
INSTALLERS['python3pip'].add_dependency_file('requirements.txt')
|
|
|
|
INSTALLERS['python3pip'].add_dependency_call(['pip', 'install', '-r',
|
|
|
|
'requirements.txt'])
|
|
|
|
|
|
|
|
INSTALLERS['python3pip3'] = INSTALLERS['python3pip'].copy()
|
|
|
|
INSTALLERS['python3pip3'].manager = 'pip3'
|
|
|
|
INSTALLERS['python3pip3'].dependency_call = [['pip3', 'install', '-r',
|
|
|
|
'requirements.txt']]
|
|
|
|
|
|
|
|
# Nodejs plugin installer
|
|
|
|
Installer('nodejs', 'application/javascript', exe='node', manager='npm',
|
|
|
|
entry='{name}.js')
|
|
|
|
INSTALLERS['nodejs'].add_entrypoint('{name}')
|
|
|
|
INSTALLERS['nodejs'].add_dependency_call(['npm', 'install', '--omit=dev'])
|
|
|
|
INSTALLERS['nodejs'].add_dependency_file('package.json')
|
|
|
|
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def help_alias(targets: list):
|
|
|
|
if len(targets) == 0:
|
2022-10-06 21:48:52 +02:00
|
|
|
parser.print_help(sys.stdout)
|
2022-10-20 21:43:55 +02:00
|
|
|
else:
|
2022-10-21 20:51:13 +02:00
|
|
|
print('try "reckless {} -h"'.format(' '.join(targets)))
|
2022-10-20 21:43:55 +02:00
|
|
|
sys.exit(1)
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
2023-04-07 00:04:44 +02:00
|
|
|
def _search_repo(name: str, url: str) -> Union[InstInfo, None]:
|
2022-10-06 21:48:52 +02:00
|
|
|
"""look in given repo and, if found, populate InstInfo"""
|
|
|
|
# Remove api subdomain, subdirectories, etc.
|
|
|
|
repo = url.split('/')
|
|
|
|
while '' in repo:
|
|
|
|
repo.remove('')
|
|
|
|
repo_name = None
|
2022-10-28 18:02:42 +02:00
|
|
|
parsed_url = urlparse(url)
|
|
|
|
if 'github.com' not in parsed_url.netloc:
|
|
|
|
# FIXME: Handle non-github repos.
|
2023-04-07 00:04:44 +02:00
|
|
|
return None
|
2022-10-28 18:02:42 +02:00
|
|
|
if len(parsed_url.path.split('/')) < 2:
|
2023-04-07 00:04:44 +02:00
|
|
|
return None
|
2022-10-28 18:02:42 +02:00
|
|
|
start = 1
|
|
|
|
# Maybe we were passed an api.github.com/repo/<user> url
|
|
|
|
if 'api' in parsed_url.netloc:
|
|
|
|
start += 1
|
|
|
|
repo_user = parsed_url.path.split('/')[start]
|
|
|
|
repo_name = parsed_url.path.split('/')[start + 1]
|
|
|
|
|
2022-10-06 21:48:52 +02:00
|
|
|
# Get details from the github API.
|
2023-04-06 02:28:17 +02:00
|
|
|
api_url = f'{API_GITHUB_COM}/repos/{repo_user}/{repo_name}/contents/'
|
2022-10-06 21:48:52 +02:00
|
|
|
plugins_cont = api_url
|
2022-11-09 15:23:25 +01:00
|
|
|
r = urlopen(plugins_cont, timeout=5)
|
2022-11-08 16:56:32 +01:00
|
|
|
if r.status != 200:
|
2023-04-12 21:27:34 +02:00
|
|
|
print(f"Plugin repository {api_url} unavailable")
|
2023-04-07 00:04:44 +02:00
|
|
|
return None
|
2022-10-06 21:48:52 +02:00
|
|
|
# Repo is for this plugin
|
2023-04-12 20:14:00 +02:00
|
|
|
if repo_name.lower() == name.lower():
|
|
|
|
MyPlugin = InstInfo(repo_name,
|
2022-10-28 18:02:42 +02:00
|
|
|
f'https://github.com/{repo_user}/{repo_name}',
|
|
|
|
api_url)
|
2022-10-06 21:48:52 +02:00
|
|
|
if not MyPlugin.get_inst_details():
|
2023-04-07 00:04:44 +02:00
|
|
|
return None
|
2022-10-06 21:48:52 +02:00
|
|
|
return MyPlugin
|
|
|
|
# Repo contains multiple plugins?
|
2022-11-09 15:23:25 +01:00
|
|
|
for x in json.loads(r.read().decode()):
|
2023-04-12 20:14:00 +02:00
|
|
|
if x["name"].lower() == name.lower():
|
2022-10-06 21:48:52 +02:00
|
|
|
# Look for the rest of the install details
|
|
|
|
# These are in lightningd/plugins directly
|
|
|
|
if 'lightningd/plugins/' in x['html_url']:
|
2023-04-12 20:14:00 +02:00
|
|
|
MyPlugin = InstInfo(x['name'],
|
2022-10-06 21:48:52 +02:00
|
|
|
'https://github.com/lightningd/plugins',
|
|
|
|
x['git_url'])
|
|
|
|
MyPlugin.subdir = x['name']
|
|
|
|
# submodules from another github repo
|
|
|
|
else:
|
2023-04-12 20:14:00 +02:00
|
|
|
MyPlugin = InstInfo(x['name'], x['html_url'], x['git_url'])
|
2022-10-06 21:48:52 +02:00
|
|
|
# Submodule URLs are appended with /tree/<commit hash>
|
|
|
|
if MyPlugin.repo.split('/')[-2] == 'tree':
|
|
|
|
MyPlugin.commit = MyPlugin.repo.split('/')[-1]
|
|
|
|
MyPlugin.repo = MyPlugin.repo.split('/tree/')[0]
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'repo using commit: {MyPlugin.commit}')
|
2022-10-06 21:48:52 +02:00
|
|
|
if not MyPlugin.get_inst_details():
|
2023-04-12 21:27:34 +02:00
|
|
|
logging.debug((f'Found plugin in {url}, but missing install '
|
|
|
|
'details'))
|
2023-04-07 00:04:44 +02:00
|
|
|
return None
|
2022-10-06 21:48:52 +02:00
|
|
|
return MyPlugin
|
2023-04-07 00:04:44 +02:00
|
|
|
return None
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def _install_plugin(src: InstInfo) -> bool:
|
2022-10-06 21:48:52 +02:00
|
|
|
"""make sure the repo exists and clone it."""
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'Install requested from {src}.')
|
2022-10-06 21:48:52 +02:00
|
|
|
if RECKLESS_CONFIG is None:
|
|
|
|
print('error: reckless install directory unavailable')
|
|
|
|
sys.exit(2)
|
2022-11-09 15:23:25 +01:00
|
|
|
|
2022-10-06 21:48:52 +02:00
|
|
|
# Use a unique directory for each cloned repo.
|
|
|
|
clone_path = 'reckless-{}'.format(str(hash(os.times()))[-9:])
|
2022-11-08 19:57:56 +01:00
|
|
|
clone_path = Path(tempfile.gettempdir()) / clone_path
|
|
|
|
inst_path = Path(RECKLESS_CONFIG.reckless_dir) / src.name
|
2022-10-24 23:00:26 +02:00
|
|
|
if Path(clone_path).exists():
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'{clone_path} already exists - deleting')
|
2022-10-06 21:48:52 +02:00
|
|
|
shutil.rmtree(clone_path)
|
|
|
|
# clone git repository to /tmp/reckless-...
|
|
|
|
if ('http' in src.repo[:4]) or ('github.com' in src.repo):
|
2023-04-06 02:28:17 +02:00
|
|
|
if 'github.com' in src.repo:
|
|
|
|
url = f"{GITHUB_COM}" + src.repo.split("github.com")[-1]
|
|
|
|
else:
|
|
|
|
url = src.repo
|
2023-04-14 20:16:50 +02:00
|
|
|
git = Popen(['git', 'clone', url, str(clone_path)],
|
|
|
|
stdout=PIPE, stderr=PIPE)
|
2022-10-06 21:48:52 +02:00
|
|
|
git.wait()
|
|
|
|
if git.returncode != 0:
|
2023-04-14 20:16:50 +02:00
|
|
|
logging.debug(git.stderr.read().decode())
|
2022-10-24 23:00:26 +02:00
|
|
|
if Path(clone_path).exists():
|
2022-10-06 21:48:52 +02:00
|
|
|
remove_dir(clone_path)
|
|
|
|
print('Error: Failed to clone repo')
|
|
|
|
return False
|
|
|
|
plugin_path = clone_path
|
|
|
|
if src.subdir is not None:
|
2022-11-08 19:57:56 +01:00
|
|
|
plugin_path = Path(clone_path) / src.subdir
|
2022-10-06 21:48:52 +02:00
|
|
|
if src.commit:
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f"Checking out commit {src.commit}")
|
2022-10-06 21:48:52 +02:00
|
|
|
checkout = Popen(['git', 'checkout', src.commit],
|
2023-04-06 02:03:47 +02:00
|
|
|
cwd=str(plugin_path), stdout=PIPE, stderr=PIPE)
|
2022-10-06 21:48:52 +02:00
|
|
|
checkout.wait()
|
|
|
|
if checkout.returncode != 0:
|
|
|
|
print(f'failed to checkout referenced commit {src.commit}')
|
|
|
|
return False
|
2022-10-19 00:44:53 +02:00
|
|
|
|
2023-04-06 17:45:09 +02:00
|
|
|
# Find a suitable installer
|
|
|
|
for name, inst_method in INSTALLERS.items():
|
|
|
|
if not (inst_method.installable() and inst_method.executable()):
|
|
|
|
continue
|
|
|
|
if inst_method.dependency_file is not None:
|
|
|
|
if inst_method.dependency_file not in os.listdir(plugin_path):
|
|
|
|
continue
|
|
|
|
logging.debug(f"using installer {name}")
|
|
|
|
INSTALLER = inst_method
|
|
|
|
break
|
|
|
|
# try it out
|
|
|
|
if INSTALLER and INSTALLER.dependency_call:
|
|
|
|
for call in INSTALLER.dependency_call:
|
|
|
|
logging.debug(f"Install: invoking '{call}'")
|
|
|
|
if logging.root.level < logging.WARNING:
|
|
|
|
pip = Popen(call, cwd=plugin_path, text=True)
|
|
|
|
else:
|
|
|
|
pip = Popen(call, cwd=plugin_path, stdout=PIPE, stderr=PIPE,
|
|
|
|
text=True)
|
|
|
|
pip.wait()
|
|
|
|
# FIXME: handle output of multiple calls
|
|
|
|
|
2022-10-06 21:48:52 +02:00
|
|
|
if pip.returncode == 0:
|
|
|
|
print('dependencies installed successfully')
|
|
|
|
else:
|
|
|
|
print('error encountered installing dependencies')
|
2023-04-14 20:16:50 +02:00
|
|
|
if pip.stdout:
|
|
|
|
logging.debug(pip.stdout.read())
|
2022-10-06 21:48:52 +02:00
|
|
|
return False
|
|
|
|
test_log = []
|
2023-04-12 20:14:00 +02:00
|
|
|
try:
|
|
|
|
test = run([Path(plugin_path).joinpath(src.entry)],
|
|
|
|
cwd=str(plugin_path), stdout=PIPE, stderr=PIPE,
|
|
|
|
text=True, timeout=3)
|
2022-10-06 21:48:52 +02:00
|
|
|
for line in test.stderr:
|
|
|
|
test_log.append(line.strip('\n'))
|
2023-04-12 20:14:00 +02:00
|
|
|
returncode = test.returncode
|
2023-04-06 17:45:09 +02:00
|
|
|
except TimeoutExpired:
|
2023-04-12 20:14:00 +02:00
|
|
|
# If the plugin is still running, it's assumed to be okay.
|
|
|
|
returncode = 0
|
|
|
|
pass
|
|
|
|
if returncode != 0:
|
|
|
|
logging.debug("plugin testing error:")
|
|
|
|
for line in test_log:
|
|
|
|
logging.debug(f' {line}')
|
|
|
|
print('plugin testing failed')
|
|
|
|
return False
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
# Find this cute little plugin a forever home
|
2023-04-06 02:03:47 +02:00
|
|
|
shutil.copytree(str(plugin_path), inst_path)
|
2022-10-06 21:48:52 +02:00
|
|
|
print(f'plugin installed: {inst_path}')
|
|
|
|
remove_dir(clone_path)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-10-21 22:21:35 +02:00
|
|
|
def install(plugin_name: str):
|
|
|
|
"""downloads plugin from source repos, installs and activates plugin"""
|
|
|
|
assert isinstance(plugin_name, str)
|
|
|
|
src = search(plugin_name)
|
2022-10-06 21:48:52 +02:00
|
|
|
if src:
|
2023-04-12 20:14:00 +02:00
|
|
|
logging.debug(f'Retrieving {src.name} from {src.repo}')
|
2022-10-06 21:48:52 +02:00
|
|
|
if not _install_plugin(src):
|
|
|
|
print('installation aborted')
|
|
|
|
sys.exit(1)
|
2023-04-12 20:14:00 +02:00
|
|
|
|
|
|
|
# Match case of the containing directory
|
|
|
|
for dirname in os.listdir(RECKLESS_CONFIG.reckless_dir):
|
|
|
|
if dirname.lower() == src.name.lower():
|
|
|
|
inst_path = Path(RECKLESS_CONFIG.reckless_dir)
|
|
|
|
inst_path = inst_path / dirname / src.entry
|
|
|
|
RECKLESS_CONFIG.enable_plugin(inst_path)
|
|
|
|
enable(src.name)
|
|
|
|
return
|
|
|
|
print(('dynamic activation failed: '
|
|
|
|
f'{src.name} not found in reckless directory'))
|
|
|
|
sys.exit(1)
|
2022-10-21 22:21:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def uninstall(plugin_name: str):
|
|
|
|
"""disables plugin and deletes the plugin's reckless dir"""
|
|
|
|
assert isinstance(plugin_name, str)
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'Uninstalling plugin {plugin_name}')
|
2022-10-21 22:21:35 +02:00
|
|
|
disable(plugin_name)
|
2023-04-12 20:14:00 +02:00
|
|
|
inst = InferInstall(plugin_name)
|
|
|
|
if not Path(inst.entry).exists():
|
|
|
|
print(f'cannot find installed plugin at expected path {inst.entry}')
|
|
|
|
sys.exit(1)
|
|
|
|
logging.debug(f'looking for {str(Path(inst.entry).parent)}')
|
|
|
|
if remove_dir(str(Path(inst.entry).parent)):
|
|
|
|
print(f"{inst.name} uninstalled successfully.")
|
2022-10-21 22:21:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def search(plugin_name: str) -> InstInfo:
|
|
|
|
"""searches plugin index for plugin"""
|
2022-10-06 21:48:52 +02:00
|
|
|
ordered_repos = RECKLESS_SOURCES
|
|
|
|
for r in RECKLESS_SOURCES:
|
2022-10-21 22:21:35 +02:00
|
|
|
# Search repos named after the plugin first
|
2022-10-06 21:48:52 +02:00
|
|
|
if r.split('/')[-1].lower() == plugin_name.lower():
|
|
|
|
ordered_repos.remove(r)
|
|
|
|
ordered_repos.insert(0, r)
|
|
|
|
for r in ordered_repos:
|
|
|
|
p = _search_repo(plugin_name, r)
|
|
|
|
if p:
|
|
|
|
print(f"found {p.name} in repo: {p.repo}")
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f"entry: {p.entry}")
|
2022-10-06 21:48:52 +02:00
|
|
|
if p.subdir:
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'sub-directory: {p.subdir}')
|
2022-10-06 21:48:52 +02:00
|
|
|
return p
|
|
|
|
print(f'Unable to locate source for plugin {plugin_name}')
|
|
|
|
|
|
|
|
|
2022-10-26 21:03:44 +02:00
|
|
|
class RPCError(Exception):
|
|
|
|
"""lightning-cli fails to connect to lightningd RPC"""
|
|
|
|
def __init__(self, err):
|
|
|
|
self.err = err
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'RPCError({self.err})'
|
|
|
|
|
|
|
|
|
|
|
|
class CLIError(Exception):
|
|
|
|
"""lightningd error response"""
|
|
|
|
def __init__(self, code, message):
|
|
|
|
self.code = code
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'CLIError({self.code} {self.message})'
|
|
|
|
|
|
|
|
|
|
|
|
def lightning_cli(*args, timeout=15) -> dict:
|
|
|
|
# CLI commands will be added to any necessary options
|
|
|
|
cmd = LIGHTNING_CLI_CALL.copy()
|
|
|
|
cmd.extend(args)
|
|
|
|
clncli = Popen(cmd, stdout=PIPE, stderr=PIPE)
|
|
|
|
clncli.wait(timeout=timeout)
|
|
|
|
out = clncli.stdout.read().decode()
|
|
|
|
if len(out) > 0 and out[0] == '{':
|
|
|
|
# If all goes well, a json object is typically returned
|
|
|
|
out = json.loads(out.replace('\n', ''))
|
2022-10-06 21:48:52 +02:00
|
|
|
else:
|
2022-10-26 21:03:44 +02:00
|
|
|
# help, -V, etc. may not return json, so stash it here.
|
|
|
|
out = {'content': out}
|
|
|
|
if clncli.returncode == 0:
|
|
|
|
return out
|
|
|
|
if clncli.returncode == 1:
|
|
|
|
# RPC doesn't like our input
|
|
|
|
# output contains 'code' and 'message'
|
|
|
|
raise CLIError(out['code'], out['message'])
|
|
|
|
if clncli.returncode == 2:
|
|
|
|
# RPC not available - lightningd not running or using alternate config
|
|
|
|
err = clncli.stderr.read().decode()
|
|
|
|
raise RPCError(err)
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
2022-10-21 22:21:35 +02:00
|
|
|
def enable(plugin_name: str):
|
|
|
|
"""dynamically activates plugin and adds to config (persistent)"""
|
|
|
|
assert isinstance(plugin_name, str)
|
2022-10-06 21:48:52 +02:00
|
|
|
inst = InferInstall(plugin_name)
|
|
|
|
path = inst.entry
|
2022-10-24 23:00:26 +02:00
|
|
|
if not Path(path).exists():
|
2022-10-26 21:03:44 +02:00
|
|
|
print(f'cannot find installed plugin at expected path {path}')
|
2022-10-06 21:48:52 +02:00
|
|
|
sys.exit(1)
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'activating {plugin_name}')
|
2022-10-26 21:03:44 +02:00
|
|
|
try:
|
|
|
|
lightning_cli('plugin', 'start', path)
|
|
|
|
except CLIError as err:
|
|
|
|
if 'already registered' in err.message:
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'{inst.name} is already running')
|
2022-10-06 21:48:52 +02:00
|
|
|
else:
|
|
|
|
print(f'reckless: {inst.name} failed to start!')
|
2022-10-26 21:03:44 +02:00
|
|
|
raise err
|
|
|
|
except RPCError:
|
2023-04-12 21:27:34 +02:00
|
|
|
logging.debug(('lightningd rpc unavailable. '
|
|
|
|
'Skipping dynamic activation.'))
|
2022-10-26 21:03:44 +02:00
|
|
|
RECKLESS_CONFIG.enable_plugin(path)
|
2023-04-12 20:14:00 +02:00
|
|
|
print(f'{inst.name} enabled')
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
2022-10-21 22:21:35 +02:00
|
|
|
def disable(plugin_name: str):
|
2022-10-06 21:48:52 +02:00
|
|
|
"""reckless disable <plugin>
|
|
|
|
deactivates an installed plugin"""
|
2022-10-21 22:21:35 +02:00
|
|
|
assert isinstance(plugin_name, str)
|
2022-10-06 21:48:52 +02:00
|
|
|
inst = InferInstall(plugin_name)
|
|
|
|
path = inst.entry
|
2022-10-24 23:00:26 +02:00
|
|
|
if not Path(path).exists():
|
2022-10-06 21:48:52 +02:00
|
|
|
sys.stderr.write(f'Could not find plugin at {path}\n')
|
|
|
|
sys.exit(1)
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug(f'deactivating {plugin_name}')
|
2022-10-26 21:03:44 +02:00
|
|
|
try:
|
|
|
|
lightning_cli('plugin', 'stop', path)
|
|
|
|
except CLIError as err:
|
|
|
|
if err.code == -32602:
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug('plugin not currently running')
|
2022-10-26 21:03:44 +02:00
|
|
|
else:
|
|
|
|
print('lightning-cli plugin stop failed')
|
|
|
|
raise err
|
|
|
|
except RPCError:
|
2023-04-12 21:27:34 +02:00
|
|
|
logging.debug(('lightningd rpc unavailable. '
|
|
|
|
'Skipping dynamic deactivation.'))
|
2022-10-06 21:48:52 +02:00
|
|
|
RECKLESS_CONFIG.disable_plugin(path)
|
2023-04-12 20:14:00 +02:00
|
|
|
print(f'{inst.name} disabled')
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def load_config(reckless_dir: Union[str, None] = None,
|
|
|
|
network: str = 'bitcoin') -> Config:
|
2022-10-06 21:48:52 +02:00
|
|
|
"""Initial directory discovery and config file creation."""
|
2022-10-19 00:24:10 +02:00
|
|
|
net_conf = None
|
2022-10-27 00:22:26 +02:00
|
|
|
# Does the lightning-cli already reference an explicit config?
|
2022-10-26 21:03:44 +02:00
|
|
|
try:
|
|
|
|
active_config = lightning_cli('listconfigs', timeout=3)
|
|
|
|
if 'conf' in active_config:
|
|
|
|
net_conf = LightningBitcoinConfig(path=active_config['conf'])
|
|
|
|
except RPCError:
|
|
|
|
pass
|
2022-10-06 21:48:52 +02:00
|
|
|
if reckless_dir is None:
|
2022-11-08 19:57:56 +01:00
|
|
|
reckless_dir = Path(LIGHTNING_DIR) / 'reckless'
|
2022-10-06 21:48:52 +02:00
|
|
|
else:
|
|
|
|
if not os.path.isabs(reckless_dir):
|
2022-11-08 19:57:56 +01:00
|
|
|
reckless_dir = Path.cwd() / reckless_dir
|
2022-10-27 00:22:26 +02:00
|
|
|
if LIGHTNING_CONFIG:
|
|
|
|
network_path = LIGHTNING_CONFIG
|
|
|
|
else:
|
2022-11-08 19:57:56 +01:00
|
|
|
network_path = Path(LIGHTNING_DIR) / network / 'config'
|
|
|
|
reck_conf_path = Path(reckless_dir) / f'{network}-reckless.conf'
|
2022-10-27 00:22:26 +02:00
|
|
|
if net_conf:
|
|
|
|
if str(network_path) != net_conf.conf_fp:
|
|
|
|
print('error: reckless configuration does not match lightningd:\n'
|
|
|
|
f'reckless network config path: {network_path}\n'
|
|
|
|
f'lightningd active config: {net_conf.conf_fp}')
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
# The network-specific config file (bitcoin by default)
|
|
|
|
net_conf = LightningBitcoinConfig(path=network_path)
|
2022-10-06 21:48:52 +02:00
|
|
|
# Reckless manages plugins here.
|
2022-10-21 16:35:07 +02:00
|
|
|
try:
|
|
|
|
reckless_conf = RecklessConfig(path=reck_conf_path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print('Error: reckless config file could not be written: ',
|
|
|
|
str(reck_conf_path))
|
2022-10-06 21:48:52 +02:00
|
|
|
sys.exit(1)
|
|
|
|
if not net_conf:
|
|
|
|
print('Error: could not load or create the network specific lightningd'
|
|
|
|
' config (default .lightning/bitcoin)')
|
|
|
|
sys.exit(1)
|
|
|
|
net_conf.editConfigFile(f'include {reckless_conf.conf_fp}', None)
|
|
|
|
return reckless_conf
|
|
|
|
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def get_sources_file() -> str:
|
2022-11-08 19:57:56 +01:00
|
|
|
return Path(RECKLESS_DIR) / '.sources'
|
2022-10-06 21:48:52 +02:00
|
|
|
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def sources_from_file() -> list:
|
2022-10-06 21:48:52 +02:00
|
|
|
sources_file = get_sources_file()
|
|
|
|
read_sources = []
|
|
|
|
with open(sources_file, 'r') as f:
|
|
|
|
for src in f.readlines():
|
|
|
|
if len(src.strip()) > 0:
|
|
|
|
read_sources.append(src.strip())
|
|
|
|
return read_sources
|
|
|
|
|
|
|
|
|
2022-10-21 20:51:13 +02:00
|
|
|
def loadSources() -> list:
|
2022-10-21 22:21:35 +02:00
|
|
|
"""Look for the repo sources file."""
|
2022-10-06 21:48:52 +02:00
|
|
|
sources_file = get_sources_file()
|
|
|
|
# This would have been created if possible
|
2022-10-24 23:00:26 +02:00
|
|
|
if not Path(sources_file).exists():
|
2022-11-09 15:28:15 +01:00
|
|
|
logging.debug('Warning: Reckless requires write access')
|
2022-11-02 22:09:23 +01:00
|
|
|
Config(path=str(sources_file),
|
2022-10-06 21:48:52 +02:00
|
|
|
default_text='https://github.com/lightningd/plugins')
|
|
|
|
return ['https://github.com/lightningd/plugins']
|
|
|
|
return sources_from_file()
|
|
|
|
|
|
|
|
|
2022-10-21 22:21:35 +02:00
|
|
|
def add_source(src: str):
|
|
|
|
"""Additional git repositories, directories, etc. are passed here."""
|
2022-10-06 21:48:52 +02:00
|
|
|
assert isinstance(src, str)
|
2022-10-21 22:21:35 +02:00
|
|
|
# Is it a file?
|
2022-10-06 21:48:52 +02:00
|
|
|
maybe_path = os.path.realpath(src)
|
2022-10-24 23:00:26 +02:00
|
|
|
if Path(maybe_path).exists():
|
2022-10-06 21:48:52 +02:00
|
|
|
# FIXME: This should handle either a directory or a git repo
|
|
|
|
if os.path.isdir(maybe_path):
|
|
|
|
print(f'Plugin source directory found: {maybe_path}')
|
|
|
|
elif 'github.com' in src:
|
|
|
|
my_file = Config(path=str(get_sources_file()),
|
|
|
|
default_text='https://github.com/lightningd/plugins')
|
|
|
|
my_file.editConfigFile(src, None)
|
|
|
|
|
|
|
|
|
2022-10-21 22:21:35 +02:00
|
|
|
def remove_source(src: str):
|
|
|
|
"""Remove a source from the sources file."""
|
2022-10-06 21:48:52 +02:00
|
|
|
assert isinstance(src, str)
|
|
|
|
if src in sources_from_file():
|
|
|
|
my_file = Config(path=get_sources_file(),
|
|
|
|
default_text='https://github.com/lightningd/plugins')
|
|
|
|
my_file.editConfigFile(None, src)
|
|
|
|
print('plugin source removed')
|
|
|
|
else:
|
|
|
|
print(f'source not found: {src}')
|
|
|
|
|
|
|
|
|
|
|
|
def list_source():
|
2022-10-21 22:21:35 +02:00
|
|
|
"""Provide the user with all stored source repositories."""
|
2022-10-06 21:48:52 +02:00
|
|
|
for src in sources_from_file():
|
|
|
|
print(src)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
# This default depends on the .lightning directory
|
|
|
|
parser.add_argument('-d', '--reckless-dir',
|
|
|
|
help='specify a data directory for reckless to use',
|
|
|
|
type=str, default=None)
|
|
|
|
parser.add_argument('-l', '--lightning',
|
|
|
|
help='lightning data directory (default:~/.lightning)',
|
|
|
|
type=str,
|
|
|
|
default=Path.home().joinpath('.lightning'))
|
2022-10-27 00:22:26 +02:00
|
|
|
parser.add_argument('-c', '--conf',
|
|
|
|
help=' config file used by lightningd',
|
|
|
|
type=str,
|
|
|
|
default=None)
|
2022-10-06 21:48:52 +02:00
|
|
|
parser.add_argument('-r', '--regtest', action='store_true')
|
2023-04-12 21:27:34 +02:00
|
|
|
parser.add_argument('--network',
|
|
|
|
help="specify a network to use (default: bitcoin)",
|
2023-04-06 02:11:19 +02:00
|
|
|
type=str)
|
2022-12-05 18:07:32 +01:00
|
|
|
parser.add_argument('-v', '--verbose', action="store_const",
|
2023-04-12 21:27:34 +02:00
|
|
|
dest="loglevel", const=logging.DEBUG,
|
|
|
|
default=logging.WARNING)
|
2022-10-20 20:32:52 +02:00
|
|
|
cmd1 = parser.add_subparsers(dest='cmd1', help='command',
|
|
|
|
required=True)
|
|
|
|
|
|
|
|
install_cmd = cmd1.add_parser('install', help='search for and install a '
|
|
|
|
'plugin, then test and activate')
|
|
|
|
install_cmd.add_argument('targets', type=str, nargs='*')
|
|
|
|
install_cmd.set_defaults(func=install)
|
|
|
|
|
|
|
|
uninstall_cmd = cmd1.add_parser('uninstall', help='deactivate a plugin '
|
|
|
|
'and remove it from the directory')
|
|
|
|
uninstall_cmd.add_argument('targets', type=str, nargs='*')
|
|
|
|
uninstall_cmd.set_defaults(func=uninstall)
|
|
|
|
|
|
|
|
search_cmd = cmd1.add_parser('search', help='search for a plugin from '
|
|
|
|
'the available source repositories')
|
|
|
|
search_cmd.add_argument('targets', type=str, nargs='*')
|
|
|
|
search_cmd.set_defaults(func=search)
|
|
|
|
|
|
|
|
enable_cmd = cmd1.add_parser('enable', help='dynamically enable a plugin '
|
|
|
|
'and update config')
|
|
|
|
enable_cmd.add_argument('targets', type=str, nargs='*')
|
|
|
|
enable_cmd.set_defaults(func=enable)
|
|
|
|
disable_cmd = cmd1.add_parser('disable', help='disable a plugin')
|
|
|
|
disable_cmd.add_argument('targets', type=str, nargs='*')
|
|
|
|
disable_cmd.set_defaults(func=disable)
|
|
|
|
source_parser = cmd1.add_parser('source', help='manage plugin search '
|
|
|
|
'sources')
|
|
|
|
source_subs = source_parser.add_subparsers(dest='source_subs',
|
|
|
|
required=True)
|
|
|
|
list_parse = source_subs.add_parser('list', help='list available plugin '
|
|
|
|
'sources (repositories)')
|
|
|
|
list_parse.set_defaults(func=list_source)
|
|
|
|
source_add = source_subs.add_parser('add', help='add a source repository')
|
|
|
|
source_add.add_argument('targets', type=str, nargs='*')
|
|
|
|
source_add.set_defaults(func=add_source)
|
|
|
|
source_rem = source_subs.add_parser('remove', aliases=['rem', 'rm'],
|
|
|
|
help='remove a plugin source '
|
|
|
|
'repository')
|
|
|
|
source_rem.add_argument('targets', type=str, nargs='*')
|
|
|
|
source_rem.set_defaults(func=remove_source)
|
|
|
|
|
2022-10-20 21:43:55 +02:00
|
|
|
help_cmd = cmd1.add_parser('help', help='for contextual help, use '
|
|
|
|
'"reckless <cmd> -h"')
|
2022-10-20 20:32:52 +02:00
|
|
|
help_cmd.add_argument('targets', type=str, nargs='*')
|
2022-10-20 21:43:55 +02:00
|
|
|
help_cmd.set_defaults(func=help_alias)
|
2022-10-20 20:32:52 +02:00
|
|
|
|
2022-10-06 21:48:52 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-10-20 20:32:52 +02:00
|
|
|
NETWORK = 'regtest' if args.regtest else 'bitcoin'
|
2023-04-06 02:11:19 +02:00
|
|
|
SUPPORTED_NETWORKS = ['bitcoin', 'regtest', 'liquid', 'liquid-regtest',
|
|
|
|
'litecoin', 'signet', 'testnet']
|
|
|
|
if args.network:
|
|
|
|
if args.network in SUPPORTED_NETWORKS:
|
|
|
|
NETWORK = args.network
|
|
|
|
else:
|
|
|
|
print(f"Error: {args.network} network not supported")
|
2022-10-20 20:32:52 +02:00
|
|
|
LIGHTNING_DIR = Path(args.lightning)
|
2023-04-06 02:28:17 +02:00
|
|
|
# This env variable is set under CI testing
|
|
|
|
LIGHTNING_CLI_CALL = [os.environ.get('LIGHTNING_CLI')]
|
2023-04-12 21:27:34 +02:00
|
|
|
if LIGHTNING_CLI_CALL == [None]:
|
2023-04-06 02:28:17 +02:00
|
|
|
LIGHTNING_CLI_CALL = ['lightning-cli']
|
2022-10-20 20:32:52 +02:00
|
|
|
if NETWORK != 'bitcoin':
|
|
|
|
LIGHTNING_CLI_CALL.append(f'--network={NETWORK}')
|
|
|
|
if LIGHTNING_DIR != Path.home().joinpath('.lightning'):
|
|
|
|
LIGHTNING_CLI_CALL.append(f'--lightning-dir={LIGHTNING_DIR}')
|
|
|
|
if args.reckless_dir:
|
|
|
|
RECKLESS_DIR = args.reckless_dir
|
|
|
|
else:
|
2022-11-08 19:57:56 +01:00
|
|
|
RECKLESS_DIR = Path(LIGHTNING_DIR) / 'reckless'
|
2022-10-27 00:22:26 +02:00
|
|
|
LIGHTNING_CONFIG = args.conf
|
2022-10-20 20:32:52 +02:00
|
|
|
RECKLESS_CONFIG = load_config(reckless_dir=RECKLESS_DIR,
|
|
|
|
network=NETWORK)
|
|
|
|
RECKLESS_SOURCES = loadSources()
|
2023-04-06 02:28:17 +02:00
|
|
|
API_GITHUB_COM = 'https://api.github.com'
|
|
|
|
GITHUB_COM = 'https://github.com'
|
|
|
|
# Used for blackbox testing to avoid hitting github servers
|
|
|
|
if 'REDIR_GITHUB_API' in os.environ:
|
|
|
|
API_GITHUB_COM = os.environ['REDIR_GITHUB_API']
|
|
|
|
if 'REDIR_GITHUB' in os.environ:
|
|
|
|
GITHUB_COM = os.environ['REDIR_GITHUB']
|
2022-12-05 18:07:32 +01:00
|
|
|
logging.root.setLevel(args.loglevel)
|
2022-10-20 20:32:52 +02:00
|
|
|
|
|
|
|
if 'targets' in args:
|
2022-10-21 22:21:35 +02:00
|
|
|
# FIXME: Catch missing argument
|
|
|
|
if args.func.__name__ == 'help_alias':
|
|
|
|
args.func(args.targets)
|
|
|
|
sys.exit(0)
|
|
|
|
for target in args.targets:
|
|
|
|
args.func(target)
|
2022-10-20 20:32:52 +02:00
|
|
|
else:
|
|
|
|
args.func()
|