xxh/xxhp

111 lines
4 KiB
Text
Raw Normal View History

2020-03-10 17:07:01 +00:00
#!/usr/bin/env xonsh
2020-03-11 10:47:09 +00:00
import os, sys, argparse, re
2020-03-10 17:07:01 +00:00
from sys import exit
class XxhPackage(object):
def __init__(self):
self.local_xxh_home_dir = p'~/.xxh'
self.quiet = False
2020-03-11 10:47:09 +00:00
parser = argparse.ArgumentParser(description='xxhp is xxh package manager.',
2020-03-10 17:07:01 +00:00
usage='xxhp <command>\n\n'
2020-03-13 09:43:30 +00:00
+ ' install i Install xxh package from https://github.com/xxh\n'
+ ' list l List of installed packages\n'
+ ' remove r Remove xxh package\n\n'
+ ' reinstall ri Reinstall xxh package\n\n'
2020-03-11 10:47:09 +00:00
+ 'Try `./xxhp <command> --help` to get more info.\n')
2020-03-10 17:07:01 +00:00
parser.add_argument('command', help='Command to run')
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
print('Unrecognized command\n')
parser.print_help()
sys.exit(1)
2020-03-10 19:27:01 +00:00
mkdir -p @(self.local_xxh_home_dir / 'xxh/shells') @(self.local_xxh_home_dir / 'xxh/plugins')
2020-03-10 17:07:01 +00:00
getattr(self, args.command)()
def eprint(self, *args, **kwargs):
if not self.quiet:
print(*args, file=sys.stderr, **kwargs)
def eeprint(self, *args, **kwargs):
if not self.quiet:
print(*args, file=sys.stderr, **kwargs)
exit(1)
def i(self):
return self.install()
def install(self):
parser = argparse.ArgumentParser(description='')
parser.add_argument('package', help=f"xxh-package")
parser.add_argument('-v', '--verbose', action='store_true', help=f"Verbose mode")
parser.usage = parser.format_usage().replace('usage: xxhp ', 'xxhp install ')
opt = parser.parse_args(sys.argv[2:])
2020-03-11 10:47:09 +00:00
if not re.match('^[a-zA-Z-]+$', opt.package):
self.eeprint(f'Invalid package name: {opt.package}')
2020-03-10 17:07:01 +00:00
subdir = self.package_subdir(opt.package) or self.eeprint(f"Unknown package: {opt.package}")
package_git_url = f'https://github.com/xxh/{opt.package}'
self.eprint(f"Git clone {package_git_url}")
package_dir = self.local_xxh_home_dir/'xxh'/subdir/opt.package
r = ![git clone -q @(package_git_url) @(package_dir) 1>&2]
if r.returncode != 0:
self.eeprint(f'Git clone error')
self.eprint(f"Build {opt.package}")
@(package_dir/'build.xsh') 1>&2
self.eprint(f"Installed {opt.package}")
def r(self):
return self.remove()
def remove(self):
parser = argparse.ArgumentParser(description='')
parser.add_argument('package', help=f"xxh-package")
parser.add_argument('-v', '--verbose', action='store_true', help=f"Verbose mode")
parser.usage = parser.format_usage().replace('usage: xxhp ', 'xxhp remove ')
opt = parser.parse_args(sys.argv[2:])
subdir = self.package_subdir(opt.package) or self.eeprint(f"Unknown package: {opt.package}")
package_dir = self.local_xxh_home_dir / 'xxh' / subdir / opt.package
if package_dir.exists():
rm -rf @(package_dir)
self.eprint(f"Removed {package_dir}")
else:
self.eeprint(f"Package not found: {package_dir}")
2020-03-13 09:43:30 +00:00
def ri(self):
return self.reinstall()
def reinstall(self):
self.remove()
return self.install()
2020-03-10 19:27:01 +00:00
def l(self):
return self.list()
def list(self):
parser = argparse.ArgumentParser(description='')
parser.add_argument('-v', '--verbose', action='store_true', help=f"Verbose mode")
parser.usage = parser.format_usage().replace('usage: xxhp ', 'xxhp remove ')
opt = parser.parse_args(sys.argv[2:])
packages_dir = (self.local_xxh_home_dir / 'xxh').glob('**/xxh-*')
for p in sorted(packages_dir):
print(p.name)
2020-03-10 17:07:01 +00:00
def package_subdir(self, name):
if 'xxh-shell' in name:
return 'shells'
elif 'xxh-plugin' in name:
return 'plugins'
return None
if __name__ == '__main__':
XxhPackage()