~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5493.3.1 by Guilherme Salgado
Add a new script to save the mirrors for each country in text files (named after the country's code) to be served by apache.
5
6
"""Script to save list of country mirrors for in text files.
7
8
For each country in our database, this script will create a text file,
9
named like cc.txt (where cc is the two letter country code),
10
containing the archive mirrors for that country.
11
"""
12
9678.4.39 by Barry Warsaw
[rs=gary] Fix the test_all_scripts failures by making sure apport_python_hook
13
# pylint: disable-msg=W0403
14
import _pythonpath
15
5493.3.1 by Guilherme Salgado
Add a new script to save the mirrors for each country in text files (named after the country's code) to be served by apache.
16
import os
10604.3.1 by Curtis Hovey
switch from os.rename to shutils.move which knows about filesystems.
17
import shutil
5493.3.1 by Guilherme Salgado
Add a new script to save the mirrors for each country in text files (named after the country's code) to be served by apache.
18
import tempfile
19
20
from zope.component import getUtility
21
8356.1.9 by Leonard Richardson
Renamed the base script module in scripts/, which module_rename.py didn't touch because it wasn't under lib/.
22
from lp.services.scripts.base import (
5493.3.1 by Guilherme Salgado
Add a new script to save the mirrors for each country in text files (named after the country's code) to be served by apache.
23
    LaunchpadScript, LaunchpadScriptFailure)
24
from canonical.launchpad.interfaces import (
25
    ICountrySet, IDistributionMirrorSet, MirrorContent)
26
27
28
class CacheCountryMirrors(LaunchpadScript):
29
30
    usage = '%prog <target-directory>'
31
32
    def main(self):
33
        if len(self.args) != 1:
34
            raise LaunchpadScriptFailure(
35
                "You must specify the full path of the directory where the "
36
                "files will be stored.")
37
38
        mirror_set = getUtility(IDistributionMirrorSet)
39
        [dir_name] = self.args
40
        if not os.path.isdir(dir_name):
41
            raise LaunchpadScriptFailure(
42
                "'%s' is not a directory." % dir_name)
43
44
        for country in getUtility(ICountrySet):
45
            mirrors = mirror_set.getBestMirrorsForCountry(
46
                country, MirrorContent.ARCHIVE)
47
            # Write the content to a temporary file first, to avoid problems
48
            # if the script is killed or something like that.
49
            fd, tmpfile = tempfile.mkstemp()
50
            mirrors_file = os.fdopen(fd, 'w')
51
            mirrors_file.write(
52
                "\n".join(mirror.base_url for mirror in mirrors))
53
            mirrors_file.close()
54
            filename = os.path.join(dir_name, '%s.txt' % country.iso3166code2)
10604.3.1 by Curtis Hovey
switch from os.rename to shutils.move which knows about filesystems.
55
            shutil.move(tmpfile, filename)
10604.3.2 by Curtis Hovey
Fix the file permissions set by cache-country-mirrors.
56
            os.chmod(filename, 0644)
5493.3.1 by Guilherme Salgado
Add a new script to save the mirrors for each country in text files (named after the country's code) to be served by apache.
57
58
59
if __name__ == '__main__':
60
    CacheCountryMirrors('cache-country-mirrors').lock_and_run()
61