From 819589d8ecb0a2283b080dabf267d8c21df1bded Mon Sep 17 00:00:00 2001 From: Seblu <sebastien.luttringer@smartjog.com> Date: Thu, 26 May 2011 19:49:59 +0200 Subject: [PATCH] Generic improvment. Too many things to tell. --- bin/isimage | 42 +++++++++++++++++----------------- bin/isinstall | 22 +++++++++++------- bin/isrepo | 22 +++++++++--------- installsystems/image.py | 50 +++++++++++++++++++++-------------------- installsystems/tools.py | 7 +----- 5 files changed, 72 insertions(+), 71 deletions(-) diff --git a/bin/isimage b/bin/isimage index a43a04b..29f901b 100755 --- a/bin/isimage +++ b/bin/isimage @@ -8,6 +8,8 @@ InstallSystems Image Manipulation Tool import os import argparse +import time +import datetime import installsystems from installsystems.printer import * from installsystems.image import SourceImage @@ -20,34 +22,27 @@ class DebugAction(argparse.Action): debug("Debug on") -def init(options): +def init(args): '''Create an empty fresh source image tree''' - # Directory must not exists - if os.path.exists(options.path) and os.path.isdir(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): - error("%s is not writable."%parent_path) # call init from library try: - simg = SourceImage.create(options.path, options.verbose) + simg = SourceImage.create(args.path, args.verbose) except Exception as e: error("init failed: %s." % e) -def build(options): +def build(args): '''Create a package image''' - for d in ("", "parser", "setup", "data"): - rp = os.path.join(options.path, d) - if not os.path.exists(rp): - error("Missing directory: %s" % d) - if not os.path.isdir(rp): - error("Not a directory: %s" % rp) - if not os.access(rp, os.R_OK|os.X_OK): - error("Unable to access to %s" % rp) try: - simg = SourceImage(options.path) - simg.build() + # start time + t0 = time.time() + # load source image + simg = SourceImage(args.path) + # do the job + simg.build(args.force) + # compute building time + t1 = time.time() + dt = int(t1 - t0) + arrow("Build time: %s" % datetime.timedelta(seconds=dt), 1, args.verbose) except Exception as e: error("build failed: %s." % e) @@ -59,13 +54,16 @@ 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") + subparsers = p_main.add_subparsers() # Init command parser -p_init = subparsers.add_parser("init", help=init.__doc__) +p_init = subparsers.add_parser("init", help=init.__doc__.lower()) p_init.add_argument("path", help="Path of new image directory") p_init.set_defaults(func=init) # Build command parser -p_build = subparsers.add_parser("build", help=build.__doc__) +p_build = subparsers.add_parser("build", help=build.__doc__.lower()) +p_build.add_argument('-f', "--force", action="store_true", dest="force", default=False, + help="overwrite existing image") p_build.add_argument("path", nargs="?", type=str, default=".") p_build.set_defaults(func=build) # Parse and run diff --git a/bin/isinstall b/bin/isinstall index 65cf142..b3227bd 100755 --- a/bin/isinstall +++ b/bin/isinstall @@ -8,6 +8,8 @@ InstallSystems Installation Tool import os import argparse +import time +import datetime import installsystems import installsystems.tools as istools from installsystems.printer import * @@ -30,17 +32,15 @@ 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, +p_main.add_argument("-I", "--image-repo", 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, +p_main.add_argument("-D", "--data-repo", 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('-u', "--update", action="store_true", dest="update", default=False, - help="update repository cache") p_main.add_argument("image_name", type=str, help="image to install (path or name)") @@ -58,19 +58,25 @@ try: # register command ligne repo if args.image_path is not None: repocache.register("cmdline", args.image_path, args.data_path) - # update remote info if -u options is present - if args.update: - repocache.update() + # update remote info + repocache.update() # get image package pkg = repocache.get(args.image_name, args.image_version) else: - raise Exception("Invalid image name") + 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: diff --git a/bin/isrepo b/bin/isrepo index 70082da..3900a8f 100755 --- a/bin/isrepo +++ b/bin/isrepo @@ -22,29 +22,29 @@ class DebugAction(argparse.Action): debug("Debug on") -def init(options): +def init(args): '''Create an empty fresh repo tree''' # call init from library try: - Repository.create(options.image_path, options.data_path, options.verbose) + Repository.create(args.image_path, args.data_path, args.verbose) except Exception as e: error("init failed: %s." % e) -def add(options): +def add(args): '''Add a package to repository''' try: - repo = Repository(options.image_path, options.data_path, options.verbose) - pkg = PackageImage(options.path, options.verbose) + repo = Repository(args.image_path, args.data_path, args.verbose) + pkg = PackageImage(args.path, args.verbose) pkg.check_md5() repo.add(pkg) except Exception as e: error("add failed: %s." % e) -def delete(options): +def delete(args): '''Remove a package from repository''' try: - repo = Repository(options.image_path, options.data_path, options.verbose) - repo.delete(options.image_name, options.image_version) + repo = Repository(args.image_path, args.data_path, args.verbose) + repo.delete(args.image_name, args.image_version) except Exception as e: error("del failed: %s." % e) @@ -63,18 +63,18 @@ p_main.add_argument("-D", "--data-path", dest="data_path", type=str, subparsers = p_main.add_subparsers() # Init command parser -p_init = subparsers.add_parser("init", help=init.__doc__) +p_init = subparsers.add_parser("init", help=init.__doc__.lower()) p_init.add_argument("image_path", nargs="?", default=argparse.SUPPRESS, help="Path of the new image directory") p_init.add_argument("data_path", 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__) +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__) +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) diff --git a/installsystems/image.py b/installsystems/image.py index 7340427..ed58cb7 100644 --- a/installsystems/image.py +++ b/installsystems/image.py @@ -8,7 +8,6 @@ Image stuff import os import stat -import datetime import time import json import StringIO @@ -17,7 +16,7 @@ import subprocess import json import tarfile import re -import installsystems.template +import installsystems.template as istemplate import installsystems.tools as istools from installsystems.printer import * from installsystems.tarball import Tarball @@ -53,15 +52,6 @@ class Image(object): class SourceImage(Image): '''Image source manipulation class''' - def __init__(self, path, verbose=True, pbzip2=True): - Image.__init__(self, pbzip2) - self.base_path = path - self.parser_path = os.path.join(path, "parser") - self.setup_path = os.path.join(path, "setup") - self.data_path = os.path.join(path, "data") - self.verbose = verbose - self.description = self.parse_description() - @classmethod def create(cls, path, verbose=True, pbzip2=True): '''Create an empty source image''' @@ -80,16 +70,13 @@ class SourceImage(Image): try: # create description example from template arrow("Creating description example", 2, verbose) - open(os.path.join(path, "description"), "w").write( - installsystems.template.description) + open(os.path.join(path, "description"), "w").write(istemplate.description) # create parser example from template arrow("Creating parser script example", 2, verbose) - open(os.path.join(parser_path, "01-parser.py"), "w").write( - installsystems.template.parser) + open(os.path.join(parser_path, "01-parser.py"), "w").write(istemplate.parser) # create setup example from template arrow("Creating setup script example", 2, verbose) - open(os.path.join(setup_path, "01-setup.py"), "w").write( - installsystems.template.setup) + open(os.path.join(setup_path, "01-setup.py"), "w").write(istemplate.setup) except Exception as e: raise Exception("Unable to example file: %s" % e) try: @@ -105,16 +92,35 @@ class SourceImage(Image): raise Exception("Unable to set rights on %s: %s" % (pf, e)) return cls(path, verbose, pbzip2) - def build(self): + def __init__(self, path, verbose=True, pbzip2=True): + Image.__init__(self, pbzip2) + self.base_path = path + self.parser_path = os.path.join(path, "parser") + self.setup_path = os.path.join(path, "setup") + self.data_path = os.path.join(path, "data") + self.verbose = verbose + self.valid_source_image() + self.description = self.parse_description() + + def valid_source_image(self): + '''Check if we are a valid SourceImage''' + for d in (self.base_path, self.parser_path, self.setup_path, self.data_path): + if not os.path.exists(d): + raise Exception("Missing directory: %s" % d) + if not os.path.isdir(d): + raise Exception("Not a directory: %s" % d) + if not os.access(d, os.R_OK|os.X_OK): + raise Exception("Unable to access to %s" % d) + + def build(self, overwrite=False): '''Create packaged image''' - t0 = time.time() # compute script tarball paths tarpath = os.path.join(self.base_path, "%s-%s%s" % (self.description["name"], self.description["version"], self.image_extension)) # check if free to create script tarball - if os.path.exists(tarpath): + if os.path.exists(tarpath) and overwrite == False: raise Exception("Tarball already exists. Remove it before") # printing pbzip2 status if self.pbzip2_path: @@ -148,10 +154,6 @@ class SourceImage(Image): recursive=True, filter=self.tar_scripts_filter) # closing tarball file tarball.close() - # compute building time - t1 = time.time() - dt = int(t1 - t0) - arrow("Build time: %s" % datetime.timedelta(seconds=dt), 2, self.verbose) def data_tarballs(self): '''List all data tarballs in data directory''' diff --git a/installsystems/tools.py b/installsystems/tools.py index 694b676..de0a1e2 100644 --- a/installsystems/tools.py +++ b/installsystems/tools.py @@ -26,6 +26,7 @@ def cp(source, destination): def get_path_type(path): '''Return path type. This is usefull to know what king of path is given''' + from installsystems.image import Image if path.startswith("http://") or path.startswith("https://"): return "http" elif path.startswith("ssh://"): @@ -47,9 +48,3 @@ def complete_path(path): 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