async def retx_sync_interest(self): while self.running: interest = Interest(Name(self.prefix)) interest.applicationParameters = self.encode(self.state) interest.appendParametersDigestToName() # await fetch_data_packet(self.face, interest) self.face.expressInterest(interest, self.on_sync_data) logging.info("retx") timeout = uniform(SYNC_INTERVAL_MIN, SYNC_INTERVAL_MAX) try: await asyncio.wait_for(self.sync_event.wait(), timeout) except asyncio.TimeoutError: pass self.sync_event.clear()
async def run(cmd: str): async def face_loop(): nonlocal face, running while running: face.processEvents() await asyncio.sleep(0.01) running = True face = Face() keychain = KeyChain() face.setCommandSigningInfo(keychain, keychain.getDefaultCertificateName()) event_loop = asyncio.get_event_loop() face_task = event_loop.create_task(face_loop()) if cmd == "track-repo": if len(sys.argv) < 3: print("Usage:", sys.argv[0], "track-repo <repo>", file=sys.stderr) else: repo = sys.argv[2] interest = Interest( Name(LOCAL_CMD_PREFIX).append("track-repo").append(repo)) data = await fetch_data_packet(face, interest) if isinstance(data, Data): result = struct.unpack("i", data.content.toBytes())[0] if result == 1: print("OK") elif result == 2: print("FAILED") else: print("PENDING") else: print("error: Couldn't connect to", interest.name.toUri(), file=sys.stderr) elif cmd == "create-branch": if len(sys.argv) < 4: print("Usage:", sys.argv[0], "create-branch <repo> <branch>", file=sys.stderr) else: repo = sys.argv[2] branch = sys.argv[3] interest = Interest( Name(LOCAL_CMD_PREFIX).append("create-branch").append( repo).append(branch)) data = await fetch_data_packet(face, interest) if isinstance(data, Data): result = struct.unpack("i", data.content.toBytes())[0] if result == 1: print("OK") elif result == 2: print("FAILED") else: print("PENDING") else: print("error: Couldn't connect to", interest.name.toUri(), file=sys.stderr) elif cmd == "mount": if len(sys.argv) < 4: print("Usage:", sys.argv[0], "mount <repo> <branch>", file=sys.stderr) else: repo = sys.argv[2] branch = sys.argv[3] interest = Interest( Name(LOCAL_CMD_PREFIX).append("mount").append(repo).append( branch)) data = await fetch_data_packet(face, interest) if isinstance(data, Data): print("Finished.") else: print("error: Couldn't connect to", interest.name.toUri(), file=sys.stderr) elif cmd == "unmount": if len(sys.argv) < 4: print("Usage:", sys.argv[0], "unmount <repo> <branch>", file=sys.stderr) else: repo = sys.argv[2] branch = sys.argv[3] interest = Interest( Name(LOCAL_CMD_PREFIX).append("unmount").append(repo).append( branch)) data = await fetch_data_packet(face, interest) if isinstance(data, Data): print("Finished.") else: print("error: Couldn't connect to", interest.name.toUri(), file=sys.stderr) elif cmd == "commit": if len(sys.argv) < 6: print("Usage:", sys.argv[0], "commit <repo> <branch> <dest-branch> <message>", file=sys.stderr) else: repo = sys.argv[2] branch = sys.argv[3] dest_branch = sys.argv[4] commit_msg = sys.argv[5] interest = Interest(Name(LOCAL_CMD_PREFIX).append("commit")) interest.applicationParameters = b'\x00'.join([ repo.encode(), branch.encode(), dest_branch.encode(), commit_msg.encode() ]) interest.appendParametersDigestToName() data = await fetch_data_packet(face, interest) if isinstance(data, Data): print("Finished.") else: print("error: Couldn't connect to", interest.name.toUri(), file=sys.stderr) else: print("Unrecognized command:", cmd, file=sys.stderr) running = False await face_task
async def run(local_repo_path: str, repo_prefix: str): async def face_loop(): nonlocal face, running while running: face.processEvents() await asyncio.sleep(0.01) options = {'cloning': False} running = True face = Face() keychain = KeyChain() face.setCommandSigningInfo(keychain, keychain.getDefaultCertificateName()) event_loop = asyncio.get_event_loop() face_task = event_loop.create_task(face_loop()) storage = FileStorage(local_repo_path) producer = GitProducer(face, Name(repo_prefix).append("objects"), storage) refs = [] empty_cnt = 0 while empty_cnt < 2 and running: cmd = sys.stdin.readline().rstrip("\n\r") # print("<<", cmd, file=sys.stderr) if cmd == "capabilities": print("push") print("fetch") print("option") print("") sys.stdout.flush() elif cmd.startswith("option"): opt_name, opt_val = cmd.split()[1:] if opt_name == "cloning": options['cloning'] = (opt_val == 'true') print("ok") else: print("unsupported") sys.stdout.flush() elif cmd == "list" or cmd == "list for-push": interest = Interest(Name(repo_prefix).append("ref-list")) data = await fetch_data_packet(face, interest) if isinstance(data, Data): reflist = data.content.toBytes().decode("utf-8") print(reflist) if reflist.strip() == "": refs = {} else: refs = [ item.split(" ")[1] for item in reflist.strip().split("\n") ] refs = {name.split("/")[-1] for name in refs} else: print("error: Couldn't connect to", repo_prefix, file=sys.stderr) running = False print("") sys.stdout.flush() elif cmd.startswith("fetch"): fetcher = GitFetcher(face, Name(repo_prefix).append("objects"), storage) while True: # Fetch files hash_name, ref_name = cmd.split()[1:] fetcher.fetch(hash_name, "commit") await fetcher.wait_until_finish() # Set refs file ref_file_path = os.path.join(local_repo_path, ref_name) with open(ref_file_path, 'w') as f: f.write(hash_name) # Read commands for next fetch cmd = sys.stdin.readline().rstrip("\n\r") if not cmd.startswith("fetch"): break print("") sys.stdout.flush() elif cmd.startswith("push"): while True: # Push commands # TODO: DELETE ALL HARDCODED THINGS # TODO: MAKE DATA AND INTEREST REAL branch, commit, _ = parse_push(cmd, local_repo_path) repo_name = repo_prefix.split("/")[-1] # Create Branch if not exist if branch not in refs: print("Non-existing branch. Try to create...", file=sys.stderr) interest = Interest( Name(LOCAL_CMD_PREFIX).append("create-branch").append( repo_name).append(branch)) data = await fetch_data_packet(face, interest) if isinstance(data, Data): result = struct.unpack("i", data.content.toBytes())[0] if result == 1: print("Creation succeeded", file=sys.stderr) elif result == 2: print("Creation is FAILED", file=sys.stderr) print("error refs/heads/{} FAILED".format(branch)) break else: print("Creation is PENDING", file=sys.stderr) print("error refs/heads/{} PENDING".format(branch)) break else: print("error: Couldn't connect to", interest.name.toUri(), file=sys.stderr) break # BranchInfo Interest interest = Interest( Name(repo_prefix).append("branch-info").append(branch)) data = await fetch_data_packet(face, interest) branchinfo = None if isinstance(data, Data): branchinfo = pickle.loads(data.content.toBytes()) if branchinfo is None or 'custodian' not in branchinfo.__dict__: print("ERROR Interest got no response: ", interest.name, file=sys.stderr) print("error refs/heads/{} DISCONNECTED".format(branch)) break # Push Interest interest = Interest( Name(branchinfo.custodian).append("push").append( repo_name).append(branch)) interest.applicationParameters = commit.encode("utf-8") interest.appendParametersDigestToName() interest.interestLifetimeMilliseconds = 20000 interest.mustBeFresh = True data = await fetch_data_packet(face, interest) if isinstance(data, Data): result = struct.unpack("i", data.content.toBytes())[0] if result == 1: print("OK push succeeded", interest.name, file=sys.stderr) print("ok refs/heads/{}".format(branch)) elif result == 2: print("ERROR push failed", interest.name, file=sys.stderr) print("error refs/heads/{} FAILED".format(branch)) else: print("PROCESSING push is not finished yet", interest.name, file=sys.stderr) print("error refs/heads/{} PENDING".format(branch)) else: print("ERROR Interest got no response: ", interest.name, file=sys.stderr) print("error refs/heads/{} DISCONNECTED".format(branch)) break # Read commands for next fetch cmd = sys.stdin.readline().rstrip("\n\r") if not cmd.startswith("push"): break print("") sys.stdout.flush() elif cmd == "": empty_cnt += 1 else: pass producer.cancel() running = False await face_task