~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
11882.2.2 by Jonathan Lange
Clear up a heck of a lot of imports from canonical.launchpad.interfaces.
22
from lp.registry.interfaces.distributionmirror import (
14612.2.7 by William Grant
scripts
23
    IDistributionMirrorSet,
24
    MirrorContent,
25
    )
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/.
26
from lp.services.scripts.base import (
14612.2.7 by William Grant
scripts
27
    LaunchpadScript,
28
    LaunchpadScriptFailure,
29
    )
11882.2.2 by Jonathan Lange
Clear up a heck of a lot of imports from canonical.launchpad.interfaces.
30
from lp.services.worlddata.interfaces.country import ICountrySet
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.
31
32
33
class CacheCountryMirrors(LaunchpadScript):
34
35
    usage = '%prog <target-directory>'
36
37
    def main(self):
38
        if len(self.args) != 1:
39
            raise LaunchpadScriptFailure(
40
                "You must specify the full path of the directory where the "
41
                "files will be stored.")
42
43
        mirror_set = getUtility(IDistributionMirrorSet)
44
        [dir_name] = self.args
45
        if not os.path.isdir(dir_name):
46
            raise LaunchpadScriptFailure(
47
                "'%s' is not a directory." % dir_name)
48
49
        for country in getUtility(ICountrySet):
50
            mirrors = mirror_set.getBestMirrorsForCountry(
51
                country, MirrorContent.ARCHIVE)
52
            # Write the content to a temporary file first, to avoid problems
53
            # if the script is killed or something like that.
54
            fd, tmpfile = tempfile.mkstemp()
55
            mirrors_file = os.fdopen(fd, 'w')
56
            mirrors_file.write(
57
                "\n".join(mirror.base_url for mirror in mirrors))
58
            mirrors_file.close()
59
            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.
60
            shutil.move(tmpfile, filename)
10604.3.2 by Curtis Hovey
Fix the file permissions set by cache-country-mirrors.
61
            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.
62
63
64
if __name__ == '__main__':
65
    CacheCountryMirrors('cache-country-mirrors').lock_and_run()
66