File: rm.pyx

package info (click to toggle)
obitools 3.0.1~b26%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,756 kB
  • sloc: ansic: 24,299; python: 657; sh: 27; makefile: 21
file content (87 lines) | stat: -rw-r--r-- 2,522 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#cython: language_level=3

from obitools3.uri.decode import open_uri
from obitools3.apps.config import logger
from obitools3.dms import DMS
from obitools3.apps.optiongroups import addMinimalInputOption
from obitools3.dms.view.view cimport View
from obitools3.utils cimport tostr
import os
import shutil
 
__title__="Delete a view"


def addOptions(parser):    
    addMinimalInputOption(parser)

def run(config):

    DMS.obi_atexit()
    
    logger("info", "obi rm")

    # Open the input
    input = open_uri(config['obi']['inputURI'])
    if input is None:
        raise Exception("Could not read input")
    
    # Check that it's a view
    if isinstance(input[1], View) :
        view = input[1]
    else: 
        raise NotImplementedError()
    
    dms = input[0]
    
    # Get the path to the view file to remove
    path = dms.full_path  # dms path
    view_path=path+b"/VIEWS/"
    view_path+=view.name
    view_path+=b".obiview"
    
    to_remove = {}
    # For each column:
    for col_alias in view.keys():
        col = view[col_alias]
        col_name = col.original_name
        col_version = col.version
        col_type = col.data_type
        col_ref = (col_name, col_version)
        # build file name and AVL file names
        col_file_name = f"{tostr(path)}/{tostr(col.original_name)}.obicol/{tostr(col.original_name)}@{col.version}.odc"
        if col_type in [b'OBI_CHAR', b'OBI_QUAL', b'OBI_STR', b'OBI_SEQ']:
            avl_file_name = f"{tostr(path)}/OBIBLOB_INDEXERS/{tostr(col.original_name)}_{col.version}_indexer"
        else:
            avl_file_name = None
        to_remove[col_ref] = [col_file_name, avl_file_name]
    
    # For each view:
    do_not_remove = []
    for vn in dms:
        v = dms[vn]
        # ignore the one being deleted
        if v.name != view.name:
            # check that none of the column is referenced, if referenced, remove from list to remove
            cols = [(v[c].original_name, v[c].version) for c in v.keys()]
            for col_ref in to_remove:
                if col_ref in cols:
                    do_not_remove.append(col_ref)
    
    for nr in do_not_remove:
        to_remove.pop(nr)
    
    # Close the view and the DMS
    view.close()
    input[0].close(force=True)
    
    #print(to_remove)
    
    # rm AFTER view and DMS close
    os.remove(view_path)
    for col in to_remove:
        os.remove(to_remove[col][0])
        if to_remove[col][1] is not None:
            shutil.rmtree(to_remove[col][1])