Skip to content

Commit

Permalink
Cache cloned repos per build (#10)
Browse files Browse the repository at this point in the history
* Cache cloned repos per build

* Update toptranslators.py
  • Loading branch information
TheTripleV authored Oct 4, 2021
1 parent 64f22b5 commit bde246f
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions sphinxext/toptranslators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

TRANSLATORS_MARKER_NAME = "# Translators:"

CACHE = {}

# https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-normalize-in-a-python-unicode-string/518232#518232
def strip_accents(s):
return "".join(
Expand Down Expand Up @@ -137,32 +139,41 @@ def run(self) -> List[nodes.Node]:

repo_url = f"https://github.com/{self.arguments[0]}.git"

with TempDir() as temp_dir:

if repo_url in CACHE:
temp_dir = CACHE[repo_url]
else:
temp_dir = mkdtemp()
# Clone repo
try:
Repo.clone_from(repo_url, temp_dir, multi_options=["--depth 1"])
except Exception as e:
del_directory_exists(temp_dir)
raise ExtensionError("Invalid git repository given!", e)
CACHE[repo_url] = temp_dir

top_contributors = get_top_translators(temp_dir, locale).most_common(limit)

top_contributors = get_top_translators(temp_dir, locale).most_common(limit)
if "alphabetical" in order:
top_contributors.sort(key=lambda tup: strip_accents(tup[0]))

if "alphabetical" in order:
top_contributors.sort(key=lambda tup: strip_accents(tup[0]))

return [
ContributorSource(
[
Contributor(name, hide_contributions, contributions)
for name, contributions in top_contributors
]
).build()
]
return [
ContributorSource(
[
Contributor(name, hide_contributions, contributions)
for name, contributions in top_contributors
]
).build()
]

def cleanup_tempdirs(*args, **kwargs):
for url, temp_dir in CACHE.items():
del_directory_exists(temp_dir)
CACHE.clear()


def setup(app: Sphinx) -> Dict[str, Any]:
directives.register_directive("toptranslators", TopTranslators)
app.connect("build-finished", cleanup_tempdirs)

return {
"parallel_read_safe": True,
Expand Down

0 comments on commit bde246f

Please sign in to comment.