Skip to content
tools.py 1.44 KiB
Newer Older
Seblu's avatar
Seblu committed
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Started 26/05/2011 by Seblu <seblu@seblu.net>

'''
InstallSystems Generic Tools Library
'''

import os
import hashlib
Seblu's avatar
Seblu committed
import shutil
Seblu's avatar
Seblu committed

def md5sum(path):
Seblu's avatar
Seblu committed
    '''Compute md5 of a file'''
    m = hashlib.md5()
    m.update(open(path, "r").read())
    return m.hexdigest()

def cp(source, destination):
Seblu's avatar
Seblu committed
    '''Copy a source to destination. Take care of path type'''
Seblu's avatar
Seblu committed
    stype = get_path_type(source)
    dtype = get_path_type(destination)
Seblu's avatar
Seblu committed
    if stype == dtype == "file":
        shutil.copy(source, destination)
    elif stype == "file" and dtype == "":
Seblu's avatar
Seblu committed
        raise NotImplementedError
    else:
        raise NotImplementedError
Seblu's avatar
Seblu committed

def get_path_type(path):
    '''Return path type. This is usefull to know what king of path is given'''
    from installsystems.image import Image
Seblu's avatar
Seblu committed
    if path.startswith("http://") or path.startswith("https://"):
        return "http"
    elif path.startswith("ssh://"):
        return "ssh"
Seblu's avatar
Seblu committed
    elif path.startswith("file://") or path.startswith("/") or os.path.exists(path):
Seblu's avatar
Seblu committed
        return "file"
    elif Image.check_image_name(path):
        return "name"
Seblu's avatar
Seblu committed
    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