[TIC-CORE] support local repository
[archive/20170607/tools/tic-core.git] / tic / utils / grabber.py
index ebcd054..d0af5e8 100644 (file)
@@ -24,33 +24,38 @@ import logging
 import urllib2
 import contextlib
 from urlgrabber import grabber
-from tic.utils.error import TICError, RepoError
+from tic.utils.error import TICError
 from tic.utils import process
 from tic.utils.file import copyfile
+from tic.config import configmgr
 
 def myurlgrab2(url, filename):
     logger = logging.getLogger(__name__)
     if url.startswith("file:/"):
         filepath = "/%s" % url.replace("file:", "").lstrip('/')
         if not os.path.exists(filepath):
-            raise RepoError("URLGrabber error: can't find file %s" % url)
+            raise TICError(configmgr.message['repo_not_found'] % url)
         if url.endswith('.rpm'):
             return filepath
         else:
-            copyfile(filepath, filename)
+            copyfile(filepath, os.path.dirname(filename))
             logger.info('copy file ' + filepath)
     else:
         try:
             with contextlib.closing(urllib2.urlopen(url)) as op:
                 with open(filename, 'w') as f:
                     f.write(op.read())
-            logger.info('download file from ' + str(url))
+            logger.info('download file from %s' % str(url))
         except urllib2.HTTPError as err:
             if err.code == 404:
-                msg = 'The requested url was not found (%s)' % url
+                msg = configmgr.message['repo_not_found'] % url
             else:
                 msg = str(err)
+            logger.info(err)
             raise TICError(msg)
+        except urllib2.URLError as err:
+            logger.info(err)
+            raise TICError(configmgr.message['server_error'])
     return filename
 
 def myurlgrab(url, filename, proxies, progress_obj = None):
@@ -59,7 +64,7 @@ def myurlgrab(url, filename, proxies, progress_obj = None):
     if url.startswith("file:/"):
         filepath = "/%s" % url.replace("file:", "").lstrip('/')
         if not os.path.exists(filepath):
-            raise RepoError("URLGrabber error: can't find file %s" % url)
+            raise TICError("URLGrabber error: can't find file %s" % url)
         if url.endswith('.rpm'):
             return filepath
         else: