File: upload_docs.py

package info (click to toggle)
dipy 1.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,144 kB
  • sloc: python: 92,240; makefile: 272; pascal: 183; sh: 162; ansic: 106
file content (100 lines) | stat: -rw-r--r-- 3,208 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
# Script to upload docs to gh-pages branch of dipy_web that will be
# automatically detected by the dipy website.
import os
from os import chdir as cd
import re
from subprocess import check_call
import sys


def sh(cmd):
    """Execute command in a subshell, return status code."""
    print("--------------------------------------------------")
    print(f"Executing: {cmd}")
    print("--------------------------------------------------")
    return check_call(cmd, shell=True)


# paths
docs_repo_path = "_build/docs_repo"
docs_repo_url = "https://github.com/dipy/dipy_web.git"

if __name__ == '__main__':
    # get current directory
    startdir = os.getcwd()

    # find the source version
    info_file = '../dipy/info.py'
    info_lines = open(info_file).readlines()
    source_version = '.'.join(
        [v.split('=')[1].strip(" '\n.")
         for v in info_lines if re.match(
            '^_version_(major|minor|micro|extra)',
            v)])
    print("Source version: ", source_version)

    # check for dev tag
    if source_version.split(".")[-1] == "dev":
        dev = True
        print("Development Version detected")
    else:
        dev = False

    # pull current docs_repo
    if not os.path.exists(docs_repo_path):
        print("docs_repo not found, pulling from git..")
        sh(f"git clone {docs_repo_url} {docs_repo_path}")
    cd(docs_repo_path)
    print(f"Moved to {os.getcwd()}")
    try:
        sh("git checkout gh-pages")
    except:
        while 1:
            print("\nLooks like gh-pages branch does not exist!")
            print("Do you want to create a new one? (y/n)")
            choice = str(input()).lower()
            if choice == 'y':
                sh("git checkout -b gh-pages")
                sh("rm -rf *")
                sh("git add .")
                sh("git commit -m 'cleaning gh-pages branch'")
                sh("git push origin gh-pages")
                break
            if choice == 'n':
                print("Please manually create a new gh-pages branch and try again.")
                sys.exit(0)
            else:
                print("Please enter valid choice ..")
    sh("git pull origin gh-pages")

    # check if docs for current version exists
    if os.path.exists(source_version) and not dev:
        print("docs for current version already exists")
    else:
        if dev:
            print("Re-building docs for development version")
        else:
            print("Building docs for a release")
        # build docs and copy to docs_repo
        cd(startdir)
        # remove old html and doctree files
        try:
            sh("rm -rf _build/json _build/doctrees")
        except:
            pass
        # generate new doc and copy to docs_repo
        sh("make api")
        sh("make rstexamples")
        sh("make json")
        sh(f"cp -r _build/json {docs_repo_path}/")
        cd(docs_repo_path)
        if dev:
            try:
                sh(f"rm -r {source_version}")
            except:
                pass
        sh(f"mv json {source_version}")
        sh("git add .")
        sh(f"git commit -m \"Add docs for {source_version}\"")
        sh("git push origin gh-pages")