def token_refresh(self): global post_json data = {"refresh_token": self.refresh_token} post = requests.post( 'https://websv.aliyundrive.com/token/refresh', data=json.dumps(data), headers={ 'content-type': 'application/json;charset=UTF-8' }, verify=False ) try: post_json = post.json() # 刷新配置中的token with open(os.getcwd() + '/config.json', 'rb') as f: config = json.loads(f.read()) config['REFRESH_TOKEN'] = post_json['refresh_token'] with open(os.getcwd() + '/config.json', 'w') as f: f.write(json.dumps(config)) f.flush() except Exception as e: print_warn('refresh_token已经失效') raise e access_token = post_json['access_token'] self.headers = { 'authorization': access_token } self.refresh_token = post_json['refresh_token']
def complete(self, file_id, upload_id): complete_data = { "drive_id": self.drive_id, "file_id": file_id, "upload_id": upload_id } complete_post = requests.post( 'https://api.aliyundrive.com/v2/file/complete', json.dumps(complete_data), headers=self.headers, verify=False ) complete_post_json = complete_post.json() if complete_post_json.get('code') == 'AccessTokenInvalid': print_info('AccessToken已失效,尝试刷新AccessToken中') if self.token_refresh(): print_info('AccessToken刷新成功,返回创建上传任务') self.complete(file_id, upload_id) return print_error('无法刷新AccessToken,准备退出') exit() s = time.time() - self.start_time if 'file_id' in complete_post_json: print_success('【{filename}】上传成功!消耗{s}秒'.format(filename=self.filename, s=s)) return True else: print_warn('【{filename}】上传失败!消耗{s}秒'.format(filename=self.filename, s=s)) return False
def token_refresh(self): common.LOCK.acquire() try: data = {"refresh_token": self.refresh_token} post = requests.post( 'https://websv.aliyundrive.com/token/refresh', data=json.dumps(data), headers={ 'content-type': 'application/json;charset=UTF-8' }, verify=False ) try: post_json = post.json() # 刷新配置中的token with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json', 'rb') as f: config = json.loads(f.read().decode('utf-8')) config['REFRESH_TOKEN'] = post_json['refresh_token'] with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json', 'w') as f: f.write(json.dumps(config)) f.flush() except Exception as e: print_warn('refresh_token已经失效') raise e access_token = post_json['access_token'] self.headers = { 'authorization': access_token } self.refresh_token = post_json['refresh_token'] finally: common.LOCK.release()
def complete(self): complete_data = { "drive_id": self.drive_id, "file_id": self.file_id, "upload_id": self.upload_id } complete_post = requests.post( 'https://api.aliyundrive.com/v2/file/complete', json.dumps(complete_data), headers=self.headers, verify=False) requests_post_json = complete_post.json() self.check_auth(requests_post_json, self.complete) s = time.time() - self.start_time # print(requests_post_json) # print(complete_data) if 'file_id' in requests_post_json: print_success('【{filename}】上传成功!消耗{s}秒'.format( filename=self.filename, s=s)) return True else: print_warn('【{filename}】上传失败!消耗{s}秒'.format(filename=self.filename, s=s)) return False
def build_part(part): install_dir = part.install_path() source_dir = part.source_path() build_dir = part.build_path() if part.gconfig.clean_build: remove_dir(build_dir) # copy tree: source -> build if not os.path.exists(build_dir): shutil.copytree(source_dir, build_dir) part.set_build_state("CLEAN") else: print_warn("Part '{}' has ever been built,".format(part.name), "in order to perform a clean build, use --clean-build") remove_dir(install_dir) # get module parameters and put them in 'module_params' var_name_prefix = part.build + "-" prefix_len = len(var_name_prefix) module_params = dict( (k[prefix_len:], v) for k, v in part.doc.iteritems() if len(k) > prefix_len and k[:prefix_len] == var_name_prefix) env = { "BUILD_ROOT": build_path(part.gconfig), "INSTALL_ROOT": install_path(part.gconfig), "BUILD_DIR": build_dir, "INSTALL_DIR": install_dir } # do the build prepare os.chdir(build_dir) build_prepare(part, env) # do the make main os.chdir(build_dir) if build_override(part, env) is False: part.build_module.build(part, module_params, env.copy()) part.set_build_state("BUILT") # do the make install os.chdir(build_dir) create_dir(install_dir) if install_override(part, env) is False: part.build_module.install(part, module_params, env.copy()) part.set_build_state("INSTALLED")
def install_override(part, env): if "install-override" in part.doc: override = part.doc["install-override"] if override is None: override = "" elif not isinstance(override, str): print_error("Part '{}': 'install-override' must be string".format( part.name)) print_warn( "Part '{}': 'install-override' is specified and it overrides install() method in '{}' module" .format(part.name, part.build)) err = execute_script(override.split("\n"), env) if err: print_error(err) return True return False
def get_source(part): url = part.source if url is None or isinstance(url, str) is False or len(url) == 0: print "illegal syntax specified in 'source', in part '{}'".format( part.name) exit(1) gconfig = part.gconfig dest_dir = part.source_path() temp_dir = os.path.join(source_path(gconfig), ".staged." + part.name) remove_dir(temp_dir) if gconfig.refresh_source: remove_dir(dest_dir) elif os.path.exists(dest_dir): print_warn( "The source of '{}' has been retrieved already,".format(part.name), "in order to refresh the source by retrieving again, use --refresh-source" ) return # get source parameters and put them in 'source_vars' var_name_prefix = "source-" prefix_len = len(var_name_prefix) source_vars = dict( (k[prefix_len:], v) for k, v in part.doc.iteritems() if len(k) > prefix_len and k[:prefix_len] == var_name_prefix) print "Get source of '{}' from {}".format(part.name, url) input = url pipe = ["raw", "extract", "strip", "control"] for stage in pipe: method_name = "pipe_" + stage method = globals()[method_name] output = os.path.join(temp_dir, stage) try: method(input, output, source_vars) except Exception as e: print_error("Pull source failed in '{}' stage: {}".format( stage, e)) input = output move_dir(output, dest_dir) if not DEBUG: remove_dir(temp_dir) # As the source has been refreshed hereby, the build directory should be invalid. # Remove it! remove_dir(part.build_path())
for file in file_list: tmp = { "filepath": file, "upload_time": 0, "drive_id": 0, "file_id": 0, "upload_id": 0, "part_number": 0, "chunk_size": 0, } filepath_hash = sha1(file.encode('utf-8')).hexdigest() if not filepath_hash in common.DATA['tasks']: common.DATA['tasks'][filepath_hash] = tmp if common.DATA['tasks'][filepath_hash]['upload_time'] > 0: print_warn(os.path.basename(file) + ' 已上传,无需重复上传') else: if common.DATA['tasks'][filepath_hash]['upload_time'] <= 0: # 提交线程 future = executor.submit(upload_file, FILE_PATH, file) future_list.append(future) for res in as_completed(future_list): if res.result(): common.DATA['tasks'][res.result()]['upload_time'] = time.time() common.save_task(common.DATA['tasks']) else: print_error(os.path.basename(file) + ' 上传失败') else: for file in file_list: tmp = {