|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import copy |
| 6 | +from collections import namedtuple |
| 7 | +from multiprocessing import Pool |
| 8 | +from bquick.config_parser import get_config |
| 9 | +from bquick.bigquery import table_list_handler |
| 10 | + |
| 11 | +BQ_CONFIG = get_config() |
| 12 | + |
| 13 | +DeleteProcessParam = namedtuple("DeleteProcessParam", "bq_client \ |
| 14 | + dataset \ |
| 15 | + table_name") |
| 16 | + |
| 17 | +def delete_table_by_name(bq_client, dataset, table_name, ignore_confirm=False): |
| 18 | + """Delete the table with the given name. |
| 19 | + """ |
| 20 | + __delete_table_list(bq_client, dataset, [table_name], ignore_confirm) |
| 21 | + |
| 22 | +def delete_table_using_file(bq_client, dataset, file_path, |
| 23 | + ignore_confirm=False): |
| 24 | + """Delete all the tables in the given file. |
| 25 | + """ |
| 26 | + arg_file_path = file_path |
| 27 | + if os.path.isabs(arg_file_path): |
| 28 | + delete_file_path = arg_file_path |
| 29 | + else: |
| 30 | + working_path = os.getcwd() |
| 31 | + delete_file_path = os.path.join(working_path, arg_file_path) |
| 32 | + |
| 33 | + if not os.path.exists(delete_file_path): |
| 34 | + raise ValueError("Given file path doesn't exist: %s" % arg_file_path) |
| 35 | + |
| 36 | + with open(delete_file_path) as table_list_file: |
| 37 | + table_list = [line.rstrip() for line in table_list_file.readlines()] |
| 38 | + __delete_table_list(bq_client, dataset, table_list, ignore_confirm) |
| 39 | + |
| 40 | +def delete_table_with_wildcard(bq_client, |
| 41 | + dataset, |
| 42 | + table_prefix, |
| 43 | + start_date, |
| 44 | + end_date, |
| 45 | + ignore_confirm=False): |
| 46 | + """Delete all the tables matching the wildcard table prefix. |
| 47 | + """ |
| 48 | + table_list = table_list_handler.list_wildcard_table(bq_client, |
| 49 | + dataset, |
| 50 | + table_prefix, |
| 51 | + start_date, |
| 52 | + end_date, |
| 53 | + sys.maxint) |
| 54 | + __delete_table_list(bq_client, dataset, table_list, ignore_confirm) |
| 55 | + |
| 56 | +def delete_table_with_regex(bq_client, dataset, table_name_pattern, |
| 57 | + ignore_confirm=False): |
| 58 | + """Delete all the tables that match given regex pattern. |
| 59 | + """ |
| 60 | + table_list = table_list_handler.list_regex_table(bq_client, |
| 61 | + dataset, |
| 62 | + table_name_pattern, |
| 63 | + sys.maxint) |
| 64 | + __delete_table_list(bq_client, dataset, table_list, ignore_confirm) |
| 65 | + |
| 66 | + |
| 67 | +def __delete_table_list(bq_client, dataset, table_name_list, ignore_confirm): |
| 68 | + """Delete all the tables in the list |
| 69 | + """ |
| 70 | + if not ignore_confirm: |
| 71 | + if not __confirm_delete(dataset, table_name_list): |
| 72 | + return |
| 73 | + |
| 74 | + # TODO: make parallel happen |
| 75 | +# delete_process_pool = Pool(processes=2) |
| 76 | +# delete_process_pool.map( |
| 77 | +# __delete_table_process, |
| 78 | +# [DeleteProcessParam(bq_client=copy.deepcopy(bq_client), |
| 79 | +# dataset=dataset, |
| 80 | +# table_name=table_name) \ |
| 81 | +# for table_name in table_name_list]) |
| 82 | +# delete_process_pool.close() |
| 83 | +# delete_process_pool.join() |
| 84 | + for table_name in table_name_list: |
| 85 | + param = DeleteProcessParam(bq_client=bq_client, |
| 86 | + dataset=dataset, |
| 87 | + table_name=table_name) |
| 88 | + __delete_table_process(param) |
| 89 | + |
| 90 | +def __confirm_delete(dataset, table_name_list): |
| 91 | + """This function will block process and ask for confirmation. |
| 92 | + All the table names will be shown. |
| 93 | + """ |
| 94 | + proceed_choices = ['yes', 'y'] |
| 95 | + abort_choices = ['no', 'n'] |
| 96 | + |
| 97 | + for table_name in table_name_list: |
| 98 | + print table_name |
| 99 | + |
| 100 | + print "Delete all these tables from dataset [%s]? [y or n]" % dataset |
| 101 | + |
| 102 | + while True: |
| 103 | + choice = raw_input().lower() |
| 104 | + if choice in proceed_choices: |
| 105 | + return True |
| 106 | + elif choice in abort_choices: |
| 107 | + return False |
| 108 | + else: |
| 109 | + print "Please enter [y or n]" |
| 110 | + |
| 111 | +def __delete_table_process(param): |
| 112 | + """Delete table with compressed param: |
| 113 | + Args: |
| 114 | + - param: A dict consists of: |
| 115 | + - bq_client: a bigquery service object. |
| 116 | + - dataset: the dataset to operate on. |
| 117 | + - table_name: of which table to be deleted. |
| 118 | + """ |
| 119 | + bq_client = param.bq_client |
| 120 | + dataset = param.dataset |
| 121 | + table_name = param.table_name |
| 122 | + |
| 123 | + __delete_table(bq_client, dataset, table_name) |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +def __delete_table(bq_client, dataset, table_name): |
| 128 | + """Delete the table with given name. |
| 129 | + """ |
| 130 | + try: |
| 131 | + bq_client.tables() \ |
| 132 | + .delete(projectId=BQ_CONFIG.project, |
| 133 | + datasetId=dataset, |
| 134 | + tableId=table_name) \ |
| 135 | + .execute() |
| 136 | + except Exception as e: |
| 137 | + # TODO: Logging |
| 138 | + raise |
0 commit comments