Skip to content
Snippets Groups Projects
isrepo 3.53 KiB
Newer Older
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Started 16/05/2011 by Seblu <seblu@seblu.net>

'''
InstallSystems Repository Manipulation Tool
'''

import os
import installsystems
from installsystems.printer import *
from installsystems.repository import Repository
from installsystems.image import PackageImage
Seblu's avatar
Seblu committed
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")
def init(args):
    '''Create an empty fresh repo tree'''
    # call init from library
    try:
        Repository.create(args.repo, args.verbose)
    except Exception as e:
        raise Exception("init failed: %s" % e)
def add(args):
    '''Add a package to repository'''
    try:
        repo = Repository(args.repo, args.verbose)
        pkg = PackageImage(args.path, args.verbose)
        pkg.check_md5()
        repo.add(pkg)
    except Exception as e:
        raise Exception("add failed: %s" % e)
def delete(args):
    '''Remove a package from repository'''
    try:
        repo = Repository(args.repo, args.verbose)
        repo.delete(args.image_name, args.image_version)
    except Exception as e:
        raise Exception("del failed: %s" % e)

# Top level argument parsing
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")
Seblu's avatar
Seblu committed
p_main.add_argument("-c", "--config", dest="config", type=str, default=None,
                    help="config file path")
p_main.add_argument("-r", "--repo-name", dest="repo_name", type=str, default=None,
                    help="repository name")
subparsers = p_main.add_subparsers()
# Init command parser
p_init = subparsers.add_parser("init", help=init.__doc__.lower())
Seblu's avatar
Seblu committed
p_init.add_argument("repo_image", nargs="?", default=argparse.SUPPRESS,
                    help="Path of the new image directory")
Seblu's avatar
Seblu committed
p_init.add_argument("repo_data", nargs="?", default=argparse.SUPPRESS,
                    help="Path of the new data directory")
p_init.set_defaults(func=init)
# Add command parser
p_add =  subparsers.add_parser("add", help=add.__doc__.lower())
p_add.add_argument("path", type=str)
p_add.set_defaults(func=add)
# Del command parser
p_del =  subparsers.add_parser("del", help=delete.__doc__.lower())
p_del.add_argument("image_name", type=str)
p_del.add_argument("image_version", type=str)
p_del.set_defaults(func=delete)
Seblu's avatar
Seblu committed
try:
    # Parse and run
    args = p_main.parse_args()
    # load config
    config = ConfigFile("isrepo", args.config)
    # get config repositories
    repos = config.repos
    if len(repos) == 1:
        args.repo = repos[repos.keys()[0]]
    elif args.repo_name in repos.keys():
        args.repo = repos[args.repo_name]
    else:
        raise Exception("No image repository found")
    debug("Image repo: %s" % args.repo.image)
    debug("Data repo: %s" % args.repo.data)
Seblu's avatar
Seblu committed
    args.func(args)
except Exception as e:
    p_main.print_usage()
Seblu's avatar
Seblu committed
    error(e)
except KeyboardInterrupt:
    warn("Keyboard Interrupted")