create_gh_mirrors.py 6.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -*- coding: utf-8 -*-
#
# This file is part of GetTor
#
# :authors: Israel Leiva <ilv@torproject.org>
#           see also AUTHORS file
#
# :license: This is Free Software. See LICENSE for license information.

"""create_gh_mirrors -- Create landing page and readme for Github."""

import os
import ConfigParser

import github3

import gettor.core


ilv's avatar
ilv committed
20
def create_readme(tpl_path, md_path, tb_version, links):
21
22
23
24
    """Create README file with links stored in github.links.

    :param: tpl_path (string) path to file used as template.
    :param: md_path (string) path to file generated.
ilv's avatar
ilv committed
25
    :param: tb_version (string) tor browser version.
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
    :param: links (object) github links.

    """
    lcs = ['FA', 'ZH', 'TR', 'EN']

    md_file = open(md_path, 'w')
    with open(tpl_path, 'r') as tpl_file:
        content_md = tpl_file.read()

        for lc in lcs:
            win_link = links.get('windows', lc.lower())
            win_pkg, win_sig, win_sha = [e for e in win_link.split("$") if e]

            osx_link = links.get('osx', lc.lower())
            osx_pkg, osx_sig, osx_sha = [e for e in osx_link.split("$") if e]

            linux_links = links.get('linux', lc.lower())
            linux32_link, linux64_link = linux_links.split(',')
            linux32_pkg, linux32_sig, linux32_sha = [
                e for e in linux32_link.split("$") if e
            ]
            linux64_pkg, linux64_sig, linux64_sha = [
                e for e in linux64_link.split("$") if e
            ]

            content_md = content_md.replace(
                "%WINDOWS_{}%".format(lc), win_pkg
            )
            content_md = content_md.replace(
                "%WINDOWS_{}_SIG%".format(lc), win_sig
            )

            content_md = content_md.replace(
                "%OSX_{}%".format(lc), osx_pkg
            )
            content_md = content_md.replace(
                "%OSX_{}_SIG%".format(lc), osx_sig
            )

            content_md = content_md.replace(
                "%LINUX32_{}%".format(lc), linux32_pkg
            )
            content_md = content_md.replace(
                "%LINUX32_{}_SIG%".format(lc), linux32_sig
            )
            content_md = content_md.replace(
                "%LINUX64_{}%".format(lc), linux64_pkg
            )
            content_md = content_md.replace(
                "%LINUX64_{}_SIG%".format(lc), linux64_sig
            )

ilv's avatar
ilv committed
78
        content_md = content_md.replace("%TB_VERSION%", tb_version)
79
80
        md_file.write(content_md)

ilv's avatar
ilv committed
81
    print "README generated with Tor Browser %s" % tb_version
82
83


ilv's avatar
ilv committed
84
def create_landing_html(tpl_path, html_path, tb_version, links):
85
86
87
88
    """Create README file with links stored in github.links.

    :param: tpl_path (string) path to file used as template.
    :param: html_path (string) path to file generated.
ilv's avatar
ilv committed
89
    :param: tb_version (string) tor browser version.
90
91
92
    :param: links (object) github links.

    """
ilv's avatar
ilv committed
93
    lcs = ['FA', 'ZH', 'TR', 'EN']
94
95
96

    html_file = open(html_path, 'w')
    with open(tpl_path, 'r') as tpl_file:
ilv's avatar
ilv committed
97
        content_html = tpl_file.read().replace('\n', '')
98

ilv's avatar
ilv committed
99
100
101
        for lc in lcs:
            win_link = links.get('windows', lc.lower())
            win_pkg, win_sig, win_sha = [e for e in win_link.split("$") if e]
102

ilv's avatar
ilv committed
103
104
            osx_link = links.get('osx', lc.lower())
            osx_pkg, osx_sig, osx_sha = [e for e in osx_link.split("$") if e]
105

ilv's avatar
ilv committed
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
            linux_links = links.get('linux', lc.lower())
            linux32_link, linux64_link = linux_links.split(',')
            linux32_pkg, linux32_sig, linux32_sha = [
                e for e in linux32_link.split("$") if e
            ]
            linux64_pkg, linux64_sig, linux64_sha = [
                e for e in linux64_link.split("$") if e
            ]
            
            content_html = content_html.replace(
                "%WINDOWS_{}%".format(lc), win_pkg
            )
            """
            content_html = content_html.replace(
                "%WINDOWS_{}_SIG%".format(lc), win_sig
            )
            """
            content_html = content_html.replace(
                "%OSX_{}%".format(lc), osx_pkg
            )
            """
            content_html = content_html.replace(
                "%OSX_{}_SIG%".format(lc), osx_sig
            )
            """
            content_html = content_html.replace(
                "%LINUX32_{}%".format(lc), linux32_pkg
            )
            """
            content_html = content_html.replace(
                "%LINUX32_{}_SIG%".format(lc), linux32_sig
            )
            """
            content_html = content_html.replace(
                "%LINUX64_{}%".format(lc), linux64_pkg
            )
            """
            content_html = content_html.replace(
                "%LINUX64_{}_SIG%".format(lc), linux64_sig
            )
            """
        
        content_html = content_html.replace(
            "%TB_VERSION%", tb_version
        )
151
152
        html_file.write(content_html)

ilv's avatar
ilv committed
153
    print "HTML generated with Tor Browser %s" % tb_version
154
155
156
157
158


def main():
    """Generate HTML and md files and update it in Github."""
    github_links = 'providers/github.links'
ilv's avatar
ilv committed
159
    tb_version_path = 'latest_torbrowser.cfg'
160
161
162
163
164
165
    md_path = 'upload/readme_gh.md'
    html_path = 'upload/landing_gh.html'
    md_tpl_path = 'upload/readme_gh.tpl'
    html_tpl_path = 'upload/landing_gh.tpl'
    github_access_token = ''

ilv's avatar
ilv committed
166
167
168
    tb_version_config = ConfigParser.ConfigParser()
    tb_version_config.read(tb_version_path)
    tb_version = tb_version_config.get('version', 'current')
169
170
171
172

    links = ConfigParser.ConfigParser()
    links.read(github_links)

ilv's avatar
ilv committed
173
174
    create_landing_html(html_tpl_path, html_path, tb_version, links)
    create_readme(md_tpl_path, md_path, tb_version, links)
175
176
177
178
179
180
181
182
183
184
185

    landing = open(html_path, 'r')
    content_landing = landing.read().replace('\n', '')

    readme = open(md_path, 'r')
    content_readme = readme.read()

    gh = github3.login(token=github_access_token)
    repo_landing = gh.repository('thetorproject', 'gettor')
    repo_readme = gh.repository('thetorproject', 'gettorbrowser')

ilv's avatar
ilv committed
186
    file_landing_gh = repo_landing.file_contents('index.html', 'gh-pages')
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    file_readme_gh = repo_readme.file_contents('README.md')

    data_landing = {
        'message': 'Updating landing page.',
        'content': content_landing,
        'branch': 'gh-pages'
    }

    data_readme = {
        'message': 'Updating README.',
        'content': content_readme
    }

    file_landing_gh.update(**data_landing)
    print "Landing page updated in gettor"

    file_readme_gh.update(**data_readme)
    print "README updated in gettorbrowser"

if __name__ == "__main__":
    main()