Skip to content

Commit

Permalink
Merge pull request galaxyproject#52 from galaxyproject/bump_versions
Browse files Browse the repository at this point in the history
Bump all versions
  • Loading branch information
simleo authored Jul 26, 2021
2 parents c2647ab + 8510c7b commit 69b9ee1
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
ignore = E501
223 changes: 223 additions & 0 deletions bump_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
# Copyright (c) 2021 CRS4
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""\
Bump workflow repository version.
Workflow repositories are searched for starting from the specified root
directories (the default is to search below the current directory). Uses the
same searching logic and definition of repository as the ci_find_repos Planemo
command (any directory with a .shed.yml or .dockstore.yml file).
Workflow repositories are expected to contain:
- the .ga workflow file (e.g., "consensus-from-variation.ga"), which is
expected to list its version in the "release" metadata field
- a CHANGELOG.md file with atx-style Markdown headers (the ones with the
leading hash marks), with the convention that:
- the top-level header is "# Changelog"
- level 2 headers start with ## <VERSION>
Example:
# Changelog
## [0.4] - 2021-06-16
### Changed
- Upgrade multiqc to 1.9+galaxy1
"""

import argparse
import datetime
import json
import os
import re
import string
from enum import Enum
from pathlib import Path

import marko
from marko.md_renderer import MarkdownRenderer
from packaging.version import parse as vparse, Version, LegacyVersion
from planemo.context import PlanemoContext
from planemo.shed import find_raw_repositories
from planemo.ci import filter_paths

DATE_FMT = "%Y-%m-%d"
ENTRY_TYPES = "Added", "Changed", "Fixed"
NEW_LOG_ENTRY = string.Template("""\
## [${version}] ${date}
### ${entry_type}
${msg}
""")


class Bump(Enum):
MAJOR = "major"
MINOR = "minor"
MICRO = "micro" # PEP 440
PATCH = "micro" # Semantic Versioning


# Allow "PATCH" alias, but leave it out of the help text to avoid confusion
BUMP_CHOICES = list(Bump.__members__) # includes aliases
BUMP_METAVAR = "|".join(_.name for _ in Bump) # does not include aliases


class Renderer(MarkdownRenderer):
"""\
A Markdown renderer that indents all lines in a paragraph.
"""
def render_paragraph(self, element):
new_line = False
rendered = []
for child in element.children:
prefix = self._second_prefix if new_line else ""
rendered.append(prefix + self.render(child))
new_line = isinstance(child, marko.inline.LineBreak)
children = "".join(rendered)
line = self._prefix + children + "\n"
self._prefix = self._second_prefix
return line


def get_new_release(rel, bump=Bump.MICRO):
if len(rel) == 1 and bump is Bump.MINOR or len(rel) == 2 and bump is Bump.MICRO:
return rel + (1,)
if len(rel) == 1 and bump is Bump.MICRO:
return rel + (0, 1)
return tuple(r + 1 if bump is b else r for r, b in zip(rel, Bump))


def vbump(version, bump=Bump.MICRO):
if not isinstance(version, (Version, LegacyVersion)):
version = vparse(version)
new_release = get_new_release(version.release, bump)
# same as packaging.version.Version.__str__, but release is the new one
parts = []
if version.epoch != 0:
parts.append(f"{version.epoch}!")
parts.append(".".join(str(x) for x in new_release))
if version.pre is not None:
parts.append("".join(str(x) for x in version.pre))
if version.post is not None:
parts.append(f".post{version.post}")
if version.dev is not None:
parts.append(f".dev{version.dev}")
if version.local is not None:
parts.append(f"+{version.local}")
return "".join(parts)


def get_wf_id(repo_dir):
ids = [_.name for _ in os.scandir(repo_dir) if _.name.endswith(".ga")]
if not ids:
raise RuntimeError(f".ga workflow file not found in {repo_dir}")
return ids[0]


def update_changelog(changelog, md, version, msg, date=datetime.date.today(),
entry_type="Changed"):
with open(changelog, "rt") as f:
tree = md.parse(f.read())
date = date.strftime(DATE_FMT)
entry = NEW_LOG_ENTRY.substitute(version=version, date=date, msg=msg,
entry_type=entry_type)
entry_tree = marko.block.Document(entry)
for ins_pos, elem in enumerate(tree.children):
if isinstance(elem, marko.block.Heading) and elem.level == 2:
break
else:
ins_pos = len(tree.children)
tree.children[ins_pos:ins_pos] = entry_tree.children
with open(changelog, "wt") as f:
f.write(md.render(tree))


def update_workflow(workflow, version=None, bump=None):
with open(workflow, "rt") as f:
txt = f.read()
code = json.loads(txt)
try:
old_version = code["release"]
except KeyError:
raise RuntimeError(f'{workflow} does not have a "release" metadata entry')
if not version:
if not old_version:
raise RuntimeError(f'{workflow}: "release" metadata entry is empty')
version = vbump(old_version, bump=bump)
# no json tools for the update (we'd get a huge diff due to whitespace change)
pattern = fr'"release":\s*"{old_version}"'
repl = f'"release": "{version}"'
with open(workflow, "wt") as f:
f.write(re.sub(pattern, repl, txt, 1))
return version


def find_repos(paths, exclude=()):
"""\
Find all workflow directories below each path in ``paths``.
Same as ``planemo ci_find_repos``.
"""
ctx = PlanemoContext()
kwargs = dict(recursive=True, fail_fast=True, chunk_count=1, chunk=0, exclude=exclude)
raw_repos = [_.path for _ in find_raw_repositories(ctx, paths, **kwargs)]
return [Path(_) for _ in filter_paths(ctx, raw_repos, path_type="repo", **kwargs)]


def main(args):
if args.date:
args.date = datetime.datetime.strptime(args.date, DATE_FMT)
else:
args.date = datetime.date.today()
args.bump = Bump[args.bump]
md = marko.Markdown(renderer=Renderer)
for repo in find_repos(args.root, exclude=args.exclude):
print(f"processing {repo}")
wf_id = get_wf_id(repo)
version = update_workflow(repo / wf_id, version=args.version, bump=args.bump)
msg = args.msg or f"Update for version {version}."
update_changelog(repo / "CHANGELOG.md", md, version, msg, date=args.date,
entry_type=args.entry_type)


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("root", metavar="ROOT_DIR", help="top-level directory",
nargs="*", default=[os.getcwd()])
parser.add_argument("--exclude", metavar="PATH", nargs="*", default=(),
help="paths to exclude while searching for workflow repos")
parser.add_argument("-v", "--version", metavar="STRING",
help="set new workflow version to this value (ignores '-b')")
parser.add_argument("-m", "--msg", metavar="STRING", help="log message")
parser.add_argument("-d", "--date", metavar="STRING", help="log date as YYYY-MM-DD")
parser.add_argument("-t", "--entry-type", metavar="|".join(ENTRY_TYPES),
choices=ENTRY_TYPES, default="Changed", help="log entry type")
parser.add_argument("-b", "--bump", metavar=BUMP_METAVAR, choices=BUMP_CHOICES,
default="MICRO", help="version part to bump")
main(parser.parse_args())
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
marko~=1.0
packaging~=21.0
planemo>=0.74.5
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.1.1] 2021-07-23

### Added

Added RO-Crate metadata file. No functional changes.

## [0.1] - 2021-06-23

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"format-version": "0.1",
"license": "MIT",
"release": "0.1",
"release": "0.1.1",
"name": "Parallel Accession Download",
"steps": {
"0": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.2.1] 2021-07-23

### Added

Added RO-Crate metadata file. No functional changes.

## [0.2] - 2021-04-30
### Changed
- Lower the default for consensus variant allele frequency threshold to 0.7
Expand All @@ -24,7 +30,6 @@
variants of its samples as stated in the README, independent of it residing
in low-coverage regions or not.


## [0.1]

- Initial version of COVID-19: consensus construction
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format-version": "0.1",
"license": "MIT",
"name": "COVID-19: consensus construction",
"release": "0.2",
"release": "0.2.1",
"steps": {
"0": {
"annotation": "Collection of VCFs produced by upstream workflows for variation analysis",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
0.2
---------
# Changelog

## [0.2.1] 2021-07-23

### Added

Added RO-Crate metadata file. No functional changes.

## [0.2]

- Apply the strand-bias filter only after variant annotation with snpEff. By
producing fully annotated VCFs with and without filtering, downstream
workflows can easily be switched between filtered/unfiltered input data

0.1
---------
## [0.1]

- Initial version of COVID-19: variation analysis on ARTIC ONT data workflow
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format-version": "0.1",
"license": "MIT",
"name": "COVID-19: variation analysis of ARTIC ONT data",
"release": "0.2",
"release": "0.2.1",
"steps": {
"0": {
"annotation": "ONT reads from ARTIC assay with fastqsanger encoding",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Changelog

## [0.4.1] 2021-07-23

### Added

Added RO-Crate metadata file. No functional changes.

## [0.4] - 2021-06-16

### Changed

- Upgrade multiqc to 1.9+galaxy1


## [0.3] - 2021-05-19

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format-version": "0.1",
"license": "MIT",
"name": "COVID-19: variation analysis on ARTIC PE data",
"release": "0.4",
"release": "0.4.1",
"steps": {
"0": {
"annotation": "Illumina reads from ARTIC assay with fastqsanger encoding",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Changelog

## [0.2.1] 2021-07-23

### Added

Added RO-Crate metadata file. No functional changes.

## [0.2] 2021-06-17

### Changed

- Upgrade multiqc to 1.9+galaxy1


## [0.1]


- Initial version of COVID-19: variation analysis on WGS PE data workflow
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format-version": "0.1",
"license": "MIT",
"name": "COVID-19: variation analysis on WGS PE data",
"release": "0.2",
"release": "0.2.1",
"steps": {
"0": {
"annotation": "Illumina reads with fastqsanger encoding",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
0.1
---------
# Changelog

## [0.1.2] 2021-07-23

### Added

Added RO-Crate metadata file. No functional changes.

## [0.1.1] 2021-06-14

### Fixed

Fix reference to test file in .dockstore.yml

## [0.1]

- Initial version of COVID-19: variation analysis on WGS SE data workflow
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format-version": "0.1",
"license": "MIT",
"name": "COVID-19: variation analysis on WGS SE data",
"release": "0.1",
"release": "0.1.2",
"steps": {
"0": {
"annotation": "Illumina reads with fastqsanger encoding",
Expand Down
Loading

0 comments on commit 69b9ee1

Please sign in to comment.