Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f78686a

Browse files
committed
Issue #829: Changed Python code to work well with Python3
1 parent 318726c commit f78686a

6 files changed

Lines changed: 195 additions & 175 deletions

File tree

build-tools/casacore_floatcheck

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def main(argv=None):
5858
if argv is None:
5959
argv = sys.argv
6060
if len(argv) < 3:
61-
print sys.stderr, 'run as: floatcheck.py file1 file2 [tolerance]'
61+
sys.stderr.write ('run as: floatcheck.py file1 file2 [tolerance]\n')
6262
return 1
6363
tol = 1e-5;
6464
if len(argv) > 3:

build-tools/update_measures

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import tarfile
55
import shutil
66
from ftplib import FTP
77
import optparse
8-
import string
8+
import socket
99

1010
def detect_domain():
1111
choices = ["atnf", "astron", "nrao"]
@@ -30,7 +30,7 @@ parser.add_option('-l', '--location', dest='location', type="choice",
3030
choices=["atnf", "astron", "nrao"],
3131
default=detect_domain(),
3232
help="Select TARGET from: %s [default=%%default]" % \
33-
string.join(["atnf", "astron", "nrao"], ", "))
33+
str.join(", ", ["atnf", "astron", "nrao"]))
3434
parser.add_option('-v', '--verbose', dest='verbose',
3535
action="store_true",
3636
help='show helpful status')
@@ -72,14 +72,14 @@ def get_measures_dir():
7272

7373
def vprint(msg):
7474
if OPTS.verbose:
75-
print msg
75+
print (msg)
7676

7777
# try to find out where the measures data directories are located
7878
if len(args) == 1:
7979
if os.path.exists(args[0]):
8080
measdir = args[0]
8181
else:
82-
print "Measures data directory doesn't exist. Please create it first"
82+
print ("Measures data directory doesn't exist. Please create it first")
8383
sys.exit(1)
8484
else:
8585
measdir = get_measures_dir()
@@ -90,19 +90,19 @@ vprint("Using measures data directory '%s'" % measdir)
9090
# globals
9191
md5suff = '.md5sum'
9292
name = 'measures_data.tar.bz2'
93-
dataservers = { 'ATNF': "ftp.atnf.csiro.au",
94-
'ASTRON': 'ftp.astron.nl',
95-
'NRAO': 'ftp.nrao.edu' }
96-
datadirs = {"ATNF": 'pub/software/casacore/measures_data',
97-
"ASTRON": 'pub/software/casacore/measures_data',
98-
"NRAO": 'pub/software/casacore/measures_data' }
93+
dataservers = { 'atnf': "ftp.atnf.csiro.au",
94+
'astron': 'ftp.astron.nl',
95+
'nrao': 'ftp.nrao.edu' }
96+
datadirs = {"atnf": 'pub/software/casacore/measures_data',
97+
"astron": 'pub/software/casacore/measures_data',
98+
"nrao": 'pub/software/casacore/measures_data' }
9999
tmpdata = '/tmp/'+ name
100100
tmpmd5 = tmpdata+md5suff
101101

102102
vprint("Connecting to server %s" % dataservers[OPTS.location])
103103
ftp = FTP(dataservers[OPTS.location]) # connect to host, default port
104104
ftp.login() # user anonymous, passwd anonymous@
105-
ftp.cwd(datadir[OPTS.location])
105+
ftp.cwd(datadirs[OPTS.location])
106106

107107
vprint("Checking if an update is required...")
108108

@@ -119,7 +119,7 @@ try:
119119
except IOError:
120120
md5old = None
121121

122-
#print md5new, md5old
122+
#print (md5new, md5old)
123123
if md5new != md5old:
124124
vprint("Update required. Downloading measures data archive...")
125125
ftp.retrbinary('RETR %s' % name, open(tmpdata, 'wb').write)

casa/apps/countcode

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# This script counts the lines of code in various types of source files.
44
# It has options to print summaries at various levels.
5+
#
6+
# It can be used with python version 2 and 3.
57

68
# todo:
79
# - possibly specify/override/add files/dirs to be ignored on command line
@@ -21,7 +23,7 @@ import argparse
2123
# - A list of matching file names
2224
# - The delimiter defining a comment in a single line
2325
# - The delimiters defining the start and end of a block comment
24-
# - indication if file contains code or other info (1=code, 0=other)
26+
# - Indication if file contains code or other info (1=code, 0=other)
2527
# Similar to cloc there are a few theoretical problems:
2628
# 1. If a quoted string contains comment delimiters, they are recognized
2729
# as comment delimiters. In principle some regexes could be defined to replace
@@ -30,18 +32,19 @@ import argparse
3032
# 2. A regex like '""".*"""' is greedy, thus a line like """some1"""some2"""
3133
# is fully matched. In practice such lines are not used.
3234
# See cnt.py-new for an attempt solving these issues.
33-
types = [ ('C++', ['cc', 'tcc', 'hcc', 'cpp', 'cxx'], None, [], '//', '/\*', '\*/', 1),
34-
('C++Hdr', ['h', 'hpp', 'hxx'], None, [], '//', '/\*', '\*/', 1),
35-
('C', ['c'], None, [], '//', '/\*', '\*/', 1),
36-
('Cuda', ['cu'], None, [], '//', '/\*', '\*/', 1),
37-
('OpenCL', ['cl'], None, [], '//', '/\*', '\*/', 1),
35+
types = [ ('C++', ['cc', 'tcc', 'hcc', 'cpp', 'cxx'], None, [], '//', '/*', '*/', 1),
36+
('C++Hdr', ['h', 'hpp', 'hxx'], None, [], '//', '/*', '*/', 1),
37+
('C', ['c'], None, [], '//', '/*', '*/', 1),
38+
('Cuda', ['cu'], None, [], '//', '/*', '*/', 1),
39+
('OpenCL', ['cl'], None, [], '//', '/*', '*/', 1),
3840
('Fortran', ['f', 'for'], None, [], '*', '', '', 1),
3941
('Assembly', ['m', 'S'], None, [], '', '', '', 1),
4042
('Lisp', ['lisp'], None, [], '', '', '', 1),
4143
('SQL', ['sql'], None, [], '--', '', '', 1),
42-
('Flex', ['l', 'll'], None, [], '//', '/\*', '\*/', 1),
43-
('Bison', ['y', 'yy'], None, [], '//', '/\*', '\*/', 1),
44-
('Python', ['py', 'python'], None, [], '#', '"""', '"""', 1),
44+
('TaQL', ['taql'], None, [], '#', '', '', 1),
45+
('Flex', ['l', 'll'], None, [], '//', '/*', '*/', 1),
46+
('Bison', ['y', 'yy'], None, [], '//', '/*', '*/', 1),
47+
('Python', ['py', 'python', 'python3'], None, [], '#', '"""', '"""', 1),
4548
('Perl', ['pl', 'perl'], None, [], '#', '', '', 1),
4649
('test-run', ['run'], None, [], '','','', 0),
4750
('test-in', ['in'], re.compile('in_.*'), [], '','','', 0),
@@ -66,18 +69,18 @@ types = [ ('C++', ['cc', 'tcc', 'hcc', 'cpp', 'cxx'], None, [], '//', '/\*', '\*
6669

6770
def showTypes (verbose):
6871
for (type,exts,extre,filenms,comm,scomm,ecomm,ctyp) in types:
69-
print '%-24s code=%d' % (type,ctyp)
70-
print ' file name extensions: ', exts
72+
print ('%-24s code=%d' % (type,ctyp))
73+
print (' file name extensions: ', exts)
7174
if verbose:
7275
if not extre is None:
73-
print ' extension pattern: ', extre.pattern
76+
print (' extension pattern: ', extre.pattern)
7477
if len(filenms) > 0:
75-
print ' file names: ', filenms
78+
print (' file names: ', filenms)
7679
if len(comm) > 0:
77-
print ' comment marker: ', comm
80+
print (' comment marker: ', comm)
7881
if len(scomm) > 0:
79-
print ' start comment block: ', scomm
80-
print ' end comment block: ', ecomm
82+
print (' start comment block: ', scomm)
83+
print (' end comment block: ', ecomm)
8184

8285

8386
# Define regex for a line containing an alphanumeric character
@@ -96,14 +99,27 @@ def is_textfile(filename):
9699
CHUNKSIZE = 4096
97100
while 1:
98101
chunk = fin.read(CHUNKSIZE)
99-
if '\0' in chunk:
100-
return False
102+
if sys.version_info.major == 2:
103+
if '\0' in chunk:
104+
return False
105+
else:
106+
if 0 in chunk:
107+
return False
101108
if len(chunk) < CHUNKSIZE:
102109
break
103110
finally:
104111
fin.close()
105112
return True
106113

114+
def add_escape(str):
115+
''' Add an escape character for special regex characters'''
116+
out = ''
117+
for c in str:
118+
if c in '.*[]|':
119+
out += '\\'
120+
out += c
121+
return out
122+
107123

108124
# Return tuple with nr of files, nr of lines, nr of code lines,
109125
# nr of comment lines, nr of blank lines, and nr of header lines.
@@ -122,14 +138,16 @@ def countcodecomm (filename, linecomm, scomm='', ecomm='', basic=False):
122138
skipHeader = not basic
123139
blockComm = False
124140
if len(scomm) > 0:
125-
reComm2a = re.compile('\s*' + scomm + '\s*' + ecomm + '\s*')
126-
reComm2b = re.compile('\s*' + scomm + '(.*)' + ecomm + '\s*')
127-
reSComm = re.compile(scomm)
128-
reEComm = re.compile(ecomm)
129-
reTillSComm = re.compile('.*' + scomm + '\s*')
130-
reTillEComm = re.compile('.*' + ecomm + '\s*')
131-
reFromSComm = re.compile('\s*' + scomm + '.*')
132-
reFromEComm = re.compile('\s*' + ecomm + '.*')
141+
scomm_esc = add_escape(scomm)
142+
ecomm_esc = add_escape(scomm)
143+
reComm2a = re.compile('\s*' + scomm_esc + '\s*' + ecomm_esc + '\s*')
144+
reComm2b = re.compile('\s*' + scomm_esc + '(.*)' + ecomm_esc + '\s*')
145+
reSComm = re.compile(scomm_esc)
146+
reEComm = re.compile(ecomm_esc)
147+
reTillSComm = re.compile('.*' + scomm_esc + '\s*')
148+
reTillEComm = re.compile('.*' + ecomm_esc + '\s*')
149+
reFromSComm = re.compile('\s*' + scomm_esc + '.*')
150+
reFromEComm = re.compile('\s*' + ecomm_esc + '.*')
133151
# Loop over all lines in the file.
134152
for line in f:
135153
nline += 1
@@ -257,7 +275,7 @@ def countother(filename, basic, usecode):
257275
return ('unknown', 0, (1,0,0,0,0,0))
258276
return ('unknown', 0, (1,nline,0,0,nblank,0))
259277

260-
def countfiles(dirname, test, basic, ccperc, verbose, printlevel, level, usecode, dosum):
278+
def countfiles(dirname, test, basic, ccperc, verbose, printlevel, level, usecode, dosum, warn_unknown):
261279
sums = [{}, {}]
262280
for t in types:
263281
sums[0][t[0]] = [0,0,0,0,0,0]
@@ -279,7 +297,7 @@ def countfiles(dirname, test, basic, ccperc, verbose, printlevel, level, usecode
279297
# skip symlinks because casacore contains symlink to itself
280298
continue
281299
elif stat.S_ISDIR(mode):
282-
cnts = countfiles (ffile, test, basic, ccperc, verbose, printlevel, level+1, usecode, dosum)
300+
cnts = countfiles (ffile, test, basic, ccperc, verbose, printlevel, level+1, usecode, dosum, warn_unknown)
283301
for j in [0,1]:
284302
for t in types:
285303
for i in range(len(sums[j][t[0]])):
@@ -308,7 +326,8 @@ def countfiles(dirname, test, basic, ccperc, verbose, printlevel, level, usecode
308326
for i in range(len(cnt)):
309327
sums[inx][type][i] += cnt[i]
310328
if type == 'unknown':
311-
sys.stderr.write ('Unknown type: %s\n' % ffile)
329+
if warn_unknown:
330+
sys.stderr.write ('Unknown type: %s\n' % ffile)
312331
elif verbose:
313332
sys.stderr.write ('** %s\n' % ffile)
314333
printCount (sys.stderr, type, cnt, ccperc);
@@ -336,10 +355,10 @@ def countfiles(dirname, test, basic, ccperc, verbose, printlevel, level, usecode
336355
return sums
337356

338357
def testit():
339-
print countcodecomm ('/Users/diepen/testcnt1', '#')
340-
print countcodecomm ('/Users/diepen/testcnt2', '#', '"""', '"""')
341-
print countcodecomm ('/Users/diepen/testcnt1', '#', '', '', False)
342-
print countcodecomm ('/Users/diepen/testcnt2', '#', '"""', '"""', False)
358+
print (countcodecomm ('/Users/diepen/testcnt1', '#'))
359+
print (countcodecomm ('/Users/diepen/testcnt2', '#', '"""', '"""'))
360+
print (countcodecomm ('/Users/diepen/testcnt1', '#', '', '', False))
361+
print (countcodecomm ('/Users/diepen/testcnt2', '#', '"""', '"""', False))
343362

344363

345364
if __name__ == '__main__':
@@ -350,6 +369,7 @@ if __name__ == '__main__':
350369
parser.add_argument('-s', '--sum', help='only calculate and print the sum of all file types', action='store_true')
351370
parser.add_argument('-l', '--limitperc', help='limit to the nr of code and comment lines to determine percentages', action='store_true')
352371
parser.add_argument('-p', '--printlevel', type=int, default=0, help='first directory level to print (default 0 (=top))')
372+
parser.add_argument('-w', '--warn_unknown', help='warn if a file with an unknown type is found', action='store_true')
353373
parser.add_argument('-d', '--displaytypes', help='display the currently recognized file types (full info with -v)', action='store_true')
354374
parser.add_argument('-t', '--testinclude', help='do not count test directories separately', action='store_true')
355375
parser.add_argument('-v', '--verbose', help='print count for each source file', action='store_true')
@@ -358,26 +378,26 @@ if __name__ == '__main__':
358378
if len(sys.argv) == 1:
359379
#print 'Testing the script ...'
360380
#testit()
361-
print ''
362-
print 'countcode counts per known source file type the number of source lines in the'
363-
print ' files in the given directory and recursively in its subdirectories.'
364-
print 'It supports many file types. The type is recognized from the file name extension'
365-
print ' or the shebang script type. Use -s to see all supported types.'
366-
print 'The following line types are counted:'
367-
print ' code: pure code lines)'
368-
print ' comment: pure comment lines'
369-
print ' blank: empty lines or lines containing whitespace only'
370-
print ' header: the copyright header (leading comment lines)'
371-
print ' other: all other lines (e.g., single {, /*, etc.)'
372-
print 'Unless -b is given, a pure code or comment line has to contain an alphanumeric'
373-
print ' character; e.g., a single } does not count as code line.'
374-
print 'It calculates the percentage of code and comment lines in the total number of'
375-
print ' lines or (if -l is given) in the sum of code and comment lines.'
376-
print 'Unless -t is given, files in test directories are counted separately.'
377-
print 'Normal output is written on stdout; verbose on stderr.'
378-
print 'Files with an unknown type are reported on stderr.'
379-
print 'Note that -bt should give about the same results as a tool like cloc.'
380-
print ''
381+
print ('')
382+
print ('countcode counts per known source file type the number of source lines in the')
383+
print (' files in the given directory and recursively in its subdirectories.')
384+
print ('It supports many file types. The type is recognized from the file name extension')
385+
print (' or the shebang script type. Use -s to see all supported types.')
386+
print ('The following line types are counted:')
387+
print (' code: pure code lines)')
388+
print (' comment: pure comment lines')
389+
print (' blank: empty lines or lines containing whitespace only')
390+
print (' header: the copyright header (leading comment lines)')
391+
print (' other: all other lines (e.g., single {, /*, etc.)')
392+
print ('Unless -b is given, a pure code or comment line has to contain an alphanumeric')
393+
print (' character; e.g., a single } does not count as code line.')
394+
print ('It calculates the percentage of code and comment lines in the total number of')
395+
print (' lines or (if -l is given) in the sum of code and comment lines.')
396+
print ('Unless -t is given, files in test directories are counted separately.')
397+
print ('Normal output is written on stdout; verbose on stderr.')
398+
print ('Files with an unknown type are reported on stderr.')
399+
print ('Note that -bt should give about the same results as a tool like cloc.')
400+
print ('')
381401
parser.parse_args(['-h'])
382402
else:
383403
values = parser.parse_args(sys.argv[1:])
@@ -391,6 +411,6 @@ if __name__ == '__main__':
391411
dirname = dirname[:-1]
392412
sys.stdout.write ('%s Count %s test=%d basic=%d limitperc=%d code=%d\n'%(time.ctime(),dirname,test,values.basic,values.limitperc,values.code))
393413
printHeader()
394-
countfiles (dirname, test, values.basic, values.limitperc, values.verbose, values.printlevel, 0, values.code, values.sum)
414+
countfiles (dirname, test, values.basic, values.limitperc, values.verbose, values.printlevel, 0, values.code, values.sum, values.warn_unknown)
395415
printHeader()
396416
sys.stdout.write ('%s Count %s test=%d basic=%d limitperc=%d code=%d\n'%(time.ctime(),dirname,test,values.basic,values.limitperc,values.code))

casa/apps/plotmemory

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22

33
import numpy as np
4-
import matplotlib.pyplot as plt
4+
#import matplotlib.pyplot as plt
55
import argparse
66

77
parser = argparse.ArgumentParser(description = "Show plots of the output of watchmemory")

casa/apps/watchmemory

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
import time
44
import string
55
import sys
6-
import commands
6+
import os
77

88
def get_cpumem(pid):
99
# http://unix.stackexchange.com/questions/58539/top-and-ps-not-showing-the-same-cpu-result
1010
if pid.isdigit():
1111
cmd="ps -p "+str(pid)+" -o pid,%cpu,%mem,etime,cputime"
1212
else:
1313
cmd="ps -C "+pid+" -o pid,%cpu,%mem,etime,cputime"
14-
psoutput=commands.getoutput(cmd).split("\n")
15-
if len(psoutput)<2:
14+
psoutput=os.popen(cmd).read().split("\n")
15+
if len(psoutput) < 2:
1616
return None
17-
elif len(psoutput)>2:
17+
elif len(psoutput) > 2 and len(psoutput[2]) > 0:
1818
sys.stderr.write("warning: multiple processes called "+str(pid)+", picking first\n")
1919
d = psoutput[1].split()
2020

@@ -65,5 +65,5 @@ if __name__ == '__main__':
6565
sys.stdout.flush()
6666
time.sleep(1)
6767
except KeyboardInterrupt:
68-
print
68+
print('')
6969
exit(0)

0 commit comments

Comments
 (0)