def _subpath(self, ignore_shortlinks=False): if self.index is None: return None if self.parent.hashed_variants: vars_str = str(list(map(str, self.variant_requires))) h = sha1(vars_str.encode("utf8")) hashdir = h.hexdigest() if (not ignore_shortlinks) and \ config.use_variant_shortlinks and \ self.base is not None: # search for matching shortlink and use that path = os.path.join(self.base, config.variant_shortlinks_dirname) if os.path.exists(path): actual_root = os.path.join(self.base, hashdir) linkname = find_matching_symlink(path, actual_root) if linkname: return os.path.join(config.variant_shortlinks_dirname, linkname) return hashdir else: dirs = [x.safe_str() for x in self.variant_requires] subpath = os.path.join(*dirs) return subpath
def _subpath(self, ignore_shortlinks=False): if self.index is None: return None if self.parent.hashed_variants: vars_str = str(list(map(str, self.variant_requires))) h = sha1(vars_str.encode("utf8")) hashdir = h.hexdigest() if (not ignore_shortlinks) and \ config.use_variant_shortlinks and \ self.base is not None: # search for matching shortlink and use that path = os.path.join(self.base, config.variant_shortlinks_dirname) if os.path.exists(path): actual_root = os.path.join(self.base, hashdir) linkname = find_matching_symlink(path, actual_root) if linkname: return os.path.join( config.variant_shortlinks_dirname, linkname) return hashdir else: dirs = [x.safe_str() for x in self.variant_requires] dirs = dirs or [""] subpath = os.path.join(*dirs) return subpath
def create_unique_base26_symlink(path, source): """Create a base-26 symlink in `path` pointing to `source`. If such a symlink already exists, it is returned. Note that there is a small chance that this function may create a new symlink when there is already one pointed at `source`. Assumes `path` only contains base26 symlinks. Returns: str: Path to created symlink. """ retries = 0 while True: # if a link already exists that points at `source`, return it name = find_matching_symlink(path, source) if name: return os.path.join(path, name) # get highest current symlink in path names = [ x for x in os.listdir(path) if os.path.islink(os.path.join(path, x)) ] if names: prev = max(names) else: prev = None linkname = get_next_base26(prev) linkpath = os.path.join(path, linkname) # attempt to create the symlink try: os.symlink(source, linkpath) return linkpath except OSError as e: if e.errno != errno.EEXIST: raise # if we're here, the same named symlink was created in parallel # somewhere. Try again up to N times. # if retries > 10: raise RuntimeError( "Variant shortlink not created - there was too much contention." ) retries += 1
def create_unique_base26_symlink(path, source): """Create a base-26 symlink in `path` pointing to `source`. If such a symlink already exists, it is returned. Note that there is a small chance that this function may create a new symlink when there is already one pointed at `source`. Assumes `path` only contains base26 symlinks. Returns: str: Path to created symlink. """ retries = 0 while True: # if a link already exists that points at `source`, return it name = find_matching_symlink(path, source) if name: return os.path.join(path, name) # get highest current symlink in path names = [ x for x in os.listdir(path) if os.path.islink(os.path.join(path, x)) ] if names: prev = max(names) else: prev = None linkname = get_next_base26(prev) linkpath = os.path.join(path, linkname) # attempt to create the symlink try: os.symlink(source, linkpath) return linkpath except OSError as e: if e.errno != errno.EEXIST: raise # if we're here, the same named symlink was created in parallel # somewhere. Try again up to N times. # if retries > 10: raise RuntimeError( "Variant shortlink not created - there was too much contention.") retries += 1