How to Write a Python Script to Create and Update a Changelog | by Matthew Croak | Apr, 2022

No extra turbines, logs, or dependencies-now for Python!

Photograph by Javier Allegue Barros on Unsplash

In case you learn my previous post about learn how to write a bash script to create and replace a changelog, you would possibly discover this publish fascinating as properly. I used to be engaged on some python scripts for work when it occurred to me that some Python devs would possibly admire having a changelog script in Python.

Now they do!

I’ve added Python support to increase my original changelog script to engineers with a choice for Python. Now you can use the identical script logic with Python code. In case you’re a python developer and wish to add extra customizations to your changelog creation/era, you are able to do so extra simply now with this added help.

Because it’s the identical logic as my earlier script, I gained’t go too in depth when it comes to performance. As a substitute, I’ll spotlight some syntactic variations. Let’s bounce proper in!

First, create a changelog.py file to run the script’s logic. You can also make this file executable by operating chmod 755 changelog.py. With this it is possible for you to to run ./changelog.py out of your command line or embody it in one other script if that’s most popular by appending ./changelog.py to it.

Subsequent, we’d like our undertaking’s model to be synced with our changelog. Right here is how we get the model in Bash.

model="$(git describe --long)"

In Python, you’ll be able to obtain the identical consequence with the beneath code.

model=subprocess.check_output([“git”, “describe”, “ — long”]).strip()

In different elements of the code, you have to to make use of string interpolation. That is carried out slightly in a different way in Python than it’s in Bash. See beneath for Bash string interpolation.

identify="Matthew Croak"
introduction="My identify is $identify"
echo $introduction> "My identify is Matthew"

Now right here is string interpolation for Python.

identify="Matthew Croak"
introduction="My identify is ".format(identify)
print(introduction)> "My identify is Matthew"

String interpolation is necessary for the script as a result of we can be writing and rewriting the changelog file from offered strings. These strings will comprise up to date info, similar to as we speak’s date and the model of the bundle.

Subsequent, let’s write our init operate. This operate can be used to find out whether or not or not we wish to create a brand new changelog or just replace an present one. In bash, you test for the existence of a file like so.

if take a look at -f "$changelog" ; then
new_changelog_item
else
new_changelog
fi

In Python, we have to import the exists operate from os.path.

To import this operate, merely put from os.path import exists on the high of your changelog.py file. As soon as we’ve got it imported we will test for CHANGELOG.md with the beneath code.

if exists('CHANGELOG.md'):
new_changelog_item()
else:
new_changelog()

Subsequent, let’s go over how we learn and write our changelog file. In Bash, that is the way you learn the file:

whereas learn line; do           
# code carried out for every line
carried out < CHANGELOG.md

In Python, we make use of the in-built open and readlines features.

changelog = open('CHANGELOG.md', 'r')
changelog_items = changelog.readlines()
for line in changelog_items:
# code carried out for every line

For extra on learn how to learn a file in Python, try this post.

Along with needing to learn our file, we additionally must replace our changelog file.

In Bash, that’s carried out utilizing sed.

whereas learn line; do
if [[ $line == "## [Unreleased]"* ]]; then
sed -i 's/old_text/new_text/' CHANGELOG.md
carried out < CHANGELOG.md

In Python, we will make use of the in-built features open and writelines.

changelog = open('CHANGELOG.md', 'w')
changelog_items = changelog.writelines(new_lines)

You discover how we use "r" once we use open for studying a file and "w" once we use open for writing to a file. Under is the remainder of the code we use for updating a particular line within the changelog utilizing Python.

index = -1
changelog_items=read_changelog()
for line in changelog_items:
index+=1
if "## [Unreleased]" in line:
changelog_items[index] = "## [Unreleased]nn### Addedn- ADD CHANGE HERE!n".format(merchandise)
write_changelog_lines(changelog_items)
break

I broke out the learn/write code into their very own features to make the code extra readable. Under is the code for read_changelog.

def read_changelog():
changelog = open('CHANGELOG.md', 'r')
changelog_items = changelog.readlines()
return changelog_items

And right here is the code for write_changelog_lines.

def write_changelog_lines(changelog_items):
with open('CHANGELOG.md', 'w') as changelog:
changelog.writelines(changelog_items)

And there you’ve gotten it! Added python help for a pure script to include in your app to generate a changelog, add changelog gadgets. It additionally provides you flexibility when it comes to format and changelog content material with out the necessity for turbines, logs, or different dependencies. The total code is on GitHub.

More Posts