<commit_before>#!/usr/bin/env python

import rethinkdb as r
import optparse
import sys


def delete_project(conn, project_id):
    project = r.table("projects").get(project_id).run(conn)
    if project is None:
        print "No such project: %s" % (project_id)
        sys.exit(1)
    print "Deleting project %s with id %s" % (project['name'], project_id)
    r.table("datadirs_denorm")\
     .get_all(project_id, index="project_id")\
     .delete().run(conn)
    dirs = list(r.table("datadirs")
                .get_all(project_id, index="project")
                .run(conn))
    for dir in dirs:
        files = dir['datafiles']
        if len(files) > 0:
            r.table("datafiles").get_all(*files).delete().run(conn)
    r.table("datadirs")\
     .get_all(project_id, index="project")\
     .delete()\
     .run(conn)
    r.table("projects").get(project_id).delete().run(conn)


def list_projects(conn):<commit_msg>Cleanup projdel.py to work with new data model.<commit_after>#!/usr/bin/env python

import rethinkdb as r
import optparse
import sys


def delete_project(conn, project_id):
    project = r.table("projects").get(project_id).run(conn)
    if project is None:
        print "No such project: %s" % (project_id)
        sys.exit(1)
    print "Deleting project %s with id %s" % (project['name'], project_id)
    r.table("project2datadir")\
     .get_all(project_id, index="project_id")\
     .delete().run(conn)
    dirs = list(r.table("datadirs")
                .get_all(project_id, index="project")
                .run(conn))
    for dir in dirs:
        file_ids = list(r.table("datadir2datafile")
                        .get_all(dir['id'], index="datadir_id")
                        .run(conn))
        ids = []
        for fid in file_ids:
            ids.append(fid['datafile_id'])
        if ids:
            r.table('datafiles').get_all(*ids).delete().run(conn)
        r.table("datadir2datafile")\
         .get_all(dir['id'], index="datadir_id")\
         .delete().run(conn)
    r.table("datadirs")\
     .get_all(project_id, index="project")\
     .delete()\
     .run(conn)
    r.table("projects").get(project_id).delete().run(conn)


def list_projects(conn):
{'fraction_non_alphanumeric': 0.1579426490669094, 'fraction_numerical': 0.0027309968138370506, 'mean_word_length': 2.6210873146622733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4907401', 'n_tokens_mistral': 759, 'n_tokens_neox': 746, 'n_words': 142}