import os import shutil import zipfile import re from datetime import datetime def calculate_file_percentages(directory, file_counts): code_extensions = ['.html', '.css', '.js', '.py'] # Add more extensions if needed # Count the occurrences of each file extension for filename in os.listdir(directory): if os.path.isfile(os.path.join(directory, filename)): _, extension = os.path.splitext(filename) if extension: extension = extension.lower() # Convert extension to lowercase file_counts[extension] = file_counts.get(extension, 0) + 1 if extension in code_extensions: file_path = os.path.join(directory, filename) with open(file_path, 'r', encoding='utf-8') as file: content = file.read() if contains_code(content, extension): file_counts[extension] += 1 def contains_code(content, extension): if extension == '.html': css_pattern = r'' return re.search(css_pattern, content, re.IGNORECASE) elif extension == '.css': # Modify this pattern to match your CSS content detection requirements css_pattern = r'\w+\s*{\s*\w+\s*:\s*\w+\s*;.*?}' return re.search(css_pattern, content) elif extension == '.js': js_pattern = r'' return re.search(js_pattern, content, re.IGNORECASE) elif extension == '.py': # Modify this pattern to match your Python content detection requirements py_pattern = r'\bimport\b|\bfrom\b' return re.search(py_pattern, content) else: return False def find_files_with_extensions(extensions): script_directory = os.path.dirname(os.path.abspath(__file__)) found_files = [] for root, _, files in os.walk(script_directory): for file in files: if file.endswith(tuple(extensions)): found_files.append(os.path.join(root, file)) return found_files def get_non_ignored_files(files, ignore_folder): def is_inside_ignore_folder(file_path, ignore_folder): ignore_folder = os.path.abspath(ignore_folder) file_path = os.path.abspath(file_path) return file_path.startswith(ignore_folder) non_ignored_files = [] for file in files: abs_file_path = os.path.abspath(file) if not is_inside_ignore_folder(abs_file_path, ignore_folder) \ and not is_inside_ignore_folder(abs_file_path, 'prevvers') \ and not is_inside_ignore_folder(abs_file_path, 'futures') \ and not abs_file_path.endswith('exp'): non_ignored_files.append(file) return non_ignored_files def convert_to_txt(files): txt_files = [] for file in files: if os.path.exists(file): # Check if the source file exists new_filename = os.path.splitext(file)[0] + ".txt" try: os.rename(file, new_filename) txt_files.append(new_filename) except Exception as e: print(f"Error renaming file {file} to {new_filename}: {e}") else: print(f"Source file {file} does not exist. Skipping.") return txt_files def duplicate_files(files, output_folder): if not os.path.exists(output_folder): os.mkdir(output_folder) duplicated_files = [] for file in files: # Exclude files named "freerepo.py" if os.path.basename(file) != "freerepo.py": duplicated_file = os.path.join(output_folder, os.path.basename(file) + ".duplicate") shutil.copy2(file, duplicated_file) duplicated_files.append(duplicated_file) return duplicated_files def is_inside_ignore_folder(file_path, ignore_folder): if os.path.isabs(file_path) and os.path.isabs(ignore_folder): return os.path.commonpath([file_path, ignore_folder]) == ignore_folder elif not os.path.isabs(file_path) and not os.path.isabs(ignore_folder): return os.path.commonpath([file_path, os.path.abspath(ignore_folder)]) == os.path.abspath(ignore_folder) else: return False def generate_html_with_iframes_and_p_elements(files, output_file, branches_html_file, file_percentages): with open(output_file, "w") as f: f.write('') f.write('\n') f.write('\n') f.write('\n') f.write('\n') f.write('\n') f.write('

Your Repo

\n') f.write('

Edit the yourrepo.html to change this details.

\n') f.write('

\n') f.write('

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce interdum vitae eros in luctus.\n') f.write('
Suspendisse potenti. In hac habitasse platea dictumst.\n') f.write('
Pellentesque tincidunt iaculis turpis et luctus.

\n') f.write('

\n') f.write('

* COMING SOON *

\n') f.write('

Sed et libero sed quam vulputate ultrices.

\n') f.write('\n') f.write('\n') f.write(f'\n') f.write(f'\n') f.write(f'\n') f.write(f'

Code Data:

\n') f.write(f'\n') for file in files: filename = os.path.basename(file) file_name_without_extension = os.path.splitext(filename)[0] f.write(f'

{file_name_without_extension}

\n') f.write(f'\n') f.write(f'

\n') f.write('\n') f.write('') def generate_ex_html(ex_html_file): with open(ex_html_file, "w") as f: f.write("\n") f.write("\n") f.write("Ex1\n") f.write("\n") f.write("\n") f.write('

This is the Ex1 HTML page

\n') f.write('\n') f.write("\n") f.write("\n") def generate_commit_msg_html(commit_msg_file, commit_msg, file_percentages): current_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') with open(commit_msg_file, "w") as f: f.write("\n") f.write("\n") f.write("Commit Message\n") f.write("\n") f.write("\n") f.write(f"

Date: {current_date}

\n") f.write(f"

Commit msg: {commit_msg}

\n") f.write("

File Percentages:

\n") f.write(f"
{file_percentages}
\n") f.write("\n") f.write("\n") def calculate_file_percentages(directory, file_counts): code_extensions = ['.html', '.css', '.js', '.py'] # Add more extensions if needed for filename in os.listdir(directory): if os.path.isfile(os.path.join(directory, filename)): if filename != 'freerepo.py': # Exclude 'freerepo.py' from the calculation _, extension = os.path.splitext(filename) if extension: extension = extension.lower() # Convert extension to lowercase file_counts[extension] = file_counts.get(extension, 0) + 1 def generate_file_percentages_html(file_counts, output_file): total_files = sum(file_counts.values()) with open(output_file, "w") as f: f.write("\n") f.write("\n") f.write("File Percentages\n") f.write("\n") f.write("\n") f.write("

File Percentages

\n") for extension, count in file_counts.items(): percentage = (count / total_files) * 100 f.write(f"

{extension}: {percentage:.2f}%

\n") f.write("\n") f.write("\n") def main(): print("Choose an option:") print("1. Create Repo") print("2. Update Repo") option = input("Enter the option number (1 or 2): ") output_folder = "YourRepo" if option == "1": extensions_to_find = [".md", ".html", ".css", ".js", ".py"] matching_files = find_files_with_extensions(extensions_to_find) if not os.path.exists(output_folder): os.mkdir(output_folder) ignore_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "ignore")) non_ignored_files = get_non_ignored_files(matching_files, ignore_folder) duplicated_files = duplicate_files(non_ignored_files, output_folder) txt_files = convert_to_txt(duplicated_files) output_html_file = os.path.join(output_folder, "yourrepo.html") branches_html_file = os.path.join(output_folder, "branches.html") file_counts = {} calculate_file_percentages(os.path.dirname(os.path.abspath(__file__)), file_counts) total_files = sum(file_counts.values()) file_percentages_html_file = os.path.join(output_folder, "file_percentages.html") generate_file_percentages_html(file_counts, file_percentages_html_file) generate_html_with_iframes_and_p_elements(txt_files, output_html_file, branches_html_file, file_percentages_html_file) generate_ex_html(os.path.join(output_folder, "ex1.html")) with zipfile.ZipFile(os.path.join(output_folder, "yourrepo.zip"), "w") as zipf: for file in non_ignored_files: # Exclude files named "freerepo.py" from the zip archive if not os.path.basename(file) == "freerepo.py": zipf.write(file, os.path.relpath(file, start=output_folder)) print(f"HTML file '{output_html_file}' created with iframes,

elements, and file percentages.") print("The generated files are stored in the 'YourRepo' folder.") elif option == "2": extensions_to_find = [".md", ".html", ".css", ".js", ".py"] matching_files = find_files_with_extensions(extensions_to_find) ignore_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "ignore")) non_ignored_files = get_non_ignored_files(matching_files, ignore_folder) duplicated_files = duplicate_files(non_ignored_files, output_folder) txt_files = convert_to_txt(duplicated_files) commit_msg_html_file = os.path.join(output_folder, "commit_message.html") file_counts = {} calculate_file_percentages(os.path.dirname(os.path.abspath(__file__)), file_counts) total_files = sum(file_counts.values()) file_percentages = "" for extension, count in file_counts.items(): percentage = (count / total_files) * 100 file_percentages += f"{extension}: {percentage:.2f}%\n" commit_msg = input("Enter the commit message: ") generate_commit_msg_html(commit_msg_html_file, commit_msg, file_percentages) print(f"HTML file '{commit_msg_html_file}' created with the specified commit message and file percentages.") print("The generated files are stored in the 'YourRepo' folder.") with zipfile.ZipFile(os.path.join(output_folder, "yourrepo.zip"), "w") as zipf: for file in non_ignored_files: # Exclude files named "freerepo.py" from the zip archive if not os.path.basename(file) == "freerepo.py": zipf.write(file, os.path.relpath(file, start=output_folder)) if __name__ == "__main__": while True: main() run_again = input("Do you want to run another script? (y/n): ") if run_again.lower() != "y": break print("Goodbye!")