From b81a51b8bed6548bd322a919710ad4b835614e20 Mon Sep 17 00:00:00 2001 From: DaniloBarros Date: Nov 08 2017 14:54:54 +0000 Subject: [PATCH 1/2] Change tmp folder to default gettempdir folder - Add default system temporary folder to all files in project - Add test case to check if the uncompressed source path is under default temporary folder Signed-off-by: gabrielsclimaco Signed-off-by: DaniloBarros --- diff --git a/kiskadee/fetchers/anitya.py b/kiskadee/fetchers/anitya.py index 741c057..f317f0d 100644 --- a/kiskadee/fetchers/anitya.py +++ b/kiskadee/fetchers/anitya.py @@ -39,7 +39,8 @@ class Fetcher(kiskadee.fetchers.Fetcher): def get_sources(self, source_data): """Download packages from some Anitya Backend.""" - path = tempfile.mkdtemp() + tmp_path = tempfile.gettempdir() + path = tempfile.mkdtemp(dir=tmp_path) backend_name = source_data.get('meta').get('backend').lower() run_backend = self._load_backend(backend_name) if run_backend: diff --git a/kiskadee/fetchers/debian.py b/kiskadee/fetchers/debian.py index c2492e3..3c5872b 100644 --- a/kiskadee/fetchers/debian.py +++ b/kiskadee/fetchers/debian.py @@ -38,7 +38,8 @@ class Fetcher(kiskadee.fetchers.Fetcher): def get_sources(self, source_data): """Download packages from some debian mirror.""" - path = tempfile.mkdtemp() + tmp_path = tempfile.gettempdir() + path = tempfile.mkdtemp(dir=tmp_path) url = self._dsc_url(source_data) try: subprocess.check_output( @@ -112,7 +113,8 @@ class Fetcher(kiskadee.fetchers.Fetcher): :returns: The path to the Sources.gz file """ - path = tempfile.mkdtemp() + tmp_path = tempfile.gettempdir() + path = tempfile.mkdtemp(dir=tmp_path) return os.path.dirname(self.download(path, url, 'Sources.gz')) def _uncompress_gz(self, path): diff --git a/kiskadee/fetchers/juliet.py b/kiskadee/fetchers/juliet.py index b3f8ceb..5f30c30 100644 --- a/kiskadee/fetchers/juliet.py +++ b/kiskadee/fetchers/juliet.py @@ -16,9 +16,12 @@ class Fetcher(kiskadee.fetchers.Fetcher): """Download Juliet 1.2 from SARD's website.""" juliet_url = 'https://samate.nist.gov/SRD/testsuites/juliet/' juliet_filename = 'Juliet_Test_Suite_v1.2_for_C_Cpp.zip' + tmp_path = tempfile.gettempdir() return self.download( - tempfile.mkdtemp(), juliet_url + juliet_filename, juliet_filename + tempfile.mkdtemp(dir=tmp_path), + juliet_url + juliet_filename, + juliet_filename ) @kiskadee.queue.package_enqueuer diff --git a/kiskadee/runner.py b/kiskadee/runner.py index 26005cd..d2fbc37 100644 --- a/kiskadee/runner.py +++ b/kiskadee/runner.py @@ -120,7 +120,8 @@ class Runner: os.path.dirname(compressed_source) ) ) - uncompressed_source_path = tempfile.mkdtemp() + tmp_path = tempfile.gettempdir() + uncompressed_source_path = tempfile.mkdtemp(dir=tmp_path) try: shutil.unpack_archive( compressed_source, diff --git a/kiskadee/tests/test_plugins.py b/kiskadee/tests/test_plugins.py index 3d8f65f..030da9a 100644 --- a/kiskadee/tests/test_plugins.py +++ b/kiskadee/tests/test_plugins.py @@ -38,7 +38,8 @@ class DebianFetcherTestCase(unittest.TestCase): self.data = self.debian_fetcher.config def _download_sources_gz(self): - path = tempfile.mkdtemp() + tmp_path = tempfile.gettempdir() + path = tempfile.mkdtemp(dir=tmp_path) source = 'kiskadee/tests/test_source/Sources.gz' shutil.copy2(source, path) return path @@ -51,7 +52,8 @@ class DebianFetcherTestCase(unittest.TestCase): self.assertEqual(url, expected_url) def test_uncompress_sources_gz(self): - temp_dir = tempfile.mkdtemp() + tmp_path = tempfile.gettempdir() + temp_dir = tempfile.mkdtemp(dir=tmp_path) self.debian_fetcher._download_sources_gz = self._download_sources_gz temp_dir = self.debian_fetcher._download_sources_gz() self.debian_fetcher._uncompress_gz(temp_dir) @@ -60,7 +62,8 @@ class DebianFetcherTestCase(unittest.TestCase): self.assertTrue('Sources' in files) def test_enqueue_a_valid_pkg(self): - temp_dir = tempfile.mkdtemp() + tmp_path = tempfile.gettempdir() + temp_dir = tempfile.mkdtemp(dir=tmp_path) self.debian_fetcher._download_sources_gz = self._download_sources_gz temp_dir = self.debian_fetcher._download_sources_gz() self.debian_fetcher._uncompress_gz(temp_dir) diff --git a/kiskadee/tests/test_runner.py b/kiskadee/tests/test_runner.py index de3f47f..3776e89 100644 --- a/kiskadee/tests/test_runner.py +++ b/kiskadee/tests/test_runner.py @@ -1,4 +1,5 @@ import unittest +import tempfile from kiskadee.runner import Runner import kiskadee.fetchers.example @@ -74,7 +75,8 @@ class AnalyzersTestCase(unittest.TestCase): source_path = self.runner._path_to_uncompressed_source( source_to_analysis, kiskadee.fetchers.example.Fetcher() ) - + tmp_path = tempfile.gettempdir() + self.assertTrue(source_path.find(tmp_path) >= 0) self.assertIsNotNone(source_path) def test_invalid_path_to_uncompressed_source(self): diff --git a/setup.py b/setup.py index bd9a3f4..54d82e7 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ import os # https://github.com/zzzeek/sqlalchemy/blob/master/setup.py kiskadeefile = os.path.join(os.path.dirname(__file__), 'kiskadee', '__init__.py') - +# os.environ['TMPDIR'] = '/tmp' with open(kiskadeefile) as stream: regex = re.compile(r".*__version__ = '(.*?)'", re.S) __version__ = regex.match(stream.read()).group(1) From 40e74db9ae059232ff8ebef0bc44d3eedb260478 Mon Sep 17 00:00:00 2001 From: DaniloBarros Date: Nov 08 2017 14:56:07 +0000 Subject: [PATCH 2/2] Fix log to only print when necessary - Now the log only prints when the folder is actually removed Signed-off-by: DaniloBarros Signed-off-by: gabrielsclimaco --- diff --git a/kiskadee/runner.py b/kiskadee/runner.py index d2fbc37..95b410b 100644 --- a/kiskadee/runner.py +++ b/kiskadee/runner.py @@ -134,13 +134,13 @@ class Runner: # not delete the source code used on tests. if not compressed_source.find("kiskadee/tests") > -1: shutil.rmtree(os.path.dirname(compressed_source)) + kiskadee.logger.debug( + 'ANALYSIS: Remove {} temp directory' + .format(os.path.dirname(compressed_source)) + ) kiskadee.logger.debug( 'ANALYSIS: Unpacked {} source'.format(package['name']) ) - kiskadee.logger.debug( - 'ANALYSIS: Remove {} temp directory' - .format(os.path.dirname(compressed_source)) - ) return uncompressed_source_path except Exception as err: kiskadee.logger.debug('Something went wrong') diff --git a/setup.py b/setup.py index 54d82e7..bd9a3f4 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ import os # https://github.com/zzzeek/sqlalchemy/blob/master/setup.py kiskadeefile = os.path.join(os.path.dirname(__file__), 'kiskadee', '__init__.py') -# os.environ['TMPDIR'] = '/tmp' + with open(kiskadeefile) as stream: regex = re.compile(r".*__version__ = '(.*?)'", re.S) __version__ = regex.match(stream.read()).group(1)