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