|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from subprocess import call |
| 4 | +import argparse |
| 5 | +import datetime |
| 6 | +import glob |
| 7 | +import json |
| 8 | +import os |
| 9 | +import re |
| 10 | +import shutil |
| 11 | +import tempfile |
| 12 | +import time |
| 13 | +import urllib3 |
| 14 | + |
| 15 | +CRATES_IO_INDEX_GIT_LOC = "https://github.com/rust-lang/crates.io-index.git" |
| 16 | +RE_REGEX = re.compile(r"Regex::new\((r?\".*?\")\)") |
| 17 | +KNOWN_UNMAINTAINED_CRATES = set(["queryst-prime", "oozz"]) |
| 18 | + |
| 19 | +# if only requests was in the standard library... |
| 20 | +urllib3.disable_warnings() |
| 21 | +http = urllib3.PoolManager() |
| 22 | + |
| 23 | + |
| 24 | +def argparser(): |
| 25 | + p = argparse.ArgumentParser("A script to scrape crates.io for regex.") |
| 26 | + p.add_argument("-c", "--crates-index", metavar="CRATES_INDEX_DIR", |
| 27 | + help=("A directory where we can find crates.io-index " |
| 28 | + + "(if this isn't set it will be automatically " |
| 29 | + + "downloaded).")) |
| 30 | + p.add_argument("-o", "--output-file", metavar="OUTPUT", |
| 31 | + default="crates_regex.rs", |
| 32 | + help="The name of the output file to create.") |
| 33 | + return p |
| 34 | + |
| 35 | + |
| 36 | +PRELUDE = """// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 37 | +// file at the top-level directory of this distribution and at |
| 38 | +// http://rust-lang.org/COPYRIGHT. |
| 39 | +// |
| 40 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 41 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 42 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 43 | +// option. This file may not be copied, modified, or distributed |
| 44 | +// except according to those terms. |
| 45 | +
|
| 46 | +// DO NOT EDIT. Automatically generated by 'scripts/scrape_crates_io.py' |
| 47 | +// on {date}. |
| 48 | +
|
| 49 | +
|
| 50 | +
|
| 51 | +""" |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + args = argparser().parse_args() |
| 56 | + out = open(os.path.abspath(args.output_file), "w") |
| 57 | + out.write(PRELUDE.format(date=str(datetime.datetime.now()))) |
| 58 | + if args.crates_index: |
| 59 | + args.crates_index = os.path.abspath(args.crates_index) |
| 60 | + |
| 61 | + # enter our scratch directory |
| 62 | + old_dir = os.getcwd() |
| 63 | + work_dir = tempfile.mkdtemp(prefix="scrape-crates-io") |
| 64 | + os.chdir(work_dir) |
| 65 | + |
| 66 | + crates_index = (args.crates_index |
| 67 | + if os.path.join(old_dir, args.crates_index) |
| 68 | + else download_crates_index()) |
| 69 | + |
| 70 | + for (name, vers) in iter_crates(crates_index): |
| 71 | + if name in KNOWN_UNMAINTAINED_CRATES: |
| 72 | + continue |
| 73 | + |
| 74 | + with Crate(work_dir, name, vers) as c: |
| 75 | + i = 0 |
| 76 | + for line in c.iter_lines(): |
| 77 | + for r in RE_REGEX.findall(line): |
| 78 | + print((name, vers, r)) |
| 79 | + if len(r) >= 2 and r[-2] == "\\": |
| 80 | + continue |
| 81 | + out.write("// {}-{}: {}\n".format(name, vers, r)) |
| 82 | + out.write("consistent!({}_{}, {});\n\n".format( |
| 83 | + name.replace("-", "_"), i, r)) |
| 84 | + out.flush() |
| 85 | + i += 1 |
| 86 | + |
| 87 | + # Leave the scratch directory |
| 88 | + os.chdir(old_dir) |
| 89 | + shutil.rmtree(work_dir) |
| 90 | + out.close() |
| 91 | + |
| 92 | + |
| 93 | +def download_crates_index(): |
| 94 | + if call(["git", "clone", CRATES_IO_INDEX_GIT_LOC]) != 0: |
| 95 | + print("Error cloning the crates.io index") |
| 96 | + exit(1) |
| 97 | + return "crates.io-index" |
| 98 | + |
| 99 | + |
| 100 | +def iter_crates(crates_index): |
| 101 | + exclude = set(["config.json", ".git"]) |
| 102 | + for crate_index_file in iter_files(crates_index, exclude=exclude): |
| 103 | + with open(crate_index_file) as f: |
| 104 | + most_recent = list(f) |
| 105 | + most_recent = most_recent[len(most_recent) - 1] |
| 106 | + |
| 107 | + crate_info = json.loads(most_recent) |
| 108 | + if "regex" not in set(d["name"] for d in crate_info["deps"]): |
| 109 | + continue |
| 110 | + |
| 111 | + if crate_info["yanked"]: |
| 112 | + continue |
| 113 | + yield (crate_info["name"], crate_info["vers"]) |
| 114 | + |
| 115 | + |
| 116 | +def iter_files(d, exclude=set()): |
| 117 | + for x in os.listdir(d): |
| 118 | + if x in exclude: |
| 119 | + continue |
| 120 | + |
| 121 | + fullfp = os.path.abspath(d + "/" + x) |
| 122 | + if os.path.isfile(fullfp): |
| 123 | + yield fullfp |
| 124 | + elif os.path.isdir(fullfp): |
| 125 | + for f in iter_files(fullfp, exclude): |
| 126 | + yield f |
| 127 | + |
| 128 | + |
| 129 | +class Crate(object): |
| 130 | + def __init__(self, work_dir, name, version): |
| 131 | + self.name = name |
| 132 | + self.version = version |
| 133 | + self.url = ("https://crates.io/api/v1/crates/{name}/{version}/download" |
| 134 | + .format(name=self.name, version=self.version)) |
| 135 | + self.filename = "{}/{}-{}.tar.gz".format( |
| 136 | + work_dir, self.name, self.version) |
| 137 | + |
| 138 | + def __enter__(self): |
| 139 | + max_retries = 1 |
| 140 | + retries = 0 |
| 141 | + while retries < max_retries: |
| 142 | + retries += 1 |
| 143 | + |
| 144 | + r = http.request("GET", self.url, preload_content=False) |
| 145 | + try: |
| 146 | + print("[{}/{}] Downloading {}".format( |
| 147 | + retries, max_retries + 1, self.url)) |
| 148 | + with open(self.filename, "wb") as f: |
| 149 | + while True: |
| 150 | + data = r.read(1024) |
| 151 | + if not data: |
| 152 | + break |
| 153 | + f.write(data) |
| 154 | + except requests.exceptions.ConnectionError: |
| 155 | + time.sleep(1) |
| 156 | + r.release_conn() |
| 157 | + continue |
| 158 | + |
| 159 | + r.release_conn() |
| 160 | + break |
| 161 | + |
| 162 | + call(["tar", "-xf", self.filename]) |
| 163 | + |
| 164 | + return self |
| 165 | + |
| 166 | + def __exit__(self, ty, value, tb): |
| 167 | + # We are going to clean up the whole temp dir anyway, so |
| 168 | + # we don't really need to do this. Its nice to clean up |
| 169 | + # after ourselves though. |
| 170 | + try: |
| 171 | + shutil.rmtree(self.filename[:-len(".tar.gz")]) |
| 172 | + os.remove(self.filename) |
| 173 | + except _: |
| 174 | + pass |
| 175 | + |
| 176 | + def iter_srcs(self): |
| 177 | + g = "{crate}/**/*.rs".format(crate=self.filename[:-len(".tar.gz")]) |
| 178 | + for rsrc in glob.iglob(g): |
| 179 | + yield rsrc |
| 180 | + |
| 181 | + def iter_lines(self): |
| 182 | + for src in self.iter_srcs(): |
| 183 | + with open(src) as f: |
| 184 | + for line in f: |
| 185 | + yield line |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == "__main__": |
| 189 | + main() |
0 commit comments