Skip to content

Commit 17558b8

Browse files
author
alrex
authored
chore: moving code to prepare the release to eachdist (open-telemetry#808)
1 parent fe4f341 commit 17558b8

File tree

2 files changed

+133
-53
lines changed

2 files changed

+133
-53
lines changed

scripts/eachdist.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#!/usr/bin/env python3
22

33
import argparse
4+
import os
5+
import re
46
import shlex
57
import shutil
68
import subprocess
79
import sys
810
from collections import namedtuple
911
from configparser import ConfigParser
12+
from datetime import datetime
1013
from inspect import cleandoc
1114
from itertools import chain
1215
from pathlib import Path, PurePath
@@ -236,6 +239,15 @@ def setup_instparser(instparser):
236239
"pytestargs", nargs=argparse.REMAINDER, help=extraargs_help("pytest")
237240
)
238241

242+
releaseparser = subparsers.add_parser(
243+
"release", help="Prepares release, used by maintainers and CI",
244+
)
245+
releaseparser.set_defaults(func=release_args)
246+
releaseparser.add_argument("--version", required=True)
247+
releaseparser.add_argument(
248+
"releaseargs", nargs=argparse.REMAINDER, help=extraargs_help("pytest")
249+
)
250+
239251
return parser.parse_args(args)
240252

241253

@@ -503,6 +515,118 @@ def lint_args(args):
503515
)
504516

505517

518+
def update_changelog(path, version, new_entry):
519+
unreleased_changes = False
520+
try:
521+
with open(path) as changelog:
522+
text = changelog.read()
523+
if "## Version {}".format(version) in text:
524+
raise AttributeError(
525+
"{} already contans version {}".format(path, version)
526+
)
527+
with open(path) as changelog:
528+
for line in changelog:
529+
if line.startswith("## Unreleased"):
530+
unreleased_changes = False
531+
elif line.startswith("## "):
532+
break
533+
elif len(line.strip()) > 0:
534+
unreleased_changes = True
535+
536+
except FileNotFoundError:
537+
print("file missing: {}".format(path))
538+
return
539+
540+
if unreleased_changes:
541+
print("updating: {}".format(path))
542+
text = re.sub("## Unreleased", new_entry, text)
543+
with open(path, "w") as changelog:
544+
changelog.write(text)
545+
546+
547+
def update_changelogs(targets, version):
548+
print("updating CHANGELOG")
549+
today = datetime.now().strftime("%Y-%m-%d")
550+
new_entry = "## Unreleased\n\n## Version {}\n\nReleased {}".format(
551+
version, today
552+
)
553+
errors = False
554+
for target in targets:
555+
try:
556+
update_changelog(
557+
"{}/CHANGELOG.md".format(target), version, new_entry
558+
)
559+
except Exception as err: # pylint: disable=broad-except
560+
print(str(err))
561+
errors = True
562+
563+
if errors:
564+
sys.exit(1)
565+
566+
567+
def find(name, path):
568+
for root, _, files in os.walk(path):
569+
if name in files:
570+
return os.path.join(root, name)
571+
return None
572+
573+
574+
def update_version_files(targets, version):
575+
print("updating version.py files")
576+
update_files(
577+
targets,
578+
version,
579+
"version.py",
580+
"__version__ .*",
581+
'__version__ = "{}"'.format(version),
582+
)
583+
584+
585+
def update_dependencies(targets, version):
586+
print("updating dependencies")
587+
update_files(
588+
targets,
589+
version,
590+
"setup.cfg",
591+
r"(opentelemetry-.*)= (.*)",
592+
r"\1= " + version,
593+
)
594+
595+
596+
def update_files(targets, version, filename, search, replace):
597+
errors = False
598+
for target in targets:
599+
curr_file = find(filename, target)
600+
if curr_file is None:
601+
print("file missing: {}/{}".format(target, filename))
602+
continue
603+
604+
with open(curr_file) as _file:
605+
text = _file.read()
606+
607+
if version in text:
608+
print("{} already contans version {}".format(curr_file, version))
609+
errors = True
610+
continue
611+
612+
with open(curr_file, "w") as _file:
613+
_file.write(re.sub(search, replace, text))
614+
615+
if errors:
616+
sys.exit(1)
617+
618+
619+
def release_args(args):
620+
print("preparing release")
621+
622+
rootpath = find_projectroot()
623+
targets = list(find_targets_unordered(rootpath))
624+
version = args.version
625+
update_dependencies(targets, version)
626+
update_version_files(targets, version)
627+
update_changelogs(targets, version)
628+
629+
506630
def test_args(args):
507631
clean_remainder_args(args.pytestargs)
508632
execute_args(

scripts/prepare_release.sh

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,57 +21,6 @@ if [[ ! "${VERSION}" =~ ^([0-9])(\.*[0-9]{1,5}[a-b]*){1,3}$ ]]; then
2121
exit 1
2222
fi
2323

24-
function update_version_file() {
25-
errors=0
26-
for f in `find . -name version.py`; do
27-
# check if version is already in version.py
28-
grep -q ${VERSION} $f;
29-
rc=$?
30-
if [ $rc == 0 ]; then
31-
errors=1
32-
echo "${f} already contains ${VERSION}"
33-
continue
34-
fi
35-
# update version.py
36-
perl -i -pe "s/__version__.*/__version__ = \"${VERSION}\"/g" ${f};
37-
git add ${f};
38-
echo "Updating ${f}"
39-
done
40-
if [ ${errors} != 0 ]; then
41-
echo "::set-output name=version_updated::0"
42-
exit 0
43-
fi
44-
}
45-
46-
function update_changelog() {
47-
errors=0
48-
RELEASE_DATE=`date +%F`
49-
for f in `find . -name CHANGELOG.md`; do
50-
# check if version is already in CHANGELOG
51-
grep -q ${VERSION} $f;
52-
rc=$?
53-
if [ $rc == 0 ]; then
54-
errors=1
55-
echo "${f} already contains ${VERSION}"
56-
continue
57-
fi
58-
# check if changelog contains any new details
59-
changes=`sed -n '/## Unreleased/,/^##/p' ${f} | grep -v '^##' | wc -w | awk '{$1=$1;print}'`
60-
if [ ${changes} != "0" ]; then
61-
# update CHANGELOG.md
62-
perl -i -pe 's/## Unreleased.*/## Unreleased\n\n## '${VERSION}'\n\nReleased '${RELEASE_DATE}'/' ${f};
63-
git add ${f};
64-
echo "Updating ${f}"
65-
else
66-
echo "Skipping ${f}, no changes detected"
67-
fi
68-
done
69-
if [ ${errors} != 0 ]; then
70-
echo "::set-output name=version_updated::0"
71-
exit 0
72-
fi
73-
}
74-
7524
# create the release branch
7625
git fetch origin master
7726
git checkout master
@@ -81,8 +30,15 @@ git push origin release/${VERSION}
8130

8231
# create a temporary branch to create a PR for updated version and changelogs
8332
git checkout -b release/${VERSION}-auto
84-
update_version_file
85-
update_changelog
33+
./scripts/eachdist.py release --version ${VERSION}
34+
rc=$?
35+
if [ $rc != 0 ]; then
36+
echo "::set-output name=version_updated::0"
37+
exit 0
38+
fi
39+
40+
git add **/version.py **/setup.cfg **/CHANGELOG.md
41+
8642
git commit -m "updating changelogs and version to ${VERSION}"
8743

8844
echo "Time to create a release, here's a sample title:"

0 commit comments

Comments
 (0)