1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 06:46:15 +01:00

Move print_msg() to the 'osc.output' module

This commit is contained in:
Daniel Mach 2024-03-05 15:40:37 +01:00
parent 27bf13a774
commit 866d178345
9 changed files with 48 additions and 41 deletions

View File

@ -1,7 +1,7 @@
from . import api from . import api
from .common import format_msg_project_package_options from .common import format_msg_project_package_options
from .common import print_msg
from .. import oscerr from .. import oscerr
from ..output import print_msg
def add_channels(apiurl, project, package=None, enable_all=False, skip_disabled=False, print_to="debug"): def add_channels(apiurl, project, package=None, enable_all=False, skip_disabled=False, print_to="debug"):

View File

@ -1,27 +1,4 @@
import sys from ..output.output import print_msg
def print_msg(*args, print_to="debug"):
from .. import conf
if print_to is None:
return
elif print_to == "debug":
# print a debug message to stderr if config["debug"] is set
if conf.config["debug"]:
print("DEBUG:", *args, file=sys.stderr)
elif print_to == "verbose":
# print a verbose message to stdout if config["verbose"] or config["debug"] is set
if conf.config["verbose"] or conf.config["debug"]:
print(*args)
elif print_to == "stdout":
# print the message to stdout
print(*args)
elif print_to == "stderr":
# print the message to stderr
print(*args, file=sys.stderr)
else:
raise ValueError(f"Invalid value of the 'print_to' option: {print_to}")
def format_msg_project_package_options( def format_msg_project_package_options(

View File

@ -21,6 +21,7 @@ from . import commandline
from . import conf as osc_conf from . import conf as osc_conf
from . import core as osc_core from . import core as osc_core
from . import oscerr from . import oscerr
from . import output
from .OscConfigParser import configparser from .OscConfigParser import configparser
from .oscssl import CertVerificationError from .oscssl import CertVerificationError
from .util.cpio import CpioError from .util.cpio import CpioError
@ -108,8 +109,8 @@ def run(prg, argv=None):
except AttributeError: except AttributeError:
body = '' body = ''
_private.print_msg(e.hdrs, print_to="debug") output.print_msg(e.hdrs, print_to="debug")
_private.print_msg(body, print_to="debug") output.print_msg(body, print_to="debug")
if e.code in [400, 403, 404, 500]: if e.code in [400, 403, 404, 500]:
if b'<summary>' in body: if b'<summary>' in body:
@ -153,7 +154,7 @@ def run(prg, argv=None):
print(e.message, file=sys.stderr) print(e.message, file=sys.stderr)
except oscerr.OscIOError as e: except oscerr.OscIOError as e:
print(e.msg, file=sys.stderr) print(e.msg, file=sys.stderr)
_private.print_msg(e.e, print_to="debug") output.print_msg(e.e, print_to="debug")
except (oscerr.WrongOptions, oscerr.WrongArgs) as e: except (oscerr.WrongOptions, oscerr.WrongArgs) as e:
print(e, file=sys.stderr) print(e, file=sys.stderr)
return 2 return 2

View File

@ -33,6 +33,7 @@ from . import commands as osc_commands
from . import conf from . import conf
from . import git_scm from . import git_scm
from . import oscerr from . import oscerr
from . import output
from . import store as osc_store from . import store as osc_store
from .core import * from .core import *
from .grabber import OscFileGrabber from .grabber import OscFileGrabber
@ -269,7 +270,7 @@ class MainCommand(Command):
def load_commands(self): def load_commands(self):
if IN_VENV: if IN_VENV:
_private.print_msg("Running in virtual environment, skipping loading plugins installed outside the virtual environment.", print_to="stderr") output.print_msg("Running in virtual environment, skipping loading plugins installed outside the virtual environment.", print_to="stderr")
for module_prefix, module_path in self.MODULES: for module_prefix, module_path in self.MODULES:
module_path = os.path.expanduser(module_path) module_path = os.path.expanduser(module_path)
@ -4315,7 +4316,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
except: except:
print('Error while checkout package:\n', package, file=sys.stderr) print('Error while checkout package:\n', package, file=sys.stderr)
_private.print_msg('Note: You can use "osc delete" or "osc submitpac" when done.\n', print_to="verbose") output.print_msg('Note: You can use "osc delete" or "osc submitpac" when done.\n', print_to="verbose")
@cmdln.alias('branchco') @cmdln.alias('branchco')
@cmdln.alias('bco') @cmdln.alias('bco')
@ -4466,7 +4467,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
if opts.checkout: if opts.checkout:
checkout_package(apiurl, targetprj, package, server_service_files=False, checkout_package(apiurl, targetprj, package, server_service_files=False,
expand_link=True, prj_dir=Path(targetprj)) expand_link=True, prj_dir=Path(targetprj))
_private.print_msg('Note: You can use "osc delete" or "osc submitpac" when done.\n', print_to="verbose") output.print_msg('Note: You can use "osc delete" or "osc submitpac" when done.\n', print_to="verbose")
else: else:
apiopt = '' apiopt = ''
if conf.get_configParser().get("general", "apiurl", fallback=None) != apiurl: if conf.get_configParser().get("general", "apiurl", fallback=None) != apiurl:
@ -10125,7 +10126,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
def _load_plugins(self): def _load_plugins(self):
if IN_VENV: if IN_VENV:
_private.print_msg("Running in virtual environment, skipping loading legacy plugins.", print_to="stderr") output.print_msg("Running in virtual environment, skipping loading legacy plugins.", print_to="stderr")
return return
plugin_dirs = [ plugin_dirs = [

View File

@ -21,10 +21,10 @@ import urllib3.response
import urllib3.util import urllib3.util
from . import __version__ from . import __version__
from . import _private
from . import conf from . import conf
from . import oscerr from . import oscerr
from . import oscssl from . import oscssl
from . import output
from .util.helper import decode_it from .util.helper import decode_it
@ -690,7 +690,7 @@ class SignatureAuthHandler(AuthHandlerBase):
return False return False
if not self.ssh_keygen_path: if not self.ssh_keygen_path:
_private.print_msg("Skipping signature auth because ssh-keygen is not available", print_to="debug") output.print_msg("Skipping signature auth because ssh-keygen is not available", print_to="debug")
return False return False
if not self.sshkey_known(): if not self.sshkey_known():

View File

@ -50,6 +50,7 @@ from . import _private
from . import conf from . import conf
from . import meter from . import meter
from . import oscerr from . import oscerr
from . import output
from . import store as osc_store from . import store as osc_store
from .connection import http_request, http_GET, http_POST, http_PUT, http_DELETE from .connection import http_request, http_GET, http_POST, http_PUT, http_DELETE
from .store import Store from .store import Store
@ -524,7 +525,7 @@ class Serviceinfo:
raise oscerr.PackageNotInstalled(f"obs-service-{cmd[0]}") raise oscerr.PackageNotInstalled(f"obs-service-{cmd[0]}")
cmd[0] = "/usr/lib/obs/service/" + cmd[0] cmd[0] = "/usr/lib/obs/service/" + cmd[0]
cmd = cmd + ["--outdir", temp_dir] cmd = cmd + ["--outdir", temp_dir]
_private.print_msg("Run source service:", " ".join(cmd), print_to="verbose") output.print_msg("Run source service:", " ".join(cmd), print_to="verbose")
r = run_external(*cmd) r = run_external(*cmd)
if r != 0: if r != 0:
@ -3893,7 +3894,7 @@ def set_devel_project(apiurl, prj, pac, devprj=None, devpac=None, print_to="debu
devprj, devprj,
devpac, devpac,
) )
_private.print_msg(msg, print_to=print_to) output.print_msg(msg, print_to=print_to)
package_obj = obs_api.Package.from_api(apiurl, prj, pac) package_obj = obs_api.Package.from_api(apiurl, prj, pac)
@ -4811,7 +4812,7 @@ def get_review_list(
xpath_base = xpath_join(xpath_base, 'action/source/@%(kind)s=\'%(val)s\'', op='or', inner=True) xpath_base = xpath_join(xpath_base, 'action/source/@%(kind)s=\'%(val)s\'', op='or', inner=True)
xpath = xpath_join(xpath, xpath_base % {'kind': kind, 'val': val}, op='and', nexpr_parentheses=True) xpath = xpath_join(xpath, xpath_base % {'kind': kind, 'val': val}, op='and', nexpr_parentheses=True)
_private.print_msg(f"[ {xpath} ]", print_to="debug") output.print_msg(f"[ {xpath} ]", print_to="debug")
res = search(apiurl, request=xpath) res = search(apiurl, request=xpath)
collection = res['request'] collection = res['request']
requests = [] requests = []
@ -4952,7 +4953,7 @@ def get_exact_request_list(
if req_type: if req_type:
xpath += f" and action/@type='{req_type}'" xpath += f" and action/@type='{req_type}'"
_private.print_msg(f"[ {xpath} ]", print_to="debug") output.print_msg(f"[ {xpath} ]", print_to="debug")
res = search(apiurl, request=xpath) res = search(apiurl, request=xpath)
collection = res['request'] collection = res['request']
@ -5625,7 +5626,7 @@ def checkout_package(
oldproj = None oldproj = None
if conf.config['checkout_rooted']: if conf.config['checkout_rooted']:
if prj_dir.stem == '/': if prj_dir.stem == '/':
_private.print_msg(f"checkout_rooted ignored for {prj_dir}", print_to="verbose") output.print_msg(f"checkout_rooted ignored for {prj_dir}", print_to="verbose")
# ?? should we complain if not is_project_dir(prj_dir) ?? # ?? should we complain if not is_project_dir(prj_dir) ??
else: else:
# if we are inside a project or package dir, ascend to parent # if we are inside a project or package dir, ascend to parent
@ -5652,7 +5653,7 @@ def checkout_package(
root_dots = root_dots / ("../" * n) root_dots = root_dots / ("../" * n)
if str(root_dots) != '.': if str(root_dots) != '.':
_private.print_msg(f"{prj_dir} is project dir of {oldproj}. Root found at {os.path.abspath(root_dots)}", print_to="verbose") output.print_msg(f"{prj_dir} is project dir of {oldproj}. Root found at {os.path.abspath(root_dots)}", print_to="verbose")
prj_dir = root_dots / prj_dir prj_dir = root_dots / prj_dir
if not pathname: if not pathname:

View File

@ -1,5 +1,6 @@
from .key_value_table import KeyValueTable from .key_value_table import KeyValueTable
from .input import get_user_input from .input import get_user_input
from .output import print_msg
from .tty import colorize from .tty import colorize
from .widechar import wc_ljust from .widechar import wc_ljust
from .widechar import wc_width from .widechar import wc_width

26
osc/output/output.py Normal file
View File

@ -0,0 +1,26 @@
import sys
def print_msg(*args, print_to="debug"):
from .. import conf
if print_to is None:
return
elif print_to == "debug":
# print a debug message to stderr if config["debug"] is set
if conf.config["debug"]:
print("DEBUG:", *args, file=sys.stderr)
elif print_to == "verbose":
# print a verbose message to stdout if config["verbose"] or config["debug"] is set
if conf.config["verbose"] or conf.config["debug"]:
print(*args)
elif print_to == "stdout":
# print the message to stdout
print(*args)
elif print_to == "stderr":
# print the message to stderr
print(*args, file=sys.stderr)
else:
raise ValueError(f"Invalid value of the 'print_to' option: {print_to}")

View File

@ -3,8 +3,8 @@ import io
import unittest import unittest
import osc.conf import osc.conf
from osc._private import print_msg
from osc.output import KeyValueTable from osc.output import KeyValueTable
from osc.output import print_msg
class TestKeyValueTable(unittest.TestCase): class TestKeyValueTable(unittest.TestCase):