Skip to content

Commit

Permalink
DMC-1105: adapt syntax to python3
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-manzi committed Oct 13, 2018
1 parent 0cdaacb commit 7a62372
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 69 deletions.
26 changes: 13 additions & 13 deletions example/python/gfal2_bring_online.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : bring online a file with gfal 2.0
Expand All @@ -9,7 +9,7 @@

if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: %s [surl]" % sys.argv[0]
print("Usage: %s [surl]" % sys.argv[0])
sys.exit(1)

surl = sys.argv[1]
Expand All @@ -18,23 +18,23 @@
try:
# bring_online(surl, pintime, timeout, async)
(status, token) = ctx.bring_online(surl, 60, 60, False)
print "Got token %s" % token
print("Got token %s" % token)
while status == 0:
status = ctx.bring_online_poll(surl, token)
print "File brought online!"
except gfal2.GError, e:
print "Could not bring the file online:"
print "\t", e.message
print "\t Code", e.code
print("File brought online!")
except gfal2.GError as e:
print("Could not bring the file online:")
print("\t", e.message)
print("\t Code", e.code)
sys.exit(2)

try:
ctx.release(surl, token)
print "File released!"
except gfal2.GError, e:
print "Could not release the file:"
print "\t", e.message
print "\t Code", e.code
print("File released!")
except gfal2.GError as e:
print("Could not release the file:")
print("\t", e.message)
print("\t Code", e.code)
sys.exit(3)

sys.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion example/python/gfal2_bulk_bring_online.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : bring online a set of files with gfal2
Expand Down
22 changes: 11 additions & 11 deletions example/python/gfal2_bulk_copy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : copy a set of files in one go
Expand All @@ -9,11 +9,11 @@
import sys

def event_callback(event):
print "[%s] %s %s %s" % (event.timestamp, event.domain, event.stage, event.description)
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),
print("[%4d] %.2fMB (%.2fKB/s)\r" % (elapsed, transferred / 1048576, average / 1024)),
sys.stdout.flush()


Expand All @@ -38,7 +38,7 @@ def monitor_callback(src, dst, average, instant, transferred, elapsed):
sources.append(l[0])
destinations.append(l[1])

print "Found %d pairs" % len(sources)
print("Found %d pairs" % len(sources))

# Instantiate gfal2
ctx = gfal2.creat_context()
Expand All @@ -50,11 +50,11 @@ def monitor_callback(src, dst, average, instant, transferred, elapsed):

if options.overwrite:
params.overwrite = True
print "Enabled overwrite"
print("Enabled overwrite")

if options.validate:
params.checksum_check = True
print "Enabled checksum check"
print("Enabled checksum check")

# Copy!
# In this case, an exception will be thrown if the whole process fails
Expand All @@ -63,17 +63,17 @@ def monitor_callback(src, dst, average, instant, transferred, elapsed):
try:
errors = ctx.filecopy(params, sources, destinations)
if not errors:
print "Copy succeeded!"
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)
print("%s => %s failed [%d] %s" % (src, dst, e.code, e.message))
else:
print "%s => %s succeeded!" % (src, dst)
except Exception, e:
print "Copy failed: %s" % str(e)
print("%s => %s succeeded!" % (src, dst))
except Exception as e:
print("Copy failed: %s" % str(e))
sys.exit(1)

40 changes: 20 additions & 20 deletions example/python/gfal2_copy_multiple_cred.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

def event_callback(event):
#print event
print "[%s] %s %s %s" % (event.timestamp, event.domain, event.stage, event.description)
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),
print("[%4d] %.2fMB (%.2fKB/s)\r" % (elapsed, transferred / 1048576, average / 1024)),
sys.stdout.flush()

if __name__ == '__main__':
Expand All @@ -27,9 +27,9 @@ def monitor_callback(src, dst, average, instant, transferred, elapsed):
help = 'Checksum (i.e. ADLER32:1234)')
parser.add_option('-o', '--overwrite', dest = 'overwrite', action = 'store_true',
help = 'Overwrite destination')
parser.add_option('--source_cred', dest = 'source_cred', help = 'Credentials for the source storage')
parser.add_option('--source_cred', dest = 'source_cred', help = 'Credentials for the source storage')

parser.add_option('--dest_cred', dest = 'dest_cred', help = 'Credentials for the destination storage')
parser.add_option('--dest_cred', dest = 'dest_cred', help = 'Credentials for the destination storage')

(options, args) = parser.parse_args()
if len(args) != 2:
Expand All @@ -38,45 +38,45 @@ def monitor_callback(src, dst, average, instant, transferred, elapsed):
source = args[0]
dest = args[1]

print "Source: %s" % source
print "Destination: %s" % dest
print("Source: %s" % source)
print("Destination: %s" % dest)

# Instantiate gfal2
ctx = gfal2.creat_context()
gfal2.set_verbose(gfal2.verbose_level.debug)
gfal2.set_verbose(gfal2.verbose_level.debug)
# 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"
print("Enabled overwrite")

if options.validate or options.checksum:
params.checksum_check = True
print "Enabled checksum check"
print("Enabled checksum check")

if options.checksum:
(alg, val) = options.checksum.split(':')
params.set_user_defined_checksum(alg, val)
print "User defined checksum: %s:%s" % params.get_user_defined_checksum()
print("User defined checksum: %s:%s" % params.get_user_defined_checksum())

if options.source_space_token:
params.src_spacetoken = options.source_space_token
print "Source space token: %s" % params.src_spacetoken
print("Source space token: %s" % params.src_spacetoken)
if options.dest_space_token:
params.dst_spacetoken = options.dest_space_token
print "Destination space token: %s" % params.dst_spacetoken
print("Destination space token: %s" % params.dst_spacetoken)

if options.source_cred:
if options.source_cred:
s_cred = gfal2.cred_new("X509_CERT",options.source_cred)
gfal2.cred_set(ctx,source,s_cred)
print "Source credentials: %s" % options.source_cred
if options.dest_cred:
gfal2.cred_set(ctx,source,s_cred)
print("Source credentials: %s" % options.source_cred)
if options.dest_cred:
d_cred = gfal2.cred_new("X509_CERT",options.dest_cred)
gfal2.cred_set(ctx,dest,d_cred)
print "Destination credentials: %s" % options.dest_cred
print("Destination credentials: %s" % options.dest_cred)
# Five minutes timeout
params.timeout = 300

Expand All @@ -86,8 +86,8 @@ def monitor_callback(src, dst, average, instant, transferred, elapsed):
# (i.e. source can be file:/// and destination gsiftp://)
try:
r = ctx.filecopy(params, source, dest)
print "Copy succeeded!"
except Exception, e:
print "Copy failed: %s" % str(e)
print("Copy succeeded!")
except Exception as e:
print("Copy failed: %s" % str(e))
sys.exit(1)

16 changes: 8 additions & 8 deletions example/python/gfal2_get_turls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : get list of turls/replicas with gfal 2.0
Expand All @@ -9,19 +9,19 @@

if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: %s [surl]" % sys.argv[0]
print("Usage: %s [surl]" % sys.argv[0])
sys.exit(1)

surl = sys.argv[1]

ctx = gfal2.creat_context()
try:
replicas = ctx.getxattr(surl, 'user.replicas')
print replicas
except gfal2.GError, e:
print "Could not get the replicas:"
print "\t", e.message
print "\t Code", e.code
replicas = ctx.getxattr(surl, 'user.replicas')
print(replicas)
except gfal2.GError as e:
print("Could not get the replicas:")
print("\t", e.message)
print("\t Code", e.code)
sys.exit(2)

sys.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion example/python/gfal2_long_listing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python

import sys
import gfal2
Expand Down
10 changes: 5 additions & 5 deletions example/python/gfal2_python_listdir.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : list the content of a directory with gfal 2.0
Expand All @@ -13,14 +13,14 @@
if __name__ == '__main__':
# comment for usage
if(len(sys.argv) < 2):
print "\nUsage\t %s [gfal_folder] \n"%(sys.argv[0])
print " Example: %s lfn:/grid/dteam/ "%(sys.argv[0])
print " %s srm://myserver.com/myhome/ \n"%(sys.argv[0])
print("\nUsage\t %s [gfal_folder] \n"%(sys.argv[0]))
print(" Example: %s lfn:/grid/dteam/ "%(sys.argv[0]))
print(" %s srm://myserver.com/myhome/ \n"%(sys.argv[0]))
os._exit(1)

ctx = gfal2.creat_context()
my_dir = ctx.listdir(sys.argv[1])
for d in my_dir:
print d
print(d)

# no closedir needed, done automatically
12 changes: 6 additions & 6 deletions example/python/gfal2_python_read.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : open/read/close a file with gfal 2.0
Expand All @@ -13,9 +13,9 @@
if __name__ == '__main__':
# comment for usage
if(len(sys.argv) < 2):
print "\nUsage\t %s [gfal_url] \n"%(sys.argv[0])
print " Example: %s lfn:/grid/dteam/myfile "%(sys.argv[0])
print " %s srm://myserver.com/myhome/myfile \n"%(sys.argv[0])
print("\nUsage\t %s [gfal_url] \n"%(sys.argv[0]))
print(" Example: %s lfn:/grid/dteam/myfile "%(sys.argv[0]))
print(" %s srm://myserver.com/myhome/myfile \n"%(sys.argv[0]))
os._exit(1)

ctx = gfal2.creat_context()
Expand All @@ -26,11 +26,11 @@
# Hex dump
str = []
for byte in content:
print "%2X " % ord(byte),
print("%2X " % ord(byte)),
if byte in printable:
str.append(byte)
else:
str.append('.')
print '\t', ' '.join(str)
print('\t', ' '.join(str))

# no close needed, done automatically with the destruction of the file handle
6 changes: 3 additions & 3 deletions example/python/gfal2_recursive_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _crawl(self, url, out, level=0):
entries = [f for f in self.context.listdir(url)
if f != '.' and f != '..']
except gfal2.GError:
print >>out, tabbing, '!'
out.write(tabbing, '!')
return

for f in entries:
Expand All @@ -68,9 +68,9 @@ def _crawl(self, url, out, level=0):

# Print entry
if self.long:
print >>out, tabbing, self._long_format(f, fstat)
out.write(tabbing, self._long_format(f, fstat))
else:
print >>out, tabbing, self._short_format(f)
out.write(tabbing, self._short_format(f))

# Descend
if self.recursive and stat.S_ISDIR(fstat.st_mode):
Expand Down
2 changes: 1 addition & 1 deletion example/python/gfal2_simple_listing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python

import sys
import gfal2
Expand Down

0 comments on commit 7a62372

Please sign in to comment.