Newer
Older
# -*- python -*-
# -*- coding: utf-8 -*-
# Installsystems is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Installsystems is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with Installsystems. If not, see <http://www.gnu.org/licenses/>.
import codecs
import ConfigParser
import cStringIO
import difflib
import stat
import subprocess
import tarfile
import time
import installsystems.template as istemplate
import installsystems.tools as istools
from installsystems.exception import *
from installsystems.tools import PipeFile
from installsystems.tarball import Tarball
# format should be a float X.Y but for compatibility reason it's a string
# before version 6, it's strict string comparaison
format = "1"
raise ISError(u"Invalid image name %s" % buf)
@staticmethod
def check_image_version(buf):
raise ISError(u"Invalid image version %s" % buf)
@staticmethod
def compare_versions(v1, v2):
'''
For backward compatibility, image class offer a method to compare image versions
But code is now inside tools
return istools.compare_versions(v1, v2)
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")
for d in (path, build_path, parser_path, setup_path, payload_path):
if not os.path.exists(d) or not os.path.isdir(d):
os.mkdir(d)
raise ISError(u"Unable to create directory: %s" % d, e)
# create dict of file to create
examples = {}
# create description example from template
examples["description"] = {"path": "description",
"content": istemplate.description % {
"name": "",
"version": "1",
"description": "",
"author": "",
"is_min_version": installsystems.version}}
# create changelog example from template
examples["changelog"] = {"path": "changelog", "content": istemplate.changelog}
# create build example from template
examples["build"] = {"path": "build/01-build.py", "content": istemplate.build}
# 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(u"Creating %s example" % name)
expath = os.path.join(path, examples[name]["path"])
if not force and os.path.exists(expath):
warn(u"%s already exists. Skipping!" % expath)
continue
open(expath, "w").write(examples[name]["content"])
except Exception as e:
raise ISError(u"Unable to create example file", e)
# setting executable rights on files in setup and parser
umask = os.umask(0)
os.umask(umask)
for dpath in (build_path, parser_path, setup_path):
raise ISError(u"Unable to set rights on %s" % pf, e)
if not istools.isfile(path):
raise NotImplementedError("SourceImage must be local")
self.base_path = os.path.abspath(path)
for pathtype in ("build", "parser", "setup", "payload"):
setattr(self, u"%s_path" % pathtype, os.path.join(self.base_path, pathtype))
self.description = self.parse_description()
self.image_name = u"%s-%s%s" % (self.description["name"],
self.description["version"],
self.extension)
'''
Check if we are a valid SourceImage directories
'''
for d in (self.base_path, self.build_path, self.parser_path,
self.setup_path, self.payload_path):
raise ISError(u"Invalid source image: directory %s is missing" % d)
raise ISError(u"Invalid source image: %s is not a directory" % d)
if not os.access(d, os.R_OK|os.X_OK):
raise ISError(u"Invalid source image: unable to access to %s" % d)
if not os.path.exists(os.path.join(self.base_path, "description")):
raise ISError("Invalid source image: no description file")
def build(self, force=False, force_payload=False, check=True, script=True):
# check if free to create script tarball
if os.path.exists(self.image_name) and force == False:
raise ISError("Tarball already exists. Remove it before")
self.check_scripts(self.parser_path)
self.check_scripts(self.setup_path)
# remove list
rl = set()
# run build script
if script:
rl |= set(self.run_scripts(self.build_path, self.payload_path))
if force_payload:
rl |= set(self.select_payloads())
# remove payloads
Loading full blame...