From 7a623727d67e6a6bd63794d6c5d9b8aa0198d257 Mon Sep 17 00:00:00 2001 From: Andrea Manzi Date: Sat, 13 Oct 2018 20:38:16 +0200 Subject: [PATCH] DMC-1105: adapt syntax to python3 --- example/python/gfal2_bring_online.py | 26 +++++++------- example/python/gfal2_bulk_bring_online.py | 2 +- example/python/gfal2_bulk_copy.py | 22 ++++++------ example/python/gfal2_copy_multiple_cred.py | 40 +++++++++++----------- example/python/gfal2_get_turls.py | 16 ++++----- example/python/gfal2_long_listing.py | 2 +- example/python/gfal2_python_listdir.py | 10 +++--- example/python/gfal2_python_read.py | 12 +++---- example/python/gfal2_recursive_ls.py | 6 ++-- example/python/gfal2_simple_listing.py | 2 +- 10 files changed, 69 insertions(+), 69 deletions(-) diff --git a/example/python/gfal2_bring_online.py b/example/python/gfal2_bring_online.py index 4abdf97..d4a12a8 100755 --- a/example/python/gfal2_bring_online.py +++ b/example/python/gfal2_bring_online.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- ## # Example : bring online a file with gfal 2.0 @@ -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] @@ -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) diff --git a/example/python/gfal2_bulk_bring_online.py b/example/python/gfal2_bulk_bring_online.py index f0c7026..4b94d58 100755 --- a/example/python/gfal2_bulk_bring_online.py +++ b/example/python/gfal2_bulk_bring_online.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- ## # Example : bring online a set of files with gfal2 diff --git a/example/python/gfal2_bulk_copy.py b/example/python/gfal2_bulk_copy.py index 31ecf5f..572d5a6 100755 --- a/example/python/gfal2_bulk_copy.py +++ b/example/python/gfal2_bulk_copy.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- ## # Example : copy a set of files in one go @@ -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() @@ -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() @@ -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 @@ -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) diff --git a/example/python/gfal2_copy_multiple_cred.py b/example/python/gfal2_copy_multiple_cred.py index acb4ec4..0984130 100755 --- a/example/python/gfal2_copy_multiple_cred.py +++ b/example/python/gfal2_copy_multiple_cred.py @@ -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__': @@ -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: @@ -38,12 +38,12 @@ 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 @@ -51,32 +51,32 @@ def monitor_callback(src, dst, average, instant, transferred, elapsed): 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 @@ -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) diff --git a/example/python/gfal2_get_turls.py b/example/python/gfal2_get_turls.py index 863553b..c631a70 100755 --- a/example/python/gfal2_get_turls.py +++ b/example/python/gfal2_get_turls.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- ## # Example : get list of turls/replicas with gfal 2.0 @@ -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) diff --git a/example/python/gfal2_long_listing.py b/example/python/gfal2_long_listing.py index 40688a1..a7ab4ef 100755 --- a/example/python/gfal2_long_listing.py +++ b/example/python/gfal2_long_listing.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python import sys import gfal2 diff --git a/example/python/gfal2_python_listdir.py b/example/python/gfal2_python_listdir.py index eb526a7..07800d1 100755 --- a/example/python/gfal2_python_listdir.py +++ b/example/python/gfal2_python_listdir.py @@ -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 @@ -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 diff --git a/example/python/gfal2_python_read.py b/example/python/gfal2_python_read.py index d555ad6..1b8e8c5 100755 --- a/example/python/gfal2_python_read.py +++ b/example/python/gfal2_python_read.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- ## # Example : open/read/close a file with gfal 2.0 @@ -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() @@ -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 diff --git a/example/python/gfal2_recursive_ls.py b/example/python/gfal2_recursive_ls.py index 5874da6..fe082ee 100755 --- a/example/python/gfal2_recursive_ls.py +++ b/example/python/gfal2_recursive_ls.py @@ -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: @@ -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): diff --git a/example/python/gfal2_simple_listing.py b/example/python/gfal2_simple_listing.py index 74c55ee..98c264a 100755 --- a/example/python/gfal2_simple_listing.py +++ b/example/python/gfal2_simple_listing.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python import sys import gfal2