Exemplo n.º 1
0
    def extract(self, src_dir, dest_dir):
        filename = self._get_filename()
        archive_file = os.path.join(src_dir, filename)
        if os.path.isfile(archive_file):
            if not os.path.exists(dest_dir):
                self.log.debug("Creating destination dir '%s'" % dest_dir)
                os.makedirs(dest_dir)
            self.log.info("Extract '%s' to '%s'" % (archive_file, dest_dir))
            extract_file(archive_file, dest_dir)

        else:
            raise FileExtractError("Could not extract '%s' to '%s'. File does "
                                     "not exist." % (archive_file, dest_dir))
Exemplo n.º 2
0
    def create(self, stdout=False):
        if not os.path.isdir(self.dir):
            os.makedirs(self.dir)
        self.tmp_dir = mkdtemp(prefix="tmp-rules-", dir=self.dir)
        self.log.debug("Created temporary directory '%s'" % self.tmp_dir)
        downloader = UrlDownloader(None, self.url)
        source = downloader.copy(self.tmp_dir)
        hashcalc = Hash(source)
        md5 = hashcalc.md5()
        self.log.debug("md5 hash is '%s'" % md5)
        sha1 = hashcalc.sha1()
        self.log.debug("sha1 hash is '%s'" % sha1)

        filename = os.path.basename(source)
        if not "-" in filename and (not self.name or not self.version):
            self.clean()
            raise RulesCreateError("Could not detect name and "\
                    "version from file '%s'" % filename)

        if "-" in filename:
            suffixes = [".tar.gz", ".tar.bz2", ".tgz", "tbz2", ".zip"]
            for suffix in suffixes:
                if filename.endswith(suffix):
                    index = filename.rfind(suffix)
                    filename = filename[:index]
            split = filename.rsplit("-", 1)
            detected_name = split[0]
            name = detected_name.lower()
            version = split[1]
            detected_version = version
        else:
            name = self.name
            version = self.version
            detected_name = self.name
            detected_version = self.version

        if not self.template:
            extract_file(source, self.tmp_dir)
            template = None
            for detector in detectors:
                if detector.detect(name, self.tmp_dir):
                    template = detector.get_template()
                    name = detector.get_name()
                    break
            if not template:
                self.log.info("Could not detected template for '%s'. "
                              "Using default." % name)
                template = Template("default.template")
        else:
            template = Template(self.templatename + ".template")

        self.log.debug("Detected rules name is '%s'" % detected_name)
        self.log.debug("Detected rules version is '%s'" % detected_version)
        self.log.info("Rules name is '%s'" % name)
        self.log.info("Rules version is '%s'" % version)

        vars = dict()
        vars["name"] = name
        vars["version"] = version
        vars["md5"] = md5
        vars["sha1"] = sha1
        vars["url"] = self.url
        vars["rootdir"] = self.dir
        vars["rules"] = self.rules_dir
        vars["rulesname"] = name.replace("-","").capitalize()
        vars["detectedname"] = detected_name
        vars["detectedversion"] = detected_version

        # self.name and self.version may contain templates
        if self.name:
            name = self.name % vars
        if self.version:
            version = self.version % vars

        if stdout:
            print template.replace(vars)
        else:
            new_rules_dir = os.path.join(real_path(self.rules_dir[0]), name)
            if not os.path.exists(new_rules_dir):
                os.makedirs(new_rules_dir)
            try:
                rulesfile = os.path.join(new_rules_dir, "rules.py")
                f = open(rulesfile, "w")
                self.log.info("Creating new rules file '%s'" % rulesfile)
                f.write(template.replace(vars))
            finally:
                f.close()

        self.clean()