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

Fix local building in git projects - v2 #1669

Merged
merged 3 commits into from
Nov 29, 2024
Merged
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
24 changes: 13 additions & 11 deletions osc/commandline.py
Original file line number Diff line number Diff line change
@@ -5387,7 +5387,7 @@
from .core import parseRevisionOption
from .core import print_request_list
from .core import revision_is_empty
from .core import run_external
from .core import run_obs_scm_bridge

Check warning on line 5390 in osc/commandline.py

Codecov / codecov/patch

osc/commandline.py#L5390

Added line #L5390 was not covered by tests
from .core import show_files_meta
from .core import show_project_meta
from .core import show_scmsync
@@ -5506,10 +5506,7 @@

scm_url = show_scmsync(apiurl, project)
if scm_url is not None and not opts.native_obs_package:
if not os.path.isfile('/usr/lib/obs/service/obs_scm_bridge'):
raise oscerr.OscIOError(None, 'Install the obs-scm-bridge package to work on packages managed in scm (git)!')
os.putenv("OSC_VERSION", get_osc_version())
run_external(['/usr/lib/obs/service/obs_scm_bridge', '--outdir', str(prj_dir), '--url', scm_url])
run_obs_scm_bridge(url=scm_url, target_dir=str(prj_dir))

Check warning on line 5509 in osc/commandline.py

Codecov / codecov/patch

osc/commandline.py#L5509

Added line #L5509 was not covered by tests

Project.init_project(apiurl, prj_dir, project, conf.config['do_package_tracking'], scm_url=scm_url)
print(statfrmt('A', prj_dir))
@@ -7272,6 +7269,7 @@
@cmdln.alias('repos')
@cmdln.alias('platforms')
def do_repositories(self, subcmd, opts, *args):
from . import store as osc_store

Check warning on line 7272 in osc/commandline.py

Codecov / codecov/patch

osc/commandline.py#L7272

Added line #L7272 was not covered by tests
from .core import build_table
from .core import get_repos_of_project
from .core import get_repositories_of_project
@@ -7302,11 +7300,12 @@
project = self._process_project_name(args[0])
package = args[1]
elif len(args) == 0:
if is_package_dir('.'):
package = store_read_package('.')
project = store_read_project('.')
elif is_project_dir('.'):
project = store_read_project('.')
store = osc_store.get_store(".")
if store.is_package:
package = store.package
project = store.project
elif store.is_project:
project = store.project

Check warning on line 7308 in osc/commandline.py

Codecov / codecov/patch

osc/commandline.py#L7303-L7308

Added lines #L7303 - L7308 were not covered by tests
else:
raise oscerr.WrongArgs('Wrong number of arguments')

@@ -7734,7 +7733,10 @@

if not opts.local_package:
store = osc_store.get_store(Path.cwd(), print_warnings=True)
store.assert_is_package()
if isinstance(store, git_scm.store.GitStore):
opts.local_package = True

Check warning on line 7737 in osc/commandline.py

Codecov / codecov/patch

osc/commandline.py#L7736-L7737

Added lines #L7736 - L7737 were not covered by tests
else:
store.assert_is_package()

Check warning on line 7739 in osc/commandline.py

Codecov / codecov/patch

osc/commandline.py#L7739

Added line #L7739 was not covered by tests

try:
if opts.alternative_project and opts.alternative_project == store.project:
12 changes: 12 additions & 0 deletions osc/conf.py
Original file line number Diff line number Diff line change
@@ -1308,6 +1308,18 @@ def apiurl_aliases(self):
ini_key="download-assets-cmd",
) # type: ignore[assignment]

obs_scm_bridge_cmd: str = Field(
default=
shutil.which("obs_scm_bridge", path="/usr/lib/obs/service")
or "/usr/lib/obs/service/obs_scm_bridge",
description=textwrap.dedent(
"""
Path to the 'obs_scm_bridge' tool used for cloning scmsync projects and packages.
"""
),
ini_key="obs-scm-bridge-cmd",
) # type: ignore[assignment]

vc_cmd: str = Field(
default=shutil.which("vc", path="/usr/lib/build:/usr/lib/obs-build") or "/usr/lib/build/vc",
description=textwrap.dedent(
15 changes: 10 additions & 5 deletions osc/core.py
Original file line number Diff line number Diff line change
@@ -3080,6 +3080,14 @@
return pkg_path


def run_obs_scm_bridge(url: str, target_dir: str):
if not os.path.isfile(conf.config.obs_scm_bridge_cmd):
raise oscerr.OscIOError(None, "Install the obs-scm-bridge package to work on packages managed in scm (git)!")
env = os.environ.copy()
env["OSC_VERSION"] = get_osc_version()
run_external([conf.config.obs_scm_bridge_cmd, "--outdir", target_dir, "--url", url], env=env)

Check warning on line 3088 in osc/core.py

Codecov / codecov/patch

osc/core.py#L3084-L3088

Added lines #L3084 - L3088 were not covered by tests


def checkout_package(
apiurl: str,
project: str,
@@ -3153,9 +3161,6 @@
root = ET.fromstring(meta_data)
scmsync_element = root.find("scmsync")
if not native_obs_package and scmsync_element is not None and scmsync_element.text is not None:
if not os.path.isfile('/usr/lib/obs/service/obs_scm_bridge'):
raise oscerr.OscIOError(None, 'Install the obs-scm-bridge package to work on packages managed in scm (git)!')
scm_url = scmsync_element.text
directory = make_dir(apiurl, project, package, pathname, prj_dir, conf.config['do_package_tracking'], outdir)

if revision is not None:
@@ -3163,10 +3168,10 @@
# we need also take into account that the url was different at that point of time
from .obs_api.scmsync_obsinfo import ScmsyncObsinfo
scmsync_obsinfo = ScmsyncObsinfo.from_api(apiurl, project, package, rev=revision)
scm_url = f"{scmsync_obsinfo.url}#{scmsync_obsinfo.revision}"

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'scm_url' is unnecessary as it is
redefined
before this value is used.

os.putenv("OSC_VERSION", get_osc_version())
run_external(['/usr/lib/obs/service/obs_scm_bridge', '--outdir', directory, '--url', scm_url])
scm_url = scmsync_element.text
run_obs_scm_bridge(url=scm_url, target_dir=directory)

Check warning on line 3174 in osc/core.py

Codecov / codecov/patch

osc/core.py#L3173-L3174

Added lines #L3173 - L3174 were not covered by tests

Package.init_package(apiurl, project, package, directory, size_limit, meta, progress_obj, scm_url)

28 changes: 27 additions & 1 deletion osc/git_scm/store.py
Original file line number Diff line number Diff line change
@@ -29,10 +29,26 @@
def __init__(self, path, check=True):
self.path = path
self.abspath = os.path.abspath(self.path)
try:
self.toplevel = self._run_git(["rev-parse", "--show-toplevel"])
self.toplevel = os.path.abspath(self.toplevel)
except subprocess.CalledProcessError:
self.toplevel = None

# TODO: how to determine if the current git repo contains a project or a package?
self.is_project = False
self.is_package = os.path.exists(os.path.join(self.abspath, ".git"))
self.is_package = False

if self.toplevel:
# NOTE: we have only one store in project-git for all packages
config_path = os.path.join(self.toplevel, "_config")
pbuild_path = os.path.join(self.toplevel, "_pbuild")
if self.toplevel == self.abspath and (os.path.isfile(config_path) or os.path.isfile(pbuild_path)):
self.is_project = True
self.is_package = False

Check warning on line 48 in osc/git_scm/store.py

Codecov / codecov/patch

osc/git_scm/store.py#L47-L48

Added lines #L47 - L48 were not covered by tests
else:
self.is_project = False
self.is_package = True

self._package = None
self._project = None
@@ -68,11 +84,21 @@

@property
def project(self):
if self._project is None:
try:
# NOTE: this never triggers if a store is retrieved from osc.store.get_store(),
# because obs_scm store takes precedence as .osc is present
with open(os.path.join(self.toplevel, ".osc/_project")) as f:
self._project = f.readline().strip()

Check warning on line 92 in osc/git_scm/store.py

Codecov / codecov/patch

osc/git_scm/store.py#L92

Added line #L92 was not covered by tests
except FileNotFoundError:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass

if self._project is None:
# get project from the branch name
branch = self._run_git(["branch", "--show-current"])

# HACK: replace hard-coded mapping with metadata from git or the build service
# NOTE: you never know which git repo is supposed to be used in which project
if branch == "factory":
self._project = "openSUSE:Factory"
else:
4 changes: 2 additions & 2 deletions osc/obs_api/package.py
Original file line number Diff line number Diff line change
@@ -20,9 +20,9 @@ class Package(XmlModel):
xml_attribute=True,
)

title: str = Field()
title: Optional[str] = Field()

description: str = Field()
description: Optional[str] = Field()

devel: Optional[PackageDevel] = Field()

20 changes: 13 additions & 7 deletions osc/store.py
Original file line number Diff line number Diff line change
@@ -21,14 +21,20 @@
"""

# if there are '.osc' and '.git' directories next to each other, '.osc' takes preference
if os.path.exists(os.path.join(path, ".osc")):
store = None

try:
store = Store(path, check)
elif os.path.exists(os.path.join(path, ".git")):
store = git_scm.GitStore(path, check)
if print_warnings:
git_scm.warn_experimental()
else:
store = None
except oscerr.NoWorkingCopy:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass

if not store:
try:
store = git_scm.GitStore(path, check)
if print_warnings:
git_scm.warn_experimental()

Check warning on line 35 in osc/store.py

Codecov / codecov/patch

osc/store.py#L34-L35

Added lines #L34 - L35 were not covered by tests
except oscerr.NoWorkingCopy:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass

if not store:
msg = f"Directory '{path}' is not a working copy"