From 0f241165f8381a84a120e9aa19039ca9b8a135b7 Mon Sep 17 00:00:00 2001
From: Seblu <sebastien.luttringer@smartjog.com>
Date: Thu, 26 May 2011 16:21:06 +0200
Subject: [PATCH] Introduce new module tools

New module tools offer functions to base is class and also to setup scripts.
Drop RepositoryBase class.
---
 bin/isinstall                |  5 ++--
 installsystems/repository.py | 42 +++++++-------------------------
 installsystems/tools.py      | 46 ++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 36 deletions(-)
 create mode 100644 installsystems/tools.py

diff --git a/bin/isinstall b/bin/isinstall
index 03adea6..0b8b13b 100755
--- a/bin/isinstall
+++ b/bin/isinstall
@@ -9,6 +9,7 @@ InstallSystems Installation Tool
 import os
 import argparse
 import installsystems
+import installsystems.tools as istools
 from installsystems.printer import *
 from installsystems.repository import RepositoryCache
 from installsystems.image import PackageImage
@@ -48,8 +49,8 @@ try:
     # Partial parse
     args = p_main.parse_known_args()[0]
     # looks if arguments is a file or image name
-    if os.path.exists(args.image_name):
-        pkg = PackageImage(args.image_name)
+    if istools.get_path_type(args.image_name) == "file":
+        pkg = PackageImage(istools.complete_path(args.image_name))
     else:
         # init repo cache object
         repocache = RepositoryCache(args.cache_path, verbose=args.verbose)
diff --git a/installsystems/repository.py b/installsystems/repository.py
index dfc71a7..8498ab6 100644
--- a/installsystems/repository.py
+++ b/installsystems/repository.py
@@ -11,39 +11,13 @@ import time
 import shutil
 import json
 import installsystems
+import installsystems.tools as istools
 from installsystems.printer import *
 from installsystems.tarball import Tarball
 from installsystems.image import Image, PackageImage
 from installsystems.database import Database
 
-class RepositoryBase(object):
-    '''Base repository class'''
-
-    def complete_path(self, path):
-        '''Format a path to be complete'''
-        if self.path_type(path) in ('http', 'ssh'):
-            return path
-        else:
-            return os.path.abspath(path)
-
-    def path_type(self, path):
-        '''Return path type'''
-        if path.startswith("http://") or path.startswith("https://"):
-            return "http"
-        # elif path.startswith("ssh://"):
-        #     return "ssh"
-        return "file"
-
-    def cp(self, source, destination):
-        '''Copy a source to destination. Take care of path type'''
-        stype = self.path_type(source)
-        dtype = self.path_type(destination)
-        if stype == dtype == "file":
-            shutil.copy(source, destination)
-        elif stype == "file" and dtype == "":
-            pass
-
-class Repository(RepositoryBase):
+class Repository(object):
     '''Repository class'''
 
     last_name = "last"
@@ -136,7 +110,7 @@ class Repository(RepositoryBase):
         return ts
 
 
-class RepositoryCache(RepositoryBase):
+class RepositoryCache(object):
     '''Local repository cache class'''
 
     def __init__(self, cache_path, verbose=True):
@@ -154,8 +128,8 @@ class RepositoryCache(RepositoryBase):
 
     def register(self, name, image, data):
         '''Register a repository to track'''
-        self.repos[name] = Repository(self.complete_path(image),
-                                      self.complete_path(data),
+        self.repos[name] = Repository(istools.complete_path(image),
+                                      istools.complete_pathh(data),
                                       verbose=self.verbose)
 
     def update(self):
@@ -167,9 +141,9 @@ class RepositoryCache(RepositoryBase):
                                                            self.last(r)))
             if self.repos[r].last() > self.last(r):
                 # copy last file
-                self.cp(self.repos[r].last_path, os.path.join(self.last_path, r))
+                istools.cp(self.repos[r].last_path, os.path.join(self.last_path, r))
                 # copy db file
-                self.cp(self.repos[r].db.path, os.path.join(self.db_path, r))
+                istools.cp(self.repos[r].db.path, os.path.join(self.db_path, r))
                 arrow("%s updated" % r, 2, self.verbose)
 
     def last(self, reponame):
@@ -191,7 +165,7 @@ class RepositoryCache(RepositoryBase):
         # get remote image
         remotepath = os.path.join(self.repos[reponame].image_path, filename)
         arrow("Copying from repository", 2, self.verbose)
-        self.cp(remotepath, localpath)
+        istools.cp(remotepath, localpath)
         return localpath
 
     def find_image(self, name, version):
diff --git a/installsystems/tools.py b/installsystems/tools.py
new file mode 100644
index 0000000..294a7b0
--- /dev/null
+++ b/installsystems/tools.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Started 26/05/2011 by Seblu <seblu@seblu.net>
+
+'''
+InstallSystems Generic Tools Library
+'''
+
+import os
+
+def cp(self, source, destination):
+    '''Copy a source to destination. Take care of path type'''
+    stype = path_type(source)
+    dtype = path_type(destination)
+    if stype == dtype == "file":
+        shutil.copy(source, destination)
+    elif stype == "file" and dtype == "":
+        pass
+
+def get_path_type(path):
+    '''Return path type. This is usefull to know what king of path is given'''
+    if path.startswith("http://") or path.startswith("https://"):
+        return "http"
+    elif path.startswith("ssh://"):
+        return "ssh"
+    elif path.startswith("file://") or os.path.exists(path):
+        return "file"
+    return None
+
+def complete_path(path):
+    '''Format a path to be complete'''
+    ptype = get_path_type(path)
+    if ptype in ("http", "ssh"):
+        return path
+    elif ptype == "file":
+        if path.startswith("file://"):
+            path = path[len("file://")]
+        return os.path.abspath(path)
+    else:
+        return None
+
+def path_strip_file(path):
+    '''Remove file:// header of a local file path'''
+    if path.startswith("file://"):
+        return path[len("file://")]
+    return path
-- 
GitLab