Gitlab Community Edition Instance

rm.py 1.21 KB
Newer Older
mhellka's avatar
mhellka committed
1
2
3
4
"""
Delete files from an archive.
"""
from contextlib import ExitStack
Marcel Hellkamp's avatar
Marcel Hellkamp committed
5
from pycdstar3 import ApiError
mhellka's avatar
mhellka committed
6
7
8
9
10

# TODO: Delete directories or glob patterns


def register(subparsers):
11
12
13
14
15
16
17
18
19
20
    parser = subparsers.add_parser(
        "rm", help=__doc__.strip().splitlines()[0], description=__doc__
    )

    parser.add_argument(
        "-f",
        "--force",
        action="store_true",
        help="Ignore missing files or access errors",
    )
mhellka's avatar
mhellka committed
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

    parser.add_argument("ARCHIVE", help="Archive ID")
    parser.add_argument("FILES", nargs="+", help="File(s) to remove.")
    parser.set_defaults(main=remove_files)


def remove_files(ctx, args):
    client = ctx.client
    vault = ctx.vault
    archive = args.ARCHIVE

    files = args.FILES
    force = args.force

    with ExitStack() as stack:
        if len(files) > 1:
            stack.enter_context(client.begin(autocommit=True))

        for i, file in enumerate(files):
            file = file.lstrip("/")
            try:
                ctx.print("Deleting {}/{}".format(archive, file))
                client.delete_file(vault, archive, file)
            except ApiError as e:
                if e.error == "FileNotFound" and force:
                    continue
                raise