Update to genscripts rev 382. This has more fixes for py3k and the modular rewrite...
[gentoolkit.git] / pym / gentoolkit / eclean / pkgindex.py
1 #!/usr/bin/python
2
3 # Copyright 2003-2010 Gentoo Foundation
4 # Distributed under the terms of the GNU General Public License v2
5
6 from __future__ import print_function
7
8
9 import subprocess
10 import sys
11
12 import gentoolkit.pprinter as pp
13
14 import portage
15 from portage import os
16
17
18 class PkgIndex(object):
19         """Handle the cleaning of the binpkg Package
20         Index file
21
22         @type output: class
23         @param output: optional output class for printing
24         """
25
26         def __init__(self, controller=None):
27                 self.controller = controller
28
29
30         def _get_emaint_binhost(self):
31                 """Obtain a reference to the binhost module class
32
33                 @sets: self.binhost to BinhostHandler class
34                 @rtype: boolean
35                 """
36                 try:
37                         self.emaint_control = Modules()
38                         self.binhost = self.emaint_control._get_class('binhost')
39                 except InvalidModuleName as er:
40                         print( pp.error("Error importing emaint binhost module"), file=sys.stderr)
41                         print( pp.error("Original error: " + er), file=sys.stderr)
42                 except:
43                         return False
44                 return True
45
46
47         def _load_modules(self):
48                 """Import the emaint modules and report the success/fail of them
49                 """
50                 try:
51                         from emaint.module import Modules
52                         from emaint.main import TaskHandler
53                 except ImportError as e:
54                         return False
55                 return True
56
57
58         def clean_pkgs_index(self,):
59                 """This will clean the binpkgs packages index file"""
60                 go = self._load_modules()
61                 if go:
62                         if self.get_emaint_binhost():
63                                 self.taskmaster = TaskHandler(show_progress_bar=True)
64                                 tasks = [self.binhost]
65                                 self.taskmaster.run_tasks(tasks)
66
67
68         def call_emaint(self):
69                 """Run the stand alone emaint script from
70                 a subprocess call.
71
72                 @rtype: integer
73                 @return: the difference in file size
74                 """
75                 file_ = os.path.join(portage.settings['PKGDIR'], 'Packages')
76                 statinfo = os.stat(file_)
77                 size1 = statinfo.st_size
78                 command = "emaint --fix binhost"
79                 try:
80                         retcode = subprocess.call(command, shell=True)
81                         if retcode < 0:
82                                 print( pp.error("Child was terminated by signal" + str(-retcode)), file=sys.stderr)
83                 except OSError as e:
84                         print( pp.error("Execution failed:" + e), file=sys.stderr)
85                 print()
86                 statinfo = os.stat(file_)
87                 clean_size = size1 - statinfo.st_size
88                 self.controller(clean_size, "Packages Index", file_, "Index")
89                 return clean_size