File: gfal2_bulk_copy.py

package info (click to toggle)
gfal2-bindings 1.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 688 kB
  • sloc: cpp: 2,574; python: 1,050; sh: 109; makefile: 92
file content (79 lines) | stat: -rwxr-xr-x 2,582 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : copy a set of files in one go
#
import gfal2
import optparse
import shlex
import sys

def event_callback(event):
    print("[%s] %s %s %s" % (event.timestamp, event.domain, event.stage, event.description))


def monitor_callback(src, dst, average, instant, transferred, elapsed):
    print("[%4d] %.2fMB (%.2fKB/s)\r" % (elapsed, transferred / 1048576, average / 1024)),
    sys.stdout.flush()


if __name__ == '__main__':
    # Parse arguments
    parser = optparse.OptionParser(usage = 'usage: %prog [options] file')
    parser.add_option('-K', '--validate', dest = 'validate', action = 'store_true',
                        help = 'Validate source and destination checksum',
                        default = False)
    parser.add_option('-o', '--overwrite', dest = 'overwrite', action = 'store_true',
                        help = 'Overwrite destination')

    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error('Incorrect number of arguments. Need a file containing the list of sources and destinations.')
    
    sources = []
    destinations = []
    for line in open(args[0]).readlines():
        l = shlex.split(line)
        if l:
            sources.append(l[0])
            destinations.append(l[1])
            
    print("Found %d pairs" % len(sources))
       
    # Instantiate gfal2
    ctx = gfal2.creat_context()

    # Set transfer parameters
    params = ctx.transfer_parameters()
    params.event_callback   = event_callback
    params.monitor_callback = monitor_callback
    
    if options.overwrite:
        params.overwrite = True
        print("Enabled overwrite")

    if options.validate:
        params.checksum_check = True
        print("Enabled checksum check")
        
    # Copy!
    # In this case, an exception will be thrown if the whole process fails
    # If any transfer fail, the method will return a list of GError objects, one per file
    # being None if that file succeeded
    try:
        errors = ctx.filecopy(params, sources, destinations)
        if not errors:
            print("Copy succeeded!")
        else:
            for i in range(len(errors)):
                e = errors[i]
                src = sources[i]
                dst = destinations[i]
                if e:
                    print("%s => %s failed [%d] %s" % (src, dst, e.code, e.message))
                else:
                    print("%s => %s succeeded!" % (src, dst))
    except Exception as e:
        print("Copy failed: %s" % str(e))
        sys.exit(1)