| 1 | """Tool to re-generate and save vega_datasets/datasets.json |
| 2 | |
| 3 | Usage: |
| 4 | $ python generate_datasets_json.py v1.24.0 |
| 5 | |
| 6 | The second argument is the name of the desired version tag within |
| 7 | http://github.com/vega/vega-datasets/ |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | import os |
| 12 | import subprocess |
| 13 | import sys |
| 14 | |
| 15 | |
| 16 | def main(tag): |
| 17 | cwd = os.path.dirname(__file__) |
| 18 | datasets_src = os.path.join(cwd, "vega-datasets") |
| 19 | if not os.path.exists(datasets_src): |
| 20 | print("Cloning vega-datsets...") |
| 21 | subprocess.check_call( |
| 22 | ["git", "clone", "http://github.com/vega/vega-datasets.git"], cwd=cwd |
| 23 | ) |
| 24 | print(f"Checking out '{tag}'") |
| 25 | subprocess.check_call(["git", "checkout", tag], cwd=datasets_src) |
| 26 | |
| 27 | data_dir = os.path.abspath(os.path.join(datasets_src, "data")) |
| 28 | datasets_file = os.path.abspath( |
| 29 | os.path.join(cwd, "..", "vega_datasets", "datasets.json") |
| 30 | ) |
| 31 | core_file = os.path.abspath(os.path.join(cwd, "..", "vega_datasets", "core.py")) |
| 32 | |
| 33 | print(f"Extracting datasets from {data_dir}") |
| 34 | datasets = {} |
| 35 | for filename in os.listdir(data_dir): |
| 36 | name, fmt = os.path.splitext(filename) |
| 37 | datasets[name] = {"filename": filename, "format": fmt[1:]} |
| 38 | |
| 39 | print(f"Writing datsets to {datasets_file}") |
| 40 | with open(datasets_file, "w") as f: |
| 41 | json.dump(datasets, f, indent=2, sort_keys=True) |
| 42 | |
| 43 | print("Updating SOURCE_TAG in core file") |
| 44 | subprocess.check_call( |
| 45 | ["sed", "-i", ".bak", f"s/SOURCE_TAG.*/SOURCE_TAG = {tag!r}/g", core_file] |
| 46 | ) |
| 47 | subprocess.check_call(["rm", f"{core_file}.bak"]) |
| 48 | |
| 49 | |
| 50 | if __name__ == "__main__": |
| 51 | if len(sys.argv) == 2: |
| 52 | main(sys.argv[1]) |
| 53 | else: |
| 54 | print(__doc__) |