194 lines
9.1 KiB
Python
194 lines
9.1 KiB
Python
import os
|
||
import sys
|
||
import time
|
||
import pathlib
|
||
import subprocess
|
||
|
||
################################################################################
|
||
def add_indent(input_str, num_spaces, leading_char, secondary_char=None):
|
||
primary_char_indent = (' ' * num_spaces) + leading_char + ' '
|
||
if secondary_char:
|
||
secondary_char_indent = (' ' * num_spaces) + secondary_char + ' '
|
||
else:
|
||
secondary_char_indent = primary_char_indent
|
||
return (primary_char_indent + input_str).replace('\n', '\n' + secondary_char_indent)
|
||
|
||
################################################################################
|
||
def find_allspice_config_files_in_depot(depot_root):
|
||
print("🔸 Finding AllSpice project folders...")
|
||
# Initialize a list for storing found allspice.txt paths
|
||
config_file_paths = []
|
||
# Walk the depot tree
|
||
for root, dirs, files in os.walk(depot_root):
|
||
for file_name in files:
|
||
if file_name == "allspice.txt":
|
||
config_file_path = os.path.join(root, file_name)
|
||
print(add_indent(os.path.dirname(config_file_path), 8, '📂'))
|
||
config_file_paths.append(config_file_path)
|
||
# Return config file paths found
|
||
return config_file_paths
|
||
|
||
################################################################################
|
||
def initialize_git_repo():
|
||
print(add_indent("Initializing git repository", 8, '🛠️'))
|
||
if os.path.isdir('.git'):
|
||
print(add_indent(".git folder already exists", 15, '🔹'))
|
||
else:
|
||
git_init_output = os.popen("git init .").read().strip()
|
||
print(add_indent(git_init_output, 15, '➖'))
|
||
|
||
################################################################################
|
||
def add_allspice_remote():
|
||
print(add_indent("Adding AllSpice remote", 8, '➕'))
|
||
# Check if remote already exists
|
||
git_remote_list_output = os.popen("git remote -v").read().strip()
|
||
if allspice_repo_url in git_remote_list_output:
|
||
print(add_indent("AllSpice remote already exists", 15, '🔹'))
|
||
else:
|
||
git_add_remote_output = os.popen("git remote add origin " + allspice_repo_url).read().strip()
|
||
print(add_indent(git_add_remote_output, 15, '➖'))
|
||
|
||
################################################################################
|
||
def fetch_from_allspice():
|
||
print(add_indent("Fetching from AllSpice remote", 8, '🔄'))
|
||
git_fetch_process = subprocess.Popen(['git fetch'], stdout = subprocess.PIPE, stderr = subprocess.PIPE, text = True, shell = True)
|
||
std_out, std_err = git_fetch_process.communicate()
|
||
if std_out or std_err:
|
||
print(add_indent(std_out.strip(), 15, '➖'))
|
||
print(add_indent(std_err.strip(), 15, '➖'))
|
||
else:
|
||
print(add_indent("Nothing new...", 15, '🔹'))
|
||
|
||
################################################################################
|
||
def checkout_develop_branch():
|
||
print(add_indent("Checking out develop branch", 8, '🛒'))
|
||
git_checkout_develop_process = subprocess.Popen(['git checkout develop'], stdout = subprocess.PIPE, stderr = subprocess.PIPE, text = True, shell = True)
|
||
std_out, std_err = git_checkout_develop_process.communicate()
|
||
print(add_indent(std_err.strip(), 15, '➖'))
|
||
print(add_indent(std_out.strip(), 15, '➖'))
|
||
|
||
################################################################################
|
||
def show_git_status():
|
||
print(add_indent("Git status:", 8, '📋'))
|
||
git_status_output = os.popen("git status").read().strip()
|
||
if "Untracked" in git_status_output:
|
||
print(add_indent(git_status_output, 15, '➖'))
|
||
else:
|
||
print(add_indent(git_status_output, 15, '➖'))
|
||
|
||
################################################################################
|
||
def get_list_of_modified_files():
|
||
print(add_indent("Modified files found:", 8, '▪️'))
|
||
git_modified_files_output = os.popen("git ls-files --modified").read().strip()
|
||
modified_files = git_modified_files_output.split('\n') if git_modified_files_output else []
|
||
if modified_files:
|
||
for cur_file in modified_files:
|
||
print(add_indent(str(cur_file), 15, '📄'))
|
||
else:
|
||
print(add_indent("None", 15, '🔹'))
|
||
return modified_files
|
||
|
||
################################################################################
|
||
def get_list_of_untracked_files():
|
||
print(add_indent("Untracked files found:", 8, '▪️'))
|
||
git_untracked_files_output = os.popen("git ls-files --others --exclude-standard").read().strip()
|
||
untracked_files = git_untracked_files_output.split('\n') if git_untracked_files_output else []
|
||
if untracked_files:
|
||
for cur_file in untracked_files:
|
||
print(add_indent(str(cur_file), 15, '📄'))
|
||
else:
|
||
print(add_indent("None", 15, '🔹'))
|
||
return untracked_files
|
||
|
||
################################################################################
|
||
def get_list_of_added_files_not_yet_committed():
|
||
print(add_indent("Added files for commit found:", 8, '▪️'))
|
||
git_changes_to_be_committed_output = os.popen("git diff --cached --name-only").read().strip()
|
||
added_files = git_changes_to_be_committed_output.split('\n') if git_changes_to_be_committed_output else []
|
||
if added_files:
|
||
for cur_file in added_files:
|
||
print(add_indent(str(cur_file), 15, '📄'))
|
||
else:
|
||
print(add_indent("None", 15, '🔹'))
|
||
return added_files
|
||
|
||
################################################################################
|
||
def add_modified_or_untracked_files_for_commit():
|
||
git_add_output = os.popen("git add .").read().strip()
|
||
print(add_indent("Added modified and/or untracked files to stage for commit", 8, '➕'))
|
||
|
||
################################################################################
|
||
def prompt_user_for_commit_message():
|
||
print("")
|
||
print(add_indent("Please enter your commit message for project " + proj_dir + ":", 8, '➡️'))
|
||
commit_message = input()
|
||
print("")
|
||
return commit_message
|
||
|
||
################################################################################
|
||
def commit_changes(commit_message):
|
||
print(add_indent("Committing with the following message: " + "\"" + commit_message + "\"", 8, '✉️'))
|
||
git_commit_output = os.popen("git commit -m \"" + commit_message + "\"").read().strip()
|
||
print(add_indent(git_commit_output, 15, '➖'))
|
||
|
||
################################################################################
|
||
def push_commit_to_allspice():
|
||
print(add_indent("Pushing changes to AllSpice on the develop branch...", 8, '📤'))
|
||
git_push_process = subprocess.Popen(['git push'], stdout = subprocess.PIPE, stderr = subprocess.PIPE, text = True, shell = True)
|
||
std_out, std_err = git_push_process.communicate()
|
||
print(add_indent(std_err.strip(), 15, '➖'))
|
||
print(add_indent(std_out.strip(), 15, '➖'))
|
||
|
||
################################################################################
|
||
if __name__ == "__main__":
|
||
print("🔸 Starting Perforce -> AllSpice sync...")
|
||
# Get the absolute path to depot root
|
||
depot_root_absolute_path = os.path.abspath(".")
|
||
# Find AllSpice config files in the Perforce depot
|
||
allspice_config_paths = find_allspice_config_files_in_depot(".")
|
||
print("\n--------------------------------------------------\n")
|
||
# Process the found config files
|
||
for config_file_path in allspice_config_paths:
|
||
# Get the directory path without the filename
|
||
proj_dir = os.path.dirname(config_file_path)
|
||
print("🔸 Processing project folder " + proj_dir)
|
||
# Read file contents
|
||
with open(config_file_path, 'r') as f:
|
||
allspice_repo_url = f.read().strip()
|
||
print(add_indent("AllSpice URL is " + allspice_repo_url, 8, '🌐'))
|
||
# Change directory to project directory root
|
||
os.chdir(proj_dir)
|
||
# Initialize a git repository in this folder
|
||
initialize_git_repo()
|
||
# Add the AllSpice remote
|
||
add_allspice_remote()
|
||
# Git fetch
|
||
fetch_from_allspice()
|
||
# Checkout develop branch
|
||
checkout_develop_branch()
|
||
# Show git status
|
||
show_git_status()
|
||
# Get list of modified files
|
||
modified_files = get_list_of_modified_files()
|
||
# Get list of untracked files
|
||
untracked_files = get_list_of_untracked_files()
|
||
# If there are any untracked or modified files, add them for commit
|
||
if modified_files or untracked_files:
|
||
# Add files to commit
|
||
add_modified_or_untracked_files_for_commit()
|
||
# If there added files to be committed, prompt for a commit message and commit
|
||
# This step/check ensures that any previously halted commits are taken care of
|
||
added_files = get_list_of_added_files_not_yet_committed()
|
||
if added_files:
|
||
# Ask user for a commit message
|
||
commit_message = prompt_user_for_commit_message()
|
||
# Prepare a commit with the commit message
|
||
commit_changes(commit_message)
|
||
# Push commit to allspice
|
||
push_commit_to_allspice()
|
||
print(add_indent("Completed processing project folder " + proj_dir, 8, '☑️'))
|
||
# Change directory back to depot root
|
||
os.chdir("..")
|
||
print("\n--------------------------------------------------\n")
|
||
print("✅ Perforce->AllSpice sync completed!")
|