Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP organizing things into classes #10

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,072 changes: 0 additions & 1,072 deletions module_build/builders/mock_builder.py

This file was deleted.

144 changes: 71 additions & 73 deletions module_build/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import sys
import traceback

from module_build.builders.mock_builder import MockBuilder
from module_build.log import init_logging, logger
from module_build.metadata import (load_modulemd_file_from_path,
generate_module_stream_version)
from module_build.metadata import generate_module_stream_version, load_modulemd_file_from_path
from module_build.mock.build.init import MockBuilder
from module_build.stream import ModuleStream


class FullPathAction(argparse.Action):
""" A custom argparse action which converts all relative paths to absolute. """
"""A custom argparse action which converts all relative paths to absolute."""

def __call__(self, parser, args, values, option_string=None):
full_path = self._get_full_path(values)
Expand All @@ -35,67 +34,66 @@ def _get_full_path(self, path):


def get_arg_parser():
description = (
"""
description = """
module-build is a command line utility which enables you to build modules locally.
"""
)
parser = argparse.ArgumentParser("module-build", description=description,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("workdir", type=str, action=FullPathAction,
help=("The working directory where the build of a module stream will"
" happen."))

group = parser.add_mutually_exclusive_group(required=True)

group.add_argument("-f", "--modulemd", type=str, action=FullPathAction,
help="Path to the modulemd yaml file")
parser = argparse.ArgumentParser("module-build", description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# group.add_argument("-g", "--git-branch", type=str,
# help=("URL to the git branch where the modulemd yaml file resides."))
parser.add_argument("workdir", type=str, action=FullPathAction, help=("The working directory where the build of a module stream will" " happen."))
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-f", "--modulemd", type=str, action=FullPathAction, help="Path to the modulemd yaml file")
parser.add_argument("-t", "--rootdir", type=str, help=("Provides a new location for you buildroots."))
parser.add_argument("-g", "--module-context", type=str, help=("When set it will only build the selected context from the modules stream."))
parser.add_argument("-c", "--mock-cfg", help="Path to the mock config.", default=".", type=str, required=True, action=FullPathAction)
parser.add_argument("-r", "--resume", action="store_true", help="If set it will try to continue the build where it failed last time.")
parser.add_argument("-u", "--no-stdout", action="store_true", help="If set default logger output will be written only to file, not stdout.")
parser.add_argument("-w", "--workers", type=int, default=1, help="If set default logger output will be written only to file, not stdout.")
parser.add_argument(
"-n",
"--module-name",
type=str,
help=("You can define the module name with this option if it is not" " set in the provided modulemd yaml file."),
)
parser.add_argument(
"-s",
"--module-stream",
type=str,
help=("You can define the module stream name with this option if it is not" " set in the provided modulemd yaml file."),
)
parser.add_argument(
"-l",
"--module-version",
type=int,
help=(
"You can define the module stream name with this option. If it is not"
" set the current unix timestamp will be used instead. This option is"
" also required when using the resume feature. You can specify which "
"module stream version you want to resume."
),
)
parser.add_argument(
"-m",
"--srpm-dir",
type=str,
help=("Path to directory with SRPMs. When set, all module components will be" " build from given sources."),
action=FullPathAction,
)

parser.add_argument("-c", "--mock-cfg", help="Path to the mock config.",
default=".", type=str, required=True,
action=FullPathAction)

parser.add_argument("-r", "--resume", action="store_true",
help="If set it will try to continue the build where it failed last time.")

parser.add_argument("-n", "--module-name", type=str,
help=("You can define the module name with this option if it is not"
" set in the provided modulemd yaml file."))

parser.add_argument("-s", "--module-stream", type=str,
help=("You can define the module stream name with this option if it is not"
" set in the provided modulemd yaml file."))

parser.add_argument("-l", "--module-version", type=int,
help=("You can define the module stream name with this option. If it is not"
" set the current unix timestamp will be used instead. This option is"
" also required when using the resume feature. You can specify which "
"module stream version you want to resume."))

parser.add_argument("-m", "--srpm-dir", type=str,
help=("Path to directory with SRPMs. When set, all module components will be"
" build from given sources."),
action=FullPathAction)

parser.add_argument("-g", "--module-context", type=str,
help=("When set it will only build the selected context from the modules"
" stream."))
# TODO verbose is not implemented
# parser.add_argument("-v", "--verbose", action="store_true",
# help="Will display all output to stdout.")

parser.add_argument("-d", "--debug", action="store_true",
help="When the module build fails it will start the python `pdb` debugger.")
parser.add_argument("-d", "--debug", action="store_true", help="When the module build fails it will start the python `pdb` debugger.")
parser.set_defaults(add_repo=[])
parser.add_argument("-p", "--add-repo", type=str, action=FullPathAction,
help=("With this option you can provide external RPM repositories to the"
" buildroots of the module build. Can be used multiple times."))

parser.add_argument("-t", "--rootdir", type=str,
help=("Provides a new location for you buildroots."))
parser.add_argument(
"-p",
"--add-repo",
type=str,
action=FullPathAction,
help=("With this option you can provide external RPM repositories to the buildroots of the module build. Can be used multiple times."),
)

return parser

Expand All @@ -105,14 +103,15 @@ def main():
args = parser.parse_args()

if args.resume and args.module_version is None:
parser.error("when using -r/--resume you need also set -l/--module-version so we can "
"can identify which contexts build need to be resumed.")
parser.error(
"when using -r/--resume you need also set -l/--module-version so we can " "can identify which contexts build need to be resumed."
)

# TODO this needs to be updated when scm checkout will be added
yaml_filename = args.modulemd.split("/")[-1].rsplit(".", 1)[0]
init_logging(args.workdir, yaml_filename, logger)
init_logging(args.workdir, yaml_filename, args.no_stdout, logger)

# PHASE1: Load metadata and configuration provided by the user
# PHASE1: Load metadata and configuration provided by the user
logger.info("Processing provided module stream metadata...")
if args.modulemd:
mmd = load_modulemd_file_from_path(args.modulemd)
Expand Down Expand Up @@ -142,9 +141,7 @@ def main():
# PHASE2: init the builder
if args.module_context:
log_msg = "Starting to build the '{name}:{stream}:{context}' of the module stream.".format(
name=module_stream.name,
stream=module_stream.stream,
context=args.module_context
name=module_stream.name, stream=module_stream.stream, context=args.module_context
)
else:
log_msg = "Starting to build the '{name}:{stream}' module stream.".format(
Expand All @@ -155,10 +152,9 @@ def main():
logger.info(log_msg)

# TODO add exceptions
mock_builder = MockBuilder(args.mock_cfg, args.workdir,
args.add_repo, args.rootdir, args.srpm_dir)
mock_builder = MockBuilder(args.mock_cfg, args.workdir, args.add_repo, args.rootdir, args.srpm_dir, args.workers)

# PHASE3: try to build the module stream
# PHASE3: try to build the module stream
try:
mock_builder.build(module_stream, args.resume, context_to_build=args.module_context)
except Exception:
Expand All @@ -167,22 +163,24 @@ def main():

if args.debug:
print(formated_tb)
msg = ("The program is now in debug mode after encountering an exception."
" When encountering an error the `pdb` python debugger is"
" started. See the following variables to check the state of the build:\n\n"
"`formated_tb` - formated traceback of the exception\n"
"`exc_info` - tuple with the exception information\n"
"`module_stream` - object of module stream metadata loaded from your "
"modulemd file\n"
"`mock_builder` - object which holds the state of your module build\n\n")
msg = (
"The program is now in debug mode after encountering an exception."
" When encountering an error the `pdb` python debugger is"
" started. See the following variables to check the state of the build:\n\n"
"`formated_tb` - formated traceback of the exception\n"
"`exc_info` - tuple with the exception information\n"
"`module_stream` - object of module stream metadata loaded from your "
"modulemd file\n"
"`mock_builder` - object which holds the state of your module build\n\n"
)
logger.info(msg)

pdb.set_trace()
else:
print(formated_tb)
raise exc_info[1]

# PHASE4: Make a final report on the module stream build
# PHASE4: Make a final report on the module stream build
# TODO implement final_report
mock_builder.final_report()

Expand Down
7 changes: 7 additions & 0 deletions module_build/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@

SRPM_MAPPING_FILENAME = "srpm_mapping"
ROOT_BATCH_FOLDER = "build_batches"

# Context
SRPM_BUILDROOT_PROFILE = "srpm-buildroot"
RPM_BUILDROOT_PROFILE = "buildroot"

# Batch
DEFAULT_LICENSE_TYPE = "MIT"
19 changes: 9 additions & 10 deletions module_build/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
logger = logging.getLogger("module-build")


def init_logging(cwd, yaml_filename, logger):
main_log_file_path = cwd + "/{yaml}-module-build-{timestamp}.log".format(
yaml=yaml_filename,
timestamp=int(time.time())
)
log_format = '%(asctime)s | %(levelname)s | %(message)s'
def init_logging(cwd, yaml_filename, no_stdout, logger):
main_log_file_path = cwd + "/{yaml}-module-build-{timestamp}.log".format(yaml=yaml_filename, timestamp=int(time.time()))
log_format = "%(asctime)s | %(levelname)s | %(message)s"

logger.setLevel("INFO")

Expand All @@ -20,8 +17,10 @@ def init_logging(cwd, yaml_filename, logger):
main_log_handle.setFormatter(log_formatter)

logger.addHandler(main_log_handle)
# at the same time we want to write to stdout
cli_handler = logging.StreamHandler(sys.stdout)
cli_handler.setFormatter(log_formatter)

logger.addHandler(cli_handler)
if not no_stdout:
# at the same time we want to write to stdout
cli_handler = logging.StreamHandler(sys.stdout)
cli_handler.setFormatter(log_formatter)

logger.addHandler(cli_handler)
Loading