Skip to content

feat: add Versioning tool/script #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pydatastructs/utils/tests/test_code_quality.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os, re, sys, pydatastructs, inspect
from typing import Type

def _list_files(checker):
def _list_files(checker, root_path=None):
root_path = os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
os.pardir, os.pardir))
os.pardir, os.pardir)) if root_path is None else root_path
code_files = []
for (dirpath, _, filenames) in os.walk(root_path):
for _file in filenames:
Expand Down
70 changes: 70 additions & 0 deletions scripts/versioning/update_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import sys

from pydatastructs.utils.tests.test_code_quality import _list_files

# Files to be updated with the new version number
# The first element of the tuple is the directory path
# and the second element is a lambda function that
# returns True if the file should be updated.
FILES_TO_BE_SCANNED = {
'pydatastructs': ('./pydatastructs', lambda _file: _file.endswith('.py')),
'docs': ('./docs/source', lambda _file: _file.endswith('.rst') or _file.endswith('.py'))
}


def update_version_in_files(file_paths, origin_version, new_version):
"""
Updates the version number in the specified files.

Parameters
==========

file_paths: list
List of file paths to be updated.
origin_version: str
The original version number to be replaced.
new_version: str
The new version number to replace the original.

Returns
=======

None
"""
was_updated = False
for path in file_paths:
with open(path, 'r') as file:
data = file.read()
if origin_version in data:
was_updated = True
data = data.replace(origin_version, new_version)
with open(path, 'w') as file:
file.write(data)

return was_updated


def main():
if len(sys.argv) != 3:
print('Usage: python update_version.py <origin_version> <new_version>')
return

origin_version, new_version = sys.argv[1], sys.argv[2]
print(f'Updating version number from {origin_version} to {new_version}...')

file_paths = ['README.md', 'setup.py']

for _, (dir_path, checker) in FILES_TO_BE_SCANNED.items():
file_paths.extend(_list_files(checker, dir_path))

was_updated = update_version_in_files(
file_paths, origin_version, new_version)

if was_updated:
print('Version number updated successfully!')
else:
print('WARNING: Version number not found in the specified files.')


if __name__ == "__main__":
main()
Loading