Skip to content
Snippets Groups Projects
Commit a22b222e authored by Seblu's avatar Seblu
Browse files

Check scripts syntax (by compiling) before adding

parent 880c528c
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,7 @@ def build(args):
# load source image
simg = SourceImage(args.path)
# do the job
simg.build(args.force)
simg.build(force=args.force, check=args.check)
# compute building time
t1 = time.time()
dt = int(t1 - t0)
......@@ -65,6 +65,9 @@ p_init.set_defaults(func=init)
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('-c', "--no-check", action="store_false", dest="check", default=True,
help="do not check compilation before adding scripts")
p_build.add_argument("path", nargs="?", type=str, default=".")
p_build.set_defaults(func=build)
# Parse and run
......
......@@ -129,14 +129,18 @@ class SourceImage(Image):
if not os.path.exists(os.path.join(self.base_path, "description")):
raise Exception("No description file")
def build(self, overwrite=False):
def build(self, force=False, check=True):
'''
Create packaged image
'''
# check if free to create script tarball
if os.path.exists(self.image_name) and overwrite == False:
if os.path.exists(self.image_name) and force == False:
raise Exception("Tarball already exists. Remove it before")
# Create payload files
# Check python file
if check:
self._check_scripts(self.parser_path)
self._check_scripts(self.setup_path)
# Create payload files
payloads = self._create_payloads()
# generate a JSON description
jdesc = self.generate_json_description(payloads)
......@@ -259,12 +263,11 @@ class SourceImage(Image):
for fi in os.listdir(directory):
# check name
if not re.match("\d+-.*\.py$", fi):
debug("name %s skipped: invalid name" % fi)
debug("%s skipped: invalid name" % fi)
continue
# adding file
ti = tarball.gettarinfo(os.path.join(directory, fi),
arcname=os.path.join(basedirectory,
os.path.basename(fi)))
arcname=os.path.join(basedirectory, fi))
ti.mode = 0755
ti.uid = ti.gid = 0
ti.uname = ti.gname = "root"
......@@ -272,6 +275,25 @@ class SourceImage(Image):
arrow("%s added" % fi)
arrowlevel(-1)
def _check_scripts(self, directory):
'''
Check if scripts inside a directory can be compiled
'''
basedirectory = os.path.basename(directory)
arrow("Checking %s scripts" % basedirectory)
arrowlevel(1)
# checking each file
for fi in os.listdir(directory):
# check name
if not re.match("\d+-.*\.py$", fi):
debug("%s skipped: invalid name" % fi)
continue
# compiling file
fs = open(os.path.join(directory, fi), "rb").read()
compile(fs, fi, mode="exec")
arrow(fi)
arrowlevel(-1)
def generate_json_description(self, payloads):
'''
Generate a JSON description file
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment