2021-05-26 18:37:26 +02:00
#! /usr/bin/env python3
2021-05-10 17:23:40 +02:00
2021-04-28 20:11:42 +02:00
import argparse
2021-04-23 20:01:40 +02:00
import json
import os
2021-05-26 18:39:42 +02:00
from pathlib import Path
2021-04-23 20:01:40 +02:00
2021-04-28 20:11:42 +02:00
from jinja2 import Environment , FileSystemLoader
2021-04-23 20:01:40 +02:00
2021-05-28 17:17:16 +02:00
def generate_READMEs ( app_path : str ) :
app_path = Path ( app_path )
2021-04-23 20:01:40 +02:00
2021-05-26 18:43:26 +02:00
if not app_path . exists ( ) :
2021-04-28 20:14:48 +02:00
raise Exception ( " App path provided doesn ' t exists ?! " )
2021-04-23 20:01:40 +02:00
2021-05-26 18:43:26 +02:00
manifest = json . load ( open ( app_path / " manifest.json " ) )
2021-05-21 20:12:00 +02:00
upstream = manifest . get ( " upstream " , { } )
2022-02-23 08:07:45 +01:00
catalog = json . load ( open ( Path ( os . path . abspath ( __file__ ) ) . parent . parent . parent / " apps.json " ) )
2022-02-23 07:36:19 +01:00
from_catalog = catalog . get ( manifest [ ' id ' ] , { } )
2021-05-26 18:43:26 +02:00
if not upstream and not ( app_path / " doc " / " DISCLAIMER.md " ) . exists ( ) :
2021-05-26 18:46:29 +02:00
print (
" There ' s no ' upstream ' key in the manifest, and doc/DISCLAIMER.md doesn ' t exists - therefore assuming that we shall not auto-update the README.md for this app yet. "
)
2021-05-21 20:12:00 +02:00
return
2021-05-26 18:39:42 +02:00
env = Environment ( loader = FileSystemLoader ( Path ( __file__ ) . parent / " templates " ) )
2021-04-23 20:01:40 +02:00
2021-04-28 20:14:48 +02:00
for lang , lang_suffix in [ ( " en " , " " ) , ( " fr " , " _fr " ) ] :
2021-04-23 20:01:40 +02:00
2021-05-26 18:45:39 +02:00
template = env . get_template ( f " README { lang_suffix } .md.j2 " )
2021-04-23 20:01:40 +02:00
2021-08-22 16:57:07 +02:00
if ( app_path / " doc " / f " DESCRIPTION { lang_suffix } .md " ) . exists ( ) :
description = ( app_path / " doc " / f " DESCRIPTION { lang_suffix } .md " ) . read_text ( )
# Fallback to english if maintainer too lazy to translate the description
elif ( app_path / " doc " / " DESCRIPTION.md " ) . exists ( ) :
description = ( app_path / " doc " / " DESCRIPTION.md " ) . read_text ( )
else :
description = None
2021-05-26 18:43:26 +02:00
if ( app_path / " doc " / " screenshots " ) . exists ( ) :
2021-04-28 20:14:48 +02:00
screenshots = os . listdir ( os . path . join ( app_path , " doc " , " screenshots " ) )
if " .gitkeep " in screenshots :
screenshots . remove ( " .gitkeep " )
else :
screenshots = [ ]
2021-04-23 20:01:40 +02:00
2021-05-26 18:43:26 +02:00
if ( app_path / " doc " / f " DISCLAIMER { lang_suffix } .md " ) . exists ( ) :
disclaimer = ( app_path / " doc " / f " DISCLAIMER { lang_suffix } .md " ) . read_text ( )
2021-04-28 20:14:48 +02:00
# Fallback to english if maintainer too lazy to translate the disclaimer idk
2021-05-26 18:43:26 +02:00
elif ( app_path / " doc " / " DISCLAIMER.md " ) . exists ( ) :
disclaimer = ( app_path / " doc " / " DISCLAIMER.md " ) . read_text ( )
2021-04-28 20:14:48 +02:00
else :
disclaimer = None
2021-04-23 20:01:40 +02:00
2022-02-23 00:44:44 +01:00
# Get the current branch using git inside the app path
2022-08-05 23:06:38 +02:00
default_branch = from_catalog . get ( ' branch ' , ' master ' )
current_branch = os . popen ( f " git -C { app_path } rev-parse --abbrev-ref HEAD " ) . read ( ) . strip ( )
if default_branch != current_branch :
os . system ( f " git -C { app_path } fetch origin { default_branch } 2>/dev/null " )
default_branch_version = os . popen ( f " git -C { app_path } show FETCH_HEAD:manifest.json | jq -r .version " ) . read ( ) . strip ( )
else :
default_branch_version = None # we don't care in that case
2022-02-23 00:44:44 +01:00
2021-05-26 18:46:29 +02:00
out = template . render (
lang = lang ,
upstream = upstream ,
2021-08-22 16:57:07 +02:00
description = description ,
2021-05-26 18:46:29 +02:00
screenshots = screenshots ,
disclaimer = disclaimer ,
manifest = manifest ,
2022-08-05 23:06:38 +02:00
current_branch = current_branch ,
default_branch = default_branch ,
default_branch_version = default_branch_version ,
2021-05-26 18:46:29 +02:00
)
2021-05-26 18:43:26 +02:00
( app_path / f " README { lang_suffix } .md " ) . write_text ( out )
2021-04-28 20:14:48 +02:00
if __name__ == " __main__ " :
2021-05-26 18:46:29 +02:00
parser = argparse . ArgumentParser (
description = " Automatically (re)generate README for apps "
)
parser . add_argument (
" app_path " , help = " Path to the app to generate/update READMEs for "
)
2021-04-28 20:14:48 +02:00
args = parser . parse_args ( )
2021-05-28 17:17:16 +02:00
generate_READMEs ( args . app_path )