[TIC-CORE] support local repository
[archive/20170607/tools/tic-core.git] / tic / utils / grabber.py
index 93bc8ea..d0af5e8 100644 (file)
 
 import os
 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 TICError(configmgr.message['repo_not_found'] % url)
+        if url.endswith('.rpm'):
+            return filepath
+        else:
+            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 %s' % str(url))
+        except urllib2.HTTPError as err:
+            if err.code == 404:
+                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):
     logger = logging.getLogger(__name__)
     g = grabber.URLGrabber()
-    
     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:
@@ -65,13 +96,4 @@ def myurlgrab(url, filename, proxies, progress_obj = None):
     return filename
 
 if __name__ == '__main__':
-    # file url
-    full_url = 'file://home/shinchulwoo/project/tic_view.json'
-    filename = '/var/tmp/tic_view.json'
-    myurlgrab(full_url, filename, None)
-    # http url
-    full_url = 'https://download.tizen.org/snapshots/tizen/mobile/latest/repos/arm64-wayland/packages/repodata/repomd.xml'
-    filename = '/var/tmp/repomd.xml'
-    myurlgrab(full_url, filename, None)
-    
-    
+    pass
\ No newline at end of file