Skip to content

Commit

Permalink
move yaml lib into artcommon lib (#1162)
Browse files Browse the repository at this point in the history
* move yaml lib into artcommon lib

* update name

* fix ut
  • Loading branch information
Ximinhan authored Dec 6, 2024
1 parent 2e987cf commit 679a335
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 43 deletions.
21 changes: 20 additions & 1 deletion artcommon/artcommonlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import aiohttp
import requests
from tenacity import retry, wait_fixed, stop_after_attempt

from ruamel.yaml import YAML
from artcommonlib.constants import RELEASE_SCHEDULES

LOGGER = logging.getLogger(__name__)
Expand All @@ -34,6 +34,25 @@ def remove_suffix(s: str, suffix: str) -> str:
return s[:]


def new_roundtrip_yaml_handler():
"""
Creates and returns a configured YAML handler with specific formatting settings.
Returns:
YAML: A YAML handler configured with:
- round-trip (rt) mode for preserving comments and formatting
- disabled flow style for better readability
- preserved quotes
- 4096 character line width
- custom indentation (2 spaces for mappings, 4 for sequences)
"""
yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
yaml.indent(mapping=2, sequence=4, offset=2)
return yaml


def convert_remote_git_to_https(source_url: str):
"""
Accepts a source git URL in ssh or https format and return it in a normalized
Expand Down
8 changes: 2 additions & 6 deletions pyartcd/pyartcd/pipelines/build_microshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

from artcommonlib.arch_util import brew_arch_for_go_arch
from artcommonlib.assembly import AssemblyTypes
from artcommonlib.util import get_ocp_version_from_group
from artcommonlib.util import get_ocp_version_from_group, new_roundtrip_yaml_handler
from artcommonlib import exectools
from doozerlib.util import isolate_nightly_name_components
from ghapi.all import GhApi
from ruamel.yaml import YAML
from semver import VersionInfo
from tenacity import retry, stop_after_attempt, wait_fixed

Expand All @@ -30,10 +29,7 @@
default_release_suffix,
get_microshift_builds)

yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
yaml = new_roundtrip_yaml_handler()


class BuildMicroShiftPipeline:
Expand Down
8 changes: 2 additions & 6 deletions pyartcd/pyartcd/pipelines/build_microshift_bootc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import asyncio
import traceback
from typing import Optional
from ruamel.yaml import YAML

from artcommonlib.assembly import AssemblyTypes
from artcommonlib.util import get_ocp_version_from_group, isolate_major_minor_in_group
from artcommonlib.util import get_ocp_version_from_group, isolate_major_minor_in_group, new_roundtrip_yaml_handler
from artcommonlib.konflux.konflux_build_record import KonfluxBuildOutcome, Engine, ArtifactType, KonfluxBuildRecord
from artcommonlib.konflux.konflux_db import KonfluxDb
from artcommonlib.arch_util import brew_arch_for_go_arch
Expand All @@ -27,10 +26,7 @@
from pyartcd.plashets import build_plashets, plashet_config_for_major_minor
from doozerlib.constants import ART_DEFAULT_IMAGE_REPO

yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
yaml = new_roundtrip_yaml_handler()


class BuildMicroShiftBootcPipeline:
Expand Down
8 changes: 2 additions & 6 deletions pyartcd/pyartcd/pipelines/gen_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import aiohttp
import click
from ghapi.all import GhApi
from ruamel.yaml import YAML

from artcommonlib.util import split_git_url, merge_objects, get_inflight, isolate_major_minor_in_group
from artcommonlib.util import split_git_url, merge_objects, get_inflight, isolate_major_minor_in_group, new_roundtrip_yaml_handler
from artcommonlib import exectools
from doozerlib.cli.get_nightlies import rc_api_url
from pyartcd import constants, jenkins
Expand All @@ -21,10 +20,7 @@
from pyartcd.jenkins import start_build_sync
from pyartcd.runtime import Runtime

yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
yaml = new_roundtrip_yaml_handler()


class GenAssemblyPipeline:
Expand Down
16 changes: 2 additions & 14 deletions pyartcd/pyartcd/pipelines/prepare_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
from subprocess import PIPE
from typing import Dict, List, Optional, Sequence, Tuple
from jira.resources import Issue
from ruamel.yaml import YAML
from tenacity import retry, stop_after_attempt, wait_fixed
from datetime import datetime, timedelta, timezone

from artcommonlib.assembly import AssemblyTypes, assembly_group_config
from artcommonlib.model import Model
from artcommonlib.util import get_assembly_release_date
from artcommonlib.util import get_assembly_release_date, new_roundtrip_yaml_handler
from elliottlib.errata import set_blocking_advisory, get_blocking_advisories, push_cdn_stage, is_advisory_editable
from elliottlib.errata_async import AsyncErrataAPI
from elliottlib.errata import set_blocking_advisory, get_blocking_advisories
Expand All @@ -38,6 +37,7 @@


_LOGGER = logging.getLogger(__name__)
yaml = new_roundtrip_yaml_handler()


class PrepareReleasePipeline:
Expand Down Expand Up @@ -387,10 +387,6 @@ async def load_releases_config(self) -> Optional[None]:
return None
async with aiofiles.open(path, "r") as f:
content = await f.read()
yaml = YAML(typ="safe")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
return yaml.load(content)

@cached_property
Expand Down Expand Up @@ -504,10 +500,6 @@ async def load_group_config(self) -> Dict:
await self.clone_build_data(repo)
async with aiofiles.open(repo / "group.yml", "r") as f:
content = await f.read()
yaml = YAML(typ="safe")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
return yaml.load(content)

def check_blockers(self):
Expand Down Expand Up @@ -607,10 +599,6 @@ async def update_build_data(self, advisories: Dict[str, int], jira_issue_key: Op
f.write(group_config)
else:
# update releases.yml (if we are operating on a non-stream assembly)
yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
async with aiofiles.open(repo / "releases.yml", "r") as f:
old = await f.read()
releases_config = yaml.load(old)
Expand Down
7 changes: 2 additions & 5 deletions pyartcd/pyartcd/pipelines/review_cvp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
import click
from ghapi.all import GhApi
from artcommonlib import exectools
from artcommonlib.util import new_roundtrip_yaml_handler
from pyartcd import jenkins, constants
from pyartcd.cli import cli, click_coroutine, pass_runtime
from pyartcd.git import GitRepository
from pyartcd.runtime import Runtime
from ruamel.yaml import YAML

yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
yaml = new_roundtrip_yaml_handler()


class ReviewCVPPipeline:
Expand Down
7 changes: 2 additions & 5 deletions pyartcd/pyartcd/pipelines/update_golang.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from artcommonlib.release_util import split_el_suffix_in_release
from artcommonlib.rpm_utils import parse_nvr
from artcommonlib import exectools
from artcommonlib.util import new_roundtrip_yaml_handler
from pyartcd import jenkins
from pyartcd.constants import GITHUB_OWNER
from pyartcd.cli import cli, click_coroutine, pass_runtime
Expand All @@ -26,11 +27,7 @@
from elliottlib import util as elliottutil

_LOGGER = logging.getLogger(__name__)

yaml = YAML(typ="rt")
yaml.default_flow_style = False
yaml.preserve_quotes = True
yaml.width = 4096
yaml = new_roundtrip_yaml_handler()


def is_latest_build(ocp_version: str, el_v: int, nvr: str, koji_session) -> bool:
Expand Down

0 comments on commit 679a335

Please sign in to comment.