• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

4dn-dcic / dcicwrangling / 8545209979

03 Apr 2024 08:37PM UTC coverage: 33.718% (+1.6%) from 32.1%
8545209979

push

github

web-flow
Merge pull request #113 from 4dn-dcic/ajs_add_del_script

New property or item status change script

81 of 95 new or added lines in 3 files covered. (85.26%)

8 existing lines in 8 files now uncovered.

1256 of 3725 relevant lines covered (33.72%)

1.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/scripts/delete_item_or_fields.py
1
#!/usr/bin/env python3
NEW
2
'''
×
3
Given a list of item IDs or search result will:
4
    1. given one of more values in the '--fields' options delete those fields from the item
5
    2. if no fields option then set the status of the item(s) to deleted
6
Useful for deleting multiple fields or changing item status to deleted
7
NOTE: can use patch_field_for_many_items script to delete a single field for many items
8
but this script is more direct/flexible in some ways
9
'''
NEW
10
import argparse
×
NEW
11
from dcicutils.ff_utils import get_metadata, delete_metadata, delete_field
×
NEW
12
from functions import script_utils as scu
×
13

14

NEW
15
def get_args(args):
×
NEW
16
    parser = argparse.ArgumentParser(
×
17
        parents=[scu.create_input_arg_parser(), scu.create_ff_arg_parser()],
18
        formatter_class=argparse.RawDescriptionHelpFormatter,
19
    )
NEW
20
    parser.add_argument('--fields',
×
21
                        nargs='+',
22
                        help="With this option these fields will be removed from the items.")
NEW
23
    args = parser.parse_args(args)
×
NEW
24
    return args
×
25

26

27
def main():  # pragma: no cover
28
    args = get_args()
29
    auth = scu.authenticate(key=args.key, keyfile=args.keyfile, env=args.env)
30
    dry_run = True
31
    if args.dbupdate:
32
        dry_run = False
33

34
    print('#', auth.get('server'))
35
    id_list = scu.get_item_ids_from_args(args.input, auth, args.search)
36
    del_flds = None
37
    if args.fields:
38
        fields = args.fields
39
        del_flds = ','.join(fields)
40
    problems = []
41
    for iid in id_list:
42
        try:
43
            get_metadata(iid, auth, add_on='frame=object')
44
        except Exception:
45
            problems.append(iid)
46
            continue
47

48
        if del_flds:
49
            # we have field(s) that we want to delete from provided items
50
            print(f"Will delete {del_flds} from {iid}")
51
            if dry_run:
52
                print("DRY RUN")
53
            else:
54
                try:
55
                    res = delete_field(iid, del_flds, auth)
56
                    status = res.get('status')
57
                    if status.lower() == 'success':
58
                        print(status)
59
                    else:
60
                        print(res)
61
                        problems.append(iid)
62
                except Exception as e:
63
                    print(f"PROBLEM: {e}")
64
                    problems.append(iid)
65
        else:
66
            # we want to set the status of the items in id_list to deleted
67
            print(f"Will set status of {iid} to DELETED")
68
            if dry_run:
69
                print("DRY RUN")
70
            else:
71
                try:
72
                    res = delete_metadata(iid, auth)
73
                    status = res.get('status')
74
                    if status.lower() == 'success':
75
                        print(status)
76
                    else:
77
                        print(res)
78
                        problems.append(iid)
79
                except Exception as e:
80
                    print(f"PROBLEM: {e}")
81
                    problems.append(iid)
82

83
    if problems:
84
        print('THERE WAS A PROBLEM DELETING METADATA FOR THE FOLLOWING:')
85
        for p in problems:
86
            print(p)
87

88

NEW
89
if __name__ == '__main__':
×
NEW
90
    main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc