2023-02-08 14:27:58 +00:00
|
|
|
"""Cliget - install cli tools in you user profile
|
|
|
|
|
|
|
|
Usage:
|
2023-02-09 11:39:36 +00:00
|
|
|
cliget [-v] [-c URL] list
|
|
|
|
cliget [-v] [-c URL] search PAT
|
2023-02-15 17:24:14 +00:00
|
|
|
cliget [-v] [-c URL] install TOOL ...
|
2023-02-09 11:39:36 +00:00
|
|
|
cliget [-v] [-c URL] update [--all] [TOOL ...]
|
2023-02-08 14:27:58 +00:00
|
|
|
|
|
|
|
list list all installed tools
|
2023-02-09 11:39:36 +00:00
|
|
|
search search for tools in the catalog with the given pattern
|
2023-02-15 17:24:14 +00:00
|
|
|
install TOOL install some tools
|
2023-02-08 14:27:58 +00:00
|
|
|
update list all updatable tools
|
|
|
|
update TOOL update tools
|
|
|
|
update --all update all updatable tools
|
|
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help
|
|
|
|
-c, --catalog URL
|
2023-02-09 11:39:36 +00:00
|
|
|
-v, --verbose
|
2023-02-08 14:27:58 +00:00
|
|
|
"""
|
|
|
|
|
2023-02-15 17:24:14 +00:00
|
|
|
import logging
|
2023-02-09 11:39:36 +00:00
|
|
|
import sys, os
|
2023-02-08 14:27:58 +00:00
|
|
|
from docopt import docopt
|
2023-02-09 11:39:36 +00:00
|
|
|
from yaml import load, SafeLoader
|
|
|
|
from semver import VersionInfo
|
|
|
|
import re
|
2023-02-08 14:27:58 +00:00
|
|
|
from subprocess import run, CalledProcessError, TimeoutExpired
|
2023-02-09 11:39:36 +00:00
|
|
|
from fuzzywuzzy import fuzz
|
2023-02-15 17:24:14 +00:00
|
|
|
import requests
|
2023-02-09 11:39:36 +00:00
|
|
|
|
2023-02-15 17:24:14 +00:00
|
|
|
def trace(*mess):
|
|
|
|
if "TRACE" in os.environ:
|
|
|
|
print("TRACE", mess)
|
|
|
|
|
|
|
|
def warn(*mess):
|
|
|
|
print(f"WARN {mess[0]}")
|
2023-02-08 14:27:58 +00:00
|
|
|
|
|
|
|
class DotDict(dict):
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return self[name] if name in self else None
|
|
|
|
|
|
|
|
def loadcatalog(options)->dict:
|
2023-02-09 11:39:36 +00:00
|
|
|
catalog=options.get('__catalog', 'catalog.yaml')
|
2023-02-15 17:24:14 +00:00
|
|
|
o = load(open(catalog), SafeLoader)
|
|
|
|
trace(o)
|
|
|
|
return { k:DotDict(v) for k,v in o.items()}
|
2023-02-08 14:27:58 +00:00
|
|
|
|
2023-02-09 11:39:36 +00:00
|
|
|
def find_semver(s:str) -> VersionInfo:
|
|
|
|
ver = VersionInfo(0,0,0)
|
|
|
|
try:
|
|
|
|
ver = VersionInfo.parse(s)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
ver = VersionInfo(*list(i.group(0) for i in re.finditer('\d+', s))[:3])
|
|
|
|
except Exception as e:
|
2023-02-15 17:24:14 +00:00
|
|
|
trace("parse error", e)
|
2023-02-09 11:39:36 +00:00
|
|
|
return ver
|
|
|
|
|
2023-02-08 14:27:58 +00:00
|
|
|
|
|
|
|
_version = lambda cmd: run([cmd, '--version'], input='', text=True, capture_output=True, check=True, timeout=0.1).stdout.split('\n')[0]
|
|
|
|
|
2023-02-09 11:39:36 +00:00
|
|
|
def _internal_list(options) -> tuple[str,VersionInfo]:
|
|
|
|
"""list installed tools and their version"""
|
2023-02-08 14:27:58 +00:00
|
|
|
ctl = loadcatalog(options)
|
2023-02-09 11:39:36 +00:00
|
|
|
for cli, props in ctl.items():
|
2023-02-08 14:27:58 +00:00
|
|
|
# search in path
|
|
|
|
try:
|
2023-02-09 11:39:36 +00:00
|
|
|
vers = _version(cli)
|
2023-02-15 17:24:14 +00:00
|
|
|
trace(cli, vers)
|
2023-02-09 11:39:36 +00:00
|
|
|
yield cli, props, find_semver(vers)
|
2023-02-08 14:27:58 +00:00
|
|
|
except CalledProcessError:
|
2023-02-15 17:24:14 +00:00
|
|
|
trace(cli, "call error")
|
2023-02-08 14:27:58 +00:00
|
|
|
except TimeoutExpired:
|
2023-02-15 17:24:14 +00:00
|
|
|
trace(cli, "timeout")
|
2023-02-08 14:27:58 +00:00
|
|
|
except FileNotFoundError:
|
2023-02-15 17:24:14 +00:00
|
|
|
trace(cli, "not found")
|
2023-02-08 14:27:58 +00:00
|
|
|
|
2023-02-09 11:39:36 +00:00
|
|
|
def dolist(options):
|
|
|
|
for (cli, _, ver) in _internal_list(options):
|
|
|
|
print(cli, ver)
|
|
|
|
|
|
|
|
def dosearch(options):
|
|
|
|
pat = options.PAT
|
|
|
|
ctl = loadcatalog(options)
|
|
|
|
L = []
|
|
|
|
for cli, props in ctl.items():
|
2023-02-15 17:24:14 +00:00
|
|
|
trace(cli, props)
|
2023-02-09 11:39:36 +00:00
|
|
|
rtitle = fuzz.ratio(cli, pat)
|
|
|
|
rdesc = fuzz.partial_ratio(props['desc'], pat)
|
|
|
|
score = 2 * rtitle + rdesc
|
|
|
|
L.append((cli, props['desc'], score))
|
|
|
|
L = sorted(L, key=lambda x: -x[-1])
|
|
|
|
# TODO format a as table
|
|
|
|
print("\n".join("|".join(map(str,l)) for l in L[:10]))
|
|
|
|
|
|
|
|
def dolistupdate(options):
|
|
|
|
print("look for updatables")
|
|
|
|
for (cli, props, ver) in _internal_list(options):
|
|
|
|
# get last version online
|
|
|
|
if props.github:
|
|
|
|
pass
|
|
|
|
pass
|
2023-02-15 17:24:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _gh_version(repo:str) -> [VersionInfo|None]:
|
|
|
|
[owner, repo] = repo.split("/")
|
|
|
|
url = f'https://api.github.com/repos/{owner}/{repo}/releases/latest'
|
|
|
|
response = requests.get(url)
|
|
|
|
return response.json().get("name")
|
|
|
|
|
|
|
|
def doinstall(options):
|
|
|
|
tools = options.TOOL
|
|
|
|
ctl = loadcatalog(options)
|
|
|
|
for tool in tools:
|
|
|
|
if tool in ctl:
|
|
|
|
props = ctl[tool]
|
|
|
|
if props.github:
|
|
|
|
vers = _gh_version(props.github)
|
|
|
|
trace(vers)
|
|
|
|
else:
|
|
|
|
warn(f'{tool} not in catalog')
|
|
|
|
|
2023-02-09 11:39:36 +00:00
|
|
|
|
2023-02-08 14:27:58 +00:00
|
|
|
if __name__ == '__main__':
|
2023-02-15 17:24:14 +00:00
|
|
|
if "DEBUG" in os.environ:
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logging.debug("debug is on")
|
|
|
|
else:
|
|
|
|
logging.info("not in debug")
|
2023-02-08 14:27:58 +00:00
|
|
|
options = docopt(__doc__, version='Cliget 0.1.0')
|
2023-02-09 11:39:36 +00:00
|
|
|
options = DotDict({k.replace('-','_'):v for (k,v) in options.items() if v is not None})
|
2023-02-15 17:24:14 +00:00
|
|
|
trace(options)
|
2023-02-08 14:27:58 +00:00
|
|
|
if options.list:
|
|
|
|
dolist(options)
|
|
|
|
elif options.search:
|
|
|
|
dosearch(options)
|
2023-02-15 17:24:14 +00:00
|
|
|
elif options.install or options.update:
|
2023-02-09 11:39:36 +00:00
|
|
|
if not options.__all and len(options.TOOL)==0:
|
|
|
|
dolistupdate(options)
|
2023-02-15 17:24:14 +00:00
|
|
|
elif len(options.TOOL)>0:
|
|
|
|
doinstall(options)
|
2023-02-09 11:39:36 +00:00
|
|
|
else:
|
|
|
|
print("not implemented")
|