Newer
Older
# -*- python -*-
# -*- coding: utf-8 -*-
# Started 10/05/2011 by Seblu <seblu@seblu.net>
'''
Image stuff
'''
import os
import stat
import time
import json
import ConfigParser
import subprocess
import gzipstream #until python support gzip not seekable
import installsystems.template as istemplate
import installsystems.tools as istools
from installsystems.tools import PipeFile
from installsystems.tarball import Tarball
format = "1"
if re.match("^[-_\w]+$", buf) is None:
raise Exception("Invalid image name %s" % buf)
@staticmethod
def check_image_version(buf):
if re.match("^\d+$", buf) is None:
raise Exception("Invalid image version %s" % buf)
def create(cls, path, force=False):
if not istools.isfile(path):
raise NotImplementedError("SourceImage must be local")
# main path
parser_path = os.path.join(path, "parser")
setup_path = os.path.join(path, "setup")
if not os.path.exists(d) or not os.path.isdir(d):
os.mkdir(d)
raise Exception("Unable to create directory: %s: %s" % (d, e))
# create dict of file to create
examples = {}
# create description example from template
examples["description"] = {"path": "description", "content": istemplate.description}
# create changelog example from template
examples["changelog"] = {"path": "changelog", "content": istemplate.changelog}
# create parser example from template
examples["parser"] = {"path": "parser/01-parser.py", "content": istemplate.parser}
# create setup example from template
examples["setup"] = {"path": "setup/01-setup.py", "content": istemplate.setup}
for name in examples:
try:
arrow("Creating %s example" % name)
expath = os.path.join(path, examples[name]["path"])
if not force and os.path.exists(expath):
warn("%s already exists. Skipping!" % expath)
continue
open(expath, "w").write(examples[name]["content"])
except Exception as e:
raise Exception("Unable to create example file: %s" % e)
# setting executable rights on files in setup and parser
umask = os.umask(0)
os.umask(umask)
for dpath in (parser_path, setup_path):
for f in os.listdir(dpath):
except Exception as e:
raise Exception("Unable to set rights on %s: %s" % (pf, e))
if not istools.isfile(path):
raise NotImplementedError("SourceImage must be local")
self.base_path = path
self.parser_path = os.path.join(path, "parser")
self.setup_path = os.path.join(path, "setup")
self.payload_path = os.path.join(path, "payload")
self.validate_source_files()
self.description = self.parse_description()
# script tarball path
self.image_name = "%s-%s%s" % (self.description["name"],
self.description["version"],
self.extension)
def validate_source_files(self):
'''
Check if we are a valid SourceImage directories
'''
for d in (self.base_path, self.parser_path, self.setup_path, self.payload_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)
if not os.path.exists(os.path.join(self.base_path, "description")):
raise Exception("No description file")
def build(self, force=False, check=True):
# check if free to create script tarball
if os.path.exists(self.image_name) and force == False:
raise Exception("Tarball already exists. Remove it before")
# Check python file
if check:
self.check_scripts(self.parser_path)
self.check_scripts(self.setup_path)
# generate a JSON description
jdesc = self.generate_json_description(payloads)
'''
Create a script tarball in current directory
'''
# create tarball
arrow("Creating image tarball")
arrowlevel(1)
arrow("Name %s" % self.image_name)
tarball = Tarball.open(self.image_name, mode="w:gz", dereference=True)
raise Exception("Unable to create tarball %s: %s" % (self.image_name, e))
tarball.add_str("description.json", jdescription, tarfile.REGTYPE, 0644)
tarball.add_str("changelog", self.changelog.verbatim, tarfile.REGTYPE, 0644)
tarball.add_str("format", self.format, tarfile.REGTYPE, 0644)
self._add_scripts(tarball, self.parser_path)
self._add_scripts(tarball, self.setup_path)
# closing tarball file
tarball.close()
Loading full blame...