diff --git a/.gitignore b/.gitignore index 1682f1d..f523f52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build *~ ognibuild.egg-info +dist diff --git a/ognibuild/__init__.py b/ognibuild/__init__.py index f37d875..a9dfcac 100644 --- a/ognibuild/__init__.py +++ b/ognibuild/__init__.py @@ -225,17 +225,3 @@ def run_dist(session): run_with_build_fixer(session, ['make', 'dist']) raise NoBuildToolsFound() - - -class PlainSession(object): - """Session ignoring user.""" - - def create_home(self): - pass - - def check_call(self, args): - return subprocess.check_call(args) - - def Popen(self, args, stdout=None, stderr=None, user=None, cwd=None): - return subprocess.Popen( - args, stdout=stdout, stderr=stderr, cwd=cwd) diff --git a/ognibuild/__main__.py b/ognibuild/__main__.py index 2367354..d2b5f3a 100644 --- a/ognibuild/__main__.py +++ b/ognibuild/__main__.py @@ -17,7 +17,7 @@ import os import sys -from . import PlainSession, run_dist, NoBuildToolsFound, note +from . import run_dist, NoBuildToolsFound, note def main(): @@ -27,15 +27,24 @@ def main(): parser.add_argument( '--directory', '-d', type=str, help='Directory for project.', default='.') + parser.add_argument( + '--schroot', type=str, help='schroot to run in.') args = parser.parse_args() - session = PlainSession() - os.chdir(args.directory) - try: - if args.subcommand == 'dist': - run_dist(session) - except NoBuildToolsFound: - note('No build tools found.') - return 1 - return 0 + if args.schroot: + from .session.schroot import SchrootSession + session = SchrootSession(args.schroot) + else: + from .session.plain import PlainSession + session = PlainSession() + with session: + os.chdir(args.directory) + try: + if args.subcommand == 'dist': + run_dist(session) + except NoBuildToolsFound: + note('No build tools found.') + return 1 + return 0 + sys.exit(main()) diff --git a/ognibuild/session/plain.py b/ognibuild/session/plain.py new file mode 100644 index 0000000..deb27cb --- /dev/null +++ b/ognibuild/session/plain.py @@ -0,0 +1,35 @@ +#!/usr/bin/python +# Copyright (C) 2019-2020 Jelmer Vernooij +# encoding: utf-8 +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +from . import Session + +import subprocess + + +class PlainSession(Session): + """Session ignoring user.""" + + def create_home(self): + pass + + def check_call(self, args): + return subprocess.check_call(args) + + def Popen(self, args, stdout=None, stderr=None, user=None, cwd=None): + return subprocess.Popen( + args, stdout=stdout, stderr=stderr, cwd=cwd) diff --git a/ognibuild/schroot.py b/ognibuild/session/schroot.py similarity index 91% rename from ognibuild/schroot.py rename to ognibuild/session/schroot.py index cf09d5d..cb03f6f 100644 --- a/ognibuild/schroot.py +++ b/ognibuild/session/schroot.py @@ -21,7 +21,10 @@ import subprocess from typing import Optional, List, Dict -class Session(object): +from . import Session, SessionSetupFailure + + +class SchrootSession(Session): _cwd: Optional[str] _location: Optional[str] @@ -44,8 +47,12 @@ class Session(object): ['schroot', '-c', 'session:' + self.session_id, '-e']) def __enter__(self) -> 'Session': - self.session_id = subprocess.check_output( - ['schroot', '-c', self.chroot, '-b']).strip().decode() + try: + self.session_id = subprocess.check_output( + ['schroot', '-c', self.chroot, '-b']).strip().decode() + except subprocess.CalledProcessError: + # TODO(jelmer): Capture stderr and forward in SessionSetupFailure + raise SessionSetupFailure() return self def __exit__(self, exc_type, exc_val, exc_tb):