Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkhtml: Generate local JSON file with core modules and their last commit #2140

Merged
merged 18 commits into from Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
db79c1c
utils/mkhtml.py: generate local JSON file with PGMS and their last co…
tmszi Jan 29, 2022
1137a32
GitHub action push to main branch
tmszi Jan 30, 2022
f62abb0
Revert "GitHub action push to main branch"
tmszi Feb 11, 2022
d60c66e
Revert "utils/mkhtml.py: generate local JSON file with PGMS and their…
tmszi Feb 11, 2022
3ae3a82
utils/mkhtml.py: generate local JSON file with PGMS and their last co…
tmszi Feb 11, 2022
28047cf
Make JSON file only instead of source code patch
tmszi Feb 13, 2022
fc46089
utils/mkhtml.py: generate local JSON file with PGMs and their last co…
tmszi Feb 13, 2022
7216e8f
utils/mkhtml.py: generate local JSON file with core modules and their…
tmszi Feb 18, 2022
36fbec3
Better way for *.html file detection
tmszi Feb 18, 2022
75e35bb
Incorporate suggestions
tmszi Feb 19, 2022
98483b6
utils/mkhtml.py: fix flake8 error unused imported module
tmszi Feb 19, 2022
0ffd81d
utils/generate_last_commit_file.py: improve catch *.html file
tmszi Feb 19, 2022
3f61f88
Replace subprocess.check() func stdout and stderr param with capture_…
tmszi Feb 19, 2022
9ff2a65
utils/generate_last_commit_file.py: replace unused dirs var with _
tmszi Feb 19, 2022
82e7b3c
Revert "Replace subprocess.check() func stdout and stderr param with …
tmszi Feb 19, 2022
de8157e
Incorporate suggestions
tmszi Feb 20, 2022
53d6276
utils/test_generate_last_commit_file.py: fix flake8 error unused impo…
tmszi Feb 20, 2022
d2e88c7
Use strict ISO 8601 date time format for git commit author date time
tmszi Apr 13, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/gen_pgms_last_commit_json_file.yml
@@ -0,0 +1,30 @@
name: Generate PGMS last commit JSON file
tmszi marked this conversation as resolved.
Show resolved Hide resolved

on:
push:
branches:
- 'main'
- 'releasebranch_7_8'
- 'releasebranch_8_0'
ninsbl marked this conversation as resolved.
Show resolved Hide resolved
tmszi marked this conversation as resolved.
Show resolved Hide resolved

jobs:
build:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Execute script
working-directory: ./utils
run: |
python gen_pgms_last_commit.py ../
tmszi marked this conversation as resolved.
Show resolved Hide resolved

- uses: actions/upload-artifact@v2
with:
name: pgms-with-last-commit-json-file
path: pgms_with_last_commit.json
60 changes: 60 additions & 0 deletions utils/gen_pgms_last_commit.py
@@ -0,0 +1,60 @@
#!/usr/bin/env python3

"""
Script for creating an pgms_with_last_commit.json file contains all pgms
with their last commit
{"pgm": {"commit": "git log -1 PGM src dir"}}.
tmszi marked this conversation as resolved.
Show resolved Hide resolved

Used by GitHub actions workflow.

python gen_pgms_last_commit.py src_root_dir

@author Tomas Zigo <tomas.zigo slovanet.sk>
"""

import json
import os
import subprocess
import shutil
import sys


def get_last_commit(src_dir):
"""Generate JSON object with the following structure e.g.
{"pgm": {"commit": "git log -1 PGM src dir"}}

:param str src_dir: root src dir

:return JSON obj result: JSON object
"""
result = {}
if not shutil.which("git"):
sys.exit("Git command was not found. Please install it.")
for root, dirs, files in os.walk(src_dir):
for f in files:
if f.endswith(".html"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to limit that to HTML files? We are interested in history of the whole directory, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a directory filter that filters directories that are not core modules based on the assumption that each core module has a manual page in the root directory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, f is not used to get the path. Can you rewrite the code so that the operation on the whole directory is not a inside of a loop over files and add comments? Perhaps something like:

if not contains_html_file(files):
    # No HTML file in the directory, no need to store metadata.
    continue
# ...git here

rel_path = os.path.relpath(root)
stdout, stderr = subprocess.Popen(
["git", "log", "-1", rel_path],
tmszi marked this conversation as resolved.
Show resolved Hide resolved
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).communicate()
if stderr:
sys.exit(stderr.decode())
if stdout:
result[os.path.basename(rel_path)] = {
"commit": stdout.decode(),
tmszi marked this conversation as resolved.
Show resolved Hide resolved
}
return result


def main():
if len(sys.argv) < 2:
sys.exit("Set root source dir script arg, please.")
src_dir = sys.argv[1]
with open(os.path.join(src_dir, "pgms_with_last_commit.json"), "w") as f:
json.dump(get_last_commit(src_dir), f, indent=4)


if __name__ == "__main__":
main()
56 changes: 32 additions & 24 deletions utils/mkhtml.py
Expand Up @@ -48,15 +48,7 @@
import grass.script as gs
except ImportError:
# During compilation GRASS GIS
_ = str

class gs:
def warning(message):
pass

def fatal(message):
pass

gs = None

HEADERS = {
"User-Agent": "Mozilla/5.0",
Expand Down Expand Up @@ -199,6 +191,12 @@ def get_last_git_commit(src_dir, is_addon, addon_path):
possible download commit from GitHub API server
values of keys have "unknown" string
"""

def parse_commit():
git_log["commit"] = commit[0].split(" ")[-1]
commit_date = commit[2].lstrip("Date:").strip()
git_log["date"] = commit_date.rsplit(" ", 1)[0]

unknown = "unknown"
git_log = {"commit": unknown, "date": unknown}
datetime_format = "%A %b %d %H:%M:%S %Y" # e.g. Sun Jan 16 23:09:35 2022
Expand Down Expand Up @@ -234,24 +232,34 @@ def get_last_git_commit(src_dir, is_addon, addon_path):
stderr = decode(stderr)

if stderr and "fatal: not a git repository" in stderr:
tmszi marked this conversation as resolved.
Show resolved Hide resolved
response = download_git_commit(
url=grass_addons_url if is_addon else grass_modules_url,
response_format="application/json",
)
if response:
commit = json.loads(response.read())
if commit:
git_log["commit"] = commit[0]["sha"]
git_log["date"] = datetime.strptime(
commit[0]["commit"]["author"]["date"],
"%Y-%m-%dT%H:%M:%SZ",
).strftime(datetime_format)
if gs:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole "can't import grass.script therefore I'm compiling" is shaky, but perhaps good enough for this PR.

response = download_git_commit(
url=grass_addons_url if is_addon else grass_modules_url,
response_format="application/json",
)
if response:
commit = json.loads(response.read())
if commit:
tmszi marked this conversation as resolved.
Show resolved Hide resolved
git_log["commit"] = commit[0]["sha"]
git_log["date"] = datetime.strptime(
commit[0]["commit"]["author"]["date"],
"%Y-%m-%dT%H:%M:%SZ",
).strftime(datetime_format)
tmszi marked this conversation as resolved.
Show resolved Hide resolved
else:
tmszi marked this conversation as resolved.
Show resolved Hide resolved
pgms_with_last_commit = os.path.join(
topdir,
"pgms_with_last_commit.json",
)
if os.path.exists(pgms_with_last_commit):
with open(pgms_with_last_commit) as f:
pgms_with_last_commit = json.load(f)
if pgm in pgms_with_last_commit:
commit = pgms_with_last_commit[pgm]["commit"].splitlines()
parse_commit()
else:
if stdout:
commit = stdout.splitlines()
git_log["commit"] = commit[0].split(" ")[-1]
commit_date = commit[2].lstrip("Date:").strip()
git_log["date"] = commit_date.rsplit(" ", 1)[0]
parse_commit()
tmszi marked this conversation as resolved.
Show resolved Hide resolved
return git_log
tmszi marked this conversation as resolved.
Show resolved Hide resolved


Expand Down