def test_replace_in_file(inp, pattern, new, leading_whitespace, exp): with tempfile.NamedTemporaryFile('w+t') as f: f.write(inp) f.seek(0) replace_in_file(pattern, new, f.name, leading_whitespace) f.seek(0) obs = f.read() assert exp == obs
def migrate(self, recipe_dir: str, attrs: "AttrsTypedDict", **kwargs: Any) -> None: with indir(recipe_dir): for b in self.bad_install: replace_in_file( f"script: {b}", "script: {{ PYTHON }} -m pip install . --no-deps -vv", "meta.yaml", )
def migrate(self, recipe_dir: str, attrs: "AttrsTypedDict", **kwargs: Any) -> "MigrationUidTypedDict": with indir(recipe_dir): for f, p, n in self.patterns: p = eval_version(p) n = eval_version(n) replace_in_file(p, n, f, leading_whitespace=False) self.set_build_number("meta.yaml") return super().migrate(recipe_dir, attrs)
def migrate(self, recipe_dir: str, attrs: "AttrsTypedDict", **kwargs: Any) -> "MigrationUidTypedDict": with indir(recipe_dir): build_idx = [ l.rstrip() for l in attrs["raw_meta_yaml"].split("\n") ].index("build:", ) line = attrs["raw_meta_yaml"].split("\n")[build_idx + 1] spaces = len(line) - len(line.lstrip()) replace_in_file( "build:", f"build:\n{' ' * spaces}noarch: python", "meta.yaml", leading_whitespace=False, ) replace_in_file( "script:.+?", "script: python -m pip install --no-deps --ignore-installed .", "meta.yaml", ) replace_in_file( " build:", " host:", "meta.yaml", leading_whitespace=False, ) if "pip" not in attrs["req"]: replace_in_file( " host:", " host:\n - pip", "meta.yaml", leading_whitespace=False, ) self.set_build_number("meta.yaml") return super().migrate(recipe_dir, attrs)
def migrate(self, recipe_dir: str, attrs: "AttrsTypedDict", **kwargs: Any) -> None: cb_work_dir = _get_source_code(recipe_dir) if cb_work_dir is None: return with indir(cb_work_dir): # look for a license file license_files = [ s for s in os.listdir(".") if any(s.lower().startswith(k) for k in ["license", "copying", "copyright"]) ] eval_xonsh(f"rm -r {cb_work_dir}") # if there is a license file in tarball update things if license_files: with indir(recipe_dir): """BSD 3-Clause License Copyright (c) 2017, Anthony Scopatz Copyright (c) 2018, The Regro Developers All rights reserved.""" with open("meta.yaml", "r") as f: raw = f.read() lines = raw.splitlines() ptn = re.compile(r"(\s*?)" + "license:") for i, line in enumerate(lines): m = ptn.match(line) if m is not None: break # TODO: Sketchy type assertion assert m is not None ws = m.group(1) if len(license_files) == 1: replace_in_file( line, line + "\n" + ws + f"license_file: {list(license_files)[0]}", "meta.yaml", ) else: # note that this white space is not perfect but works for # most of the situations replace_in_file( line, line + "\n" + ws + "license_file: \n" + "".join(f"{ws*2}- {z} \n" for z in license_files), "meta.yaml", )
def migrate( self, recipe_dir: str, attrs: "AttrsTypedDict", hash_type: str = "sha256", **kwargs: Any, ) -> "MigrationUidTypedDict": # Render with new version but nothing else version = attrs["new_version"] assert isinstance(version, str) with indir(recipe_dir): with open("meta.yaml", "r") as fp: text = fp.read() res = re.search(r"\s*-?\s*url:.*?\n( {4}-.*\n?)*", text) if res: url = res.group() else: raise ValueError("Could not match url") if "cran.r-project.org/src/contrib" in url or "cran_mirror" in url: version = version.replace("_", "-") with indir(recipe_dir), env.swap(VERSION=version): for f, p, n in self.patterns: p = eval_version(p) n = eval_version(n) replace_in_file(p, n, f) with open("meta.yaml", "r") as fp: text = fp.read() # render the text and check that the URL exists, if it doesn't try variations # if variations then update url rendered = parse_meta_yaml(render_meta_yaml(text)) # only run for single url recipes as the moment if (isinstance(rendered["source"], dict) and isinstance(rendered["source"].get("url", []), str) and requests.get(rendered["source"]["url"]).status_code != 200): with indir(recipe_dir): for (a, b), (c, d) in product( permutations(["v{{ v", "{{ v"]), permutations([".zip", ".tar.gz"]), ): inner_text = text.replace(a, b).replace(c, d) rendered = parse_meta_yaml(render_meta_yaml(inner_text)) if requests.get( rendered["source"]["url"]).status_code == 200: text = inner_text # The above clauses could do bad things the version # itself text = text.replace("version: v{{ v", "version: {{ v") with open("meta.yaml", "w") as fp: fp.write(text) break # Get patterns to replace checksum for each platform rendered_text = render_meta_yaml(text) urls = self.find_urls(rendered_text) new_patterns = self.get_hash_patterns("meta.yaml", urls, hash_type) with indir(recipe_dir): for f, p, n in new_patterns: p = eval_version(p) n = eval_version(n) replace_in_file(p, n, f) self.set_build_number("meta.yaml") return super().migrate(recipe_dir, attrs)
def migrate(self, recipe_dir: str, attrs: "AttrsTypedDict", **kwargs: Any) -> None: # r- recipes have a special syntax here if (attrs.get("feedstock_name", "").startswith("r-") or attrs.get( "name", "").startswith("r-")) and "r-base" in attrs["raw_meta_yaml"]: if attrs.get("feedstock_name", None) is not None: if attrs.get("feedstock_name", None).endswith("-feedstock"): name = attrs.get("feedstock_name")[:-len("-feedstock")] else: name = attrs.get("feedstock_name") else: name = attrs.get("name", None) _do_r_license_munging(name, recipe_dir) return try: cb_work_dir = _get_source_code(recipe_dir) except Exception: return if cb_work_dir is None: return with indir(cb_work_dir): # look for a license file license_files = [ s for s in os.listdir(".") if any(s.lower().startswith(k) for k in ["license", "copying", "copyright"]) ] eval_cmd(f"rm -r {cb_work_dir}") # if there is a license file in tarball update things if license_files: with indir(recipe_dir): """BSD 3-Clause License Copyright (c) 2017, Anthony Scopatz Copyright (c) 2018, The Regro Developers All rights reserved.""" with open("meta.yaml") as f: raw = f.read() lines = raw.splitlines() ptn = re.compile(r"(\s*?)" + "license:") for i, line in enumerate(lines): m = ptn.match(line) if m is not None: break # TODO: Sketchy type assertion assert m is not None ws = m.group(1) if len(license_files) == 1: replace_in_file( line, line + "\n" + ws + f"license_file: {list(license_files)[0]}", "meta.yaml", ) else: # note that this white space is not perfect but works for # most of the situations replace_in_file( line, line + "\n" + ws + "license_file: \n" + "".join(f"{ws*2}- {z} \n" for z in license_files), "meta.yaml", )