def _averaging_includes(self, lnk_set, ref_set): dup_ref_set = ref_set[:] for n in range(len(lnk_set), 0, -1): lnk_ref_set = [] ref_num = 0 dup_num = len(dup_ref_set) if n == 0: ref_num = dup_num else: ref_num = JRand.rand_int( 1, min(int(dup_num / n * 2 - 1), dup_num)) for m in range(ref_num): cls = random.choice(dup_ref_set) dup_ref_set.remove(cls) lnk_ref_set.append(cls) if (ref_num < 5) and (random.random() < 0.5): sel_set = [] for cls in ref_set: if cls not in lnk_ref_set: sel_set.append(cls) sel_num = len(sel_set) if sel_num > 0: for m in range(JRand.rand_int(1, min(sel_num, 9))): cls = random.choice(sel_set) sel_set.remove(cls) lnk_ref_set.append(cls) lnk_set[n - 1].setIncludes(lnk_ref_set)
def makeClsMixCode(self): if (self._include_classes is not None): copyArray = None t = len(self._include_classes) n = len(self.methods) for i in range(n): m = self.methods[i] if (copyArray is None) or (len(copyArray) <= 0): copyArray = self._include_classes[:] classes = JRand.rand_lave_list(copyArray, n, i, JRand.rand_int(1, t)) m.makeBody(classes) else: for m in self.methods: m.makeBody()
def make_dir_tree(root_dir, depth, nums): dirs = [] depth_map = {} for i in range(nums): d = JRand.rand_nearest(depth + 1) dir_path = None if (d > 1): levels = None if (d in depth_map): levels = depth_map[d] if (random.random() < 0.8): parent = os.path.dirname(random.choice(levels)) dir_path = JFillerHelper.rand_dir_path_by_depth( parent, 1) levels.append(dir_path) else: dir_path = JFillerHelper.rand_dir_path_by_depth( root_dir, d) levels.append(dir_path) else: dir_path = JFillerHelper.rand_dir_path_by_depth( root_dir, d) levels = [dir_path] depth_map[d] = levels else: dir_path = root_dir if dir_path is not None: dirs.append(dir_path) return dirs
def __init__(self, className, baseClass=None, protocol=None): JOcClass.__init__(self, className, baseClass, protocol) self.imports = [] self.variables = {} self._include_classes = None # 引用的类数组 JOcHelper.randVars(self.variables, 0.85, JRand.rand_nearest(8)) # 初始化私有类变量
def _rand_lnk_classes(self, left=1, right=16): classes = [] classes_copy = self._lnk_classes[:] weight = JRand.rand_int(left, min(len(classes_copy), right)) for i in range(weight): cls = random.choice(classes_copy) classes_copy.remove(cls) classes.append(cls) return classes
def _make_macros_resource(self, macros_name, indent=1): classes = self._rand_lnk_classes(2) pos = JRand.rand_int(0, len(classes)-2) s = '' for i in range(len(classes)): cls = classes[i] s += ST(indent) + JOcMixHelper.callClass(cls) + ' \\' + SN if i == pos: s += ST(indent) + self._cheater_cls.className + '::' + JCheatMaker.get_assets_func() + '();' + ' \\' + SN s += ST(indent) + 'NSLog(@"perform ' + macros_name + ' done");' + SN self.add_interface_macros(macros_name, s)
def _duplicate_includes(self, lnk_set, ref_set): dup_ref_set = ref_set[:] for n in range(len(lnk_set)): lnk_ref_num = JRand.rand_int(1, min(len(dup_ref_set), 19)) lnk_ref_set = [] for m in range(lnk_ref_num): cls = random.choice(dup_ref_set) dup_ref_set.remove(cls) lnk_ref_set.append(cls) lnk_set[n].setIncludes(lnk_ref_set) if len(dup_ref_set) == 0: dup_ref_set = ref_set[:]
def randStatement(self, useClasses=None): r = JRand.rand_nearest(5) if (r == 0): self.__make__statement(useClasses) elif (r == 1): self.__make__if(useClasses) elif (r == 2): self.__make__while(useClasses) elif (r == 3): self.__make__for(useClasses) elif (r == 4): self.__make__switch(useClasses)
def makeBody(self, refClasses=None): # 初始化局部变量 if (self.argNames is None): JOcHelper.randVars(self.variables, 0.00, random.randint(0, 5)) else: JOcHelper.randVars(self.variables, 0.85, JRand.rand_nearest(5)) # 生成逻辑 tree = JOcMixBlock(self) self.lineTree = tree if (refClasses is not None): others = refClasses[:] n = JRand.rand_int(1, len(refClasses)) for i in range(n): cls = JRand.rand_lave_list(others, n, i, 1) tree.randStatement(cls) else: n = random.randint(1, 3) for i in range(n): tree.randStatement() if (self.ret != 'void'): tree.makeReturn()
def _build_mix(self): cls_num = self._class_nums import_less = self._import_less stack_depth = self._stack_depth cls_phase_set = [] for i in range(stack_depth, 0, -1): slice_num = 0 if i == 0: slice_num = cls_num else: slice_num = JRand.rand_int( import_less, min(int(cls_num / i * 2 - import_less), cls_num)) if slice_num > 0: slice_cls_set = [] for n in range(slice_num): cls = JOcMixClass(self._rand_cls_name()) self._mix_classes.append(cls) slice_cls_set.append(cls) cls_phase_set.append(slice_cls_set) # print('slice_num=%d' % slice_num) cls_num -= slice_num lnk_set = cls_phase_set[0] self._lnk_classes.extend(lnk_set) for i in range(1, stack_depth, 1): ref_set = cls_phase_set[i] if len(ref_set) < len(lnk_set): self._duplicate_includes(lnk_set, ref_set) else: self._averaging_includes(lnk_set, ref_set) lnk_set = ref_set method_range = self._method_range for cls in self._mix_classes: cls.makeClsDeclare(JRand.rand_int(method_range[0], method_range[1])) for cls in self._mix_classes: cls.makeClsMixCode()
def __make__declare(self): var_list = self.variables.keys() # 生成指令表 msgNums = 1 + JRand.rand_nearest(4) # [1, 5] for i in range(msgNums): while (True): n = JOcHelper.var(self.messages) if (n not in var_list): self.messages.append(n) break # 生成参数表 argNums = msgNums if (msgNums != 1) else (0 if ( random.random() < 0.5) else 1) if (argNums > 0): self.argTypes = [] self.argNames = [] for i in range(argNums): self.argTypes.append(JRand.chose_nearest(OC_BASE)) while (True): n = JOcHelper.var(self.argNames) if (n not in var_list): self.argNames.append(n) break
def _make_macros_disk_cheat(self, macros_name, indent=1): classes = self._rand_lnk_classes(2) pos = JRand.rand_int(0, len(classes) - 2) s = '' for i in range(len(classes)): cls = classes[i] s += ST(indent + 1) + JOcMixHelper.callClass(cls) + ' \\' + SN if i == pos: s += ST(indent) + 'dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{' + ' \\' + SN s += ST(indent+1) + self._cheater_cls.className + '::' + JCheatMaker.get_disk_i_func() + '();' + ' \\' + SN s += ST(indent+1) + self._cheater_cls.className + '::' + JCheatMaker.get_disk_o_func() + '();' + ' \\' + SN s += ST(indent) + '});' + ' \\' + SN s += ST(indent + 1) + 'NSLog(@"perform ' + macros_name + ' done");' + SN self.add_interface_macros(macros_name, s)
def fill_dir_tree(dirs, byte_size, file_nums, file_ext_list): tree = [] total_dirs = len(dirs) chose_flag = True copy_ext_list = file_ext_list[:] for i in range(total_dirs): ext_list = [] if chose_flag: if (len(copy_ext_list) > 0): ext = random.choice(copy_ext_list) ext_list.append(ext) copy_ext_list.remove(ext) else: chose_flag = False copy_ext_list = file_ext_list[:] ext_list.append(JRand.chose_nearest(copy_ext_list)) else: ext_list.append(JRand.chose_nearest(copy_ext_list)) dir_size = JRand.rand_lave(byte_size, total_dirs, i) dir_sons = JRand.rand_lave(file_nums, total_dirs, i) byte_size -= dir_size file_nums -= dir_sons tree.append(JFillerFolder(dirs[i], dir_size, dir_sons, ext_list)) return tree
def save_to_disk(self): files = [] byte_size = self._dir_size for i in range(self._children): file_path = self.rand_child_path() file_size = JRand.rand_lave(byte_size, self._children, i) byte_size -= file_size stream = JFillerFile.get_bytes(file_size) f = open(file_path, 'wb') f.write(stream) f.flush() f.close() files.append(file_path) return files
def makeClsDeclare(self, radix): methods = self.methods if (methods is None): methods = [] self.methods = methods flag = False # 是否有静态函数的标记 size = radix - JRand.rand_nearest(radix) + 1 for i in range(size): m = JOcMixMethod(self) m.index = i methods.append(m) scope = None if (i == size - 1) and (not flag): scope = '+' # 强制添加一个静态函数 else: scope = random.choice(OC_METH) if (JOcMixHelper.isClassMethod(scope)): flag = True m.makeDeclare(scope)
def makeDeclare(self, scope): self.scope = scope self.ret = JRand.chose_nearest(OC_RET) self.__make__declare()
def rand_file_ext(left=2, min_right=3, right_float=3): return '.' + JWords.rand_lowers( left, min_right + JRand.rand_nearest(right_float))