Skip to content

Commit

Permalink
Merge pull request #68 from multiversx/integrate-sys-tests-ci
Browse files Browse the repository at this point in the history
add python script to update go.mod with a specific hash in pipeline
  • Loading branch information
btc-fan authored Aug 8, 2024
2 parents b3052db + 07a714d commit 49142bd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/update-go-mod/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.32.3
68 changes: 68 additions & 0 deletions scripts/update-go-mod/update-go-mod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import re
import sys

def update_go_mod_file(go_mod_path, new_hash):
"""
Update the go.mod file with the new commit hash.
"""
try:
with open(go_mod_path, 'r') as file:
content = file.read()
# Print the original content for debugging
print("Original go.mod content:")
print(content)
updated_content = re.sub(r'(github\.com/multiversx/mx-chain-go\s+)[^\s]+', f'github.com/multiversx/mx-chain-go {new_hash}', content)

# Print updated content for verification
print("Updated go.mod content:")
print(updated_content)

# Write the updated content back to the file
with open(go_mod_path, 'w') as file:
file.write(updated_content)

print(f"Successfully updated go.mod with the latest commit hash: {new_hash}")
except Exception as e:
print(f"Failed to update go.mod file: {str(e)}")
sys.exit(1)

def run_go_mod_tidy():
"""
Run `go mod tidy` to ensure the go.mod file is tidy.
"""
try:
print("Running go mod tidy...")
subprocess.run(['go', 'mod', 'tidy'], check=True)
print("Successfully ran go mod tidy.")

except subprocess.CalledProcessError as e:
print(f"Failed to run go mod tidy: {e.stderr}")
sys.exit(1)

def main():
# Assuming the script is run from the root of mx-chain-simulator-go
go_mod_path = './go.mod'

if len(sys.argv) != 2:
print("Usage: update-go-mod.py <commit_hash>")
sys.exit(1)

latest_commit_hash = sys.argv[1]

print(f"go.mod path: {go_mod_path}")
print(f"Latest commit hash received: {latest_commit_hash}")

update_go_mod_file(go_mod_path, latest_commit_hash)

# Run go mod tidy after updating go.mod
run_go_mod_tidy()

print("Python Script executed successfully.")

if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"Main() Script execution failed: {str(e)}")
sys.exit(1)

0 comments on commit 49142bd

Please sign in to comment.