[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Bug 20087 - Clean cache before or after ABI comparison
From: Chenxiong Qi <cqi@redhat.com>
Cache data, currently containing downloaded RPM packages from Koji, is
stored in XDG_CACHE_HOME. This patch allows user to delete cache before
or after the ABI comparison, or both.
* configure.ac: Require shutil module.
* doc/manuals/fedabipkgdiff.rst: Add document for new option
clean-cache, clean-cache-before, and clean-cache-after.
* tools/fedabipkgdiff (clean_cache): New decorator to ensure
cache is cleaned according to specified clean-specific options.
(build_commandline_args_parser): Add new option --clean-cache,
--clean-cache-before and --clean-cache-after.
(diff_local_rpm_with_latest_rpm_from_koji): Decorated by
clean_cache.
(diff_latest_rpms_based_on_distros): Likewise.
(diff_two_nvras_from_koji): Likewise.
* bash-completion/fedabipkgdiff: Add new options.
* tests/mockfedabipkgdiff.in (get_download_dir): Rewrite to
behave just like the original get_download_dir.
(mock_get_download_dir): Removed.
(DOWNLOAD_CACHE_DIR): New global variable pointing directory
holding packages during tests.
(run_fedabipkgdiff): Mock original get_download_dir with the
rewrite get_download_dir.
* tests/runtestfedabipkgdiff.py.in (run_fedabipkgdiff_tests):
Add --clean-cache to run tests to ensure no regression.
Signed-off-by: Chenxiong Qi <cqi@redhat.com>
---
bash-completion/fedabipkgdiff | 5 +++-
configure.ac | 2 +-
doc/manuals/fedabipkgdiff.rst | 14 +++++++++
tests/mockfedabipkgdiff.in | 28 ++++++++---------
tests/runtestfedabipkgdiff.py.in | 4 ++-
tools/fedabipkgdiff | 65 ++++++++++++++++++++++++++++++++++++++++
6 files changed, 99 insertions(+), 19 deletions(-)
diff --git a/bash-completion/fedabipkgdiff b/bash-completion/fedabipkgdiff
index c01a2ae..789d990 100644
--- a/bash-completion/fedabipkgdiff
+++ b/bash-completion/fedabipkgdiff
@@ -16,7 +16,10 @@ _fedabipkgdiff_module()
--dso-only
--no-default-suppression
--no-devel-pkg
- --abipkgdiff"
+ --abipkgdiff
+ --clean-cache
+ --clean-cache-before
+ --clean-cache-after"
COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
return 0
;;
diff --git a/configure.ac b/configure.ac
index 882c652..bf3ff20 100644
--- a/configure.ac
+++ b/configure.ac
@@ -338,7 +338,7 @@ if test x$CHECK_DEPS_FOR_FEDABIPKGDIFF = xyes; then
REQUIRED_PYTHON_MODULES_FOR_FEDABIPKGDIFF="\
argparse logging os re subprocess sys urlparse \
- xdg koji mock rpm imp tempfile mimetypes"
+ xdg koji mock rpm imp tempfile mimetypes shutil"
if test x$ENABLE_FEDABIPKGDIFF != xno; then
AX_CHECK_PYTHON_MODULES([$REQUIRED_PYTHON_MODULES_FOR_FEDABIPKGDIFF],
diff --git a/doc/manuals/fedabipkgdiff.rst b/doc/manuals/fedabipkgdiff.rst
index 7656410..c585266 100644
--- a/doc/manuals/fedabipkgdiff.rst
+++ b/doc/manuals/fedabipkgdiff.rst
@@ -142,6 +142,20 @@ Options
Specify an alternative abipkgdiff instead of the one installed in system.
+ * ``--clean-cache-before``
+
+ Clean cache before ABI comparison.
+
+ * ``--clean-cache-after``
+
+ Clean cache after ABI comparison.
+
+ * ``--clean-cache``
+
+ If you want to clean cache both before and after ABI comparison,
+ ``--clean-cache`` is the convenient way for you to save typing of two
+ options at same time.
+
.. _build:
Note that a build is a specific version and release of an RPM package.
diff --git a/tests/mockfedabipkgdiff.in b/tests/mockfedabipkgdiff.in
index 48a1866..aac99d1 100644
--- a/tests/mockfedabipkgdiff.in
+++ b/tests/mockfedabipkgdiff.in
@@ -68,24 +68,18 @@ ABIPKGDIFF = '@abs_top_builddir@/tools/abipkgdiff'
FEDABIPKGDIFF = '@abs_top_srcdir@/tools/fedabipkgdiff'
INPUT_DIR = '@abs_top_srcdir@/tests/data/test-fedabipkgdiff'
OUTPUT_DIR = '@abs_top_builddir@/tests/output/test-fedabipkgdiff'
-DOWNLOAD_CACHE_DIR_PREFIX=os.path.join(OUTPUT_DIR, 'download-cache')
TEST_TOPDIR = 'file://{0}'.format(INPUT_DIR)
-def get_download_dir():
-
- if not os.path.exists(DOWNLOAD_CACHE_DIR_PREFIX):
- try:
- os.makedirs(DOWNLOAD_CACHE_DIR_PREFIX)
- except:
- pass
- if not os.path.exists(DOWNLOAD_CACHE_DIR_PREFIX):
- sys.exit(1);
-
- return tempfile.mkdtemp(dir=DOWNLOAD_CACHE_DIR_PREFIX)
+DOWNLOAD_CACHE_DIR_PREFIX=os.path.join(OUTPUT_DIR, 'download-cache')
+if not os.path.exists(DOWNLOAD_CACHE_DIR_PREFIX):
+ os.makedirs(DOWNLOAD_CACHE_DIR_PREFIX)
+DOWNLOAD_CACHE_DIR=tempfile.mkdtemp(dir=DOWNLOAD_CACHE_DIR_PREFIX)
-DOWNLOAD_CACHE_DIR = get_download_dir()
-def mock_get_download_dir():
+def get_download_dir():
+ """Mock get_download_dir"""
+ if not os.path.exists(DOWNLOAD_CACHE_DIR):
+ os.makedirs(DOWNLOAD_CACHE_DIR)
return DOWNLOAD_CACHE_DIR
@@ -399,12 +393,14 @@ class MockClientSession(object):
@patch('koji.ClientSession', new=MockClientSession)
@patch('fedabipkgdiff.DEFAULT_KOJI_TOPURL', new=TEST_TOPDIR)
@patch('fedabipkgdiff.DEFAULT_ABIPKGDIFF', new=ABIPKGDIFF)
-@patch('fedabipkgdiff.get_download_dir', side_effect=mock_get_download_dir)
-def run_fedabipkgdiff(get_download_dir):
+@patch('fedabipkgdiff.get_download_dir', new=get_download_dir)
+def run_fedabipkgdiff():
return fedabipkgdiff_mod.main()
+
def do_main():
run_fedabipkgdiff()
+
if __name__ == '__main__':
do_main()
diff --git a/tests/runtestfedabipkgdiff.py.in b/tests/runtestfedabipkgdiff.py.in
index 5b10a71..0ac054a 100755
--- a/tests/runtestfedabipkgdiff.py.in
+++ b/tests/runtestfedabipkgdiff.py.in
@@ -129,7 +129,8 @@ def run_fedabipkgdiff_tests():
output_path = os.path.join(TEST_BUILD_DIR, output_path)
cmd = [FEDABIPKGDIFF,
'--no-default-suppression',
- '--show-identical-binaries'] + args
+ '--show-identical-binaries',
+ '--clean-cache'] + args
with open(output_path, 'w') as out_file:
subprocess.call(cmd, stdout=out_file)
@@ -163,6 +164,7 @@ def main():
result = run_fedabipkgdiff_tests()
return not result
+
if __name__ == '__main__':
exit_code = main()
sys.exit(exit_code)
diff --git a/tools/fedabipkgdiff b/tools/fedabipkgdiff
index f9504f4..f8381ac 100755
--- a/tools/fedabipkgdiff
+++ b/tools/fedabipkgdiff
@@ -28,6 +28,7 @@ import logging
import mimetypes
import os
import re
+import shutil
import subprocess
import sys
@@ -151,6 +152,14 @@ class CannotFindLatestBuildError(Exception):
"""Cannot find latest build from a package"""
+class SetCleanCacheAction(argparse._StoreTrueAction):
+ """Custom Action making clean-cache as bundle of clean-cache-before and clean-cache-after"""
+
+ def __call__(self, parser, namespace, values, option_string=None):
+ setattr(namespace, 'clean_cache_before', self.const)
+ setattr(namespace, 'clean_cache_after', self.const)
+
+
def is_distro_valid(distro):
"""Adjust if a distro is valid
@@ -223,6 +232,37 @@ def log_call(func):
return proxy
+def clean_cache(func):
+ """Clean cache according to the specified options from command line
+
+ This is a decorator. Use it just as any other Python decorator.
+
+ Currently, cache contains downloaded RPM packages from Koji. This decorator
+ is able to clean cache before or after ABI comparison, or both.
+
+ :param func: the method to compare ABI against select RPM packages.
+ :type func: callable object.
+ """
+ @log_call
+ def delete_caches():
+ download_dir = get_download_dir()
+ if global_config.dry_run:
+ print 'DRY-RUN: Delete cached downloaded RPM packages at {0}'.format(download_dir)
+ else:
+ logger.debug('Delete cached downloaded RPM packages at {0}'.format(download_dir))
+ shutil.rmtree(download_dir)
+
+ def wrapper(*args, **kwargs):
+ if global_config.clean_cache_before:
+ delete_caches()
+ result = func(*args, **kwargs)
+ if global_config.clean_cache_after:
+ delete_caches()
+ return result
+
+ return wrapper
+
+
class RPM(object):
"""Wrapper of RPM representing a RPM got from Koji
@@ -1034,6 +1074,7 @@ def run_abipkgdiff(rpm_col1, rpm_col2):
return max(return_codes)
+@clean_cache
@log_call
def diff_local_rpm_with_latest_rpm_from_koji():
"""Diff against local rpm and remove latest rpm
@@ -1067,6 +1108,7 @@ def diff_local_rpm_with_latest_rpm_from_koji():
return run_abipkgdiff(rpm_col1, rpm_col2)
+@clean_cache
@log_call
def diff_latest_rpms_based_on_distros():
"""abipkgdiff rpms based on two distros
@@ -1098,6 +1140,7 @@ def diff_latest_rpms_based_on_distros():
return run_abipkgdiff(rpm_col1, rpm_col2)
+@clean_cache
@log_call
def diff_two_nvras_from_koji():
"""Diff two nvras from koji
@@ -1259,6 +1302,28 @@ def build_commandline_args_parser():
action='store_true',
dest='error_on_warning',
help='Raise error instead of warning')
+ parser.add_argument(
+ '--clean-cache',
+ required=False,
+ action=SetCleanCacheAction,
+ dest='clean_cache',
+ default=None,
+ help='A convenient way to clean cache without specifying '
+ '--clean-cache-before and --clean-cache-after at same time')
+ parser.add_argument(
+ '--clean-cache-before',
+ required=False,
+ action='store_true',
+ dest='clean_cache_before',
+ default=None,
+ help='Clean cache before ABI comparison')
+ parser.add_argument(
+ '--clean-cache-after',
+ required=False,
+ action='store_true',
+ dest='clean_cache_after',
+ default=None,
+ help='Clean cache after ABI comparison')
return parser
--
2.7.4