OSDN Git Service

Initial commit
[rebornos/cnchi-gnome-osdn.git] / Cnchi / update_db.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # update_db.py
5 #
6 # Copyright © 2013-2018 Antergos
7 #
8 # This file is part of Cnchi.
9 #
10 # Cnchi is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Cnchi is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # The following additional terms are in effect as per Section 7 of the license:
21 #
22 # The preservation of all legal notices and author attributions in
23 # the material or in the Appropriate Legal Notices displayed
24 # by works containing it is required.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with Cnchi; If not, see <http://www.gnu.org/licenses/>.
28
29 """ Updates mirrorlists and databases """
30
31 import logging
32 import os
33 import shutil
34 import subprocess
35
36 import misc.extra as misc
37
38 def sync():
39     """ Synchronize cached writes to persistent storage """
40     try:
41         subprocess.check_call(['/usr/bin/sync'])
42     except subprocess.CalledProcessError as why:
43         logging.warning(
44             "Can't synchronize cached writes to persistent storage: %s",
45             why)
46
47 def update_mirrorlists():
48     """ Make sure we have the latest mirrorlist files """
49     """ Remove antergos-mirrorlis, and add reborn-mirrorlist (Rafael) """
50     mirrorlists = [
51         "/etc/pacman.d/mirrorlist",
52         "/etc/pacman.d/reborn-mirrorlist"]
53     cmd = [
54         'pacman',
55         '-Syy',
56         '--noconfirm',
57         '--noprogressbar',
58         '--quiet',
59         'pacman-mirrorlist',
60         'reborn-mirrorlist']
61     with misc.raised_privileges():
62         try:
63             with open(os.devnull, 'w') as fnull:
64                 subprocess.call(cmd, stdout=fnull,
65                                 stderr=subprocess.STDOUT)
66             # Use the new downloaded mirrorlist (.pacnew) files (if any)
67             for mirrorlist in mirrorlists:
68                 pacnew_path = mirrorlist + ".pacnew"
69                 if os.path.exists(pacnew_path):
70                     shutil.copy(pacnew_path, mirrorlist)
71             sync()
72             logging.debug("Mirrorlists updated successfully")
73         except subprocess.CalledProcessError as why:
74             logging.warning(
75                 'Cannot update mirrorlists files: %s', why)
76         except OSError as why:
77             logging.warning('Error copying new mirrorlist files: %s', why)
78
79 def test():
80     """ Tests this module """
81     update_mirrorlists()
82     with misc.raised_privileges():
83         sync()
84
85
86 if __name__ == '__main__':
87     test()