def __init__(self, task): self.watch_file = task['file_dir'] + task['file_name'] self.watch_file_dir = task['file_dir'] if not OS.is_file(self.watch_file): # 删除操作 exit() f = OS.open(self.watch_file) self.lines = f.readlines(99999) f.close() self.host = task['upload_host'] # 主机 self.port = task['upload_port'] # 端口 self.username = task['upload_user'] # 用户名 self.password = task['upload_pwd'] # 密码 self.upload_root = task['upload_root'] # 密码 self.root = task['root'] # 密码
def read_each_line(file_path, func=None): if func is None: return fp = OS.open(file_path, "r") for line in fp: func(line) fp.close()
def out(string, path=None): if path is not None: f = OS.open(path, 'a') f.write(string + '\n') f.close() else: print(string)
def process(self): f = OS.open(self.watch_file, 'w') f.write(''.join(self.lines)) f.close() if self.host != '127.0.0.1': STD.flush('start upload') self.upload()
def __init__(self, pack): for i, line in enumerate(pack.lines): try: pattern = 'import (.*) from (.*)' res = re.findall(pattern, line) if len(res) > 0: pack.lines[i] = '' continue if line.startswith('export'): pack.lines[i] = '' continue pattern = '[\'|\"](.*\.js)[\'|\"]' res = re.findall(pattern, line) if len(res) <= 0: continue s = res[0] if s.startswith('http'): continue for a in range(30): js_path = pack.watch_file_dir + '../' * a + s if OS.is_file(js_path): break ctime = str(os.path.getmtime(js_path)) pack.require_js.add(os.path.abspath(js_path)) pack.lines[i] = re.sub('[\'|\"].*\.js[\'|\"]', '"' + s + '?v=' + ctime + '"', line) except: pack.lines[i] = line
def once_read_cnt(fname, read_size): f = OS.open(fname, "r") while True: block = f.read(read_size) if block: yield block else: f.close() return
def read_reverse(file_path, func=None): file_no_break_path = file_path + '_no_break' OS.open(file_no_break_path, 'w').close() once_read_size = 3333333 f = OS.open(file_no_break_path, 'a') for block in Util.once_read_cnt(file_path, once_read_size): block = block.replace("\n", '{{{') f.write(block) f.close() f = codecs.open(file_no_break_path, encoding='utf-8') f.seek(0, os.SEEK_END) last_position = f.tell() last = '' val = 1 isEnd = False time1 = Util.time() content = '' while 1: last_position -= val if last_position < 0: val = val + last_position last_position = 0 isEnd = True f.seek(last_position) try: print('content:' + content) content = f.read(val) except: Util.err('content:' + content) exit() _list = content.split('{{{') _list.reverse() _list[0] = _list[0] + last last = _list.pop() func(_list) if isEnd: func([last]) break f.close() print('kill_time:' + str(int(time.time()) - time1)) os.remove(file_path) os.remove(file_no_break_path)
def process(self): try: self.require[os.path.abspath(self.watch_file)] = self.require_js for parent in self.require: if os.path.abspath(self.watch_file) in self.require[parent]: parent = parent.replace('\\dist', '') f = OS.open(parent) content = f.read(99999) if content[-1:] == '\n': content = content[:-1] else: content += '\n' f.close() f = OS.open(parent, 'w') f.write(content) f.close() except: Util.err() super().process()
def __init__(self, pack): try: # 打包babel f_new_path = pack.watch_file + '_new' print(f_new_path) f_new_fp = OS.open(f_new_path, 'w') f_new_fp.write(''.join(pack.lines)) f_new_fp.close() f_babel_path = pack.watch_file + '_babel' cmd = 'babel %s -o %s' % (f_new_path, f_babel_path) child = subprocess.Popen(cmd, shell=True) child.wait() f_babel_fp = OS.open(f_babel_path) pack.lines = f_babel_fp.readlines(9999999) f_babel_fp.close() os.remove(f_new_path) os.remove(f_babel_path) except: Util.err()
def __init__(self, pack): for i, line in enumerate(pack.lines): pattern = 'require\([\'|\"](.*?)[\'|\"]\)' res = re.search(pattern, line) if res is None: continue tpl_path = pack.watch_file_dir + res.group(1) try: tpl_fp = OS.open(tpl_path) content = tpl_fp.read(9999999) tpl_fp.close() line2 = re.sub('require\([\'|\"](.*?)[\'|\"]\)', '`' + content + '`', line) pack.lines[i] = line2 except: Util.err() pack.lines[i] = line
def all_file_flush(): for file_path in Util.file_dict: if Util.file_dict[file_path].closed: Util.file_dict[file_path] = OS.open(file_path, "a") Util.file_dict[file_path].flush()
def append_data(file_path, content): if file_path not in Util.file_dict or Util.file_dict[ file_path].closed or not Util.file_dict[file_path]: Util.file_dict[file_path] = OS.open(file_path, "a") Util.file_dict[file_path].write(content)
def add_file(): now_file = OS.open('tmp_' + str(now_index), 'w') now_file.write(chr(5).join(stack)) now_file.close()
def kill_repeat(split_char, path_old, index_arr=None): if index_arr is None: index_arr = [0] print('kill_path:' + path_old) if path_old in Util.file_dict: Util.file_dict[path_old].close() lines = set() need_key = len(index_arr) path_new = path_old + '_repeat' if os.path.exists(path_old): OS.open(path_new, 'w').close() OS.rm_file(path_new) os.rename(path_old, path_new) else: return lines start = Util.time() new = OS.open(path_new) old = OS.open(path_old, 'a') def add_file(): now_file = OS.open('tmp_' + str(now_index), 'w') now_file.write(chr(5).join(stack)) now_file.close() stack = [] now_index = 0 count = 0 for line in new: stack.append(line) if count > 200000: count = 0 add_file() now_index += 1 stack = [] count += 1 add_file() max_index = now_index def handle_line(_line): if len(_line) < 2: return lineArr = _line.split(split_char) try: if need_key > 1: unique_id = Util.get_unique_id(lineArr[index_arr[0]], lineArr[index_arr[1]]) else: unique_id = Util.get_unique_id(lineArr[index_arr[0]]) except: Util.err(_line) return if unique_id not in lines: lines.add(unique_id) old.write(_line) while now_index >= 0: stack = OS.open('tmp_' + str(now_index), 'r').read().split(chr(5)) while len(stack) > 0: handle_line(stack.pop()) now_index -= 1 old.close() new.close() for i in range(max_index, -1, -1): print('./tmp_' + str(i)) os.remove('./tmp_' + str(i)) print(Util.time() - start) os.remove(path_new) return lines