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 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
# This must not be an unicode string, because configobj don't decode configspec
# with the provided encoding
[image]
name = IS_name
version = IS_version
description = string
author = string
is_min_version = IS_min_version
[compressor]
__many__ = force_list
default_compressor = "gzip"
raise ISError(u"Invalid image name %s" % buf)
# return the image name, because this function is used by ConfigObj
# validate to ensure the image name is correct
return buf
@staticmethod
def check_image_version(buf):
if re.match("^\d+(\.\d+)*(([~+]).*)?$", buf) is None:
raise ISError(u"Invalid image version %s" % buf)
# return the image version, because this function is used by ConfigObj
# validate to ensure the image version is correct
return buf
@staticmethod
def check_min_version(version):
'''
Check InstallSystems min version
'''
if istools.compare_versions(installsystems.version, version) < 0:
raise ISError("Minimum Installsystems version not satisfied "
"(%s)" % version)
# return the version, because this function is used by ConfigObj
# validate to ensure the version is correct
return version
@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 __init__(self):
self.modules = {}
def _load_module(self, name, filename, code=None):
'''
Create a python module from a string or a filename
# unicode safety check
assert(isinstance(name, unicode))
assert(isinstance(filename, unicode))
assert(code is None or isinstance(code, str))
# load code if not provided
if code is None:
code = open(filename, "r").read()
# create an empty module
module = imp.new_module(name)
# compile module code
try:
bytecode = compile(code, filename.encode(locale.getpreferredencoding()), "exec")
except Exception as e:
raise ISError(u"Unable to compile %s" % filename, e)
self.secure_exec_bytecode(bytecode, name, module.__dict__)
except Exception as e:
raise ISError(u"Unable to load %s" % filename, e)
return module
def load_modules(self, select_scripts):
'''
Load all modules selected by generator select_scripts
select_scripts is a generator which return tuples (fp, fn, fc) where:
fp is unicode file path of the module
fn is unicode file name of the module (basename)
fc is unicode file content
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
self.modules = {}
for fp, fn, fc in select_scripts():
# check input unicode stuff
assert(isinstance(fp, unicode))
assert(isinstance(fn, unicode))
assert(isinstance(fc, str))
arrow(fn)
module_name = os.path.splitext(fn.split('-', 1)[1])[0]
self.modules[module_name] = self._load_module(module_name, fp, fc)
arrowlevel(level=old_level)
def run_scripts(self, scripts_name, select_scripts, exec_directory, global_dict):
'''
Execute scripts selected by generator select_scripts
scripts_name is only for display the first arrow before execution
select_scripts is a generator which return tuples (fp, fn, fc) where:
fp is file path of the scripts
fn is file name of the scripts (basename)
fc is file content
exec_directory is the cwd of the running script
global_dict is the globals environment given to scripts
'''
arrow(u"Run %s scripts" % scripts_name)
# backup current directory and loaded modules
cwd = os.getcwd()
for fp, fn, fc in select_scripts():
# check input unicode stuff
assert(isinstance(fp, unicode))
assert(isinstance(fn, unicode))
assert(isinstance(fc, str))
arrow(fn, 1)
# backup arrow level
# chdir in exec_directory
os.chdir(exec_directory)
# compile source code
bytecode = compile(fc, fn.encode(locale.getpreferredencoding()), "exec")
raise ISError(u"Unable to compile script %s" % fp, e)
# add current image
global_dict["image"] = self
Loading full blame...