spotify-metadata-parquet / code /check_package.py
alpha31476's picture
Add dataset card, analysis code, and release documentation
9a1f607 verified
Raw History Blame Contribute Delete
3.65 kB
"""Check code syntax, table configs, portable paths and cleared notebooks.
This does not upload or execute the historical analysis code.
"""
import argparse
import ast
import json
import os
from pathlib import Path
import re
import subprocess
import yaml
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--data-dir', type=Path, default=Path(__file__).resolve().parents[1])
args = parser.parse_args()
root = args.data_dir.resolve(strict=True)
checked = []
python_files = list(root.rglob('*.py'))
for path in python_files:
ast.parse(path.read_text(encoding='utf-8'), filename=path.name)
checked.append(f'Python syntax: {len(python_files)} files')
manifest = json.loads((root / 'manifest.json').read_text(encoding='utf-8'))
card = yaml.safe_load((root / 'README.md').read_text(encoding='utf-8').split('---', 2)[1])
expected = {
name: (Path(filename).stem + '/' + name + '/part-*.parquet', len(table['parts']))
for filename, database in manifest['databases'].items()
for name, table in database['tables'].items()
}
configs = card['configs']
assert len(configs) == len(expected) == 13
assert {item['config_name'] for item in configs} == set(expected)
assert [item['config_name'] for item in configs if item.get('default')] == ['tracks']
count = 0
for item in configs:
data = item['data_files']
assert len(data) == 1 and data[0]['split'] == 'train'
pattern, parts = expected[item['config_name']]
assert data[0]['path'] == pattern
assert len(list(root.glob(pattern))) == parts
count += parts
checked.append(f'Dataset configurations: 13 tables, {count} Parquet parts')
notebooks = list((root / 'code/original').glob('*.ipynb'))
assert len(notebooks) == 2
for path in notebooks:
notebook = json.loads(path.read_text(encoding='utf-8'))
for cell in notebook['cells']:
if cell['cell_type'] == 'code':
assert cell['execution_count'] is None and cell['outputs'] == []
checked.append('Both original notebooks have cleared output and run counts')
for path in root.rglob('*'):
if not path.is_file() or path.suffix in {'.parquet', '.pyc', '.png'}:
continue
if set(path.relative_to(root).parts) & {'.cache', '__pycache__', '.venv', 'work', '.git'}:
continue
text = path.read_text(encoding='utf-8-sig')
# Match literal credentials and machine-specific Windows user folders.
assert not re.search(r'\bhf_[A-Za-z0-9]{20,}\b', text), 'Possible token: ' + path.name
assert not re.search(r'[A-Za-z]:[\\/]+Users[\\/]', text, re.I), 'Local user path: ' + path.name
assert 'session_context.jsonl' not in path.name
checked.append('No literal Hugging Face tokens or local Windows user paths in text files')
if os.name == 'nt':
scripts = list((root / 'code/original').glob('*.ps1'))
for path in scripts:
quoted = str(path).replace("'", "''")
command = "$tokens=$null; $errors=$null; [System.Management.Automation.Language.Parser]::ParseFile('" + quoted + "', [ref]$tokens, [ref]$errors) | Out-Null; if ($errors.Count) { $errors | Out-String | Write-Error; exit 1 }"
subprocess.run(['powershell', '-NoProfile', '-NonInteractive', '-Command', command], check=True, capture_output=True, text=True)
checked.append(f'PowerShell syntax: {len(scripts)} files')
print(json.dumps({'status': 'passed', 'checks': checked}, indent=2))
if __name__ == '__main__':
main()