def copystore(ui, srcrepo, destpath): '''copy files from store of srcrepo in destpath returns destlock ''' destlock = None try: hardlink = None num = 0 srcpublishing = srcrepo.ui.configbool('phases', 'publish', True) srcvfs = scmutil.vfs(srcrepo.sharedpath) dstvfs = scmutil.vfs(destpath) for f in srcrepo.store.copylist(): if srcpublishing and f.endswith('phaseroots'): continue dstbase = os.path.dirname(f) if dstbase and not dstvfs.exists(dstbase): dstvfs.mkdir(dstbase) if srcvfs.exists(f): if f.endswith('data'): # 'dstbase' may be empty (e.g. revlog format 0) lockfile = os.path.join(dstbase, "lock") # lock to avoid premature writing to the target destlock = lock.lock(dstvfs, lockfile) hardlink, n = util.copyfiles(srcvfs.join(f), dstvfs.join(f), hardlink) num += n if hardlink: ui.debug("linked %d files\n" % num) else: ui.debug("copied %d files\n" % num) return destlock except: # re-raises release(destlock) raise
def copystore(ui, srcrepo, destpath): '''copy files from store of srcrepo in destpath returns destlock ''' destlock = None try: hardlink = None num = 0 srcpublishing = srcrepo.ui.configbool('phases', 'publish', True) for f in srcrepo.store.copylist(): if srcpublishing and f.endswith('phaseroots'): continue src = os.path.join(srcrepo.sharedpath, f) dst = os.path.join(destpath, f) dstbase = os.path.dirname(dst) if dstbase and not os.path.exists(dstbase): os.mkdir(dstbase) if os.path.exists(src): if dst.endswith('data'): # lock to avoid premature writing to the target destlock = lock.lock(os.path.join(dstbase, "lock")) hardlink, n = util.copyfiles(src, dst, hardlink) num += n if hardlink: ui.debug("linked %d files\n" % num) else: ui.debug("copied %d files\n" % num) return destlock except: release(destlock) raise
def copystore(ui, srcrepo, destpath): """copy files from store of srcrepo in destpath returns destlock """ destlock = None try: hardlink = None num = 0 srcpublishing = srcrepo.ui.configbool("phases", "publish", True) srcvfs = scmutil.vfs(srcrepo.sharedpath) dstvfs = scmutil.vfs(destpath) for f in srcrepo.store.copylist(): if srcpublishing and f.endswith("phaseroots"): continue dstbase = os.path.dirname(f) if dstbase and not dstvfs.exists(dstbase): dstvfs.mkdir(dstbase) if srcvfs.exists(f): if f.endswith("data"): # 'dstbase' may be empty (e.g. revlog format 0) lockfile = os.path.join(dstbase, "lock") # lock to avoid premature writing to the target destlock = lock.lock(dstvfs, lockfile) hardlink, n = util.copyfiles(srcvfs.join(f), dstvfs.join(f), hardlink) num += n if hardlink: ui.debug("linked %d files\n" % num) else: ui.debug("copied %d files\n" % num) return destlock except: # re-raises release(destlock) raise
def unlock(self): ''' task.unlock() Releases the lock. If the lock was not held, this may remove another thread's lock! ''' lock.release(self.filename(hash_only=True))
def run_thread(n): for i in range(100000): # 先要获取锁: lock.acquire() try: # 放心地改吧: change_it(n) finally: # 改完了一定要释放锁: lock.release()
def update_ledger(c, filename, ip): if(ip == helper.find_ip()): print("Same server as client") c.send(helper.pad_string("Server doesnt need your ledger").encode()) if lock.locked(): lock.release() return if lock.locked() and not lock.check_lock(ip): send_error(c, "Error 123: Server currently busy") return # start with the time start = time.time() # open a temporary file to store the received bytes file = open(filename, 'wb') byte = 0 # send confirmation c.send(helper.pad_string("Server is ready to update its ledger").encode()) while True: # receive 1024 bytes at a time and write them to a file bytes = c.recv(BYTES_TO_SEND) bytes = encryption.decrypt_using_private_key(bytes) file.write(bytes) byte += BYTES_TO_SEND # break infinite loop once all bytes are transferred if not bytes: break # close the file once transfer is complete file.close() # time and space prints end = time.time() print("Finished running download of file %s in %.2f seconds" % (filename, float(end - start))) print(byte, "bytes sent") # Release the lock if one is present if lock.locked() and lock.check_lock(ip): print("Server lock released by ", lock.return_lock()) lock.release()
def copystore(ui, srcrepo, destpath): """copy files from store of srcrepo in destpath returns destlock """ destlock = None try: hardlink = None num = 0 closetopic = [None] def prog(topic, pos): if pos is None: closetopic[0] = topic else: ui.progress(topic, pos + num) srcpublishing = srcrepo.publishing() srcvfs = scmutil.vfs(srcrepo.sharedpath) dstvfs = scmutil.vfs(destpath) for f in srcrepo.store.copylist(): if srcpublishing and f.endswith("phaseroots"): continue dstbase = os.path.dirname(f) if dstbase and not dstvfs.exists(dstbase): dstvfs.mkdir(dstbase) if srcvfs.exists(f): if f.endswith("data"): # 'dstbase' may be empty (e.g. revlog format 0) lockfile = os.path.join(dstbase, "lock") # lock to avoid premature writing to the target destlock = lock.lock(dstvfs, lockfile) hardlink, n = util.copyfiles(srcvfs.join(f), dstvfs.join(f), hardlink, progress=prog) num += n if hardlink: ui.debug("linked %d files\n" % num) if closetopic[0]: ui.progress(closetopic[0], None) else: ui.debug("copied %d files\n" % num) if closetopic[0]: ui.progress(closetopic[0], None) return destlock except: # re-raises release(destlock) raise
def copystore(ui, srcrepo, destpath): '''copy files from store of srcrepo in destpath returns destlock ''' destlock = None try: hardlink = None num = 0 closetopic = [None] def prog(topic, pos): if pos is None: closetopic[0] = topic else: ui.progress(topic, pos + num) srcpublishing = srcrepo.publishing() srcvfs = scmutil.vfs(srcrepo.sharedpath) dstvfs = scmutil.vfs(destpath) for f in srcrepo.store.copylist(): if srcpublishing and f.endswith('phaseroots'): continue dstbase = os.path.dirname(f) if dstbase and not dstvfs.exists(dstbase): dstvfs.mkdir(dstbase) if srcvfs.exists(f): if f.endswith('data'): # 'dstbase' may be empty (e.g. revlog format 0) lockfile = os.path.join(dstbase, "lock") # lock to avoid premature writing to the target destlock = lock.lock(dstvfs, lockfile) hardlink, n = util.copyfiles(srcvfs.join(f), dstvfs.join(f), hardlink, progress=prog) num += n if hardlink: ui.debug("linked %d files\n" % num) if closetopic[0]: ui.progress(closetopic[0], None) else: ui.debug("copied %d files\n" % num) if closetopic[0]: ui.progress(closetopic[0], None) return destlock except: # re-raises release(destlock) raise
_("updating to bookmark @ on branch %s\n") % bn) except KeyError: try: uprev = destrepo.branchtip('default') except error.RepoLookupError: uprev = destrepo.lookup('tip') if not status: bn = destrepo[uprev].branch() status = _("updating to branch %s\n") % bn destrepo.ui.status(status) _update(destrepo, uprev) if update in destrepo._bookmarks: bookmarks.setcurrent(destrepo, update) finally: release(srclock, destlock) if cleandir is not None: shutil.rmtree(cleandir, True) if srcpeer is not None: srcpeer.close() return srcpeer, destpeer def _showstats(repo, stats): repo.ui.status( _("%d files updated, %d files merged, " "%d files removed, %d files unresolved\n") % stats) def updaterepo(repo, node, overwrite): """Update the working directory to node.
checkout = srcrepo.lookup(update) for test in (checkout, 'default', 'tip'): if test is None: continue try: uprev = destrepo.lookup(test) break except error.RepoLookupError: continue bn = destrepo[uprev].branch() destrepo.ui.status(_("updating to branch %s\n") % bn) _update(destrepo, uprev) return srcrepo, destrepo finally: release(srclock, destlock) if dircleanup is not None: dircleanup.cleanup() def _showstats(repo, stats): repo.ui.status(_("%d files updated, %d files merged, " "%d files removed, %d files unresolved\n") % stats) def update(repo, node): """update the working directory to node, merging linear changes""" stats = mergemod.update(repo, node, False, False, None) _showstats(repo, stats) if stats[3]: repo.ui.status(_("use 'hg resolve' to retry unresolved file merges\n")) return stats[3] > 0
if update: dest_repo.ui.status(_("updating working directory\n")) if update is not True: checkout = update for test in (checkout, 'default', 'tip'): try: uprev = dest_repo.lookup(test) break except: continue _update(dest_repo, uprev) return src_repo, dest_repo finally: release(src_lock, dest_lock) if dir_cleanup is not None: dir_cleanup.cleanup() def _showstats(repo, stats): stats = ((stats[0], _("updated")), (stats[1], _("merged")), (stats[2], _("removed")), (stats[3], _("unresolved"))) note = ", ".join([_("%d files %s") % s for s in stats]) repo.ui.status("%s\n" % note) def update(repo, node): """update the working directory to node, merging linear changes""" stats = _merge.update(repo, node, False, False, None) _showstats(repo, stats)
for test in (checkout, 'default', 'tip'): if test is None: continue try: uprev = dest_repo.lookup(test) break except error.RepoLookupError: continue bn = dest_repo[uprev].branch() dest_repo.ui.status(_("updating to branch %s\n") % encoding.tolocal(bn)) _update(dest_repo, uprev) return src_repo, dest_repo finally: release(src_lock, dest_lock) if dir_cleanup is not None: dir_cleanup.cleanup() def _showstats(repo, stats): repo.ui.status(_("%d files updated, %d files merged, " "%d files removed, %d files unresolved\n") % stats) def update(repo, node): """update the working directory to node, merging linear changes""" stats = _merge.update(repo, node, False, False, None) _showstats(repo, stats) if stats[3]: repo.ui.status(_("use 'hg resolve' to retry unresolved file merges\n")) return stats[3] > 0
def fuct(self): lock = threading.Lock() lock.acquire() self.executeSQL() lock.release()
def clone(ui, peeropts, source, dest=None, pull=False, rev=None, update=True, stream=False, branch=None): """Make a copy of an existing repository. Create a copy of an existing repository in a new directory. The source and destination are URLs, as passed to the repository function. Returns a pair of repository peers, the source and newly created destination. The location of the source is added to the new repository's .hg/hgrc file, as the default to be used for future pulls and pushes. If an exception is raised, the partly cloned/updated destination repository will be deleted. Arguments: source: repository object or URL dest: URL of destination repository to create (defaults to base name of source repository) pull: always pull from source repository, even in local case or if the server prefers streaming stream: stream raw data uncompressed from repository (fast over LAN, slow over WAN) rev: revision to clone up to (implies pull=True) update: update working directory after clone completes, if destination is local repository (True means update to default rev, anything else is treated as a revision) branch: branches to clone """ if isinstance(source, str): origsource = ui.expandpath(source) source, branch = parseurl(origsource, branch) srcpeer = peer(ui, peeropts, source) else: srcpeer = source.peer() # in case we were called with a localrepo branch = (None, branch or []) origsource = source = srcpeer.url() rev, checkout = addbranchrevs(srcpeer, srcpeer, branch, rev) if dest is None: dest = defaultdest(source) if dest: ui.status(_("destination directory: %s\n") % dest) else: dest = ui.expandpath(dest) dest = util.urllocalpath(dest) source = util.urllocalpath(source) if not dest: raise util.Abort(_("empty destination path is not valid")) destvfs = scmutil.vfs(dest, expandpath=True) if destvfs.lexists(): if not destvfs.isdir(): raise util.Abort(_("destination '%s' already exists") % dest) elif destvfs.listdir(): raise util.Abort(_("destination '%s' is not empty") % dest) srclock = destlock = cleandir = None srcrepo = srcpeer.local() try: abspath = origsource if islocal(origsource): abspath = os.path.abspath(util.urllocalpath(origsource)) if islocal(dest): cleandir = dest copy = False if (srcrepo and srcrepo.cancopy() and islocal(dest) and not phases.hassecret(srcrepo)): copy = not pull and not rev if copy: try: # we use a lock here because if we race with commit, we # can end up with extra data in the cloned revlogs that's # not pointed to by changesets, thus causing verify to # fail srclock = srcrepo.lock(wait=False) except error.LockError: copy = False if copy: srcrepo.hook('preoutgoing', throw=True, source='clone') hgdir = os.path.realpath(os.path.join(dest, ".hg")) if not os.path.exists(dest): os.mkdir(dest) else: # only clean up directories we create ourselves cleandir = hgdir try: destpath = hgdir util.makedir(destpath, notindexed=True) except OSError as inst: if inst.errno == errno.EEXIST: cleandir = None raise util.Abort(_("destination '%s' already exists") % dest) raise destlock = copystore(ui, srcrepo, destpath) # copy bookmarks over srcbookmarks = srcrepo.join('bookmarks') dstbookmarks = os.path.join(destpath, 'bookmarks') if os.path.exists(srcbookmarks): util.copyfile(srcbookmarks, dstbookmarks) # Recomputing branch cache might be slow on big repos, # so just copy it def copybranchcache(fname): srcbranchcache = srcrepo.join('cache/%s' % fname) dstbranchcache = os.path.join(dstcachedir, fname) if os.path.exists(srcbranchcache): if not os.path.exists(dstcachedir): os.mkdir(dstcachedir) util.copyfile(srcbranchcache, dstbranchcache) dstcachedir = os.path.join(destpath, 'cache') # In local clones we're copying all nodes, not just served # ones. Therefore copy all branch caches over. copybranchcache('branch2') for cachename in repoview.filtertable: copybranchcache('branch2-%s' % cachename) # we need to re-init the repo after manually copying the data # into it destpeer = peer(srcrepo, peeropts, dest) srcrepo.hook('outgoing', source='clone', node=node.hex(node.nullid)) else: try: destpeer = peer(srcrepo or ui, peeropts, dest, create=True) # only pass ui when no srcrepo except OSError as inst: if inst.errno == errno.EEXIST: cleandir = None raise util.Abort(_("destination '%s' already exists") % dest) raise revs = None if rev: if not srcpeer.capable('lookup'): raise util.Abort(_("src repository does not support " "revision lookup and so doesn't " "support clone by revision")) revs = [srcpeer.lookup(r) for r in rev] checkout = revs[0] if destpeer.local(): if not stream: if pull: stream = False else: stream = None destpeer.local().clone(srcpeer, heads=revs, stream=stream) elif srcrepo: exchange.push(srcrepo, destpeer, revs=revs, bookmarks=srcrepo._bookmarks.keys()) else: raise util.Abort(_("clone from remote to remote not supported")) cleandir = None destrepo = destpeer.local() if destrepo: template = uimod.samplehgrcs['cloned'] fp = destrepo.vfs("hgrc", "w", text=True) u = util.url(abspath) u.passwd = None defaulturl = str(u) fp.write(template % defaulturl) fp.close() destrepo.ui.setconfig('paths', 'default', defaulturl, 'clone') if update: if update is not True: checkout = srcpeer.lookup(update) uprev = None status = None if checkout is not None: try: uprev = destrepo.lookup(checkout) except error.RepoLookupError: pass if uprev is None: try: uprev = destrepo._bookmarks['@'] update = '@' bn = destrepo[uprev].branch() if bn == 'default': status = _("updating to bookmark @\n") else: status = (_("updating to bookmark @ on branch %s\n") % bn) except KeyError: try: uprev = destrepo.branchtip('default') except error.RepoLookupError: uprev = destrepo.lookup('tip') if not status: bn = destrepo[uprev].branch() status = _("updating to branch %s\n") % bn destrepo.ui.status(status) _update(destrepo, uprev) if update in destrepo._bookmarks: bookmarks.activate(destrepo, update) finally: release(srclock, destlock) if cleandir is not None: shutil.rmtree(cleandir, True) if srcpeer is not None: srcpeer.close() return srcpeer, destpeer
def clone(ui, peeropts, source, dest=None, pull=False, rev=None, update=True, stream=False, branch=None): """Make a copy of an existing repository. Create a copy of an existing repository in a new directory. The source and destination are URLs, as passed to the repository function. Returns a pair of repository peers, the source and newly created destination. The location of the source is added to the new repository's .hg/hgrc file, as the default to be used for future pulls and pushes. If an exception is raised, the partly cloned/updated destination repository will be deleted. Arguments: source: repository object or URL dest: URL of destination repository to create (defaults to base name of source repository) pull: always pull from source repository, even in local case or if the server prefers streaming stream: stream raw data uncompressed from repository (fast over LAN, slow over WAN) rev: revision to clone up to (implies pull=True) update: update working directory after clone completes, if destination is local repository (True means update to default rev, anything else is treated as a revision) branch: branches to clone """ if isinstance(source, str): origsource = ui.expandpath(source) source, branch = parseurl(origsource, branch) srcpeer = peer(ui, peeropts, source) else: srcpeer = source.peer() # in case we were called with a localrepo branch = (None, branch or []) origsource = source = srcpeer.url() rev, checkout = addbranchrevs(srcpeer, srcpeer, branch, rev) if dest is None: dest = defaultdest(source) if dest: ui.status(_("destination directory: %s\n") % dest) else: dest = ui.expandpath(dest) dest = util.urllocalpath(dest) source = util.urllocalpath(source) if not dest: raise util.Abort(_("empty destination path is not valid")) destvfs = scmutil.vfs(dest, expandpath=True) if destvfs.lexists(): if not destvfs.isdir(): raise util.Abort(_("destination '%s' already exists") % dest) elif destvfs.listdir(): raise util.Abort(_("destination '%s' is not empty") % dest) srclock = destlock = cleandir = None srcrepo = srcpeer.local() try: abspath = origsource if islocal(origsource): abspath = os.path.abspath(util.urllocalpath(origsource)) if islocal(dest): cleandir = dest copy = False if srcrepo and srcrepo.cancopy() and islocal(dest) and not phases.hassecret(srcrepo): copy = not pull and not rev if copy: try: # we use a lock here because if we race with commit, we # can end up with extra data in the cloned revlogs that's # not pointed to by changesets, thus causing verify to # fail srclock = srcrepo.lock(wait=False) except error.LockError: copy = False if copy: srcrepo.hook("preoutgoing", throw=True, source="clone") hgdir = os.path.realpath(os.path.join(dest, ".hg")) if not os.path.exists(dest): os.mkdir(dest) else: # only clean up directories we create ourselves cleandir = hgdir try: destpath = hgdir util.makedir(destpath, notindexed=True) except OSError as inst: if inst.errno == errno.EEXIST: cleandir = None raise util.Abort(_("destination '%s' already exists") % dest) raise destlock = copystore(ui, srcrepo, destpath) # copy bookmarks over srcbookmarks = srcrepo.join("bookmarks") dstbookmarks = os.path.join(destpath, "bookmarks") if os.path.exists(srcbookmarks): util.copyfile(srcbookmarks, dstbookmarks) # Recomputing branch cache might be slow on big repos, # so just copy it def copybranchcache(fname): srcbranchcache = srcrepo.join("cache/%s" % fname) dstbranchcache = os.path.join(dstcachedir, fname) if os.path.exists(srcbranchcache): if not os.path.exists(dstcachedir): os.mkdir(dstcachedir) util.copyfile(srcbranchcache, dstbranchcache) dstcachedir = os.path.join(destpath, "cache") # In local clones we're copying all nodes, not just served # ones. Therefore copy all branch caches over. copybranchcache("branch2") for cachename in repoview.filtertable: copybranchcache("branch2-%s" % cachename) # we need to re-init the repo after manually copying the data # into it destpeer = peer(srcrepo, peeropts, dest) srcrepo.hook("outgoing", source="clone", node=node.hex(node.nullid)) else: try: destpeer = peer(srcrepo or ui, peeropts, dest, create=True) # only pass ui when no srcrepo except OSError as inst: if inst.errno == errno.EEXIST: cleandir = None raise util.Abort(_("destination '%s' already exists") % dest) raise revs = None if rev: if not srcpeer.capable("lookup"): raise util.Abort( _( "src repository does not support " "revision lookup and so doesn't " "support clone by revision" ) ) revs = [srcpeer.lookup(r) for r in rev] checkout = revs[0] if destpeer.local(): if not stream: if pull: stream = False else: stream = None destpeer.local().clone(srcpeer, heads=revs, stream=stream) elif srcrepo: exchange.push(srcrepo, destpeer, revs=revs, bookmarks=srcrepo._bookmarks.keys()) else: raise util.Abort(_("clone from remote to remote not supported")) cleandir = None destrepo = destpeer.local() if destrepo: template = uimod.samplehgrcs["cloned"] fp = destrepo.vfs("hgrc", "w", text=True) u = util.url(abspath) u.passwd = None defaulturl = str(u) fp.write(template % defaulturl) fp.close() destrepo.ui.setconfig("paths", "default", defaulturl, "clone") if update: if update is not True: checkout = srcpeer.lookup(update) uprev = None status = None if checkout is not None: try: uprev = destrepo.lookup(checkout) except error.RepoLookupError: pass if uprev is None: try: uprev = destrepo._bookmarks["@"] update = "@" bn = destrepo[uprev].branch() if bn == "default": status = _("updating to bookmark @\n") else: status = _("updating to bookmark @ on branch %s\n") % bn except KeyError: try: uprev = destrepo.branchtip("default") except error.RepoLookupError: uprev = destrepo.lookup("tip") if not status: bn = destrepo[uprev].branch() status = _("updating to branch %s\n") % bn destrepo.ui.status(status) _update(destrepo, uprev) if update in destrepo._bookmarks: bookmarks.activate(destrepo, update) finally: release(srclock, destlock) if cleandir is not None: shutil.rmtree(cleandir, True) if srcpeer is not None: srcpeer.close() return srcpeer, destpeer