diff --git a/nbresnote.py b/nbresnote.py index 92b8b34cf8259fe635b290cefde82217acfd3051..229fc6aa3dee8842763fa9ad6af1bd4e5f899045 100644 --- a/nbresnote.py +++ b/nbresnote.py @@ -1,6 +1,6 @@ import json, base64, os, pickle, datetime, string, re -__version__ = "0.1.10" +__version__ = "0.1.11" ### from ipynb extract markdown and noted output for document organization class nbparser: @@ -63,7 +63,11 @@ class nbparser: for i, cell in enumerate(notebook['cells']): if not cell["source"]:continue if cell["cell_type"] == 'markdown': #markdown + if not (("source" in cell) and (len(cell["source"])!=0) and cell["source"][0].lstrip()): + # skip error/empty markdown cell. + continue a = cell["source"][0].lstrip() + #header parsing if a[0]== "#": header = a.split()[0] @@ -290,6 +294,10 @@ def write_daily(note_path, mod, record): shellsource = f"""#!/bin/bash +##### nbresnote ##### +# {__version__} +set -e + source ~/.bashrc {"conda activate "+os.environ["CONDA_DEFAULT_ENV"] if "CONDA_DEFAULT_ENV" in os.environ else ""} @@ -309,13 +317,56 @@ echo "nothing to commit" else git commit -m "Auto commit from nbresnote `date +%F`" fi +##### nbresnote ##### """ -pushsource = """#!/bin/bash +pushsource = f"""#!/bin/bash +##### nbresnote ##### +# {__version__} +set -e + cd wiki git fetch && git pull && git push +##### nbresnote ##### """ +def install(): + """install action for git repository + + Parameters + ---------- + append : bool, optional + if `True`, the hook will be written in tail of each hooks. By default, `False`. + + Raises + ------ + OSError + _description_ + """ + #Installation + # Setting git/hooks pre-commit and pre-push + if not os.path.exists(".git"): + raise OSError("Please run on the base folder of git repository.") + #if not os.path.exists(".git/hooks/pre-commit"): + print("Creating 'pre-commit' file on '.git/hooks/'.") + with open(".git/hooks/pre-commit","w") as f: + f.write(shellsource) + print("Change mod of 'pre-commit' file on '.git/hooks/' into 755(rwxr-xr-x).") + os.chmod(".git/hooks/pre-commit", 0o755) + #else: + #print("Warning : Already exists 'pre-commit' file on '.git/hooks/'. Command ignored.") + #if not os.path.exists(".git/hooks/pre-push"): + print("Creating 'pre-push' file on '.git/hooks/'.") + with open(".git/hooks/pre-push","w") as f: + f.write(pushsource) + print("Change mod of 'pre-push' file on '.git/hooks/' into 755(rwxr-xr-x).") + os.chmod(".git/hooks/pre-push", 0o755) + #else: + #print("Warning : Already exists 'pre-push' file on '.git/hooks/'. Command ignored.") + print("Done.") + return 0 #success 0 fail 1 + + if __name__ =="__main__": import sys @@ -331,30 +382,7 @@ if __name__ =="__main__": targets = open("__tmp__files").readlines() #print(targets) #DEBUG elif sys.argv[1] == "install": - #Installation - # Setting git/hooks pre-commit and pre-push - if not os.path.exists(".git"): - raise OSError("Please run on the base folder of git repository.") - if not os.path.exists(".git/hooks/pre-commit"): - print("Creating 'pre-commit' file on '.git/hooks/'.") - with open(".git/hooks/pre-commit","a") as f: - f.write(shellsource) - print("Change mod of 'pre-commit' file on '.git/hooks/' into 755(rwxr-xr-x).") - os.chmod(".git/hooks/pre-commit", 0o755) - else: - print("Warning : Already exists 'pre-commit' file on '.git/hooks/'. Command ignored.") - if not os.path.exists(".git/hooks/pre-push"): - print("Creating 'pre-push' file on '.git/hooks/'.") - with open(".git/hooks/pre-push","a") as f: - f.write(pushsource) - print("Change mod of 'pre-push' file on '.git/hooks/' into 755(rwxr-xr-x).") - os.chmod(".git/hooks/pre-push", 0o755) - else: - print("Warning : Already exists 'pre-push' file on '.git/hooks/'. Command ignored.") - print("Done.") - - exit(0) - + exit(install()) else: targets = sys.argv[1:]