Skip to content

Commit

Permalink
Merge pull request #144 from steinwurf/ctx.project_version
Browse files Browse the repository at this point in the history
added project_version as a function on the context
  • Loading branch information
jpihl authored Jan 26, 2024
2 parents 2c949a2 + b99840a commit 1ca1e15
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ of every change, see the Git log.

Latest
------
* Minor: Added project_version as a function on the Context object.
* Patch: Added dependency paths for resolve conflicts.
* Patch: Fix ``--no_resolve`` option.
* Minor: Change output so that the resolve path is printed instead of the
Expand Down
9 changes: 9 additions & 0 deletions src/wurf/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,12 @@ def default_branch(self, cwd):
output = self.ctx.cmd_and_log(args, cwd=cwd)

return output.strip().split("/")[-1]

def is_dirty(self, cwd):
"""
Returns True if the repository in directory cwd has uncommitted changes.
"""
args = [self.git_binary, "status", "--porcelain"]
output = self.ctx.cmd_and_log(args, cwd=cwd)

return output != ""
72 changes: 72 additions & 0 deletions src/wurf/waf_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# wscripts.

import sys
import os

from waflib.Configure import conf
from waflib.Errors import WafError
Expand All @@ -18,6 +19,8 @@
from . import virtualenv
from . import rewrite
from . import pip_tools
from . import error
from .registry import Registry


def extend_context(f):
Expand Down Expand Up @@ -151,3 +154,72 @@ def ensure_build(ctx):
@extend_context
def rewrite_file(ctx, filename):
return rewrite.rewrite_file(filename=filename)


@extend_context
def project_version(ctx):
"""
Return the project version. If the project is not a git repository
None is returned. If the project is a git repository the version
is constructed as follows:
1. If the current commit matches a tag the tag is used as the version.
2. If the current commit does not match a tag the latest tag and
current commit is used as the version.
3. If the repository contains uncommitted changes "-dirty" is appended.
Examples:
gc0b0b0b
gc0b0b0b-dirty
1.0.0
1.0.0-dirty
1.0.0-gc0b0b0b
1.0.0-gc0b0b0b-dirty # all together now
:param ctx: A Waf Context instance.
"""

# To avoid logs going to stdout create an logger
bldnode = ctx.path.make_node("build")
bldnode.mkdir()

log_path = os.path.join(bldnode.abspath(), "version.log")

ctx.logger = Logs.make_logger(path=log_path, name="version")
ctx.logger.debug("wurf: project version execute")

registry = Registry()
registry.provide_value("ctx", ctx)
registry.provide_value("git_binary", "git")
git = registry.require("git")

try:
# Build the registry
cwd = ctx.path.abspath()
if not git.is_git_repository(cwd):
return None

commit = git.current_commit(cwd)
tag = ([None] + git.tags(cwd))[-1]
if tag is not None:
tag = tag.rstrip()
tag_commit = git.checkout_to_commit_id(cwd, tag)
if tag_commit == commit:
commit = None
dirty = git.is_dirty(cwd)

# Build the version slug
version = []
if tag is not None:
version.append(tag)
if commit is not None:
version.append(commit[:7])
if dirty:
version.append("dirty")
version = "-".join(version)

return version
except error.CmdAndLogError as e:
ctx.logger.debug(f"wurf: project version failed: {e}")
return None
10 changes: 10 additions & 0 deletions src/wurf/waf_resolve_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def execute(self):
self.logger = Logs.make_logger(path, "resolve")
self.logger.debug(f"wurf: Resolve execute {configuration.resolver_chain()}")

# Print the project version
self.start_msg("Project version")
version = self.project_version()
self.end_msg(version or "unknown", color="GREEN" if version else "YELLOW")

self.dependency_manager: DependencyManager = self.registry.require(
"dependency_manager"
)
Expand Down Expand Up @@ -189,3 +194,8 @@ def cmd_and_log(self, cmd, **kwargs):
raise CmdAndLogError(error=e)
except Exception:
raise


WafResolveContext.__doc__ = (
"resolves the dependencies specified in the wscript's resolve function"
)
6 changes: 5 additions & 1 deletion src/wurf/waf_standalone_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


def options(opt):

opts = opt.add_option_group("Standalone archive options")

opts.add_option(
Expand Down Expand Up @@ -62,3 +61,8 @@ def get_files(self):
)

return self.base_path.ant_glob("**/*", dir=True, excl=excludes.split())


WafStandaloneContext.__doc__ = (
"creates a standalone archive that contains all dependencies"
)
4 changes: 4 additions & 0 deletions test/fake_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,7 @@ def default_branch(self, cwd):
def _to_sha1(self, data):
"""Small private helper to calculate SHA1"""
return hashlib.sha1(data.encode("utf-8")).hexdigest()

def is_dirty(self, cwd):
"""Fake whether the repository has uncommitted changes"""
return False
12 changes: 6 additions & 6 deletions waf

Large diffs are not rendered by default.

0 comments on commit 1ca1e15

Please sign in to comment.