Beispiel #1
0
def pull(defs, nickname=None):
    """pull the journal from the origin"""

    wd = f"{defs['working_path']}/journal-{defs['nickname']}"

    try:
        os.chdir(wd)
    except OSError:
        sys.exit(f"ERROR: unable to switch to working directory: {wd}")

    stdout, stderr, rc = shell_util.run("git pull")
    if rc != 0:
        print(stdout, stderr)
        sys.exit("ERROR: something went wrong with the git pull")

    print(stdout)
Beispiel #2
0
def pull(defs, nickname=None):
    """pull the journal from the origin"""

    wd = "{}/journal-{}".format(defs["working_path"], defs["nickname"])

    try:
        os.chdir(wd)
    except:
        sys.exit("ERROR: unable to switch to working directory: {}".format(wd))

    stdout, stderr, rc = shell_util.run("git pull")
    if rc != 0:
        print(stdout, stderr)
        sys.exit("ERROR: something went wrong with the git pull")

    print(stdout)
Beispiel #3
0
def connect(master_repo, working_path, defs):
    """connect to an existing journal on git on another machine"""

    # get the nickname from the master repo name
    re_name = r"journal-(.*).git"
    a = re.search(re_name, master_repo)

    if a is not None:
        nickname = a.group(1)
    else:
        sys.exit(
            "ERROR: the remote-git-repo should be of the form: machine:/dir/journal-nickname.git"
        )

    # make sure that a journal does not already exist
    if os.path.isfile(defs["param_file"]):
        sys.exit("ERROR: a journal already exists")

    # git clone the bare repo at master_repo into the working path
    try:
        os.chdir(working_path)
    except:
        sys.exit(
            "ERROR: unable to switch to directory {}".format(working_path))

    _, stderr, rc = shell_util.run("git clone " + master_repo)
    if rc != 0:
        print(stderr)
        sys.exit("ERROR: something went wrong with the git clone")

    # create (or add to) the .pyjournalrc file
    try:
        with open(defs["param_file"], "w") as f:
            f.write("[{}]\n".format("main"))
            f.write("master_repo = {}\n".format(master_repo))
            f.write("working_path = {}\n".format(working_path))
            f.write("nickname = {}\n".format(nickname))
    except:
        sys.exit("ERROR: unable to open {} for appending".format(
            defs["param_file"]))
Beispiel #4
0
def init(nickname, username, master_path, working_path, defs):
    """initialize the journal by setting up the git repo and copying the
    basic sphinx directory tree"""

    # make sure we have absolute paths
    working_path = os.path.abspath(working_path)

    # we are create the directory beneath master_path/, so make sure that
    # exists
    master_path = os.path.abspath(master_path)

    if not os.path.isdir(master_path):
        try:
            os.mkdir(master_path)
        except (FileExistsError, FileNotFoundError):
            sys.exit("ERROR: you need to specify an existing path in which to create the journal repo")

    # create the bare git repo
    git_master = f"{master_path}/journal-{nickname}.git"
    try:
        os.mkdir(git_master)
    except (FileExistsError, FileNotFoundError):
        sys.exit(f"ERROR: unable to create a directory in {master_path}")

    os.chdir(git_master)
    shell_util.run("git init --bare")

    # create the local working copy
    try:
        os.chdir(working_path)
    except OSError:
        sys.exit(f"ERROR: unable to change to {working_path}")

    shell_util.run("git clone " + git_master)

    # create the initial directory structure
    working_journal = f"{working_path}/journal-{nickname}"

    try:
        source_dir = os.path.abspath(os.path.join(defs["module_dir"], "sphinx_base/source"))
        shutil.copytree(source_dir, os.path.join(working_journal, "source"))
        shutil.copy(os.path.join(defs["module_dir"], "sphinx_base/Makefile"),
                working_journal)
    except OSError:
        sys.exit("ERROR: unable to create initial directory structure")

    # create the journal_info.py
    with open(os.path.join(working_journal, "journal_info.py"), "w") as f:
        f.write(f"username = \"{username}\"\n")

    # create the .pyjournal2rc file
    try:
        with open(defs["param_file"], "w") as f:
            f.write("[main]\n")
            f.write(f"master_repo = {git_master}\n")
            f.write(f"working_path = {working_path}\n")
            f.write(f"nickname = {nickname}\n")
            f.write(f"username = {username}\n")
    except IOError:
        sys.exit(f"ERROR: unable to open {defs['param_file']} for appending")

    defs["master_repo"] = git_master
    defs["working_path"] = working_path
    defs["nickname"] = nickname
    defs["username"] = username

    # create an initial entry saying "journal created"
    images = []
    link_file = []
    entry_util.entry("main", images, link_file, defs, string="journal created\n")

    # do a git add / push
    os.chdir(working_journal)

    shell_util.run("git add .")
    shell_util.run("git commit -m 'initial journal.tex file' .")
    shell_util.run("git push origin master")
Beispiel #5
0
def entry(topic, images, link_files, defs, string=None, use_date=None):
    """create an entry"""

    current_year = int(datetime.datetime.now().year)

    try:
        editor = os.environ["EDITOR"]
    except:
        editor = "emacs"

    if topic == "todo":
        odir = "{}/journal-{}/source/todo/".format(defs["working_path"],
                                                   defs["nickname"])
        ofile = "todo.rst"

    elif topic == "year":
        odir = "{}/journal-{}/source/year_review/".format(
            defs["working_path"], defs["nickname"])

        ofile = "year-{}.rst".format(current_year)

    else:
        # determine the filename
        if use_date is not None:
            entry_dir = use_date
        else:
            entry_dir = get_dir_string()
        ofile = entry_dir + ".rst"

        # determine the directory we place it in -- this is the form yyyy-mm-dd/
        odir = "{}/journal-{}/source/{}/{}/".format(defs["working_path"],
                                                    defs["nickname"], topic,
                                                    entry_dir)

    if not os.path.isdir(odir):
        try:
            os.mkdir(odir)
        except:
            sys.exit("ERROR: unable to make directory {}".format(odir))

    entry_file = os.path.join(odir, ofile)
    if not os.path.isfile(entry_file):
        if ofile == "todo.rst":
            header = len("todo") * "*" + "\n" + "todo\n" + len(
                "todo") * "*" + "\n"
        elif ofile.startswith("year-"):
            title = "{}".format(current_year)
            header = len(title) * "*" + "\n" + "{}\n".format(
                title) + len(title) * "*" + "\n\n"
        else:
            header = ".. _{}_{}:\n\n".format(topic, entry_dir)
            header += len(entry_dir) * "*" + "\n" + "{}\n".format(
                entry_dir) + len(entry_dir) * "*" + "\n"
        header += SYMBOLS + "\n\n"
    else:
        header = ""

    # open (and create if necessary) the entry file.
    # If we passed in a string, then write it too.
    try:
        f = open(entry_file, "a+")
    except:
        sys.exit("ERROR: unable to open {}".format(os.path.join(odir, ofile)))

    f.write(header)
    if string is not None:
        f.write(string)

    # if there are images, then copy them over and add the figure
    # headings to the entry
    unique_id = get_unique_string()

    files_copied = []
    for im in images + link_files:

        if im is None:
            continue

        # does an file by that name already live in the dest
        # directory?
        src = im
        dest = odir

        im_copy = os.path.basename(im)
        if os.path.isfile("{}/{}".format(dest, im_copy)):
            im_copy = "{}_{}".format(unique_id.replace(".", "_"), im_copy)

        dest = os.path.join(dest, im_copy)

        # copy it
        if im != "":
            try:
                shutil.copy(src, dest)
            except:
                sys.exit("ERROR: unable to copy image {} to {}".format(
                    src, dest))

            files_copied.append(im_copy)

            if im in images:
                # create a unique label for latex referencing
                idx = im_copy.lower().rfind(".jpg")
                idx = max(idx, im_copy.lower().rfind(".png"))
                idx = max(idx, im_copy.lower().rfind(".gif"))
                idx = max(idx, im_copy.lower().rfind(".pdf"))

                if idx >= 0:
                    im0 = "{}:{}".format(unique_id, im_copy[:idx])
                else:
                    sys.exit(
                        "unsupported image type -- try creating a link instead"
                    )

                # add the figure text
                for l in FIGURE_STR.split("\n"):
                    f.write("{}\n".format(
                        l.replace("@figname@",
                                  im_copy).replace("@figlabel@",
                                                   im0).rstrip()))

            else:
                # add the download directive
                f.write(":download:`{} <{}>`\n\n".format(im_copy, im_copy))

    f.close()

    # launch the editor specified in the EDITOR environment variable
    if string is None:
        if editor == "emacs":
            prog = "emacs -nw {}/{}".format(odir, ofile)
        else:
            prog = "{} {}/{}".format(editor, odir, ofile)

        stdout, stderr, rc = shell_util.run(prog)

    # commit the entry to the working git repo
    os.chdir(odir)

    stdout, stderr, rc = shell_util.run("git add " + ofile)
    stdout, stderr, rc = shell_util.run("git commit -m 'new entry' " + ofile)

    # commit any images too
    for im in files_copied:
        stdout, stderr, rc = shell_util.run("git add " + im)
        stdout, stderr, rc = shell_util.run("git commit -m 'new image' " + im)
Beispiel #6
0
def build(defs, show=0):
    """build the journal.  This entails writing the TOC files that link to
    the individual entries and then running the Sphinx make command

    """

    source_dir = get_source_dir(defs)

    topics = get_topics(defs)

    # for each topic, we want to create a "topic.rst" that then has
    # things subdivided by year-month, and that a
    # "topic-year-month.rst" that includes the individual entries
    for topic in topics:
        tdir = os.path.join(source_dir, topic)
        os.chdir(tdir)

        # look over the directories here, they will be in the form YYYY-MM-DD
        years = []
        entries = []
        for d in os.listdir(tdir):
            if os.path.isdir(os.path.join(tdir, d)):
                y, _, _ = d.split("-")
                if y not in years:
                    years.append(y)
                entries.append(d)

        years.sort()
        entries.sort()

        # we need to create ReST files of the form YYYY.rst.  These
        # will each then contain the links to the entries for that
        # year
        for y in years:
            y_entries = [q for q in entries if q.startswith(y)]

            with open("{}.rst".format(y), "w") as yf:
                yf.write("****\n")
                yf.write("{}\n".format(y))
                yf.write("****\n\n")

                yf.write(".. toctree::\n")
                yf.write("   :maxdepth: 2\n")
                yf.write("   :caption: Contents:\n\n")

                for entry in y_entries:
                    yf.write("   {}/{}.rst\n".format(entry, entry))

        # now write the topic.rst
        with open("{}.rst".format(topic), "w") as tf:
            tf.write(len(topic) * "*" + "\n")
            tf.write("{}\n".format(topic))
            tf.write(len(topic) * "*" + "\n")

            tf.write(".. toctree::\n")
            tf.write("   :maxdepth: 2\n")
            tf.write("   :caption: Contents:\n\n")

            for y in years:
                tf.write("   {}.rst\n".format(y))

    # now write the index.rst
    os.chdir(source_dir)
    with open("index.rst", "w") as mf:
        mf.write("Research Journal\n")
        mf.write("================\n\n")
        mf.write(".. toctree::\n")
        mf.write("   :maxdepth: 2\n")
        mf.write("   :caption: Contents:\n\n")

        for topic in sorted(topics):
            mf.write("   {}/{}\n".format(topic, topic))

        mf.write("\n")
        mf.write("Indices and tables\n")
        mf.write("==================\n\n")
        mf.write("* :ref:`genindex`\n")
        mf.write("* :ref:`modindex`\n")
        mf.write("* :ref:`search`\n")

    # now do the building
    build_dir = "{}/journal-{}/".format(defs["working_path"], defs["nickname"])
    os.chdir(build_dir)

    _, _, rc = shell_util.run("make html")

    if rc != 0:
        print("build may have been unsuccessful")

    index = os.path.join(build_dir, "build/html/index.html")

    # use webbrowser module
    if show == 1:
        webbrowser.open_new_tab(index)
Beispiel #7
0
def build(defs, show=0):
    """build the journal.  This entails writing the TOC files that link to
    the individual entries and then running the Sphinx make command

    """

    source_dir = get_source_dir(defs)

    topics, other = get_topics(defs)

    # for each topic, we want to create a "topic.rst" that then has
    # things subdivided by year-month, and that a
    # "topic-year-month.rst" that includes the individual entries
    for topic in topics:

        years, entries = get_topic_entries(topic, defs)
        tdir = os.path.join(source_dir, topic)
        os.chdir(tdir)

        # we need to create ReST files of the form YYYY.rst.  These
        # will each then contain the links to the entries for that
        # year
        for y in years:
            y_entries = [q for q in entries if q.startswith(y)]

            with open(f"{y}.rst", "w") as yf:
                yf.write("****\n")
                yf.write(f"{y}\n")
                yf.write("****\n\n")

                yf.write(".. toctree::\n")
                yf.write("   :maxdepth: 2\n")
                yf.write("   :caption: Contents:\n\n")

                for entry in y_entries:
                    yf.write(f"   {entry}/{entry}.rst\n")

        # now write the topic.rst
        with open(f"{topic}.rst", "w") as tf:
            tf.write(len(topic)*"*" + "\n")
            tf.write(f"{topic}\n")
            tf.write(len(topic)*"*" + "\n")

            tf.write(".. toctree::\n")
            tf.write("   :maxdepth: 2\n")
            tf.write("   :caption: Contents:\n\n")

            for y in years:
                tf.write(f"   {y}.rst\n")

    # handle the year review now
    if "year_review" in other:
        tdir = os.path.join(source_dir, "year_review")
        os.chdir(tdir)
        entries = get_year_review_entries(defs)

        with open("years.rst", "w") as tf:
            topic = "year review"
            tf.write(len(topic)*"*" + "\n")
            tf.write(f"{topic}\n")
            tf.write(len(topic)*"*" + "\n")

            tf.write(".. toctree::\n")
            tf.write("   :maxdepth: 2\n")
            tf.write("   :caption: Contents:\n\n")

            for e in entries:
                tf.write(f"   {e}\n")

    # now write the index.rst
    os.chdir(source_dir)
    with open("index.rst", "w") as mf:
        mf.write("Research Journal\n")
        mf.write("================\n\n")
        mf.write(".. toctree::\n")
        mf.write("   :maxdepth: 1\n")
        mf.write("   :caption: Contents:\n\n")

        if "todo" in other:
            mf.write("   todo/todo.rst\n")

        if "year_review" in other:
            mf.write("   year_review/years.rst\n")

        for topic in sorted(topics):
            mf.write(f"   {topic}/{topic}\n")

        mf.write("\n")
        mf.write("Indices and tables\n")
        mf.write("==================\n\n")
        mf.write("* :ref:`genindex`\n")
        mf.write("* :ref:`modindex`\n")
        mf.write("* :ref:`search`\n")

    # now do the building
    build_dir = "{}/journal-{}/".format(defs["working_path"], defs["nickname"])
    os.chdir(build_dir)

    _, _, rc = shell_util.run("make clean")
    _, _, rc = shell_util.run("make html")

    if rc != 0:
        print("build may have been unsuccessful")

    index = os.path.join(build_dir, "build/html/index.html")

    # use webbrowser module
    if show == 1:
        webbrowser.open_new_tab(index)