forked from meta-pytorch/data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.py
More file actions
92 lines (76 loc) · 2.93 KB
/
todo.py
File metadata and controls
92 lines (76 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# Scrip can be used with
# find -name '*.py' | grep -v third_party | perl -ne'print "python tools/todo.py $_"' | head -n 5 | bash
import configparser
import os
import re
import shutil
import sys
import tempfile
from github import Github # pip install PyGithub
file_name = sys.argv[1]
config = configparser.ConfigParser(allow_no_value=True)
with open(os.path.join(os.path.expanduser("~"), ".ghstackrc")) as stream:
config.read_string(stream.read())
GITHUB_KEY = config["ghstack"]["github_oauth"]
def get_git_branch_hash():
stream = os.popen("git rev-parse origin/main")
return stream.read().rstrip()
def generate_issue_id(id_or_name, title, file_name, line_number):
git_branch_hash = get_git_branch_hash()
# print(file_name, line_number, title, id_or_name)
match = re.match(r"\((\d+)\)", id_or_name)
if match:
return int(match.group(1))
match = re.match(r"\((.*)\)", id_or_name)
name = None
if match:
name = match.group(1)
if name is not None:
owner = f"cc @{name}"
else:
owner = ""
g = Github(GITHUB_KEY)
repo = g.get_repo("pytorch/data")
# label_be = repo.get_label("better-engineering" )
# labels = [label_be]
line_reference = f"https://github.com/pytorch/data/blob/{git_branch_hash}/{file_name}#L{line_number}"
line_reference = line_reference.replace("/./", "/")
body = """
This issue is generated from the TODO line
{line_reference}
{owner}
""".format(
owner=owner,
line_reference=line_reference,
)
title = f"[TODO] {title}"
issue = repo.create_issue(title=title, body=body, labels=[])
print(f"Created issue https://github.com/pytorch/data/issues/{issue.number}")
return issue.number
def update_file(file_name):
try:
f = tempfile.NamedTemporaryFile(delete=False)
shutil.copyfile(file_name, f.name)
with open(f.name) as f_inp:
with open(file_name, "w") as f_out:
for line_number, line in enumerate(f_inp.readlines()):
if not re.search(r"ignore-todo", line, re.IGNORECASE):
match = re.search(r"(.*?)#\s*todo\s*(\([^)]+\)){0,1}:{0,1}(.*)", line, re.IGNORECASE)
if match:
# print(line)
prefix = match.group(1)
text = match.group(3)
issue_id = generate_issue_id(str(match.group(2)), text, file_name, line_number + 1)
line = f"{prefix}# TODO({issue_id}):{text}\n" # ignore-todo
f_out.write(line)
except Exception as e:
shutil.copyfile(f.name, file_name)
raise e
finally:
os.unlink(f.name)
update_file(file_name)