aboutsummaryrefslogtreecommitdiff
path: root/utils/python/doc_gen/system_utils.py
blob: 326eb2115fb668836b87aeaac8d1773ccb8f11d3 (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json, shutil, zipfile, urllib, os, fnmatch

class SystemUtils:

    folder_filter = ["ble", "ble-nrf51822", "mbed-classic","nrf51-sdk"]

    ###
    # reads a file and returns a list of lines
    #
    # @param path the path where the file is located
    #
    # @return the list of lines representing the file.
    ###
    def read(self, path, plain=False):
        if plain:
            return self.__read_plain(path)
        print "Opening: " + path + " \n"
        with open(path, 'r') as file:
            return file.readlines()

    def __read_plain(self, path):
        print "Opening: " + path + " \n"
        with open(path, 'r') as file:
            return file.read()

    ###
    # writes a given set of lines to a path.
    #
    # @param path the path where the file is located
    # @param lines the lines to write
    ###
    def write(self, path, lines):
        print "Writing to: " + path + " \n"
        with open(path, 'w') as file:
            file.writelines(lines)

    #http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python
    def find_files(self, directory, pattern):

        print("DIR:")
        for root, dirs, files in os.walk(directory):
            if any(dir in root for dir in self.folder_filter):
                continue

            for basename in files:
                if fnmatch.fnmatch(basename, pattern):
                    filename = os.path.join(root, basename)
                    yield filename

    ###
    # removes files from a folder.
    ###
    def clean_dir(self, dir):
        for root, dirs, files in os.walk(dir):
            for f in files:
                os.unlink(os.path.join(root, f))
            for d in dirs:
                shutil.rmtree(os.path.join(root, d))

    ###
    # this files from one location to another
    ###
    def copy_files(self, from_dir, to_dir, pattern):


        files = self.find_files(from_dir, pattern)

        print("FILES!!!! ")
        for file in files:
            print file
            shutil.copy(file,to_dir)

    def mk_dir(self, path):
        if not os.path.exists(path):
            os.makedirs(path)

    def copytree(self, src, dst, symlinks=False, ignore=None):
        if not os.path.exists(dst):
            os.makedirs(dst)
        for item in os.listdir(src):
            s = os.path.join(src, item)
            d = os.path.join(dst, item)
            if os.path.isdir(s):
                self.copytree(s, d, symlinks, ignore)
            else:
                if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
                    shutil.copy2(s, d)

    def __add_version_info(self,version_string, extract_location):
        content_path = extract_location + "js/base.js"
        lines = self.read(content_path)
        html_string = '<div class=\'admonition warning\' style=\'margin-top:30px;\'><p class=\'admonition-title\'>Warning</p><p>You are viewing documentation for <b>' + version_string + '</b></p></div>'
        lines[0]= '$(document).ready(function() { $(\'div[role="main"]\').prepend("' + html_string + '") });'
        self.write(content_path, lines)

    def validate_version(self, working_dir, module_paths, extract_location):
        import yaml

        module_string = "/module.json"
        mkdocs_yml = yaml.load(self.read("./mkdocs.yml", plain=True))

        module_strings = []

        for current_path in module_paths:
            module_strings = module_strings + [json.loads(self.read(current_path + module_string, plain=True))["version"]]

        if module_strings[1:] != module_strings[:-1]:
            raise Exception("Version mismatch exception! microbit-dal and microbit are not compatible versions.")

        module_string = "v" + str(module_strings[0])

        if mkdocs_yml["versioning"]["runtime"] != module_string:
            #capture old site, save in docs/historic/versionNumber
            zip_dest = working_dir + "/" + str(mkdocs_yml["versioning"]["runtime"]) + ".zip"

            extract_folder = extract_location+ "/" + mkdocs_yml["versioning"]["runtime"]+"/"

            urllib.urlretrieve("https://github.com/lancaster-university/microbit-docs/archive/gh-pages.zip", zip_dest)

            zip_ref = zipfile.ZipFile(zip_dest)

            #obtain the archive prepended name
            archive_name = working_dir + "/" + zip_ref.namelist()[0]

            zip_ref.extractall(working_dir)
            zip_ref.close()

            self.copytree(archive_name, extract_folder)

            self.__add_version_info(mkdocs_yml["versioning"]["runtime"], extract_folder)

            self.clean_dir(archive_name)

            mkdocs_yml["versioning"]["runtime"] = module_string

            with open("./mkdocs.yml", "w") as f:
                yaml.dump(mkdocs_yml, f, default_flow_style=False )