archive_evt.py 6.86 KB
Newer Older
1
2
3
4
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Matthieu Haefele's avatar
fix doc    
Matthieu Haefele committed
5
archive_evt.py takes as parameter the rst file of an event where the schedule directive is used for the event's program and replace the directive with program's content in rst. Additionnaly, the number of participants is added in the event's description, just before the program.
6
7

Usage:
Matthieu Haefele's avatar
fix doc    
Matthieu Haefele committed
8
python3 archive_evt.py path/to/evt_to_archive.rst nb_participants
9
10
"""

11
import sys
Matthieu Haefele's avatar
Matthieu Haefele committed
12
import os
13
import re
Matthieu Haefele's avatar
Matthieu Haefele committed
14
15
16
try:
    import rst_directives as rd
except ImportError:
17
18
19
20
21
    head = os.getcwd()
    head, tail = os.path.split(head)
    while tail != 'website' and head != '/' and head != '':
        head, tail = os.path.split(head)

Matthieu Haefele's avatar
Matthieu Haefele committed
22
    print("You have to set up properly your PYTHONPATH:")
23
24
    if tail == "website":
        print("export PYTHONPATH=$PYTHONPATH:%s/website/plugins"%(head))
Matthieu Haefele's avatar
Matthieu Haefele committed
25
26
27
    else:
        print("export PYTHONPATH=$PYTHONPATH:/path_to_website_repo/plugins")
    sys.exit(1)
28
29

def fetch_line_schedule(evt_content):
30
    schedule_pattern = re.compile("^([\s]*)\.\. schedule::")
31
32
33
34

    for i, l in enumerate(evt_content):
        res = schedule_pattern.search(l)
        if res != None:
35
            return i, len(res.group(1))
36

37
    return -1, None
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

def fetch_indico_id(evt_content, l_sched):
    indico_url_pattern = re.compile("[\s]*:indico_url: ([\W\w0-9/:\.]*)\n")
    indico_event_pattern = re.compile("[\s]*:indico_event: ([0-9]*)")

    indico_url = ""
    indico_event = -1
    item = l_sched+1
    while (indico_url=="" or indico_event ==-1) and item < len(evt_content):
        res = indico_url_pattern.search(evt_content[item])
        if res != None:
            indico_url=res.group(1)

        res = indico_event_pattern.search(evt_content[item])
        if res != None:
            indico_event=int(res.group(1))

        item += 1

    return indico_url, indico_event, item

59
def indent_content(content, indent):
60
61
62
63
64
    to_return = ['\n']
    for line in content.splitlines():
        to_return.append(" "*indent + line + '\n')

    return to_return
65

Matthieu Haefele's avatar
Matthieu Haefele committed
66
67
68
69
70
71
72
73
74
75
76
77
78
def add_nb_participants(evt_content, nb_participants):
    looking_for = ".. section:: Programme\n"
    try:
        line = evt_content.index(looking_for)
    except:
        print("Error: event does not contain the directive: %s"%looking_for)
        sys.exit(1)

    to_be_inserted = ["    Nombre de participants : %d\n"%nb_participants, "\n"]
    return evt_content[:line] + to_be_inserted +  evt_content[line:]

def gen_rst_evt_with_program(evt_content, nb_participants):
    evt_content = add_nb_participants(evt_content, nb_participants)
79

80
    l_sched, content_indent = fetch_line_schedule(evt_content)
81
    if l_sched < 0:
Matthieu Haefele's avatar
Fix    
Matthieu Haefele committed
82
83
        print("Error: no schedule directive in the file")
        sys.exit(1)
84
85
86
    
    url, evt, last_line = fetch_indico_id(evt_content, l_sched)
    if url=="" or evt < 0:
Matthieu Haefele's avatar
Fix    
Matthieu Haefele committed
87
88
89
        print("Error: could not find indico_url or indico_event directives")
        sys.exit(1)

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
    prog_to_insert_str = rd.get_indico_event_as_schedule(url, evt)
    prog_to_insert = indent_content(prog_to_insert_str, content_indent+4)

    return evt_content[:l_sched+1] + prog_to_insert + evt_content[last_line:]

def build_support_dir(evt_orig):
    evt_name, rst_ext = os.path.splitext(evt_orig)
    evt_name = os.path.basename(evt_name)
    head = os.path.abspath(evt_orig)
    head, tail = os.path.split(head)
    while tail != 'website' and head != '/' and head != '':
        head, tail = os.path.split(head)
    if tail != "website":
        print("Error: Please convert event from the website repository")
        sys.exit(1)

    return os.path.join(head, 'website', 'content', 'attachments', 'evt', evt_name)

def truncate(dest_dir):
    head, tail = os.path.split(dest_dir)
    truncated = tail
    while tail != 'content' and head != '/' and head != '':
        head, tail = os.path.split(head)
        if tail != "content":
            truncated = os.path.join(tail, truncated)
    if tail != "content":
        print("Error: Destination directory for supports must be in the content directory")
        sys.exit(1)

    return truncated
120

121
122
123
def online_pres(url_support):
    # Considered as an online presentation if not an archive nor a pdf
    ext = os.path.splitext(url_support)
Matthieu Haefele's avatar
Matthieu Haefele committed
124
    not_online_ext = [".tgz", ".gz", ".bz2", ".zip", ".pdf"]
125
126
127
128
    res = True
    for e in not_online_ext:
        res = res and e != ext
    return res
129

130
131
def treat_support(line_orig, url_support, dest_dir, support_id):
    import urllib.request
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    new_line = ""
    new_support = None
    if online_pres(url_support):
        new_line = line_orig.split('(')[0] + '(' + url_support + ')\n'
    else:
        response = urllib.request.urlopen(url_support)
        content = response.read()
        root, ext = os.path.splitext(url_support)
        new_support = os.path.join(dest_dir, 'support%02d%s'%(support_id, ext))
        f = open(new_support, "wb")
        f.write(content)
        f.close()
        rst_support = os.path.join(truncate(dest_dir), 'support%02d%s'%(support_id, ext))
        new_line = line_orig.split('(')[0] + '(' + rst_support + ')\n'
146
147
148
149
150
151
152
153
154
    return new_line, new_support


def fetch_supports(content_with_prog, evt_orig):
    dest_dir = build_support_dir(evt_orig)
    try:
        os.mkdir(dest_dir)
    except  FileExistsError:
        print("Error: Directory %s already exists"%dest_dir)
Matthieu Haefele's avatar
Fix    
Matthieu Haefele committed
155
        sys.exit(1)
156
157
158
159
160
161
162
163
164
165

    git_helper_str = "git add \\\n%s \\\n"%evt_orig
    support_url_pattern = re.compile("[\s]*\[support [0-9]*\]\(([\W\w0-9/:\.-_]*)\)")
    support_id=0
    for i, l in enumerate(content_with_prog):
        res = support_url_pattern.search(l)
        if res != None:
            print("Treating %s"%res.group(1))
            new_line, new_support = treat_support(l, res.group(1), dest_dir, support_id)
            content_with_prog[i] = new_line
166
167
168
            if new_support != None:
              git_helper_str += "%s \\\n"%new_support
              support_id += 1
169
170
171
172
173
174
175
176
177
178
179

    return git_helper_str, content_with_prog 
    

def fetch_content(evt_orig):
    f = open(evt_orig, "r")
    c = f.readlines()
    f.close()
    return c

def write_content(content_with_prog, evt_to_treat):
180
    f = open(evt_to_treat, "w")
181
    f.write("".join(content_with_prog))
182
183
184
    f.close()

if __name__ == "__main__":
Matthieu Haefele's avatar
Matthieu Haefele committed
185
    if len(sys.argv) != 3:
186
187
188
        print("""archive_evt.py takes as parameter the rst file of an event where the schedule directive is used for the event's program and replace the directive with program's content in rst.

Usage:
Matthieu Haefele's avatar
Matthieu Haefele committed
189
python3 %s path/to/evt_to_archive.rst nb_participants"""%(sys.argv[0]))
190
191
192
        sys.exit(1)

    evt_to_treat = sys.argv[1]
Matthieu Haefele's avatar
Matthieu Haefele committed
193
    nb_participants = int(sys.argv[2])
194
    evt_content = fetch_content(evt_to_treat)
Matthieu Haefele's avatar
Matthieu Haefele committed
195
    content_with_prog = gen_rst_evt_with_program(evt_content, nb_participants)
Matthieu Haefele's avatar
Fix    
Matthieu Haefele committed
196
    user_msg, prog_to_insert = fetch_supports(content_with_prog, evt_to_treat)
197
    #write_content(content_with_prog, "pok")
198
199
    write_content(content_with_prog, evt_to_treat)
    print("\nrst file modified and supports downloaded. Please add them to the repository.\n\n%s"%user_msg)
200
201