Move all app/os specific utils to specific package
This commit is contained in:
+3
-96
@@ -1,14 +1,12 @@
|
||||
from subprocess import CalledProcessError
|
||||
import subprocess
|
||||
from difflib import get_close_matches
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
from shlex import split
|
||||
|
||||
import os
|
||||
import pickle
|
||||
import re
|
||||
|
||||
from pathlib import Path
|
||||
import six
|
||||
from .types import Command
|
||||
|
||||
|
||||
DEVNULL = open(os.devnull, 'w')
|
||||
@@ -57,59 +55,6 @@ def wrap_settings(params):
|
||||
return decorator
|
||||
|
||||
|
||||
def sudo_support(fn):
|
||||
"""Removes sudo before calling fn and adds it after."""
|
||||
@wraps(fn)
|
||||
def wrapper(command, settings):
|
||||
if not command.script.startswith('sudo '):
|
||||
return fn(command, settings)
|
||||
|
||||
result = fn(Command(command.script[5:],
|
||||
command.stdout,
|
||||
command.stderr),
|
||||
settings)
|
||||
|
||||
if result and isinstance(result, six.string_types):
|
||||
return u'sudo {}'.format(result)
|
||||
elif isinstance(result, list):
|
||||
return [u'sudo {}'.format(x) for x in result]
|
||||
else:
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
|
||||
def git_support(fn):
|
||||
"""Resolves git aliases and supports testing for both git and hub."""
|
||||
@wraps(fn)
|
||||
def wrapper(command, settings):
|
||||
# supports GitHub's `hub` command
|
||||
# which is recommended to be used with `alias git=hub`
|
||||
# but at this point, shell aliases have already been resolved
|
||||
is_git_cmd = command.script.startswith(('git', 'hub'))
|
||||
|
||||
if not is_git_cmd:
|
||||
return False
|
||||
|
||||
# perform git aliases expansion
|
||||
if 'trace: alias expansion:' in command.stderr:
|
||||
search = re.search("trace: alias expansion: ([^ ]*) => ([^\n]*)",
|
||||
command.stderr)
|
||||
alias = search.group(1)
|
||||
|
||||
# by default git quotes everything, for example:
|
||||
# 'commit' '--amend'
|
||||
# which is surprising and does not allow to easily test for
|
||||
# eg. 'git commit'
|
||||
expansion = ' '.join(map(quote, split(search.group(2))))
|
||||
new_script = command.script.replace(alias, expansion)
|
||||
|
||||
command = Command._replace(command, script=new_script)
|
||||
|
||||
return fn(command, settings)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def memoize(fn):
|
||||
"""Caches previous calls to the function."""
|
||||
memo = {}
|
||||
@@ -187,41 +132,3 @@ def replace_command(command, broken, matched):
|
||||
new_cmds = get_close_matches(broken, matched, cutoff=0.1)
|
||||
return [replace_argument(command.script, broken, new_cmd.strip())
|
||||
for new_cmd in new_cmds]
|
||||
|
||||
|
||||
@memoize
|
||||
def get_pkgfile(command):
|
||||
""" Gets the packages that provide the given command using `pkgfile`.
|
||||
|
||||
If the command is of the form `sudo foo`, searches for the `foo` command
|
||||
instead.
|
||||
"""
|
||||
try:
|
||||
command = command.strip()
|
||||
|
||||
if command.startswith('sudo '):
|
||||
command = command[5:]
|
||||
|
||||
command = command.split(" ")[0]
|
||||
|
||||
packages = subprocess.check_output(
|
||||
['pkgfile', '-b', '-v', command],
|
||||
universal_newlines=True, stderr=DEVNULL
|
||||
).splitlines()
|
||||
|
||||
return [package.split()[0] for package in packages]
|
||||
except CalledProcessError:
|
||||
return None
|
||||
|
||||
|
||||
def archlinux_env():
|
||||
if which('yaourt'):
|
||||
pacman = 'yaourt'
|
||||
elif which('pacman'):
|
||||
pacman = 'sudo pacman'
|
||||
else:
|
||||
return False, None
|
||||
|
||||
enabled_by_default = which('pkgfile')
|
||||
|
||||
return enabled_by_default, pacman
|
||||
Reference in New Issue
Block a user