Skip to content

Instantly share code, notes, and snippets.

@gochist
Last active June 4, 2019 06:58
Show Gist options
  • Save gochist/4b4cfd18390f65b1c6c9def6382f8f15 to your computer and use it in GitHub Desktop.
Save gochist/4b4cfd18390f65b1c6c9def6382f8f15 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import subprocess
import jinja2
import click
DEVNULL = open(os.devnull, 'w')
ISSUE_TEMPLATE = """\
# This is a Bug Report
## Problem
Outdated files in the {{ r_commit }} branch.
### {{ files_to_be_modified | count }} files to be modified
{% for m_file in files_to_be_modified -%}
1. [ ] {{ m_file.filepath }} {{ m_file.shortstat }}
{% endfor %}
### {{ files_to_be_renamed | count }} files to be renamed
{% for r_file in files_to_be_renamed -%}
1. [ ] {{ r_file.diff_status_letter }} {{ r_file.src_filepath }} -> {{ r_file.dest_filepath }}
{% endfor %}
### {{ files_to_be_deleted | count }} files to be deleted
{% for d_file in files_to_be_deleted -%}
1. [ ] {{ d_file }}
{% endfor %}
## Proposed Solution
{% if files_to_be_modified %}
Use `git diff` to check what is changed in the upstream. And apply the upstream changes manually
to the `{{ l10n_lang_path }}` of `{{ r_commit }}` branch.
For example:
```
# checkout `{{ r_commit }}`
...
# check what is updated in the upstream
git diff {{ l_commit }} {{ r_commit }} -- {{ files_to_be_modified.0.filepath }}
# apply changes to content/ko
vi {{ files_to_be_modified.0.filepath | replace(src_lang_path, l10n_lang_path) }}
...
# commit and push
...
# make PR to `{{ r_commit }}`
```
{% endif %}
## Pages to Update
"""
files_to_be_deleted = []
files_to_be_renamed = []
files_to_be_modified = []
def git_diff(filepath, l_commit, r_commit, shortstat=False):
cmd = ["git", "diff", l_commit, r_commit, "--", filepath]
if shortstat:
cmd = ["git", "diff", l_commit, r_commit, "--shortstat", "--", filepath]
return subprocess.check_output(cmd).decode("UTF-8").strip()
def git_exists(path, filepath):
cmd = ["git", "cat-file", "-e", "{}:{}".format(path, filepath)]
ret_code = subprocess.call(cmd, stderr=DEVNULL)
return ret_code == 0
def process_diff_status(diff_status, l_commit, r_commit, src_lang_path, l10n_lang_path):
status_letter = diff_status[0]
filepath = diff_status[1]
if git_exists(r_commit, filepath.replace(src_lang_path, l10n_lang_path)):
if status_letter == 'D':
files_to_be_deleted.append(filepath)
elif status_letter.startswith('R'):
replaced = {"diff_status_letter": diff_status[0],
"src_filepath": diff_status[1],
"dest_filepath": diff_status[2]}
files_to_be_renamed.append(replaced)
elif status_letter == 'M':
modified = {"filepath": filepath,
"shortstat": git_diff(filepath, l_commit, r_commit, shortstat=True),
"diff": git_diff(filepath, l_commit, r_commit)}
files_to_be_modified.append(modified)
def git_diff_name_status(l_commit, r_commit, src_lang_path, l10n_lang_path):
cmd = ["git", "diff", l_commit, r_commit, "--name-status", "--", src_lang_path]
name_status_output = subprocess.check_output(cmd).strip()
for line in name_status_output.decode('utf-8').splitlines():
diff_status = line.split()
process_diff_status(diff_status, l_commit, r_commit, src_lang_path, l10n_lang_path)
@click.command()
@click.argument("l-commit")
@click.argument("r-commit")
@click.option("--l10n-lang-path", help="L10n language content path (ex: content/ko)")
@click.option("--src-lang-path", help="Source language content path (default: content/en)", default="content/en")
def main(l10n_lang_path, src_lang_path, l_commit, r_commit):
"""
Find outdated l10n files by comparing l10n development branches.
ex:
scripts/compare_l10n_branches.py --l10n-lang-path=content/ko dev-1.14-ko.4 dev-1.14-ko.5
"""
git_diff_name_status(l_commit, r_commit, src_lang_path, l10n_lang_path)
issue_template = jinja2.Template(ISSUE_TEMPLATE)
ret = issue_template.render(l_commit=l_commit, r_commit=r_commit,
src_lang_path=src_lang_path, l10n_lang_path=l10n_lang_path,
files_to_be_deleted=files_to_be_deleted,
files_to_be_modified=files_to_be_modified,
files_to_be_renamed=files_to_be_renamed)
print(ret)
if __name__ == "__main__":
main()
@gochist
Copy link
Author

gochist commented Jun 4, 2019

$ scripts/compare_l10n_branches.py --l10n-lang-path=content/ko dev-1.14-ko.4 dev-1.14-ko.5

This is a Bug Report

Problem

Outdated files in the dev-1.14-ko.5 branch.

19 files to modified

  1. content/en/_index.html 1 file changed, 2 insertions(+), 2 deletions(-)
  2. content/en/docs/concepts/_index.md 1 file changed, 1 insertion(+), 1 deletion(-)
  3. content/en/docs/concepts/architecture/cloud-controller.md 1 file changed, 4 insertions(+), 4 deletions(-)
  4. content/en/docs/concepts/overview/_index.md 1 file changed, 1 insertion(+), 2 deletions(-)
  5. content/en/docs/concepts/workloads/pods/init-containers.md 1 file changed, 5 insertions(+), 4 deletions(-)
  6. content/en/docs/concepts/workloads/pods/pod-lifecycle.md 1 file changed, 1 insertion(+), 1 deletion(-)
  7. content/en/docs/concepts/workloads/pods/pod-overview.md 1 file changed, 12 insertions(+), 9 deletions(-)
  8. content/en/docs/reference/glossary/docker.md 1 file changed, 6 insertions(+), 7 deletions(-)
  9. content/en/docs/setup/_index.md 1 file changed, 88 insertions(+), 73 deletions(-)
  10. content/en/docs/setup/certificates.md 1 file changed, 1 insertion(+), 1 deletion(-)
  11. content/en/docs/setup/minikube.md 1 file changed, 124 insertions(+), 97 deletions(-)
  12. content/en/docs/setup/multiple-zones.md 1 file changed, 2 insertions(+), 2 deletions(-)
  13. content/en/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough.md 1 file changed, 3 insertions(+), 3 deletions(-)
  14. content/en/docs/tasks/tools/install-minikube.md 1 file changed, 1 insertion(+), 1 deletion(-)
  15. content/en/docs/tutorials/configuration/configure-redis-using-configmap.md 1 file changed, 1 insertion(+), 1 deletion(-)
  16. content/en/docs/tutorials/online-training/overview.md 1 file changed, 23 insertions(+), 5 deletions(-)
  17. content/en/docs/tutorials/stateful-application/cassandra.md 1 file changed, 1 insertion(+), 1 deletion(-)
  18. content/en/examples/application/guestbook/redis-slave-deployment.yaml 1 file changed, 1 insertion(+), 1 deletion(-)
  19. content/en/examples/pods/config/redis-pod.yaml 1 file changed, 3 insertions(+)

4 files to be renamed

  1. R095 content/en/docs/concepts/overview/object-management-kubectl/overview.md -> content/en/docs/concepts/overview/working-with-objects/object-management.md
  2. R096 content/en/docs/concepts/overview/object-management-kubectl/declarative-config.md -> content/en/docs/tasks/manage-kubernetes-objects/declarative-config.md
  3. R089 content/en/docs/concepts/overview/object-management-kubectl/imperative-command.md -> content/en/docs/tasks/manage-kubernetes-objects/imperative-command.md
  4. R083 content/en/docs/concepts/overview/object-management-kubectl/imperative-config.md -> content/en/docs/tasks/manage-kubernetes-objects/imperative-config.md

2 files to be deleted

  1. content/en/docs/concepts/overview/object-management-kubectl/_index.md
  2. content/en/docs/setup/pick-right-solution.md

Proposed Solution

Use git diff to check what is changed in the upstream. And apply the upstream changes manually
to the content/ko of dev-1.14-ko.5 branch.

For example:

# checkout `dev-1.14-ko.5`
...
# check what is updated in the upstream 
git diff dev-1.14-ko.4 dev-1.14-ko.5 -- content/en/_index.html
# apply changes to content/ko
vi content/ko/_index.html
...
# commit and push
...
# make PR to `dev-1.14-ko.5`

Pages to Update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment