From 738ebdc5bdff3a9e5b1b91acc0f16a5321df9b3f Mon Sep 17 00:00:00 2001 From: Athos Ribeiro Date: Jul 07 2017 01:06:59 +0000 Subject: Move helpers module to util This is just to comply with other python packages pattern of adding helper functions in an util module. --- diff --git a/kiskadee/__init__.py b/kiskadee/__init__.py index 63a5493..dfc82f4 100644 --- a/kiskadee/__init__.py +++ b/kiskadee/__init__.py @@ -13,7 +13,7 @@ Modules: - analyzers - converter - database - - helpers + - util - model - monitor - queue diff --git a/kiskadee/helpers.py b/kiskadee/helpers.py deleted file mode 100644 index 14d680b..0000000 --- a/kiskadee/helpers.py +++ /dev/null @@ -1,47 +0,0 @@ -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -"""Helper functions needing a new home.""" - -import os -import urllib.request -from contextlib import contextmanager - -import kiskadee - - -@contextmanager -def chdir(path): - """Context manager decorator for temporary directories.""" - initial_dir = os.getcwd() - os.chdir(path) - yield - os.chdir(initial_dir) - - -def download(path, url, file_name): - """Download something from the internet. - - :path: The path where the file will be placed when downloaded. - :url: Url of the file. - :file_name: The name of the file that will be saved on the disc. - :return: The absolute path to the downloaded file. - """ - try: - with chdir(path): - in_file = urllib.request.urlopen(url) - data = in_file.read() - with open(file_name, 'wb') as info: - info.write(data) - return ''.join([path, '/', file_name]) - except Exception as err: - kiskadee.logger.debug( - "Cannot download {} " - "source code".format(file_name) - ) - kiskadee.logger.debug(err) - return {} diff --git a/kiskadee/plugins/anitya.py b/kiskadee/plugins/anitya.py index a7edbd1..a983c04 100644 --- a/kiskadee/plugins/anitya.py +++ b/kiskadee/plugins/anitya.py @@ -7,7 +7,7 @@ from packaging import version import fedmsg.consumers import time -import kiskadee.helpers +import kiskadee.util import kiskadee.plugins import kiskadee.queue @@ -113,7 +113,7 @@ class Backends(): source_version = ''.join([source_data.get('version'), '.tar.gz']) homepage = source_data.get('meta').get('homepage') url = ''.join([homepage, '/archive/', source_version]) - return kiskadee.helpers.download(path, url, source_version) + return kiskadee.util.download(path, url, source_version) class AnityaConsumer(fedmsg.consumers.FedmsgConsumer): diff --git a/kiskadee/plugins/debian.py b/kiskadee/plugins/debian.py index a3141a4..e8a780a 100644 --- a/kiskadee/plugins/debian.py +++ b/kiskadee/plugins/debian.py @@ -10,7 +10,7 @@ import shutil from debian.deb822 import Sources import subprocess -from kiskadee.helpers import chdir +from kiskadee.util import chdir import kiskadee.queue RUNNING = True @@ -40,7 +40,7 @@ class Plugin(kiskadee.plugins.Plugin): def get_sources(self, source_data): """Download packages from some debian mirror.""" path = tempfile.mkdtemp() - with kiskadee.helpers.chdir(path): + with kiskadee.util.chdir(path): url = self._dsc_url(source_data) try: subprocess.check_output(['dget', url]) diff --git a/kiskadee/runner.py b/kiskadee/runner.py index aad3cd9..9bd458f 100644 --- a/kiskadee/runner.py +++ b/kiskadee/runner.py @@ -6,7 +6,7 @@ import kiskadee.queue import kiskadee.analyzers import kiskadee.model import kiskadee.database -import kiskadee.helpers +import kiskadee.util import kiskadee.converter running = True @@ -73,7 +73,7 @@ def analyze(package): reports = [] path = tempfile.mkdtemp() shutil.unpack_archive(compressed_source, path) - with kiskadee.helpers.chdir(path): + with kiskadee.util.chdir(path): kiskadee.logger.debug('ANALYSIS: Unpacked!') analyzers = plugin.analyzers() for analyzer in analyzers: diff --git a/kiskadee/util.py b/kiskadee/util.py new file mode 100644 index 0000000..14d680b --- /dev/null +++ b/kiskadee/util.py @@ -0,0 +1,47 @@ +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +"""Helper functions needing a new home.""" + +import os +import urllib.request +from contextlib import contextmanager + +import kiskadee + + +@contextmanager +def chdir(path): + """Context manager decorator for temporary directories.""" + initial_dir = os.getcwd() + os.chdir(path) + yield + os.chdir(initial_dir) + + +def download(path, url, file_name): + """Download something from the internet. + + :path: The path where the file will be placed when downloaded. + :url: Url of the file. + :file_name: The name of the file that will be saved on the disc. + :return: The absolute path to the downloaded file. + """ + try: + with chdir(path): + in_file = urllib.request.urlopen(url) + data = in_file.read() + with open(file_name, 'wb') as info: + info.write(data) + return ''.join([path, '/', file_name]) + except Exception as err: + kiskadee.logger.debug( + "Cannot download {} " + "source code".format(file_name) + ) + kiskadee.logger.debug(err) + return {}