Cegal tech blog

Automate resources version updates in project files with Python

Written by Cegal Tech Advisors | May 31, 2023 7:48:00 AM

Keeping track of and managing resources versions and version numbers can be a tedious task, especially when working on large projects with many files. It becomes even more cumbersome when these version numbers are embedded within the code across different files. However, automation with Python can come to the rescue. Working with countless integrations myself that on a regular basis needs a version change in WSDL or XSD resources because of the new element introduced or any type of change where is good to have a new version number of the service resource used, this post may be useful for you.

Today, I'll be introducing a simple Python script that helps automate the process of updating version strings and namespaces in a project's files. This script is useful in scenarios where a project has multiple files that reference a particular version string or a namespace that needs to be updated regularly, for instance, in Web Services Description Language (WSDL) and XML Schema Definition (XSD) resources. This I have done manually:

  • Create a new resource file version Service_v1.wsdl becomes Service_v2.wsdl that will be used in the project moving forward. In addition, .XSD resources follow these.
  • Find all the files in which the namespace needs to be updated
  • Open each file manually and do a find and replace
  • Check again that all the correct files have been updated
  • Most often than not my build or deployment of the project fails because of something I missed. Big human error factor here. Go back find and replace to the correct namespace.

This is exactly what the script does automatically. It works by walking through each file in a specified project directory, checking for an old version string/namespace, and replacing it with a new version string/namespace. It also has the capability to exclude specific files from this operation, which is sometimes necessary when dealing with specific files that should not be altered.

How to Use the Script

Here are the configuration steps to use the script (Dependencies: Python 3):

  1. Update the configRun.py script with the namespaces that need to be found and replaced. The old_version : new_version pairs represent the strings you want to find and replace in your project files. You can add as many pairs as you want.
  2. Set the project directory to the correct path
  3. Back up the project before you run the script. - If needed, you can always just download the latest version from the version control
  4. Set the files that you want to exclude from changes

Executing program

  1. Navigate to the location of the script, for example:
  1. run the configRun.py script in Windows cmd:

The Code

Here is the code for configRun.py:

Here is the replace_versions function where all the logic happens:

 

The script reads the configuration from the configRun.py file, performs the find/replace operations and prints a report indicating where replacements were made and how many replacements were made per file. If no replacements were made, it outputs the message "No replacements made in the project files".

The Power of Automation

The advantage of using a script like this is the time saved from manually searching and replacing version numbers in a large number of files. It also minimizes the potential for human error that could result from manual replacement.

Furthermore, the script has built-in flexibility. For example, you can easily update the strings to replace or add new ones by simply updating the configRun.py script. Also, you can specify files to be excluded from the operation, providing granular control over which files are affected by the version updates.

In conclusion, automating version updates in project files is a great example of how Python can be leveraged to perform routine tasks more efficiently and accurately. This not only saves precious development time but also helps maintain a more robust codebase. If you frequently find yourself making manual version updates across multiple files, give this script a try!