Skip to content

Commit 1963257

Browse files
author
Matthias Koeppe
committed
sage --package metrics: New
1 parent e249bef commit 1963257

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

build/sage_bootstrap/app.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import logging
2525
log = logging.getLogger()
2626

27+
from collections import defaultdict
28+
2729
from sage_bootstrap.package import Package
2830
from sage_bootstrap.tarball import Tarball, FileNotMirroredError
2931
from sage_bootstrap.updater import ChecksumUpdater, PackageUpdater
@@ -366,3 +368,63 @@ def clean(self):
366368
os.remove(filepath)
367369
count += 1
368370
print('{} files were removed from the {} directory'.format(count, SAGE_DISTFILES))
371+
372+
def metrics_cls(self, *package_classes):
373+
"""
374+
Show the metrics of given packages
375+
376+
$ sage --package metrics :standard:
377+
has_file_distros_arch_txt=131
378+
has_file_distros_conda_txt=216
379+
has_file_distros_debian_txt=125
380+
has_file_distros_fedora_txt=138
381+
has_file_distros_gentoo_txt=181
382+
has_file_distros_homebrew_txt=61
383+
has_file_distros_macports_txt=129
384+
has_file_distros_nix_txt=51
385+
has_file_distros_opensuse_txt=146
386+
has_file_distros_slackware_txt=25
387+
has_file_distros_void_txt=184
388+
has_file_patches=35
389+
has_file_spkg_check=59
390+
has_file_spkg_configure_m4=222
391+
has_file_spkg_install=198
392+
has_tarball_upstream_url=231
393+
line_count_file_patches=22561
394+
line_count_file_spkg_check=402
395+
line_count_file_spkg_configure_m4=2792
396+
line_count_file_spkg_install=2960
397+
packages=272
398+
type_standard=272
399+
"""
400+
log.debug('Computing metrics')
401+
metrics = defaultdict(int)
402+
pc = PackageClass(*package_classes)
403+
for package_name in pc.names:
404+
package = Package(package_name)
405+
metrics['packages'] += 1
406+
metrics['type_' + package.type] += 1
407+
for filenames in [['spkg-configure.m4'],
408+
['spkg-install', 'spkg-install.in'],
409+
['spkg-check', 'spkg-check.in'],
410+
['distros/arch.txt'],
411+
['distros/conda.txt'],
412+
['distros/debian.txt'],
413+
['distros/fedora.txt'],
414+
['distros/gentoo.txt'],
415+
['distros/homebrew.txt'],
416+
['distros/macports.txt'],
417+
['distros/nix.txt'],
418+
['distros/opensuse.txt'],
419+
['distros/slackware.txt'],
420+
['distros/void.txt'],
421+
['patches']]:
422+
key = filenames[0].replace('.', '_').replace('-', '_').replace('/', '_')
423+
metrics['has_file_' + key] += int(any(package.has_file(filename)
424+
for filename in filenames))
425+
if not key.startswith('distros_'):
426+
metrics['line_count_file_' + key] += sum(package.line_count_file(filename)
427+
for filename in filenames)
428+
metrics['has_tarball_upstream_url'] += int(bool(package.tarball_upstream_url))
429+
for key, value in sorted(metrics.items()):
430+
print('{0}={1}'.format(key, value))

build/sage_bootstrap/cmdline.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@
189189
42 files were removed from the .../upstream directory
190190
"""
191191

192+
epilog_metrics = \
193+
"""
194+
Print metrics of given package.
195+
196+
EXAMPLE:
197+
198+
$ sage --package metrics :standard:
199+
has_file_distros_arch_txt=131
200+
...
201+
has_file_distros_void_txt=184
202+
has_file_patches=35
203+
has_file_spkg_check=59
204+
has_file_spkg_configure_m4=222
205+
has_file_spkg_install=198
206+
has_tarball_upstream_url=231
207+
line_count_file_patches=22561
208+
...
209+
packages=272
210+
type_standard=272
211+
"""
212+
192213

193214
def make_parser():
194215
"""
@@ -346,6 +367,16 @@ def make_parser():
346367
formatter_class=argparse.RawDescriptionHelpFormatter,
347368
help='Remove outdated source tarballs from the upstream/ directory')
348369

370+
parser_metrics = subparsers.add_parser(
371+
'metrics', epilog=epilog_metrics,
372+
formatter_class=argparse.RawDescriptionHelpFormatter,
373+
help='Print metrics of given packages')
374+
parser_metrics.add_argument(
375+
'package_class', metavar='[package_name|:package_type:]',
376+
type=str, nargs='*', default=[':all:'],
377+
help=('package name or designator for all packages of a given type '
378+
'(one of :all:, :standard:, :optional:, and :experimental:; default: :all:)'))
379+
349380
return parser
350381

351382

@@ -403,6 +434,8 @@ def run():
403434
app.fix_checksum_cls(*args.package_class)
404435
elif args.subcommand == 'clean':
405436
app.clean()
437+
elif args.subcommand == 'metrics':
438+
app.metrics_cls(*args.package_class)
406439
else:
407440
raise RuntimeError('unknown subcommand: {0}'.format(args))
408441

build/sage_bootstrap/package.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,27 @@ def has_file(self, filename):
370370
"""
371371
return os.path.exists(os.path.join(self.path, filename))
372372

373+
def line_count_file(self, filename):
374+
"""
375+
Return the number of lines of the file
376+
377+
Directories are traversed recursively.
378+
379+
OUTPUT:
380+
381+
integer; 0 if the file cannot be read, 1 if it is a symlink
382+
"""
383+
filename = os.path.join(self.path, filename)
384+
if os.path.islink(filename):
385+
return 1
386+
if os.path.isdir(filename):
387+
return sum(self.line_count_file(os.path.join(filename, entry))
388+
for entry in os.listdir(filename))
389+
try:
390+
return len(list(open(filename, "rb")))
391+
except OSError:
392+
return 0
393+
373394
def _init_checksum(self):
374395
"""
375396
Load the checksums from the appropriate ``checksums.ini`` file

0 commit comments

Comments
 (0)