Run sudo.
This commit is contained in:
parent
6aaa2df421
commit
79de95e787
5 changed files with 30 additions and 24 deletions
|
@ -159,7 +159,7 @@ def main(): # noqa: C901
|
||||||
elif args.resolve == "native":
|
elif args.resolve == "native":
|
||||||
resolver = native_resolvers(session)
|
resolver = native_resolvers(session)
|
||||||
elif args.resolve == "auto":
|
elif args.resolve == "auto":
|
||||||
resolver = auto_resolver(session)
|
resolver = auto_resolver(session, explain=args.explain)
|
||||||
logging.info("Using requirement resolver: %s", resolver)
|
logging.info("Using requirement resolver: %s", resolver)
|
||||||
os.chdir(args.directory)
|
os.chdir(args.directory)
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -26,13 +26,15 @@ from buildlog_consultant.apt import (
|
||||||
)
|
)
|
||||||
|
|
||||||
from .. import DetailedFailure, UnidentifiedError
|
from .. import DetailedFailure, UnidentifiedError
|
||||||
from ..session import Session, run_with_tee
|
from ..session import Session, run_with_tee, get_user
|
||||||
from .file_search import FileSearcher, AptCachedContentsFileSearcher, GENERATED_FILE_SEARCHER, get_package_for_paths
|
from .file_search import FileSearcher, AptCachedContentsFileSearcher, GENERATED_FILE_SEARCHER, get_package_for_paths
|
||||||
|
|
||||||
|
|
||||||
def run_apt(session: Session, args: List[str]) -> None:
|
def run_apt(session: Session, args: List[str], prefix: Optional[List[str]] = None) -> None:
|
||||||
"""Run apt."""
|
"""Run apt."""
|
||||||
args = ["apt", "-y"] + args
|
if prefix is None:
|
||||||
|
prefix = []
|
||||||
|
args = prefix = ["apt", "-y"] + args
|
||||||
retcode, lines = run_with_tee(session, args, cwd="/", user="root")
|
retcode, lines = run_with_tee(session, args, cwd="/", user="root")
|
||||||
if retcode == 0:
|
if retcode == 0:
|
||||||
return
|
return
|
||||||
|
@ -49,10 +51,21 @@ class AptManager(object):
|
||||||
session: Session
|
session: Session
|
||||||
_searchers: Optional[List[FileSearcher]]
|
_searchers: Optional[List[FileSearcher]]
|
||||||
|
|
||||||
def __init__(self, session):
|
def __init__(self, session, prefix=None):
|
||||||
self.session = session
|
self.session = session
|
||||||
self._apt_cache = None
|
self._apt_cache = None
|
||||||
self._searchers = None
|
self._searchers = None
|
||||||
|
if prefix is None:
|
||||||
|
prefix = []
|
||||||
|
self.prefix = prefix
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_session(cls, session):
|
||||||
|
if get_user(session) != "root":
|
||||||
|
prefix = ["sudo"]
|
||||||
|
else:
|
||||||
|
prefix = []
|
||||||
|
return cls(session, prefix=prefix)
|
||||||
|
|
||||||
def searchers(self):
|
def searchers(self):
|
||||||
if self._searchers is None:
|
if self._searchers is None:
|
||||||
|
@ -94,10 +107,10 @@ class AptManager(object):
|
||||||
logging.info("Installing using apt: %r", packages)
|
logging.info("Installing using apt: %r", packages)
|
||||||
packages = self.missing(packages)
|
packages = self.missing(packages)
|
||||||
if packages:
|
if packages:
|
||||||
run_apt(self.session, ["install"] + packages)
|
run_apt(self.session, ["install"] + packages, prefix=self.prefix)
|
||||||
|
|
||||||
def satisfy(self, deps: List[str]) -> None:
|
def satisfy(self, deps: List[str]) -> None:
|
||||||
run_apt(self.session, ["satisfy"] + deps)
|
run_apt(self.session, ["satisfy"] + deps, prefix=self.prefix)
|
||||||
|
|
||||||
def satisfy_command(self, deps: List[str]) -> List[str]:
|
def satisfy_command(self, deps: List[str]) -> List[str]:
|
||||||
return ["apt", "satisfy"] + deps
|
return self.prefix + ["apt", "satisfy"] + deps
|
||||||
|
|
|
@ -275,28 +275,17 @@ def native_resolvers(session):
|
||||||
return StackedResolver([kls(session) for kls in NATIVE_RESOLVER_CLS])
|
return StackedResolver([kls(session) for kls in NATIVE_RESOLVER_CLS])
|
||||||
|
|
||||||
|
|
||||||
class ExplainResolver(Resolver):
|
def auto_resolver(session, explain=False):
|
||||||
def __init__(self, session):
|
|
||||||
self.session = session
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_session(cls, session):
|
|
||||||
return cls(session)
|
|
||||||
|
|
||||||
def install(self, requirements):
|
|
||||||
raise UnsatisfiedRequirements(requirements)
|
|
||||||
|
|
||||||
|
|
||||||
def auto_resolver(session):
|
|
||||||
# if session is SchrootSession or if we're root, use apt
|
# if session is SchrootSession or if we're root, use apt
|
||||||
from .apt import AptResolver
|
from .apt import AptResolver
|
||||||
from ..session.schroot import SchrootSession
|
from ..session.schroot import SchrootSession
|
||||||
|
from ..session import get_user
|
||||||
|
|
||||||
user = session.check_output(["echo", "$USER"]).decode().strip()
|
user = get_user(session)
|
||||||
resolvers = []
|
resolvers = []
|
||||||
# TODO(jelmer): Check VIRTUAL_ENV, and prioritize PypiResolver if
|
# TODO(jelmer): Check VIRTUAL_ENV, and prioritize PypiResolver if
|
||||||
# present?
|
# present?
|
||||||
if isinstance(session, SchrootSession) or user == "root":
|
if isinstance(session, SchrootSession) or user == "root" or explain:
|
||||||
resolvers.append(AptResolver.from_session(session))
|
resolvers.append(AptResolver.from_session(session))
|
||||||
resolvers.extend([kls(session) for kls in NATIVE_RESOLVER_CLS])
|
resolvers.extend([kls(session) for kls in NATIVE_RESOLVER_CLS])
|
||||||
return StackedResolver(resolvers)
|
return StackedResolver(resolvers)
|
||||||
|
|
|
@ -582,7 +582,7 @@ class AptResolver(Resolver):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_session(cls, session):
|
def from_session(cls, session):
|
||||||
return cls(AptManager(session))
|
return cls(AptManager.from_session(session))
|
||||||
|
|
||||||
def install(self, requirements):
|
def install(self, requirements):
|
||||||
missing = []
|
missing = []
|
||||||
|
|
|
@ -88,3 +88,7 @@ def run_with_tee(session: Session, args: List[str], **kwargs):
|
||||||
sys.stdout.buffer.flush()
|
sys.stdout.buffer.flush()
|
||||||
contents.append(line.decode("utf-8", "surrogateescape"))
|
contents.append(line.decode("utf-8", "surrogateescape"))
|
||||||
return p.returncode, contents
|
return p.returncode, contents
|
||||||
|
|
||||||
|
|
||||||
|
def get_user(session):
|
||||||
|
return session.check_output(["echo", "$USER"]).decode().strip()
|
||||||
|
|
Loading…
Add table
Reference in a new issue