Skip to content

Commit 307baf7

Browse files
reckless: accept a full local path as source+name
This allows installing a local plugin directly without having to modify reckless sources. Changelog-changed: Reckless can be passed a local file or directory for installation.
1 parent d8713ce commit 307baf7

File tree

1 file changed

+65
-24
lines changed

1 file changed

+65
-24
lines changed

tools/reckless

+65-24
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,30 @@ def _install_plugin(src: InstInfo) -> Union[InstInfo, None]:
12981298
return staged_src
12991299

13001300

1301+
def location_from_name(plugin_name: str) -> (str, str):
1302+
"""Maybe the location was passed in place of the plugin name. Check
1303+
if this looks like a filepath or URL and return that as well as the
1304+
plugin name."""
1305+
if not Path(plugin_name).exists():
1306+
# No path included, return the name only.
1307+
return (None, plugin_name)
1308+
1309+
# Directory containing the plugin? The plugin name should match the dir.
1310+
if os.path.isdir(plugin_name):
1311+
return (Path(plugin_name).parent, Path(plugin_name).name)
1312+
1313+
# Possibly the entrypoint itself was passed?
1314+
elif os.path.isfile(plugin_name):
1315+
if Path(plugin_name).with_suffix('').name != Path(plugin_name).parent.name or \
1316+
not Path(plugin_name).parent.parent.exists():
1317+
# If the directory is not named for the plugin, we can't infer what
1318+
# should be done.
1319+
# FIXME: return InstInfo with entrypoint rather than source str.
1320+
return (None, plugin_name)
1321+
# We have to make inferences as to the naming here.
1322+
return (Path(plugin_name).parent.parent, Path(plugin_name).with_suffix('').name)
1323+
1324+
13011325
def install(plugin_name: str) -> Union[str, None]:
13021326
"""Downloads plugin from source repos, installs and activates plugin.
13031327
Returns the location of the installed plugin or "None" in the case of
@@ -1310,33 +1334,48 @@ def install(plugin_name: str) -> Union[str, None]:
13101334
else:
13111335
name = plugin_name
13121336
commit = None
1313-
log.debug(f"Searching for {name}")
1314-
if search(name):
1315-
global LAST_FOUND
1316-
src = LAST_FOUND
1317-
src.commit = commit
1318-
log.debug(f'Retrieving {src.name} from {src.source_loc}')
1319-
try:
1320-
installed = _install_plugin(src)
1321-
except FileExistsError as err:
1322-
log.error(f'File exists: {err.filename}')
1323-
return None
1324-
LAST_FOUND = None
1325-
if not installed:
1326-
log.warning(f'{plugin_name}: installation aborted')
1337+
# Is the install request specifying a path to the plugin?
1338+
direct_location, name = location_from_name(name)
1339+
if direct_location:
1340+
logging.debug(f"install of {name} requested from {direct_location}")
1341+
src = InstInfo(name, direct_location, None)
1342+
if not src.get_inst_details():
1343+
src = None
1344+
# Treating a local git repo as a directory allows testing
1345+
# uncommitted changes.
1346+
if src and src.srctype == Source.LOCAL_REPO:
1347+
src.srctype = Source.DIRECTORY
1348+
if not direct_location or not src:
1349+
log.debug(f"direct_location {direct_location}, src: {src}")
1350+
log.debug(f"Searching for {name}")
1351+
if search(name):
1352+
global LAST_FOUND
1353+
src = LAST_FOUND
1354+
src.commit = commit
1355+
log.debug(f'Retrieving {src.name} from {src.source_loc}')
1356+
else:
13271357
return None
13281358

1329-
# Match case of the containing directory
1330-
for dirname in os.listdir(RECKLESS_CONFIG.reckless_dir):
1331-
if dirname.lower() == installed.name.lower():
1332-
inst_path = Path(RECKLESS_CONFIG.reckless_dir)
1333-
inst_path = inst_path / dirname / installed.entry
1334-
RECKLESS_CONFIG.enable_plugin(inst_path)
1335-
enable(installed.name)
1336-
return f"{installed.source_loc}"
1337-
log.error(('dynamic activation failed: '
1338-
f'{installed.name} not found in reckless directory'))
1359+
try:
1360+
installed = _install_plugin(src)
1361+
except FileExistsError as err:
1362+
log.error(f'File exists: {err.filename}')
13391363
return None
1364+
LAST_FOUND = None
1365+
if not installed:
1366+
log.warning(f'{plugin_name}: installation aborted')
1367+
return None
1368+
1369+
# Match case of the containing directory
1370+
for dirname in os.listdir(RECKLESS_CONFIG.reckless_dir):
1371+
if dirname.lower() == installed.name.lower():
1372+
inst_path = Path(RECKLESS_CONFIG.reckless_dir)
1373+
inst_path = inst_path / dirname / installed.entry
1374+
RECKLESS_CONFIG.enable_plugin(inst_path)
1375+
enable(installed.name)
1376+
return f"{installed.source_loc}"
1377+
log.error(('dynamic activation failed: '
1378+
f'{installed.name} not found in reckless directory'))
13401379
return None
13411380

13421381

@@ -1386,6 +1425,8 @@ def search(plugin_name: str) -> Union[InstInfo, None]:
13861425
if srctype in [Source.DIRECTORY, Source.LOCAL_REPO,
13871426
Source.GITHUB_REPO, Source.OTHER_URL]:
13881427
found = _source_search(plugin_name, source)
1428+
if found:
1429+
log.debug(f"{found}, {found.srctype}")
13891430
if not found:
13901431
continue
13911432
log.info(f"found {found.name} in source: {found.source_loc}")

0 commit comments

Comments
 (0)