Przeglądaj źródła

update to python3

MFTECH 1 rok temu
rodzic
commit
065640ef4b
1 zmienionych plików z 37 dodań i 36 usunięć
  1. 37
    36
      gitstats

+ 37
- 36
gitstats Wyświetl plik

@@ -1,4 +1,4 @@
1
-#!/usr/bin/env python2
1
+#!/usr/bin/env python3
2 2
 # Copyright (c) 2007-2014 Heikki Hokkanen <hoxu@users.sf.net> & others (see doc/AUTHOR)
3 3
 # GPLv2 / GPLv3
4 4
 import datetime
@@ -54,21 +54,21 @@ def getpipeoutput(cmds, quiet = False):
54 54
 	global exectime_external
55 55
 	start = time.time()
56 56
 	if not quiet and ON_LINUX and os.isatty(1):
57
-		print '>> ' + ' | '.join(cmds),
57
+		print('>> ' + ' | '.join(cmds))
58 58
 		sys.stdout.flush()
59 59
 	p = subprocess.Popen(cmds[0], stdout = subprocess.PIPE, shell = True)
60 60
 	processes=[p]
61 61
 	for x in cmds[1:]:
62 62
 		p = subprocess.Popen(x, stdin = p.stdout, stdout = subprocess.PIPE, shell = True)
63 63
 		processes.append(p)
64
-	output = p.communicate()[0]
64
+	output = (p.communicate()[0]).decode("utf-8")
65 65
 	for p in processes:
66 66
 		p.wait()
67 67
 	end = time.time()
68 68
 	if not quiet:
69 69
 		if ON_LINUX and os.isatty(1):
70
-			print '\r',
71
-		print '[%.5f] >> %s' % (end - start, ' | '.join(cmds))
70
+			print('\r')
71
+		print('[%.5f] >> %s' % (end - start, ' | '.join(cmds)))
72 72
 	exectime_external += (end - start)
73 73
 	return output.rstrip('\n')
74 74
 
@@ -86,11 +86,11 @@ def getcommitrange(defaultrange = 'HEAD', end_only = False):
86 86
 	return defaultrange
87 87
 
88 88
 def getkeyssortedbyvalues(dict):
89
-	return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
89
+	return list(map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items()))))
90 90
 
91 91
 # dict['author'] = { 'commits': 512 } - ...key(dict, 'commits')
92 92
 def getkeyssortedbyvaluekey(d, key):
93
-	return map(lambda el : el[1], sorted(map(lambda el : (d[el][key], el), d.keys())))
93
+	return list(map(lambda el : el[1], sorted(map(lambda el : (d[el][key], el), d.keys()))))
94 94
 
95 95
 def getstatsummarycounts(line):
96 96
 	numbers = re.findall('\d+', line)
@@ -207,7 +207,7 @@ class DataCollector:
207 207
 	def loadCache(self, cachefile):
208 208
 		if not os.path.exists(cachefile):
209 209
 			return
210
-		print 'Loading cache...'
210
+		print( 'Loading cache...')
211 211
 		f = open(cachefile, 'rb')
212 212
 		try:
213 213
 			self.cache = pickle.loads(zlib.decompress(f.read()))
@@ -269,7 +269,7 @@ class DataCollector:
269 269
 	##
270 270
 	# Save cacheable data
271 271
 	def saveCache(self, cachefile):
272
-		print 'Saving cache...'
272
+		print( 'Saving cache...')
273 273
 		tempfile = cachefile + '.tmp'
274 274
 		f = open(tempfile, 'wb')
275 275
 		#pickle.dump(self.cache, f)
@@ -308,7 +308,7 @@ class GitDataCollector(DataCollector):
308 308
 				self.tags[tag] = { 'stamp': stamp, 'hash' : hash, 'date' : datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d'), 'commits': 0, 'authors': {} }
309 309
 
310 310
 		# collect info on tags, starting from latest
311
-		tags_sorted_by_date_desc = map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), self.tags.items()))))
311
+		tags_sorted_by_date_desc = list(map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), self.tags.items())))))
312 312
 		prev = None
313 313
 		for tag in reversed(tags_sorted_by_date_desc):
314 314
 			cmd = 'git shortlog -s "%s"' % tag
@@ -474,7 +474,7 @@ class GitDataCollector(DataCollector):
474 474
 			try:
475 475
 				self.files_by_stamp[int(stamp)] = int(files)
476 476
 			except ValueError:
477
-				print 'Warning: failed to parse line "%s"' % line
477
+				print('Warning: failed to parse line "%s"' % line)
478 478
 
479 479
 		# extensions and size of files
480 480
 		lines = getpipeoutput(['git ls-tree -r -l -z %s' % getcommitrange('HEAD', end_only = True)]).split('\000')
@@ -563,21 +563,21 @@ class GitDataCollector(DataCollector):
563 563
 
564 564
 						files, inserted, deleted = 0, 0, 0
565 565
 					except ValueError:
566
-						print 'Warning: unexpected line "%s"' % line
566
+						print('Warning: unexpected line "%s"' % line)
567 567
 				else:
568
-					print 'Warning: unexpected line "%s"' % line
568
+					print('Warning: unexpected line "%s"' % line)
569 569
 			else:
570 570
 				numbers = getstatsummarycounts(line)
571 571
 
572 572
 				if len(numbers) == 3:
573
-					(files, inserted, deleted) = map(lambda el : int(el), numbers)
573
+					(files, inserted, deleted) = list(map(lambda el : int(el), numbers))
574 574
 					total_lines += inserted
575 575
 					total_lines -= deleted
576 576
 					self.total_lines_added += inserted
577 577
 					self.total_lines_removed += deleted
578 578
 
579 579
 				else:
580
-					print 'Warning: failed to handle line "%s"' % line
580
+					print( 'Warning: failed to handle line "%s"' % line)
581 581
 					(files, inserted, deleted) = (0, 0, 0)
582 582
 				#self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted }
583 583
 		self.total_lines += total_lines
@@ -622,16 +622,16 @@ class GitDataCollector(DataCollector):
622 622
 						self.changes_by_date_by_author[stamp][author]['commits'] = self.authors[author]['commits']
623 623
 						files, inserted, deleted = 0, 0, 0
624 624
 					except ValueError:
625
-						print 'Warning: unexpected line "%s"' % line
625
+						print( 'Warning: unexpected line "%s"' % line)
626 626
 				else:
627
-					print 'Warning: unexpected line "%s"' % line
627
+					print( 'Warning: unexpected line "%s"' % line)
628 628
 			else:
629 629
 				numbers = getstatsummarycounts(line);
630 630
 
631 631
 				if len(numbers) == 3:
632
-					(files, inserted, deleted) = map(lambda el : int(el), numbers)
632
+					(files, inserted, deleted) = list(map(lambda el : int(el), numbers))
633 633
 				else:
634
-					print 'Warning: failed to handle line "%s"' % line
634
+					print( 'Warning: failed to handle line "%s"' % line)
635 635
 					(files, inserted, deleted) = (0, 0, 0)
636 636
 	
637 637
 	def refine(self):
@@ -744,7 +744,7 @@ class HTMLReportCreator(ReportCreator):
744 744
 					shutil.copyfile(src, path + '/' + file)
745 745
 					break
746 746
 			else:
747
-				print 'Warning: "%s" not found, so not copied (searched: %s)' % (file, basedirs)
747
+				print( 'Warning: "%s" not found, so not copied (searched: %s)' % (file, basedirs))
748 748
 
749 749
 		f = open(path + "/index.html", 'w')
750 750
 		format = '%Y-%m-%d %H:%M:%S'
@@ -1153,7 +1153,7 @@ class HTMLReportCreator(ReportCreator):
1153 1153
 		f.write('<table class="tags">')
1154 1154
 		f.write('<tr><th>Name</th><th>Date</th><th>Commits</th><th>Authors</th></tr>')
1155 1155
 		# sort the tags by date desc
1156
-		tags_sorted_by_date_desc = map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), data.tags.items()))))
1156
+		tags_sorted_by_date_desc = list(map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), data.tags.items())))))
1157 1157
 		for tag in tags_sorted_by_date_desc:
1158 1158
 			authorinfo = []
1159 1159
 			self.authors_by_commits = getkeyssortedbyvalues(data.tags[tag]['authors'])
@@ -1168,7 +1168,7 @@ class HTMLReportCreator(ReportCreator):
1168 1168
 		self.createGraphs(path)
1169 1169
 	
1170 1170
 	def createGraphs(self, path):
1171
-		print 'Generating graphs...'
1171
+		print( 'Generating graphs...')
1172 1172
 
1173 1173
 		# hour of day
1174 1174
 		f = open(path + '/hour_of_day.plot', 'w')
@@ -1370,7 +1370,7 @@ plot """
1370 1370
 		for f in files:
1371 1371
 			out = getpipeoutput([gnuplot_cmd + ' "%s"' % f])
1372 1372
 			if len(out) > 0:
1373
-				print out
1373
+				print( out)
1374 1374
 
1375 1375
 	def printHeader(self, f, title = ''):
1376 1376
 		f.write(
@@ -1401,7 +1401,7 @@ plot """
1401 1401
 """)
1402 1402
 		
1403 1403
 def usage():
1404
-	print """
1404
+	text = """
1405 1405
 Usage: gitstats [options] <gitpath..> <outputpath>
1406 1406
 
1407 1407
 Options:
@@ -1412,6 +1412,7 @@ Default config values:
1412 1412
 
1413 1413
 Please see the manual page for more details.
1414 1414
 """ % conf
1415
+	print(text)
1415 1416
 
1416 1417
 
1417 1418
 class GitStats:
@@ -1442,48 +1443,48 @@ class GitStats:
1442 1443
 		except OSError:
1443 1444
 			pass
1444 1445
 		if not os.path.isdir(outputpath):
1445
-			print 'FATAL: Output path is not a directory or does not exist'
1446
+			print( 'FATAL: Output path is not a directory or does not exist')
1446 1447
 			sys.exit(1)
1447 1448
 
1448 1449
 		if not getgnuplotversion():
1449
-			print 'gnuplot not found'
1450
+			print( 'gnuplot not found')
1450 1451
 			sys.exit(1)
1451 1452
 
1452
-		print 'Output path: %s' % outputpath
1453
+		print( 'Output path: %s' % outputpath)
1453 1454
 		cachefile = os.path.join(outputpath, 'gitstats.cache')
1454 1455
 
1455 1456
 		data = GitDataCollector()
1456 1457
 		data.loadCache(cachefile)
1457 1458
 
1458 1459
 		for gitpath in args[0:-1]:
1459
-			print 'Git path: %s' % gitpath
1460
+			print( 'Git path: %s' % gitpath)
1460 1461
 
1461 1462
 			prevdir = os.getcwd()
1462 1463
 			os.chdir(gitpath)
1463 1464
 
1464
-			print 'Collecting data...'
1465
+			print( 'Collecting data...')
1465 1466
 			data.collect(gitpath)
1466 1467
 
1467 1468
 			os.chdir(prevdir)
1468 1469
 
1469
-		print 'Refining data...'
1470
+		print( 'Refining data...')
1470 1471
 		data.saveCache(cachefile)
1471 1472
 		data.refine()
1472 1473
 
1473 1474
 		os.chdir(rundir)
1474 1475
 
1475
-		print 'Generating report...'
1476
+		print( 'Generating report...')
1476 1477
 		report = HTMLReportCreator()
1477 1478
 		report.create(data, outputpath)
1478 1479
 
1479 1480
 		time_end = time.time()
1480 1481
 		exectime_internal = time_end - time_start
1481
-		print 'Execution time %.5f secs, %.5f secs (%.2f %%) in external commands)' % (exectime_internal, exectime_external, (100.0 * exectime_external) / exectime_internal)
1482
+		print( 'Execution time %.5f secs, %.5f secs (%.2f %%) in external commands)' % (exectime_internal, exectime_external, (100.0 * exectime_external) / exectime_internal))
1482 1483
 		if sys.stdin.isatty():
1483
-			print 'You may now run:'
1484
-			print
1485
-			print '   sensible-browser \'%s\'' % os.path.join(outputpath, 'index.html').replace("'", "'\\''")
1486
-			print
1484
+			print( 'You may now run:')
1485
+			print()
1486
+			print( '   sensible-browser \'%s\'' % os.path.join(outputpath, 'index.html').replace("'", "'\\''"))
1487
+			print()
1487 1488
 
1488 1489
 if __name__=='__main__':
1489 1490
 	g = GitStats()