Skip to content
Snippets Groups Projects
isinstall 2.95 KiB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Started 18/05/2011 by Seblu <seblu@seblu.net>

'''
InstallSystems Installation Tool
'''

import os
import time
import datetime
import installsystems
import installsystems.tools as istools
from installsystems.printer import *
from installsystems.repository import RepositoryCache
from installsystems.image import PackageImage
from installsystems.config import ConfigFile
# The following import can be removed when min version will be python 2.7
import installsystems.argparse as argparse

class DebugAction(argparse.Action):
    '''Set installsystems in debug mode. Argparse callback'''
    def __call__(self, parser, namespace, values, option_string=None):
        if installsystems.debug == False:
            installsystems.debug = True
            debug("Debug on")


# Argument parsing loading
p_main = argparse.ArgumentParser()
p_main.add_argument("-V", "--version", action="version", version=installsystems.version,
                    help="show installsystems version")
p_main.add_argument('-d', "--debug", action=DebugAction, nargs=0,
                    help="active debug mode")
p_main.add_argument('-q', "--quiet", action="store_false", dest="verbose", default=True,
                    help="active quiet mode")
p_main.add_argument("-c", "--config", dest="config", type=str, default=None,
                    help="config file path")
p_main.add_argument("-v", "--image-version", dest="image_version", type=int, default=None,
                    help="image version")
p_main.add_argument("-t", "--timeout", dest="timeout", type=int, default=3,
                    help="download timeout")
p_main.add_argument("image_name", type=str,
                    help="image to install (path or name)")
try:
    # Partial parse
    args = p_main.parse_known_args()[0]
    # load config
    config = ConfigFile("isinstall", args.config)
    # looks if arguments is a file or image name
    image_name_type = istools.pathtype(args.image_name)
    if image_name_type == "file":
        pkg = PackageImage(istools.abspath(args.image_name))
    elif image_name_type == "name":
        # init repo cache object
        repocache = RepositoryCache(config.cache, timeout=args.timeout, verbose=args.verbose)
        # register repositories
        repocache.register(config.repos)
        # update remote info
        repocache.update()
        # get image package
        pkg = repocache.get(args.image_name, args.image_version)
    else:
        p_main.print_usage()
        exit(1)
    # install start time
    t0 = time.time()
    # run parser scripts
    pkg.run_parser({ "parser": p_main })
    # call parser again, with extended attributes
    args = p_main.parse_args()
    # run setup scripts
    pkg.run_setup({"args": args})
    # compute building time
    t1 = time.time()
    dt = int(t1 - t0)
    arrow("Install time: %s" % datetime.timedelta(seconds=dt), 1, args.verbose)
except Exception as e:
    error(e)
except KeyboardInterrupt:
    warn("Keyboard Interrupted")