From 49dec0375ff95285c62319d0d27807a9ab66a2fd Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.dos.anjos@gmail.com>
Date: Fri, 2 Mar 2018 15:27:16 +0100
Subject: [PATCH] Simplify this script according to discussion in #60

---
 conda/clean-betas.py | 83 +++++++-------------------------------------
 1 file changed, 13 insertions(+), 70 deletions(-)

diff --git a/conda/clean-betas.py b/conda/clean-betas.py
index 1c1a0f2..1908d76 100755
--- a/conda/clean-betas.py
+++ b/conda/clean-betas.py
@@ -3,65 +3,10 @@
 # Cleans-up old beta-conda-packages to avoid storing unused packages
 
 import os
-import re
 import sys
-
 from distutils.version import StrictVersion
 
 
-package_regex = re.compile(r'^(?P<name>\S+)-(?P<version>\d+\.\d+\.\d+[abc]\d+)-(?P<python>(py\d\d))?(?P<hash>h[0-9a-f]{7})_(?P<build>\d+)(?P<extension>\.tar\.bz2)$')
-
-
-def check_regex():
-  """Tests for the above regex"""
-
-
-  def _check(s, name, version, py, _hash, build, ext):
-    m = package_regex.match(s)
-    assert m is not None, 'expected match for %s' % s
-    assert m.group('name') == name, '%r != %r' % (m.group('name'), name)
-    assert m.group('version') == version, '%r != %r' % (m.group('version'), version)
-    assert m.group('python') == py, '%r != %r' % (m.group('python'), py)
-    assert m.group('hash') == _hash, '%r != %r' % (m.group('hash'), _hash)
-    assert m.group('build') == build, '%r != %r' % (m.group('build'), build)
-    assert m.group('extension') == ext, '%r != %r' % (m.group('extension'), ext)
-
-
-  # This regexp must match the following examples:
-  _check('docs-2018.02.21b0-h7a51f39_0.tar.bz2', 'docs', '2018.02.21b0',
-      None, 'h7a51f39', '0', '.tar.bz2')
-
-  _check('docs-with-dashes-2018.02.21b0-h7a51f39_0.tar.bz2',
-      'docs-with-dashes', '2018.02.21b0', None, 'h7a51f39', '0', '.tar.bz2')
-
-  _check('bob.sp-2.0.11b0-py27h21b2d43_7.tar.bz2', 'bob.sp', '2.0.11b0',
-      'py27', 'h21b2d43', '7', '.tar.bz2')
-
-  _check('gridtk-1.5.1b0-py36h361992c_4.tar.bz2', 'gridtk', '1.5.1b0',
-      'py36', 'h361992c', '4', '.tar.bz2')
-
-  _check('bob.measure-2.5.0b0-py36h81a6768_11.tar.bz2', 'bob.measure',
-    '2.5.0b0', 'py36', 'h81a6768', '11', '.tar.bz2')
-
-  _check('bob.ip.caffe_extractor-1.1.2b0-py27hc65a447_0.tar.bz2',
-      'bob.ip.caffe_extractor', '1.1.2b0', 'py27', 'hc65a447', '0', '.tar.bz2')
-
-  _check('bob.db.msu_mfsd_mod-2.2.4b0-py27h2410e3f_2.tar.bz2',
-      'bob.db.msu_mfsd_mod', '2.2.4b0', 'py27', 'h2410e3f', '2', '.tar.bz2')
-
-  _check('bob-3.0.1b0-py27h2dcd9c5_5.tar.bz2', 'bob', '3.0.1b0',
-      'py27', 'h2dcd9c5', '5', '.tar.bz2')
-
-  # This regexp must **not** match the following examples
-  assert package_regex.match('zc.buildout-2.10.0-py27_0.tar.bz2') is None
-  assert package_regex.match('speexdsp-1.2rc3-h5bbff6d_0.tar.bz2') is None
-  assert package_regex.match('pkgtools-0.7.3-py27_0.tar.bz2') is None
-  assert package_regex.match('opencv-3.1.0-np111py27_4.tar.bz2') is None
-  assert package_regex.match('keras-gpu-2.0.8-py27_0.tar.bz2') is None
-  assert package_regex.match('bob-3.0.0-np113py36_0.tar.bz2') is None
-  assert package_regex.match('bob.xyz.bla-2.0.12-np113py27_0.tar.bz2') is None
-
-
 def main(scandir, dry_run):
 
   betas = dict()
@@ -70,20 +15,15 @@ def main(scandir, dry_run):
 
     for f in filenames:
       if f.startswith('.'): continue
-      if f.startswith('repodata.json'): continue
-      m = package_regex.match(f)
-      if m is None:
-        print('ignoring `%s\' (does not match)' % os.path.join(path, f))
-        continue
+      if not f.endswith('.tar.bz2'): continue
+      name, version, build_string = filename[:-8].rsplit('-', 2)
+      build = build_string.rsplit('_')[-1]
 
-      # got a beta package since it matches our regex, insert it into our
-      # list of packages to evaluate
-      name = os.path.basename(path) + '/' + m.group('name')
+      name = os.path.basename(path) + '/' + name
       target = os.path.join(path, f)
-      if m.group('python') is not None: name += '@' + m.group('python')
       betas.setdefault(name, []).append((
-        StrictVersion(m.group('version')), #version
-        int(m.group('build')), #build number
+        StrictVersion(version),
+        int(build), #build number
         os.path.getmtime(target), #cross-platform last modification time
         target,
         ))
@@ -96,10 +36,13 @@ def main(scandir, dry_run):
   for name in sorted(betas.keys()):
     print('\n==== packages for %s (%d) ====' % (name, len(betas[name])))
     sorted_packages = sorted(betas[name])
-    for version, build, mtime, path in sorted_packages[:-1]:
-      print('remove %s (%u)' % (path, mtime))
-      if not dry_run: os.unlink(path)
-    print('[keep] %s (%u)' % (sorted_packages[-1][3], sorted_packages[-1][2]))
+    keep_version, keep_build, _ = sorted_packages[-1]
+    for version, build, mtime, path in sorted_packages:
+      if version == keep_version and build == keep_build:
+        print('[keep] %s (%u)' % (path, mtime))
+      else:
+        print('remove %s (%u)' % (path, mtime))
+        if not dry_run: os.unlink(path)
 
 
 if __name__ == '__main__':
-- 
GitLab