When switching CI/CD system systems, you have lots of scripts with sections. Cleaning them up for the next system takes some time.
Using the above pair of scripts you can get it done quickly.
#!/bin/bash | |
# Function to preprocess the file and replace spaces in the block names | |
# Function to convert TeamCity blocks to GitLab format | |
convert_teamcity_to_gitlab() { | |
local file="$1" | |
python3 modify_teamcity_blocks.py "$file" | |
# Use gsed to replace TeamCity blockOpened with GitLab section_start | |
gsed -i -r "s/echo \"##teamcity\[blockOpened name='([^']+)'\s*(description='([^']*?)')?\]\"/echo -e \"\\\\e[0Ksection_start:\$(date +%s):\1\\\\r\\\\e[0K\3\"/" "$file" | |
# Use gsed to replace TeamCity blockClosed with GitLab section_end | |
gsed -i -r "s/echo \"##teamcity\[blockClosed name='([^']+)'\]\"/echo -e \"\\\\e[0Ksection_end:\$(date +%s):\1\\\\r\\\\e[0K\"/" "$file" | |
} | |
# Find all .sh files and Dockerfiles in the current directory | |
find . -type f \( -name "*.sh" -o -name "Dockerfile" \) -print0 | while IFS= read -r -d $'\0' file; do | |
# Convert TeamCity blocks to GitLab format for each file | |
convert_teamcity_to_gitlab "$file" | |
done | |
# Print remaining lines with "teamcity[block" using grep | |
grep -R "teamcity\[block" . |
import re | |
import sys | |
def replace_spaces_in_file(file_path): | |
# Define the regex pattern | |
pattern = r"##teamcity\[block(\w+) name=\'([^\']+)\'" | |
# Function to replace spaces with underscores in the name group | |
def replace_spaces(match): | |
block_type = match.group(1) | |
name = match.group(2).replace(' ', '_') | |
return f"##teamcity[block{block_type} name='{name}'" | |
# Read the content of the file | |
try: | |
with open(file_path, 'r') as file: | |
file_content = file.read() | |
except FileNotFoundError: | |
print(f"The file {file_path} was not found.") | |
sys.exit(1) | |
# Perform the substitution | |
modified_content = re.sub(pattern, replace_spaces, file_content) | |
# Write the modified content back to the file | |
with open(file_path, 'w') as file: | |
file.write(modified_content) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python script.py <file_path>") | |
sys.exit(1) | |
file_path = sys.argv[1] | |
replace_spaces_in_file(file_path) | |
print("File modified successfully.") |