Revive ldi list command (closes #62664)

authorJulien Cristau <julien.cristau@logilab.fr>
changeset258f6c705d29
branchdefault
phasepublic
hiddenno
parent revision#cda1e3c77e0a debrepo: add a method to get the list of changes files in the dists directory
child revision#ec6910ce87f5 Fix test failures because of missing gpg keyring
files modified by this revision
ldi.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1314954768 -7200
# Fri Sep 02 11:12:48 2011 +0200
# Node ID 258f6c705d29c0507e56bfbbf10cf45e62ad797f
# Parent cda1e3c77e0ad4939a044181c3bfa3f7bdafa2bb
Revive ldi list command (closes #62664)

diff --git a/ldi.py b/ldi.py
@@ -20,11 +20,11 @@
1  import sys
2  import shutil
3  import os
4  import os.path as osp
5  from glob import glob
6 -# from itertools import chain
7 +from itertools import chain
8 
9  from logilab.common import clcommands as cli, shellutils as sht
10 
11  from debinstall.__pkginfo__ import version
12  from debinstall import debrepo
@@ -385,134 +385,133 @@
13                      repo.sign(distdir, self.config.gpg_keyid)
14 
15  LDI.register(Publish)
16 
17 
18 -# class List(Upload):
19 -#     """list all repositories and their distributions"""
20 -#     name = "list"
21 -#     min_args = 0
22 -#     max_args = sys.maxint
23 -#     arguments = "[repository [-d | --distribution] [package.changes ...]]"
24 -#     opt_specs = [
25 -#                  ('-d', '--distribution',
26 -#                    {'dest': 'distribution',
27 -#                     'help': 'list a specific target distribution',
28 -#                    }),
29 -#                  ('-s', '--section',
30 -#                    {'dest': 'section',
31 -#                     'help': "directory that contains the dist nodes ('incoming' or 'dists')",
32 -#                     'default': 'incoming'
33 -#                    }),
34 -#                  ('-o', '--orphaned',
35 -#                    {'dest': 'orphaned',
36 -#                     'action': "store_true",
37 -#                     'default': False,
38 -#                     'help': 'report orphaned packages or files (can be slow)'
39 -#                    }),
40 -#                 ]
41 +class List(Upload):
42 +    """list all repositories and their distributions"""
43 +    name = "list"
44 +    min_args = 0
45 +    arguments = "[repository [-d | --distribution <dist>] [package.changes ...]]"
46 +    options = OPTIONS[1:] + [
47 +                 ('distribution',
48 +                   {'dest': 'distribution',
49 +                    'type': 'string',
50 +                    'short': 'd',
51 +                    'help': 'list a specific target distribution',
52 +                   }),
53 +                 ('section',
54 +                   {'dest': 'section',
55 +                    'type': 'string',
56 +                    'short': 's',
57 +                    'help': "directory that contains the dist nodes ('incoming' or 'dists')",
58 +                    'default': 'incoming',
59 +                   }),
60 +                 ('orphaned',
61 +                   {'dest': 'orphaned',
62 +                    'short': 'o',
63 +                    'type': 'yn',
64 +                    'default': False,
65 +                    'help': 'report orphaned packages or files (can be slow)'
66 +                   }),
67 +                ]
68 
69 -#     def process(self):
70 -#         if not self.args:
71 -#             destdir = self.get_config_value('destination')
72 -#             repositories = self.get_repo_list()
73 -#             self.logger.info("%s available repositories in '%s'"
74 -#                              % (len(repositories), destdir))
75 -#             repositories = sorted([repository for repository in repositories])
76 -#             print(os.linesep.join(repositories))
77 -#             return
78 +    def run(self, args):
79 +        if not args:
80 +            destdir = self.config.repositories_directory
81 +            repositories = []
82 +            for dirname in os.listdir(destdir):
83 +                try:
84 +                    self._check_repository(osp.join(destdir, dirname))
85 +                except cli.CommandError, e:
86 +                    self.logger.debug(e)
87 +                else:
88 +                    repositories.append(dirname)
89 +            self.logger.info("%s available repositories in '%s'"
90 +                             % (len(repositories), destdir))
91 +            repositories = sorted(repositories)
92 +            print os.linesep.join(repositories)
93 +            return
94 +
95 +        path = _repo_path(self.config, args.pop(0))
96 +        repo = self._check_repository(path)
97 +        if self.config.section not in ('dists', 'incoming'):
98 +            raise cli.CommandError('Unknown section %s' % self.config.section)
99 
100 -#         repository = self.args[0]
101 -#         path = self._check_repository(repository, self.options.section)
102 -#         if len(self.args)==1 and not self.options.distribution:
103 -#             lines = []
104 -#             for root, dirs, files in os.walk(path):
105 -#                 orphaned = list()
106 -#                 if dirs:
107 -#                     for d in dirs:
108 -#                         line = "%s/%s" % (repository, d)
109 -#                         if osp.islink(osp.join(root, d)):
110 -#                             line += ' is symlinked to %s' % os.readlink(osp.join(root, d))
111 -#                         else:
112 -#                             nb = len(glob(osp.join(root, d, "*.changes")))
113 -#                             if nb:
114 -#                                 line += " contains %d changes files" % nb
115 -#                             else:
116 -#                                 line += " is empty"
117 -#                             if self.options.orphaned:
118 -#                                 orphaned = self.get_orphaned_files(path, d)
119 -#                                 if orphaned:
120 -#                                     line += " and %d orphaned files" % len(orphaned)
121 -#                         lines.append(line)
122 -#             self.logger.info("%s: %s available distribution(s) in '%s' section"
123 -#                              % (repository, len(lines), self.options.section))
124 -#             for line in lines: print line
125 -#         else:
126 -#             self._print_changes_files(repository, self.options.section,
127 -#                                       self.options.distribution)
128 -#             if self.options.orphaned:
129 -#                 orphaned = self.get_orphaned_files(path, self.options.distribution)
130 -#                 if orphaned:
131 -#                     self.logger.warn("%s: has %s orphaned file(s)"
132 -#                                      % (repository, len(orphaned)))
133 -#                     print '\n'.join(orphaned)
134 -
135 -#         if self.options.section == 'incoming':
136 -#             self.logger.info("use option 'ldi list -s dists %s' to list published content" % repository)
137 -
138 -#     def _print_changes_files(self, repository, section, distribution):
139 -#         """print information about a repository and inside changes files"""
140 -#         filenames = self._find_changes_files(repository, section, distribution)
141 +        if len(args) == 0 and not self.config.distribution:
142 +            lines = []
143 +            for root, dirs, files in os.walk(osp.join(path, self.config.section)):
144 +                orphaned = []
145 +                for d in dirs:
146 +                    dirpath = osp.join(root, d)
147 +                    if osp.islink(dirpath):
148 +                        line = '%s is symlinked to %s' % (dirpath, os.readlink(dirpath))
149 +                    else:
150 +                        nb = len(glob(osp.join(dirpath, "*.changes")))
151 +                        if nb:
152 +                            line = "%s contains %d changes files" % (dirpath, nb)
153 +                        else:
154 +                            line = "%s is empty" % dirpath
155 +                        if self.config.orphaned:
156 +                            orphaned = self.get_orphaned_files(repo, d)
157 +                            if orphaned:
158 +                                line += " and %d orphaned files" % len(orphaned)
159 +                    lines.append(line)
160 +            self.logger.info("%s: %s available distribution(s) in '%s' section",
161 +                             path, len(lines), self.config.section)
162 +            for line in lines:
163 +                print line
164 +        else:
165 +            self._print_changes_files(repo, self.config.section,
166 +                                      self.config.distribution)
167 +            if self.config.orphaned:
168 +                orphaned = self.get_orphaned_files(repo, self.config.distribution)
169 +                if orphaned:
170 +                    self.logger.warn("%s: has %s orphaned file(s)",
171 +                                     path, len(orphaned))
172 +                    print '\n'.join(orphaned)
173 
174 -#         if not filenames:
175 -#             self.logger.warn("%s/%s: no changes file found" % (repository,
176 -#                                                                distribution))
177 -#         else:
178 -#             self.logger.info("%s/%s: %s available changes files"
179 -#                              % (repository, distribution, len(filenames)))
180 -#             filenames = [filename.rsplit('/', 4)[1:] for filename in filenames]
181 -#             for f in filenames:
182 -#                 print("%s/%s: %s" % (f[0], f[2], f[-1]))
183 +    def _print_changes_files(self, repository, section, distribution):
184 +        """print information about a repository and inside changes files"""
185 +        if section == 'dists':
186 +            filenames = repository.dists_changes_files(None, distrib=distribution)
187 +        else:
188 +            filenames = repository.incoming_changes_files(None, distrib=distribution)
189 
190 -#     def get_repo_list(self):
191 -#         """return list of repository and do some checks"""
192 -#         destdir = self.get_config_value('destination')
193 -#         confdir = self.get_config_value('configurations')
194 +        if not filenames:
195 +            self.logger.warn("%s/%s: no changes file found",
196 +                             getattr(repository, '%s_directory' % section),
197 +                             distribution)
198 +        else:
199 +            self.logger.info("%s/%s: %s available changes files",
200 +                             getattr(repository, '%s_directory' % section),
201 +                             distribution, len(filenames))
202 +            filenames = [filename.rsplit('/', 4)[1:] for filename in filenames]
203 +            for f in filenames:
204 +                print "%s/%s: %s" % (f[0], f[2], f[-1])
205 
206 -#         repositories = []
207 -#         for dirname in os.listdir(destdir):
208 -#             # Some administrators like to keep configuration files
209 -#             # in the same directory that the whole repositories
210 -#             if os.path.realpath(os.path.join(destdir, dirname)) == os.path.realpath(confdir):
211 -#                 self.logger.debug('skipping debinstall configuration directory %s', confdir)
212 -#                 continue
213 -#             config = osp.join(confdir, '%s-%s.conf')
214 -#             for conf in ('apt', 'ldi'):
215 -#                 conf_file = config % (dirname, conf)
216 -#                 if not osp.isfile(conf_file):
217 -#                     self.logger.error('could not find %s', conf_file)
218 -#                     break
219 -#             else:
220 -#                 repositories.append(dirname)
221 -#         return repositories
222 +    def get_orphaned_files(self, repository, distrib):
223 +        import fnmatch
224 +        if self.config.section == 'dists':
225 +            changes_files = repository.dists_changes_files(None, distrib=distrib)
226 +            directory = repository.dists_directory
227 +        else:
228 +            changes_files = repository.incoming_changes_files(None, distrib=distrib)
229 +            directory = repository.incoming_directory
230 +        tracked_files = (Changes(f).get_all_files(check_if_exists=False)
231 +                         for f in changes_files if f)
232 +        tracked_files = set(chain(*tracked_files))
233 
234 -#     def get_orphaned_files(self, repository, distrib):
235 -#         import fnmatch
236 -#         changes_files = (glob(os.path.join(repository, distrib,
237 -#                                                 '*.changes')))
238 -#         tracked_files = (Changes(f).get_all_files(check_if_exists=False)
239 -#                          for f in changes_files if f)
240 -#         tracked_files = set(tuple(chain(*tracked_files)))
241 +        untracked_files = set(glob(osp.join(directory, distrib, '*')))
242 +        orphaned_files = untracked_files - tracked_files
243 +        orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Packages*"))
244 +        orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Sources*"))
245 +        orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Contents*"))
246 +        orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Release*"))
247 +        return orphaned_files
248 
249 -#         untracked_files = set([f for f in glob(os.path.join(repository, distrib, '*'))])
250 -#         orphaned_files = untracked_files - tracked_files
251 -#         orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Packages*"))
252 -#         orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Sources*"))
253 -#         orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Contents*"))
254 -#         orphaned_files -= set(fnmatch.filter(orphaned_files, "*/Release*"))
255 -#         return orphaned_files
256 -
257 +LDI.register(List)
258 
259  class Diff(Upload):
260      """Show diffs between files published in a repository and not in another,
261      proposing to upload them.
262