Commit a14a70b2 authored by Seblu's avatar Seblu
Browse files

Add isinstall and update common classes

parent ef28b0de
Loading
Loading
Loading
Loading
+14 −15
Original line number Diff line number Diff line
@@ -9,48 +9,47 @@ InstallSystems Image Manipulation Tool
import os
import argparse
import installsystems
import installsystems.printer as p
import installsystems.image
from installsystems.printer import *
from installsystems.image import SourceImage

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
        p.debug("Debug on")
            debug("Debug on")


def init(options):
    '''Create an empty fresh source image tree'''
    # Directory must not exists
    if os.path.exists(options.path) and os.path.isdir(options.path):
        p.error("Directory already exists: %s" % options.path)
        error("Directory already exists: %s" % options.path)
    # Check if parent path is writable
    parent_path = os.path.abspath(os.path.join(options.path, "../"))
    if not os.access(parent_path, os.W_OK):
        p.error("%s is not writable."%parent_path)
        error("%s is not writable."%parent_path)
    # call init from library
    try:
        simg = installsystems.image.SourceImage(options.path,
                                                verbose=options.verbose,
                                                create=True)
        simg = SourceImage.create(options.path, options.verbose)
    except Exception as e:
        p.error("init failed: %s." % e)
        error("init failed: %s." % e)

def build(options):
    '''Create a package image'''
    for d in ("", "parser", "setup", "data"):
        rp = os.path.join(options.path, d)
        if not os.path.exists(rp):
            p.error("Missing directory: %s" % rp)
            error("Missing directory: %s" % rp)
        if not os.path.isdir(rp):
            p.error("Not a directory: %s" % rp)
            error("Not a directory: %s" % rp)
        if not os.access(rp, os.R_OK|os.X_OK):
            p.error("Unable to access to %s" % rp)
            error("Unable to access to %s" % rp)
    try:
        simg = installsystems.image.SourceImage(options.path)
        simg = SourceImage(options.path)
        simg.build()
    except Exception as e:
        p.error("build failed: %s." % e)
        error("build failed: %s." % e)

# Top level argument parsing
p_main = argparse.ArgumentParser()

bin/isinstall

0 → 100755
+69 −0
Original line number Diff line number Diff line
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Started 18/05/2011 by Seblu <seblu@seblu.net>

'''
InstallSystems Installation Tool
'''

import os
import argparse
import installsystems
from installsystems.printer import *
from installsystems.repository import RepositoryCache

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")


# 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")
p_main.add_argument("-I", "--image-path", dest="image_path", type=str, default=None,
                    help="remote image repository path")
p_main.add_argument("-D", "--data-path", dest="data_path", type=str, default=None,
                    help="remote data repository path")
p_main.add_argument("-C", "--cache-path", dest="cache_path", type=str,
                    default="/var/cache/isinstall",
                    help="local cache repository path")
p_main.add_argument("-V", "--image-version", dest="image_version", type=int, default=None,
                    help="specific image version")
p_main.add_argument("image_name", type=str,
                    help="image to install")

# program entry point
try:
    # Partial parse
    args = p_main.parse_known_args()[0]
    # create repo cache object
    repocache = RepositoryCache(args.cache_path, verbose=args.verbose)
    # register command ligne repo
    if args.image_path is not None:
        repocache.register("cmdline", args.image_path, args.data_path)
    # update remote info if available
    repocache.update()
    # get image package
    pkg = repocache.get(args.image_name, args.image_version)
    # create global dict, used to share and transmit data between scripts
    # and add parser object to allow parser script to extend parser
    gl = { "parser": p_main }
    # run parser scripts
    pkg.run_parser(gl)
    # call parser again, with extended attributes
    args = p_main.parse_args()
    # remove parser object from global
    del gl["parser"]
    gl["args"] = args
    # run setup scripts
    pkg.run_setup(gl)
except Exception as e:
    error(e)

bin/isrepo

100644 → 100755
+18 −23
Original line number Diff line number Diff line
@@ -9,49 +9,44 @@ InstallSystems Repository Manipulation Tool
import os
import argparse
import installsystems
import installsystems.repository
import installsystems.printer as p
from installsystems.printer import *
from installsystems.repository import Repository
from installsystems.image import PackageImage


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
        p.debug("Debug on")
            debug("Debug on")


def init(options):
    '''Create an empty fresh repo tree'''
    # call init from library
    try:
        repo = installsystems.repository.Repository(options.image_path,
                                                    options.data_path,
                                                    verbose=options.verbose,
                                                    create=True)
        Repository.create(options.image_path, options.data_path, options.verbose)
    except Exception as e:
        p.error("init failed: %s." % e)
        error("init failed: %s." % e)

def add(options):
    '''Add a package to repository'''
    try:
        repo = installsystems.repository.Repository(options.image_path,
                                                    options.data_path,
                                                    verbose=options.verbose)
        pkg = installsystems.image.PackageImage(options.path,
                                                verbose=options.verbose)
        repo = Repository(options.image_path, options.data_path, options.verbose)
        pkg = PackageImage(options.path, options.verbose)
        pkg.check_md5()
        repo.add(pkg)
    except Exception as e:
        p.error("add failed: %s." % e)
        error("add failed: %s." % e)

def delete(options):
    '''Remove a package from repository'''
    try:
        repo = installsystems.repository.Repository(options.image_path,
                                                    options.data_path,
                                                    verbose=options.verbose)
        repo = Repository(options.image_path, options.data_path, options.verbose)
        repo.delete(options.image_name, options.image_version)
    except Exception as e:
        p.error("del failed: %s." % e)
        error("del failed: %s." % e)

# Top level argument parsing
p_main = argparse.ArgumentParser()
@@ -62,9 +57,9 @@ p_main.add_argument('-d', "--debug", action=DebugAction, nargs=0,
p_main.add_argument('-q', "--quiet", action="store_false", dest="verbose", default=True,
                    help="active quiet mode")
p_main.add_argument("-I", "--image-path", dest="image_path", type=str,
                    help="Image repository path")
                    help="image repository path")
p_main.add_argument("-D", "--data-path", dest="data_path", type=str,
                    help="Data repository path")
                    help="data repository path")

subparsers = p_main.add_subparsers()
# Init command parser
@@ -88,8 +83,8 @@ args = p_main.parse_args()
# Check global args
if args.image_path is None:
    p_main.print_usage()
    p.error("image path missing")
    error("image path missing")
elif args.data_path is None:
    p_main.print_usage()
    p.error("data path missing")
    error("data path missing")
args.func(args)
+1 −1
Original line number Diff line number Diff line
@@ -10,4 +10,4 @@ canonical_name="installsystems"
version = "1-dev"
debug = False

import printer
__all__ = []
+91 −0
Original line number Diff line number Diff line
# -*- python -*-
# -*- coding: utf-8 -*-
# Started 24/05/2011 by Seblu <seblu@seblu.net>

'''
Database stuff
'''

import json
import os
import shutil
import tarfile
from installsystems.tarball import Tarball
from installsystems.printer import *

class Database(object):
    '''Abstract repo database stuff'''

    db_format = "1"

    @classmethod
    def create(cls, path, verbose=True):
        arrow("Creating repository database", 1, verbose)
        dbpath = os.path.abspath(path)
        if os.path.exists(dbpath):
            raise Exception("db already exists")
        try:
            tarball = Tarball.open(dbpath, mode="w:bz2", dereference=True)
            tarball.add_str("format", Database.db_format, tarfile.REGTYPE, 0444)
            tarball.close()
        except Exception as e:
            raise Exception("Create database failed: %s" % e)
        return cls(path, verbose)

    def __init__(self, path, verbose=True):
        self.path = os.path.abspath(path)
        self.verbose = verbose

    def add(self, package):
        '''Add a packaged image to a db'''
        arrow("Adding metadata to db", 1, self.verbose)
        name = "%s.json" % package.name()
        newdb_path = "%s.new" % self.path
        try:
            db = Tarball.open(self.path, mode='r:bz2')
            newdb = Tarball.open(newdb_path, mode='w:bz2')
            for ti in db.getmembers():
                if ti.name != name:
                    newdb.addfile(ti, db.extractfile(ti))
            newdb.add_str(name, package.jdescription(), tarfile.REGTYPE, 0444)
            db.close()
            newdb.close()
            shutil.move(newdb_path, self.path)
        except Exception as e:
            raise Exception("Adding metadata fail: %s" % e)

    def delete(self, name, version):
        '''Deltete a packaged image'''
        arrow("Removing metadata from db", 1, self.verbose)
        newdb_path = "%s.new" % self.path
        try:
            db = Tarball.open(self.path, mode='r:bz2')
            newdb = Tarball.open(newdb_path, mode='w:bz2')
            for ti in db.getmembers():
                if ti.name != fname:
                    newdb.addfile(ti, db.extractfile(ti))
            db.close()
            newdb.close()
            shutil.move(newdb_path, self.path)
        except Exception as e:
            raise Exception("Removing metadata fail: %s" % e)

    def find(self, name, version=None):
        '''Find last version of an image'''
        try:
            tarb = Tarball.open(self.path, mode='r:bz2')
            candidates = [ int((os.path.splitext(tpname)[0]).rsplit("-", 1)[1])
                           for tpname in tarb.getnames("%s-\d+.json" % name) ]
            tarb.close()
        except Exception as e:
            raise Exception("Find in db %s fail: %s" % (self.path, e))
        # no candidates => go west
        if len(candidates) == 0:
            return None
        # get last version
        if version is None:
            version = max(candidates)
        # check if version exists
        if int(version) not in candidates:
            return None
        return (name, version)
Loading