Skip to content

Commit 514f9c8

Browse files
authored
Update stats_cli.py
1 parent ea232f9 commit 514f9c8

1 file changed

Lines changed: 62 additions & 9 deletions

File tree

aws_doc_sdk_examples_tools/stats_cli.py

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@
1616
# Path to the YAML file you want to extract from each commit
1717
FILE_PATH = "tools/update_mirror/config.yaml"
1818

19-
# Function to run git commands
2019
def run_git_command(command, cwd=None):
20+
"""
21+
Run a Git command and return the output.
22+
23+
Args:
24+
command (str): The Git command to run.
25+
cwd (str, optional): The directory to run the command in. Defaults to None.
26+
27+
Returns:
28+
str: The output of the Git command, or None if an error occurred.
29+
"""
2130
try:
2231
result = subprocess.run(command, cwd=cwd, text=True, capture_output=True, shell=True)
2332
if result.returncode != 0:
@@ -27,8 +36,15 @@ def run_git_command(command, cwd=None):
2736
print(f"{Fore.RED}Error running command: {command}\n{Fore.RED}Error: {e}")
2837
return None
2938

30-
# Function to clone a repository if it's not already cloned
3139
def clone_or_reuse_repo(repo_url, branch, clone_dir):
40+
"""
41+
Clone a Git repository if it doesn't already exist, or reuse the existing clone.
42+
43+
Args:
44+
repo_url (str): The URL of the repository to clone.
45+
branch (str): The branch to check out.
46+
clone_dir (str): The directory to clone the repository into.
47+
"""
3248
if os.path.exists(clone_dir):
3349
print(f"{Fore.YELLOW}Repository {repo_url} already cloned in {clone_dir}. Reusing existing clone.")
3450
else:
@@ -41,8 +57,17 @@ def clone_or_reuse_repo(repo_url, branch, clone_dir):
4157
checkout_cmd = f"git checkout {branch}"
4258
run_git_command(checkout_cmd, cwd=clone_dir)
4359

44-
# Function to get the latest commit hash for the specific age in the repo
4560
def get_commit_hash_for_age(repo_dir, age):
61+
"""
62+
Get the commit hash for a specific age in the repository's history.
63+
64+
Args:
65+
repo_dir (str): The path to the Git repository.
66+
age (str): The age to search for (e.g., '1 month ago').
67+
68+
Returns:
69+
str: The commit hash corresponding to the specified age, or None if not found.
70+
"""
4671
log_cmd = f'git rev-list -1 --before="{age}" HEAD'
4772
commit_hash = run_git_command(log_cmd, cwd=repo_dir)
4873
if commit_hash:
@@ -51,14 +76,27 @@ def get_commit_hash_for_age(repo_dir, age):
5176
print(f"{Fore.RED}Failed to find commit hash for {age} in {repo_dir}")
5277
return None
5378

54-
# Function to force checkout to the specific commit hash
5579
def checkout_commit(repo_dir, commit_hash):
80+
"""
81+
Checkout a specific commit hash in the repository.
82+
83+
Args:
84+
repo_dir (str): The path to the Git repository.
85+
commit_hash (str): The commit hash to checkout.
86+
"""
5687
print(f"{Fore.CYAN}Checking out commit {commit_hash} in {repo_dir}")
5788
checkout_cmd = f'git checkout --force {commit_hash}'
5889
run_git_command(checkout_cmd, cwd=repo_dir)
5990

60-
# Function to run the specific Git log command and Python command
6191
def run_commands_in_repo(repo_dir, commit_hash, age):
92+
"""
93+
Run Git log and a Python command in a specific commit of a repository.
94+
95+
Args:
96+
repo_dir (str): The path to the repository.
97+
commit_hash (str): The commit hash to run the commands in.
98+
age (str): The age of the commit being processed.
99+
"""
62100
# Checkout the repository to the specific commit
63101
checkout_commit(repo_dir, commit_hash)
64102

@@ -74,7 +112,7 @@ def run_commands_in_repo(repo_dir, commit_hash, age):
74112

75113
print(f"{Fore.MAGENTA}Commit for {age}: {commit_hash}, Author: {author_name}, Date: {commit_date}")
76114

77-
# Now run the Python command on the repository
115+
# Run the Python command on the repository
78116
python_cmd = f'python3 -m aws_doc_sdk_examples_tools.stats "{repo_dir}"'
79117
print(f"{Fore.CYAN}Running stats command for repository: {repo_dir}")
80118
output = run_git_command(python_cmd, cwd=repo_dir)
@@ -84,14 +122,24 @@ def run_commands_in_repo(repo_dir, commit_hash, age):
84122
else:
85123
print(f"{Fore.RED}No commit found for {age} in {repo_dir}")
86124

87-
# Main function to gather file contents from specific commits and clone repositories
88125
def get_file_from_commits_and_clone(repo_path, file_path, ages):
126+
"""
127+
Extract file contents from specific commits and clone repositories for each mirror.
128+
129+
Args:
130+
repo_path (str): Path to the main repository.
131+
file_path (str): Path to the YAML file within the repository.
132+
ages (list): List of age ranges to retrieve commit hashes for.
133+
134+
Returns:
135+
dict: A dictionary mapping each age to the mirrors section of the YAML file.
136+
"""
89137
age_content_dict = {}
90138
cloned_repos = {} # To track cloned repositories and their directories
91139

92140
# Create a temporary directory for the clones
93141
with tempfile.TemporaryDirectory() as tmp_dir:
94-
# First, fetch the configuration file from the main repository for each age
142+
# Fetch the configuration file from the main repository for each age
95143
for age in ages:
96144
print(f"{Style.BRIGHT}{Fore.BLUE}#############################################################")
97145
print(f"{Style.BRIGHT}{Fore.BLUE}######################## {age.upper()} ##############################")
@@ -147,8 +195,13 @@ def get_file_from_commits_and_clone(repo_path, file_path, ages):
147195

148196
return age_content_dict
149197

150-
# Review the contents (age range as key and the list of mirror repositories as values)
151198
def display_age_content_dict(age_content_dict):
199+
"""
200+
Display the mirrors section extracted from the YAML files, grouped by age.
201+
202+
Args:
203+
age_content_dict (dict): A dictionary with age as keys and mirrors data as values.
204+
"""
152205
print(f"{Style.BRIGHT}{Fore.GREEN}File contents grouped by age range:")
153206
for age, mirrors in age_content_dict.items():
154207
print(f"{Fore.YELLOW}Age: {age}")

0 commit comments

Comments
 (0)