Change working dir
[functest-xtesting.git] / xtesting / core / campaign.py
index 5c5744e..901df58 100644 (file)
@@ -15,17 +15,17 @@ import logging.config
 import mimetypes
 import os
 import re
+import urllib
 import zipfile
 
-from urllib.parse import urlparse
 import boto3
 from boto3.s3.transfer import TransferConfig
 import botocore
-import pkg_resources
 import requests
 
 from xtesting.core import testcase
 from xtesting.utils import env
+from xtesting.utils import config
 from xtesting.utils import constants
 
 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
@@ -81,7 +81,7 @@ class Campaign():
                         "^{os.environ['HTTP_DST_URL']}/*", '',
                         output["results"][i]["details"]["links"][j])
             Campaign.__logger.debug("data to archive: \n%s", output)
-            with open("{env.get('BUILD_TAG')}.json", "w",
+            with open(f"{env.get('BUILD_TAG')}.json", "w",
                       encoding='utf-8') as dfile:
                 json.dump(output, dfile)
         except Exception:  # pylint: disable=broad-except
@@ -120,26 +120,29 @@ class Campaign():
             dst_s3_url = os.environ["S3_DST_URL"]
             multipart_threshold = 5 * 1024 ** 5 if "google" in os.environ[
                 "S3_ENDPOINT_URL"] else 8 * 1024 * 1024
-            config = TransferConfig(multipart_threshold=multipart_threshold)
-            bucket_name = urlparse(dst_s3_url).netloc
+            tconfig = TransferConfig(multipart_threshold=multipart_threshold)
+            bucket_name = urllib.parse.urlparse(dst_s3_url).netloc
             s3path = re.search(
-                '^/*(.*)/*$', urlparse(dst_s3_url).path).group(1)
+                '^/*(.*)/*$', urllib.parse.urlparse(dst_s3_url).path).group(1)
             prefix = os.path.join(s3path, build_tag)
             # pylint: disable=no-member
             for s3_object in b3resource.Bucket(bucket_name).objects.filter(
                     Prefix=f"{prefix}/"):
-                path, _ = os.path.split(s3_object.key)
+                path, _ = os.path.split(
+                    urllib.parse.unquote_plus(s3_object.key))
                 lpath = re.sub(f'^{s3path}/*', '', path)
                 if lpath and not os.path.exists(lpath):
                     os.makedirs(lpath)
-                # pylint: disable=no-member
-                b3resource.Bucket(bucket_name).download_file(
-                    s3_object.key,
-                    re.sub(f'^{s3path}/*', '', s3_object.key),
-                    Config=config)
                 Campaign.__logger.info(
                     "Downloading %s",
-                    re.sub(f'^{s3path}/*', '', s3_object.key))
+                    re.sub(f'^{s3path}/*', '',
+                           urllib.parse.unquote_plus(s3_object.key)))
+                # pylint: disable=no-member
+                b3resource.Bucket(bucket_name).download_file(
+                    urllib.parse.unquote_plus(s3_object.key),
+                    re.sub(f'^{s3path}/*', '',
+                           urllib.parse.unquote_plus(s3_object.key)),
+                    Config=tconfig)
             return Campaign.EX_OK
         except Exception:  # pylint: disable=broad-except
             Campaign.__logger.exception("Cannot publish the artifacts")
@@ -183,15 +186,15 @@ class Campaign():
             dst_s3_url = os.environ["S3_DST_URL"]
             multipart_threshold = 5 * 1024 ** 5 if "google" in os.environ[
                 "S3_ENDPOINT_URL"] else 8 * 1024 * 1024
-            config = TransferConfig(multipart_threshold=multipart_threshold)
-            bucket_name = urlparse(dst_s3_url).netloc
+            tconfig = TransferConfig(multipart_threshold=multipart_threshold)
+            bucket_name = urllib.parse.urlparse(dst_s3_url).netloc
             mime_type = mimetypes.guess_type(f'{build_tag}.zip')
-            path = urlparse(dst_s3_url).path.strip("/")
+            path = urllib.parse.urlparse(dst_s3_url).path.strip("/")
             # pylint: disable=no-member
             b3resource.Bucket(bucket_name).upload_file(
                 f'{build_tag}.zip',
                 os.path.join(path, f'{build_tag}.zip'),
-                Config=config,
+                Config=tconfig,
                 ExtraArgs={'ContentType': mime_type[
                     0] or 'application/octet-stream'})
             dst_http_url = os.environ["HTTP_DST_URL"]
@@ -217,10 +220,11 @@ def main():
     if not os.path.exists(testcase.TestCase.dir_results):
         os.makedirs(testcase.TestCase.dir_results)
     if env.get('DEBUG').lower() == 'true':
-        logging.config.fileConfig(pkg_resources.resource_filename(
-            'xtesting', constants.DEBUG_INI_PATH))
+        logging.config.fileConfig(config.get_xtesting_config(
+            'logging.debug.ini', constants.DEBUG_INI_PATH_DEFAULT))
     else:
-        logging.config.fileConfig(pkg_resources.resource_filename(
-            'xtesting', constants.INI_PATH))
+        logging.config.fileConfig(config.get_xtesting_config(
+            'logging.ini', constants.INI_PATH_DEFAULT))
     logging.captureWarnings(True)
+    os.chdir(testcase.TestCase.dir_results)
     Campaign.zip_campaign_files()