Repository: jboss-dockerfiles/dogen

Issue:
Make it possible to rename the target artifact

Discussion:

Patch:
diff --git a/Dockerfile b/Dockerfile
index c43d93e..c6d23f4 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,4 @@
-FROM alpine:3.3
+FROM alpine:3.5
 RUN apk add --update sudo bash py-setuptools git && rm -rf /var/cache/apk/*
 
 ENV DOGEN_VERSION master
diff --git a/dogen/generator.py b/dogen/generator.py
index 7205db8..7e10cc5 100644
--- a/dogen/generator.py
+++ b/dogen/generator.py
@@ -48,12 +48,12 @@ class Generator(object):
         self.ssl_verify to False.
         """
 
-        self.log.debug("Fetching '%s' file..." % location)
+        self.log.info("Fetching '%s' file..." % location)
 
         if not output:
             output = tempfile.mktemp("-dogen")
 
-        self.log.debug("Fetched file will be saved as '%s'..." % output)
+        self.log.info("Fetched file will be saved as '%s'..." % os.path.basename(output))
 
         with open(output, 'wb') as f:
             f.write(requests.get(location, verify=self.ssl_verify).content)
@@ -289,7 +289,14 @@ class Generator(object):
 
         for source in self.cfg['sources']:
             url = source['url']
+            target = source.get('target')
+
             basename = os.path.basename(url)
+
+            # In case we specify target name for the artifact - use it
+            if target:
+                basename = target
+
             files.append(basename)
             filename = ("%s/%s" % (self.output, basename))
             passed = False
@@ -297,7 +304,8 @@ class Generator(object):
                 if os.path.exists(filename):
                     self.check_sum(filename, source['md5sum'])
                     passed = True
-            except:
+            except Exception as e:
+                self.log.warn(str(e))
                 passed = False
 
             if not passed:
@@ -306,9 +314,7 @@ class Generator(object):
                     self.log.info("Using '%s' as cached location for sources" % sources_cache)
                     url = "%s/%s" % (sources_cache, basename)
 
-                self.log.info("Downloading '%s'..." % url)
-                with open(filename, 'wb') as f:
-                    f.write(requests.get(url, verify=self.ssl_verify).content)
+                self._fetch_file(url, filename)
                 self.check_sum(filename, source['md5sum'])
 
         return files
diff --git a/dogen/schema/kwalify_schema.yaml b/dogen/schema/kwalify_schema.yaml
index 628631b..d308680 100644
--- a/dogen/schema/kwalify_schema.yaml
+++ b/dogen/schema/kwalify_schema.yaml
@@ -63,6 +63,7 @@ map:
       - map:
           url: {type: str}
           md5sum: {type: str}
+          target: {type: str}
   packages:
     seq:
       - {type: str}


Test patch:
diff --git a/tests/test_unit_generate_configuration.py b/tests/test_unit_generate_configuration.py
index adcbafd..bf26c55 100644
--- a/tests/test_unit_generate_configuration.py
+++ b/tests/test_unit_generate_configuration.py
@@ -262,3 +262,53 @@ class TestConfig(unittest.TestCase):
         generator.configure()
         generator._handle_scripts()
         # success if no stack trace thrown
+
+    @mock.patch('dogen.generator.Generator.check_sum')
+    @mock.patch('dogen.generator.Generator._fetch_file')
+    def test_handling_sources(self, mock_fetch_file, mock_check_sum):
+        with self.descriptor as f:
+            f.write("sources:\n  - url: http://somehost.com/file.zip\n    md5sum: e9013fc202c87be48e3b302df10efc4b".encode())
+
+        generator = Generator(self.log, self.args)
+        generator.configure()
+        generator.handle_sources()
+
+        mock_fetch_file.assert_called_with('http://somehost.com/file.zip', 'target/file.zip')
+
+    @mock.patch('dogen.generator.os.path.exists', return_value=True)
+    @mock.patch('dogen.generator.Generator.check_sum')
+    @mock.patch('dogen.generator.Generator._fetch_file')
+    def test_handling_sources_when_local_file_exists_and_is_correct(self, mock_fetch_file, mock_check_sum, mock_path):
+        with self.descriptor as f:
+            f.write("sources:\n  - url: http://somehost.com/file.zip\n    md5sum: e9013fc202c87be48e3b302df10efc4b".encode())
+
+        generator = Generator(self.log, self.args)
+        generator.configure()
+        generator.handle_sources()
+
+        self.assertEqual(mock_fetch_file.call_count, 0)
+
+    @mock.patch('dogen.generator.os.path.exists', return_value=True)
+    @mock.patch('dogen.generator.Generator.check_sum', side_effect=[Exception("Bleh"), None])
+    @mock.patch('dogen.generator.Generator._fetch_file')
+    def test_handling_sources_when_local_file_exists_and_is_broken(self, mock_fetch_file, mock_check_sum, mock_path):
+        with self.descriptor as f:
+            f.write("sources:\n  - url: http://somehost.com/file.zip\n    md5sum: e9013fc202c87be48e3b302df10efc4b".encode())
+
+        generator = Generator(self.log, self.args)
+        generator.configure()
+        generator.handle_sources()
+
+        mock_fetch_file.assert_called_with('http://somehost.com/file.zip', 'target/file.zip')
+
+    @mock.patch('dogen.generator.Generator.check_sum')
+    @mock.patch('dogen.generator.Generator._fetch_file')
+    def test_handling_sources_with_target_filename(self, mock_fetch_file, mock_check_sum):
+        with self.descriptor as f:
+            f.write("sources:\n  - url: http://somehost.com/file.zip\n    md5sum: e9013fc202c87be48e3b302df10efc4b\n    target: target.zip".encode())
+
+        generator = Generator(self.log, self.args)
+        generator.configure()
+        generator.handle_sources()
+
+        mock_fetch_file.assert_called_with('http://somehost.com/file.zip', 'target/target.zip')
diff --git a/tests/test_unit_generate_handle_files.py b/tests/test_unit_generate_handle_files.py
index 51bd803..749eee1 100644
--- a/tests/test_unit_generate_handle_files.py
+++ b/tests/test_unit_generate_handle_files.py
@@ -41,8 +41,8 @@ class TestFetchFile(unittest.TestCase):
             mock_requests.assert_called_with('https://host/file.tmp', verify=None)
             mock_file().write.assert_called_once_with("file-content")
 
-        self.log.debug.assert_any_call("Fetching 'https://host/file.tmp' file...")
-        self.log.debug.assert_any_call("Fetched file will be saved as 'some-file'...")
+        self.log.info.assert_any_call("Fetching 'https://host/file.tmp' file...")
+        self.log.info.assert_any_call("Fetched file will be saved as 'some-file'...")
 
 
     @mock.patch('dogen.generator.tempfile.mktemp', return_value="tmpfile")
@@ -56,8 +56,8 @@ class TestFetchFile(unittest.TestCase):
             mock_requests.assert_called_with('https://host/file.tmp', verify=None)
             mock_file().write.assert_called_once_with("file-content")
 
-        self.log.debug.assert_any_call("Fetching 'https://host/file.tmp' file...")
-        self.log.debug.assert_any_call("Fetched file will be saved as 'tmpfile'...")
+        self.log.info.assert_any_call("Fetching 'https://host/file.tmp' file...")
+        self.log.info.assert_any_call("Fetched file will be saved as 'tmpfile'...")
 
 class TestCustomTemplateHandling(unittest.TestCase):
     def setUp(self):