示例#1
0
文件: utils.py 项目: jzlink/library
def loadYaml(name):
    metadata = {}
    conf = config.getInstance()
    if name not in metadata:
        filename = '%s/conf/%s.yml' % (conf['basedir'], name)
        metadata[name] = yaml.load(open(filename, 'r'))

    return metadata[name]
示例#2
0
def log(sql):
    """Used for DEBUG. Write to a log"""
    import config

    config_ = config.getInstance()

    logfile = "%s/library_sql.log" % config_["logdir"]
    fp = open(logfile, "a")
    fp.write("%s: %s\n" % (datetime.now(), sql))
    fp.close()
示例#3
0
def cutImage(imagecut, path, fanart_path, poster_path, skip_facerec=False):
    conf = config.getInstance()
    fullpath_fanart = os.path.join(path, fanart_path)
    fullpath_poster = os.path.join(path, poster_path)
    aspect_ratio = conf.face_aspect_ratio()
    if conf.face_aways_imagecut():
        imagecut = 1
    elif conf.download_only_missing_images(
    ) and not file_not_exist_or_empty(fullpath_poster):
        return
    # imagecut为4时同时也是有码影片 也用人脸识别裁剪封面
    if imagecut == 1 or imagecut == 4:  # 剪裁大封面
        try:
            img = Image.open(fullpath_fanart)
            width, height = img.size
            if width / height > 2 / 3:  # 如果宽度大于2
                if imagecut == 4:
                    # 以人像为中心切取
                    img2 = img.crop(
                        face_crop_width(fullpath_fanart, width, height))
                elif skip_facerec:
                    # 有码封面默认靠右切
                    img2 = img.crop((width - int(height / 3) * aspect_ratio, 0,
                                     width, height))
                else:
                    # 以人像为中心切取
                    img2 = img.crop(
                        face_crop_width(fullpath_fanart, width, height))
            elif width / height < 2 / 3:  # 如果高度大于3
                # 从底部向上切割
                img2 = img.crop(
                    face_crop_height(fullpath_fanart, width, height))
            else:  # 如果等于2/3
                img2 = img
            img2.save(fullpath_poster)
            print(f"[+]Image Cutted!     {Path(fullpath_poster).name}")
        except Exception as e:
            print(e)
            print('[-]Cover cut failed!')
    elif imagecut == 0:  # 复制封面
        shutil.copyfile(fullpath_fanart, fullpath_poster)
        print(f"[+]Image Copyed!     {Path(fullpath_poster).name}")
示例#4
0
    def add(self, *args):
        types = ["db", "api", "web", "work", "mem", "mon", "stat", "work-api", "work-enrich", "ratelimiter"]
        if 0 == len(args) or args[0] not in types:
            raise Fail("must specify what type of instance to add (e.g., %s)" % types)

        add = args[0]
        count = 1

        if 2 == len(args):
            try:
                count = int(args[1])
            except Exception:
                raise Fail("last argument should be number of %s instances to add" % add)

        sim = []
        placements = defaultdict(int)
        placements["us-east-1a"] = 0
        placements["us-east-1b"] = 0
        placements["us-east-1c"] = 0
        top = -1

        dbStack = None
        # infer the suffix number for the new instance (e.g., api4, db2, etc.)
        for instance in self.instances:
            if "db_stack" in instance.tags:
                dbStack = instance.tags["db_stack"]
            if instance.name.startswith(add):
                sim.append(instance)
                cur = int(instance.name[len(add) :])

                placements[instance.placement] += 1

                if cur > top:
                    top = cur

        top += 1

        # assumes all instances are sequential and zero-indexed
        # TODO: reuse old / deleted IDs
        # assert len(sim) == top

        if 0 == len(sim):
            instance = config.getInstance(add)

            sim.append(utils.AttributeDict({"config": instance}))

        conf = dict(sim[0].config).copy()

        # Add db stack
        if dbStack is not None:
            conf["db_stack"] = dbStack

        if isinstance(conf["roles"], basestring):
            conf["roles"] = eval(conf["roles"])

        # attempt to distribute the node evenly across availability zones by
        # placing this new node into the AZ which has the minimum number of
        # existing nodes
        placements = sorted(placements.iteritems(), key=lambda p: (p[1], p[0]))
        # pprint(placements)

        pool = Pool(8)
        instances = []

        def _create_instance(i):
            cur_conf = conf.copy()
            cur_conf["name"] = "%s%d" % (add, top + i)

            # TODO: this assumes nodes were previously evenly distributed
            # instead, calculate minimal placement each iteration
            placement = placements[i % len(placements)][0]
            cur_conf["placement"] = placement

            # create and bootstrap the new instance
            utils.log(
                "[%s] creating instance %s in availability zone %s" % (self, cur_conf["name"], cur_conf["placement"])
            )
            instance = AWSInstance(self, cur_conf)

            try:
                instance.create()
                instances.append(instance)
            except Exception:
                utils.printException()
                utils.log("error adding instance %s" % instance)
                raise

        # initialize instances in parallel
        for i in xrange(count):
            pool.spawn(_create_instance, i)

        pool.join()

        if len(instances) != count:
            utils.log("[%s] error: failed to add %d instances" % (self, count))
            return

        if add in ["api", "web"]:
            utils.log("[%s] checking for matching ELBs within stack %s" % (self, self.name))

            # attempt to find the ELB associated with nodes of the desired type for this stack
            elb = self._get_elb(sim)

            # register the new instance with the appropriate ELB
            if elb is not None:
                utils.log("[%s] registering instances with ELB '%s'" % (self, elb))
                elb.register_instances(list(i.instance_id for i in instances))
            else:
                utils.log("[%s] unable to find ELB for instances'" % (self,))
        elif add == "db":
            # TODO: check if we can initialize N instances in parallel here instead of synchronously
            for instance in instances:
                # initialize new DB instance
                # --------------------------
                db_instances = self.db_instances
                assert len(db_instances) > 0

                host = db_instances[0]
                port = conf["mongodb"]["port"]
                replSet = conf["mongodb"]["replSet"]

                utils.log("[%s] registering instance '%s' with replica set '%s'" % (self, instance.name, replSet))

                # register new instance with existing replia set
                node_name = "%s:%d" % (instance.private_ip_address, port)
                mongo_cmd = 'rs.add("%s")' % node_name

                utils.log("[%s] added instance '%s' to replica set '%s'" % (self, instance.name, replSet))
                status = self.run_mongo_cmd(mongo_cmd, transform=True, db="admin", slave_okay=False)

                pprint(status)
                utils.log()

                status = self._get_replset_status()
                found = False

                for node in status.members:
                    ip = node.name.split(":")[0].lower()

                    if ip == instance.private_ip_address:
                        found = True
                        break

                if found:
                    utils.log("[%s] instance '%s' is online with replica set '%s'" % (self, instance.name, replSet))
                else:
                    utils.log("[%s] error adding instance '%s' to replica set '%s'" % (self, instance.name, replSet))
                    status.members = list(dict(m) for m in status.members)
                    pprint(dict(status))

        self.instances.extend(instances)
        utils.log("[%s] done creating %d %s instance%s" % (self, count, add, "s" if 1 != count else ""))
示例#5
0
 def __init__(self):
     self.conf = config.getInstance()
     self.ordered_cols = []
     self.col_attributes = []
     self.matching_cols = []
     self._metadata = {}