Commit a22b222e authored by Seblu's avatar Seblu
Browse files

Check scripts syntax (by compiling) before adding

parent 880c528c
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -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
+28 −6
Original line number Diff line number Diff line
@@ -129,13 +129,17 @@ 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")
        # 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
@@ -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