Skip to content

Commit

Permalink
ref: use multiprocessing pool for getting releases
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarnajjar committed Dec 28, 2021
1 parent ee51567 commit 201699d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
16 changes: 10 additions & 6 deletions pautomate/common/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import shlex
import subprocess
from os import path
from os.path import isdir, basename, join
from typing import Dict

from .printing import print_green
Expand Down Expand Up @@ -79,12 +79,13 @@ def get_branches_info(repo_path: str) -> str:
return shell(f'git -C {repo_path} branch -a')


def get_lastest_stable_release(repo_path: str) -> str:
def get_lastest_stable_release(repo_path: str, summery_info: Dict[str, str]) -> None:
"""git fetch --tags
git tag --sort=version:refname |grep -v "[a-zA-Z]" |tail -1
Arguments:
repo_path {str} -- path to repo
summery_info {Dict[str, str]} -- the result of the repo: release
"""
cmd = f'git -C {repo_path} fetch --tags'
shell_first(cmd)
Expand All @@ -94,7 +95,8 @@ def get_lastest_stable_release(repo_path: str) -> str:
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
# exclude any version having a letter in it (v.., beta, alpha, etc)
grep_cmd = 'grep -v "[a-zA-Z]"'
# if rg not installed, use grep here instead
grep_cmd = 'rg -v "[a-zA-Z]"'
cmd = shlex.split(grep_cmd)
grep_ps = subprocess.Popen(
cmd, stdin=tag_ps.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
Expand All @@ -105,7 +107,9 @@ def get_lastest_stable_release(repo_path: str) -> str:
cmd, stdin=grep_ps.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
tag_ps.stdout.close()
return tail_ps.communicate()[0].decode('utf-8').strip()
latest_stable_release = tail_ps.communicate()[0].decode('utf-8').strip()
if latest_stable_release != '':
summery_info.update({basename(repo_path): latest_stable_release})


def fetch_repo(
Expand All @@ -122,8 +126,8 @@ def fetch_repo(
url {str} -- repo url in gitlab
summery_info {Dict[str, str]} -- the result of the cloning/fetching
"""
repo_path = path.join(working_directory, repo_path)
if path.isdir(repo_path):
repo_path = join(working_directory, repo_path)
if isdir(repo_path):
print_green(f'Fetching {repo_path}')
shell_first(f'git -C {repo_path} fetch --prune')
remote_banches = shell(f'git -C {repo_path} ls-remote --heads')
Expand Down
18 changes: 15 additions & 3 deletions pautomate/git_repos/releases.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Get latest stable release of repositories in the current directory.
"""
from multiprocessing import Manager
from multiprocessing import Pool
from os.path import basename
from typing import List
from typing import Optional
Expand All @@ -25,7 +27,17 @@ def get_releases(
if filter_args:
repos = filter_list(repos, filter_args)

manager = Manager()
summery_info: Dict[str, str] = manager.dict()

pool = Pool(processes=8)
for repo_path in repos:
latest_stable_release = get_lastest_stable_release(repo_path)
if latest_stable_release != '':
print(f'{basename(repo_path)}: {latest_stable_release}')
pool.apply_async(
get_lastest_stable_release, args=(repo_path, summery_info),
)
pool.close()
pool.join()

for repo_name, release in summery_info.items():
print(f'{repo_name}: {release}')

0 comments on commit 201699d

Please sign in to comment.