aboutsummaryrefslogtreecommitdiffstats
path: root/update_manual.py
blob: ece5e5bf4a29fc5bdda360fa78e22597de29afe4 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
""" Script to update a manual in one language for one release

    Usage: python3 update_manual.py <manual_name> <release> <language> <archive>

    where:

    * manual_name can be 'mcc' or 'installer'
    * release can be 2 (installer only), 3, 4 or newer version
    * language is the language abreviation in lower case (en, fr, de, uk, tr...)
    * archive is the full path and name of the archive containing the new
      version of the manual (keep this file out of the git local repository)

    .. note:: Attention
        You need to have a clean and uptodate git local repository to use that
        script or it will try to 'git rm' files that are not under version
        control!!

"""
import sys
from os import walk, makedirs
from os.path import splitext, join, isdir
from shutil import rmtree, move
from zipfile import ZipFile
from subprocess import call
import fileinput
import time


def fix_image_path(path_to_fix, images_path):
    # first step: let's walk along the folder
    for root, dirs, files in walk(path_to_fix):
        # second step: identification of html files
        for elem in files:
            if splitext(elem)[1] == '.html':
                # third step: replacing the path
                with fileinput.input(files=join(root, elem), inplace=True) as f:
                    for line in f:
                        line = line.rstrip('\n')
                        line = line.replace('cco:/', images_path)
                        # with fileinput, you need to use 'print' and not 'write' to
                        # write in the file
                        print(line)


def fix_common_path(path_to_fix):
    # first step: let's walk along the folder
    for root, dirs, files in walk(path_to_fix):
        # second step: identification of html files
        for elem in files:
            if splitext(elem)[1] == '.html':
                # third step: replacing the path
                with fileinput.input(files=join(root, elem), inplace=True) as f:
                    for line in f:
                        line = line.rstrip('\n')
                        line = line.replace('"../common/', '"../../common/')
                        line = line.replace('(../common/', '(../../common/')
                        line = line.replace('</a></div><div class="navfooter">',
                                            '</a></div><div id="upload_date">Uploaded on ' + time.strftime("%d/%m/%Y", time.gmtime()) + '</div><div class="navfooter">')
                        line = line.replace('http://docteam.mageia.nl/favicon.png','https://www.mageia.org/g/favicon.png')
                        line = line.replace('http://docteam.mageia.nl/mageia-2013-200p.png','https://doc.mageia.org/g/mageia-2013-200p.png')
                        # with fileinput, you need to use 'print' and not 'write' to
                        # write in the file
                        print(line)


def check_changes_in_filelist(path_to_check, archive_list):
    toadd = [join(path_to_check, elem) for elem in archive_list
             if not elem.startswith("common")]
    toremove = []
    for root, dirs, files in walk(path_to_check):
        for elem in files:
            namefile = join(root, elem)
            if namefile in toadd:
                toadd.remove(namefile)
            else:
                toremove.append(namefile)
    return toadd, toremove


if __name__ == "__main__":
    if len(sys.argv) < 4:
        print("Usage: python3 update_manual.py <manual_name> " +
              "<release> <archive_base> [<language>]\n{} arguments given".format(len(sys.argv)))
    else:
        manual_name = sys.argv[1]
        release = sys.argv[2]
        if len(sys.argv) == 5:
            languages = {manual_name : sys.argv[4]}
        else:
        # define languages lists. en must be first
          languages = { 'installer':'en cs de el eo es et eu fr hr id it nl pl pt_br ro ru sk sl sq sv tr uk zh_CN',
            'mcc':'en ca cs de el es et eu fr id  t nl pl pt_br ro ru sk sl sv tr uk', 
            'draklive':'en cs de el es et eu fr hr nl ro sk sl sv uk zh_CN'}
        archive_base = sys.argv[3]
        
        # define manuals base name
        root_name = {'mcc' : 'MCC', 'installer': 'DrakX', 'draklive': 'DrakLive'}
        for language in languages[manual_name].split(' '):
            # create the path to the manual
            manual_path = join(manual_name, release, language)
            if not isdir(manual_path):
                makedirs(manual_path)
            print("Processing {} manual in {}".format(manual_name,language))
            
            # create path to common directories
            common_dir = join(manual_path, 'common')
            if not isdir(common_dir):
                makedirs(common_dir)
            common_common_path = join(manual_name, release, 'common')
            if not isdir(common_common_path):
                makedirs(common_common_path)

            # create the path of the EN manual for missing images
            image_path = "../../en/content/images/"

            # create the zip interface
            archive = '{ab}/{manual}-{lang}-WebHelp-zipped.zip'.format(ab=archive_base,manual=root_name[manual_name], lang=language.upper())
            try :
                archive_int = ZipFile(archive)
            except:
                print('{} not found'.format(archive))
                continue

            # check for changes
            files_to_add, files_to_remove = \
                    check_changes_in_filelist(manual_path, archive_int.namelist())

            # unzip the archive
            archive_int.extractall(manual_path)

            if language == "en":
                # if english, copy the common directory in the manual/release path
                rmtree(common_common_path)
                move(common_dir, common_common_path)
                # add common dir in case it's new
                files_to_add.append(common_common_path)
            else:
                # or remove the common directory and ...
                rmtree(common_dir)

            # ... fix the links
            fix_common_path(manual_path)

            # fix the links for missing images
            fix_image_path(manual_path, image_path)

            # tell git about the changes
            if len(files_to_add) == 0:
                print("Nothing to add to the git")
            else:
                command_add = ['git', 'add'] + files_to_add
                call(command_add)

            if len(files_to_remove) == 0:
                print("Nothing to remove from the git")
            else:
                command_remove = ['git', 'rm'] + files_to_remove
                call(command_remove)

            # committing
            command_commit = ['git', 'commit', '-m Adding or refreshing {} in {}'.format(manual_name,language),
                '{}/{}/{}/*'.format(manual_name,release,language)]
            call(command_commit)