Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into develop
  • Loading branch information
Tarunmeena0901 committed Jan 7, 2024
2 parents 3cd72cd + cddb99f commit 6124795
Show file tree
Hide file tree
Showing 83 changed files with 4,276 additions and 1,000 deletions.
170 changes: 170 additions & 0 deletions .github/workflows/compare_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
"""Script to encourage more efficient coding practices.
Methodology:
Utility for comparing translations between default and other languages.
This module defines a function to compare two translations
and print any missing keys in the other language's translation.
Attributes:
FileTranslation : Named tuple to represent a combination
of file and missing translations.
Fields:
- file (str): The file name.
- missing_translations (list): List of missing translations.
Functions:
compare_translations(default_translation, other_translation):
Compare two translations and print missing keys.
load_translation(filepath):
Load translation from a file.
check_translations():
Load the default translation and compare it with other translations.
main():
The main function to run the script.
Parses command-line arguments, checks for the
existence of the specified directory, and then
calls check_translations with the provided or default directory.
Usage:
This script can be executed to check and print missing
translations in other languages based on the default English translation.
Example:
python compare_translations.py
NOTE:
This script complies with our python3 coding and documentation standards
and should be used as a reference guide. It complies with:
1) Pylint
2) Pydocstyle
3) Pycodestyle
4) Flake8
"""
# standard imports
import argparse
import json
import os
import sys
from collections import namedtuple

# Named tuple for file and missing
# translations combination
FileTranslation = namedtuple("FileTranslation",
["file", "missing_translations"])


def compare_translations(default_translation,
other_translation, default_file, other_file):
"""Compare two translations and return detailed info about missing/mismatched keys.
Args:
default_translation (dict): The default translation (en.json).
other_translation (dict): The other language translation.
default_file (str): The name of the default translation file.
other_file (str): The name of the other
translation file.
Returns:
list: A list of detailed error messages for each missing/mismatched key.
"""
errors = []

# Check for missing keys in other_translation
for key in default_translation:
if key not in other_translation:
error_msg = f"Missing Key: '{key}' - This key from '{default_file}' is missing in '{other_file}'."
errors.append(error_msg)
# Check for keys in other_translation that don't match any in default_translation
for key in other_translation:
if key not in default_translation:
error_msg = f"Error Key: '{key}' - This key in '{other_file}' does not match any key in '{default_file}'."
errors.append(error_msg)
return errors


def load_translation(filepath):
"""Load translation from a file.
Args:
filepath: Path to the translation file
Returns:
translation: Loaded translation
"""
with open(filepath, "r", encoding="utf-8") as file:
translation = json.load(file)
return translation


def check_translations(directory):
"""Load default translation and compare with other translations.
Args:
directory (str): The directory containing translation files.
Returns:
None
"""
default_file = "en.json"
default_translation = load_translation(os.path.join(directory, default_file))
translations = os.listdir(directory)
translations.remove(default_file) # Exclude default translation

error_found = False

for translation_file in translations:
other_file = os.path.join(directory, translation_file)
other_translation = load_translation(other_file)

# Compare translations and get detailed error messages
errors = compare_translations(
default_translation, other_translation, default_file, translation_file
)
if errors:
error_found = True
print(f"File {translation_file} has missing translations for:")
for error in errors:
print(f" - {error}")

if error_found:
sys.exit(1) # Exit with an error status code
else:
print("All translations are present")
sys.exit(0)


def main():
"""
Parse command-line arguments, check for the existence of the specified directory
and call check_translations with the provided or default directory.
"""
parser = argparse.ArgumentParser(
description="Check and print missing translations for all non-default languages."
)
parser.add_argument(
"--directory",
type=str,
nargs="?",
default=os.path.join(os.getcwd(), "public/locales"),
help="Directory containing translation files(relative to the root directory).",
)
args = parser.parse_args()

if not os.path.exists(args.directory):
print(f"Error: The specified directory '{args.directory}' does not exist.")
sys.exit(1)

check_translations(args.directory)


if __name__ == "__main__":
main()
132 changes: 132 additions & 0 deletions .github/workflows/count_changed_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""Script to limit number of file changes in single PR.
Methodology:
Analyses the Pull request to find if the count of file changed in a pr
exceeds a pre-defined nummber 20
This scripts encourages contributors to align with project practices,
reducing the likelihood of unintentional merges into incorrect branches.
NOTE:
This script complies with our python3 coding and documentation standards.
It complies with:
1) Pylint
2) Pydocstyle
3) Pycodestyle
4) Flake8
"""

import sys
import argparse
import subprocess


def _count_changed_files(base_branch, pr_branch):
"""
Count the number of changed files between two branches.
Args:
base_branch (str): The base branch.
pr_branch (str): The PR branch.
Returns:
int: The number of changed files.
Raises:
SystemExit: If an error occurs during execution.
"""
base_branch = f"origin/{base_branch}"
pr_branch = f"origin/{pr_branch}"

command = f"git diff --name-only {base_branch}...{pr_branch} | wc -l"

try:
# Run git command to get the list of changed files
process = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
output, error = process.communicate()
except Exception as e:
print(f"Error: {e}")
sys.exit(1)

file_count = int(output.strip())
return file_count

def _arg_parser_resolver():
"""Resolve the CLI arguments provided by the user.
Args:
None
Returns:
result: Parsed argument object
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--base_branch",
type=str,
required=True,
help="Base branch where pull request should be made."
),
parser.add_argument(
"--pr_branch",
type=str,
required=True,
help="PR branch from where the pull request is made.",
),
parser.add_argument(
"--file_count",
type=int,
default=20,
help="Number of files changes allowed in a single commit")
return parser.parse_args()


def main():
"""
Execute the script's main functionality.
This function serves as the entry point for the script. It performs
the following tasks:
1. Validates and retrieves the base branch and PR commit from
command line arguments.
2. Counts the number of changed files between the specified branches.
3. Checks if the count of changed files exceeds the acceptable
limit (20).
4. Provides informative messages based on the analysis.
Raises:
SystemExit: If an error occurs during execution.
"""

args = _arg_parser_resolver()

base_branch = args.base_branch
pr_branch = args.pr_branch

print(f"You are trying to merge on branch: {base_branch}")
print(f"You are making commit from your branch: {pr_branch}")

# Count changed files
file_count = _count_changed_files(base_branch, pr_branch)
print(f"Number of changed files: {file_count}")

# Check if the count exceeds 20
if file_count > args.file_count:
print("Error: Too many files (greater than 20) changed in the pull request.")
print("Possible issues:")
print("- Contributor may be merging into an incorrect branch.")
print("- Source branch may be incorrect please use develop as source branch.")
sys.exit(1)


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

0 comments on commit 6124795

Please sign in to comment.