Skip to content

Instantly share code, notes, and snippets.

@natansil
Created February 6, 2018 14:28
Show Gist options
  • Save natansil/0e145179af02f98552e1afaf9359d38d to your computer and use it in GitHub Desktop.
Save natansil/0e145179af02f98552e1afaf9359d38d to your computer and use it in GitHub Desktop.
workspace.wix.resolved
import os
import subprocess
import sys
import re
TEMPLATE_NAME = "WORKSPACE.wix.resolved"
# this information will be retrieved from a service (one place for source of truth)
repos = {"natansil/repo_a":"git@github.com:natansil/repo_a.git",
"natansil/repo_b":"git@github.com:natansil/repo_b.git"}
last_commits = {}
for repo, repo_url in repos.iteritems():
last_commit = subprocess.check_output(['git', 'ls-remote', '--heads', repo_url, 'refs/heads/master']).decode("utf-8").splitlines()[0].split('\t')[0]
last_commits[repo] = last_commit
print last_commits
workspace_file_already_resolved = False
template_file_already_resolved = False
cwd = os.getcwd()
for file in os.listdir(cwd):
if file == "WORKSPACE":
workspace_file_already_resolved = True
print("already has WORKSPACE!")
sys.exit(0)
if file == TEMPLATE_NAME:
template_file_already_resolved = True
if not template_file_already_resolved:
raise ValueError("template file ({}) is missing".format(TEMPLATE_NAME))
with open(file) as template_file:
template_content = template_file.read()
http_archive_target_tokens = template_content.split("http_archive")
del http_archive_target_tokens[0]
for token in http_archive_target_tokens:
print token + "======\n"
replacements = {}
for repo, value_of_last_commit in last_commits.iteritems():
for http_archive in http_archive_target_tokens:
match = re.search(r'url.*"https://github.com/%s/archive/(.*).zip' % repo, http_archive)
if match:
template_commit_hash = match.group(1)
print ("match: %s" % template_commit_hash)
if value_of_last_commit == template_commit_hash:
print ("for %s: no need to replace %s" % (repo, template_commit_hash))
else:
print ("for %s: replace hash %s with hash %s" % (repo, template_commit_hash, value_of_last_commit))
replacements[template_commit_hash] = value_of_last_commit
break
print "replacements: %s" % replacements
for template_commit_hash_key, value in replacements.iteritems():
template_content = template_content.replace(template_commit_hash_key, value, 2)
print "content with replacements: %s" % template_content
with open("WORKSPACE", 'w') as output_workspace_file:
output_workspace_file.write(template_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment