FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-docs-pt-br/scripts/generate_commit_msg.py at main · Charitha-Reddy96/python-docs-pt-br · GitHub
Charitha-Reddy96
python-docs-pt-br
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-docs-pt-br
/
scripts
/
generate_commit_msg.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
89 lines (75 loc) · 2.42 KB
Breadcrumbs
python-docs-pt-br
/
scripts
/
generate_commit_msg.py
Copy path
File metadata and controls
executable file
·
89 lines (75 loc) · 2.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
"""
Generate a commit message
Parses staged files and generates a commit message with Last-Translator's as
co-authors.
Based on Stan Ulbrych's implementation for Python Doc' Polish team
"""
import
argparse
import
contextlib
import
os
from
subprocess
import
run
,
CalledProcessError
from
pathlib
import
Path
from
polib
import
pofile
,
POFile
def
generate_commit_msg
():
translators
:
set
[
str
]
=
set
()
result
=
run
(
[
"git"
,
"diff"
,
"--cached"
,
"--name-only"
,
"--diff-filter=ACM"
],
capture_output
=
True
,
text
=
True
,
check
=
True
,
)
staged
=
[
filename
for
filename
in
result
.
stdout
.
splitlines
()
if
filename
.
endswith
(
".po"
)
]
for
file
in
staged
:
staged_file
=
run
(
[
"git"
,
"show"
,
f":
{
file
}
"
],
capture_output
=
True
,
text
=
True
,
check
=
True
).
stdout
try
:
old_file
=
run
(
[
"git"
,
"show"
,
f"HEAD:
{
file
}
"
],
capture_output
=
True
,
text
=
True
,
check
=
True
,
).
stdout
except
CalledProcessError
:
old_file
=
""
new_po
=
pofile
(
staged_file
)
old_po
=
pofile
(
old_file
)
if
old_file
else
POFile
()
old_entries
=
{
entry
.
msgid
:
entry
.
msgstr
for
entry
in
old_po
}
for
entry
in
new_po
:
if
entry
.
msgstr
and
(
entry
.
msgid
not
in
old_entries
or
old_entries
[
entry
.
msgid
]
!=
entry
.
msgstr
):
translator
=
new_po
.
metadata
.
get
(
"Last-Translator"
)
translator
=
translator
.
split
(
","
)[
0
].
strip
()
if
translator
:
translators
.
add
(
f"Co-Authored-By:
{
translator
}
"
)
break
print
(
"Update translation
\n
\n
"
+
"
\n
"
.
join
(
translators
))
# contextlib implemented chdir since Python 3.11
@
contextlib
.
contextmanager
def
chdir
(
path
:
Path
):
"""Temporarily change the working directory."""
original_dir
=
Path
.
cwd
()
os
.
chdir
(
path
)
try
:
yield
finally
:
os
.
chdir
(
original_dir
)
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
(
description
=
"Generate commit message with translators as co-authors."
)
parser
.
add_argument
(
"path"
,
type
=
Path
,
nargs
=
'?'
,
default
=
"."
,
help
=
"Path to the Git repository (default: current directory)"
,
)
args
=
parser
.
parse_args
()
with
chdir
(
args
.
path
):
generate_commit_msg
()
Back
|
FazBrowse Home
|
New Git URL