Download the URLs listed in the URLS-FILE and create a copyight test for each in the current directory. If a line number is provided as a URL fragment #L2, uses only 5 lines before and after this line. If the URL is a plain GitHub URL, convert the URL to a raw URL. If
(
urls_file,
)
| 42 | ) |
| 43 | @click.help_option("-h", "--help") |
| 44 | def create_copyright_tests( |
| 45 | urls_file, |
| 46 | ): |
| 47 | """ |
| 48 | Download the URLs listed in the URLS-FILE and create a copyight test for each in the current |
| 49 | directory. |
| 50 | |
| 51 | If a line number is provided as a URL fragment #L2, uses only 5 lines before and after this |
| 52 | line. |
| 53 | |
| 54 | If the URL is a plain GitHub URL, convert the URL to a raw URL. |
| 55 | If the URL does not start with http it is treated as a plain copyright text to test |
| 56 | """ |
| 57 | |
| 58 | with open(urls_file) as urls: |
| 59 | for i, url in enumerate(urls): |
| 60 | url = url.strip() |
| 61 | if not url: |
| 62 | continue |
| 63 | |
| 64 | name = "" |
| 65 | if url.startswith("http"): |
| 66 | print(f"Fetching URL: {url}") |
| 67 | if url.startswith("https://github.com"): |
| 68 | url = url.replace("https://github.com", "https://raw.githubusercontent.com") |
| 69 | url = url.replace("/blob/", "/") |
| 70 | |
| 71 | if "github" in url: |
| 72 | segs = url.split("/") |
| 73 | org = segs[3] |
| 74 | repo = segs[4] |
| 75 | name = f"copyright-test-{timestamp()}-{i}-{org}-{repo}.copyright" |
| 76 | else: |
| 77 | print(f"Processing test: {url}") |
| 78 | name = f"copyright-test-{timestamp()}-{i}.copyright" |
| 79 | |
| 80 | |
| 81 | start_line = 0 |
| 82 | end_line = 0 |
| 83 | if "#L" in url: |
| 84 | _, _, line = url.rpartition("#L") |
| 85 | line = int(line) |
| 86 | if line > 5: |
| 87 | start_line = line - 5 |
| 88 | end_line = line + 5 |
| 89 | |
| 90 | if url.startswith("http"): |
| 91 | _header, content = get_remote_file_content(url, as_text=True) |
| 92 | else: |
| 93 | content = url |
| 94 | |
| 95 | if end_line != 0: |
| 96 | content = "\n".join(content.strip().splitlines()[start_line:end_line]) |
| 97 | |
| 98 | with open(name, "w") as out: |
| 99 | out.write(content) |
| 100 | |
| 101 | yml = EMPTY_COPY_TEST |
no test coverage detected