Parcourir la source

Switch from print to logging

Dan Rapp il y a 7 ans
Parent
révision
e5fdbf36f6

+ 3
- 2
gitstats/datacollector.py Voir le fichier

@@ -1,4 +1,5 @@
1 1
 import datetime
2
+import logging
2 3
 import os
3 4
 import pickle
4 5
 import time
@@ -81,7 +82,7 @@ class DataCollector:
81 82
     def loadCache(self, cachefile):
82 83
         if not os.path.exists(cachefile):
83 84
             return
84
-        print('Loading cache...')
85
+        logging.info('Loading cache...')
85 86
         f = open(cachefile, 'rb')
86 87
         try:
87 88
             self.cache = pickle.loads(zlib.decompress(f.read()))
@@ -143,7 +144,7 @@ class DataCollector:
143 144
     ##
144 145
     # Save cacheable data
145 146
     def saveCache(self, cachefile):
146
-        print('Saving cache...')
147
+        logging.info('Saving cache...')
147 148
         tempfile = cachefile + '.tmp'
148 149
         f = open(tempfile, 'wb')
149 150
         # pickle.dump(self.cache, f)

+ 8
- 7
gitstats/gitdatacollector.py Voir le fichier

@@ -1,4 +1,5 @@
1 1
 import datetime
2
+import logging
2 3
 import re
3 4
 import os
4 5
 
@@ -209,7 +210,7 @@ class GitDataCollector(DataCollector):
209 210
             try:
210 211
                 self.files_by_stamp[int(stamp)] = int(files)
211 212
             except ValueError:
212
-                print(f'Warning: failed to parse line "{line}"')
213
+                logging.warning(f'Failed to parse line "{line}"')
213 214
 
214 215
         # extensions and size of files
215 216
         lines = getpipeoutput(['git ls-tree -r -l -z %s' % getcommitrange(self.conf, 'HEAD', end_only=True)]).split('\000')
@@ -298,9 +299,9 @@ class GitDataCollector(DataCollector):
298 299
 
299 300
                         files, inserted, deleted = 0, 0, 0
300 301
                     except ValueError:
301
-                        print(f'Warning: unexpected line "{line}')
302
+                        logging.warning(f'unexpected line "{line}')
302 303
                 else:
303
-                    print(f'Warning: unexpected line "{line}')
304
+                    logging.warning(f'unexpected line "{line}')
304 305
             else:
305 306
                 numbers = getstatsummarycounts(line)
306 307
 
@@ -312,7 +313,7 @@ class GitDataCollector(DataCollector):
312 313
                     self.total_lines_removed += deleted
313 314
 
314 315
                 else:
315
-                    print(f'Warning: failed to handle line "{line}"')
316
+                    logging.warning(f'Failed to handle line "{line}"')
316 317
                     (files, inserted, deleted) = (0, 0, 0)
317 318
             # self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted }
318 319
         self.total_lines += total_lines
@@ -359,16 +360,16 @@ class GitDataCollector(DataCollector):
359 360
                         self.changes_by_date_by_author[stamp][author]['commits'] = self.authors[author]['commits']
360 361
                         files, inserted, deleted = 0, 0, 0
361 362
                     except ValueError:
362
-                        print(f'Warning: unexpected line "{line}')
363
+                        logging.warning(f'unexpected line "{line}')
363 364
                 else:
364
-                    print(f'Warning: unexpected line "{line}')
365
+                    logging.warning(f'unexpected line "{line}')
365 366
             else:
366 367
                 numbers = getstatsummarycounts(line)
367 368
 
368 369
                 if len(numbers) == 3:
369 370
                     (files, inserted, deleted) = map(lambda el: int(el), numbers)
370 371
                 else:
371
-                    print(f'Warning: failed to handle line "{line}"')
372
+                    logging.warning(f'Failed to handle line "{line}"')
372 373
                     (files, inserted, deleted) = (0, 0, 0)
373 374
 
374 375
     def refine(self):

+ 39
- 26
gitstats/gitstats.py Voir le fichier

@@ -1,11 +1,14 @@
1 1
 #!/usr/bin/python
2 2
 # Copyright (c) 2007-2014 Heikki Hokkanen <hoxu@users.sf.net> & others (see doc/AUTHOR)
3 3
 # GPLv2 / GPLv3
4
-import argparse
4
+import getopt
5
+import logging
5 6
 import os
6 7
 import sys
7 8
 import time
8 9
 
10
+import multiprocessing_logging
11
+
9 12
 from .gitdatacollector import GitDataCollector
10 13
 from .htmlreportcreator import HTMLReportCreator
11 14
 from .miscfuncs import getgnuplotversion
@@ -24,7 +27,8 @@ conf = {
24 27
     'linear_linestats': 1,
25 28
     'project_name': '',
26 29
     'processes': 8,
27
-    'start_date': ''
30
+    'start_date': '',
31
+    'logging': logging.INFO
28 32
 }
29 33
 
30 34
 class GitStats:
@@ -42,22 +46,32 @@ class GitStats:
42 46
     """)
43 47
 
44 48
     def run(self):
45
-        if len(sys.argv) < 2:
49
+        optlist, args = getopt.getopt(sys.argv[1:], 'hc:', ["help"])
50
+        for o, v in optlist:
51
+            if o == '-c':
52
+                key, value = v.split('=', 1)
53
+                if key not in conf:
54
+                    raise KeyError('no such key "%s" in config' % key)
55
+                if isinstance(conf[key], int):
56
+                    conf[key] = int(value)
57
+                else:
58
+                    conf[key] = value
59
+            elif o in ('-h', '--help'):
60
+                self._usage()
61
+                sys.exit()
62
+
63
+        if len(args) < 2:
46 64
             self._usage()
47 65
             sys.exit(0)
48 66
 
49
-        parser = argparse.ArgumentParser(description='GitStats')
50
-#        parser.add_argument('-c', '--config', dest='config')
51
-
52
-        (args, remaining_args) = parser.parse_known_args()
53
-#        if args.config:
54
-#            self.conf.load(args.config)
67
+        outputpath = os.path.abspath(args[-1])
68
+        paths = args[0:-1]
69
+        outputpath = os.path.abspath(outputpath)
55 70
 
71
+        logging.basicConfig(level=conf['logging'], format='%(message)s')
72
+        multiprocessing_logging.install_mp_handler()
56 73
         time_start = time.time()
57 74
 
58
-        outputpath = remaining_args[-1]
59
-        paths = remaining_args[0:-1]
60
-        outputpath = os.path.abspath(outputpath)
61 75
 
62 76
         rundir = os.getcwd()
63 77
 
@@ -66,49 +80,48 @@ class GitStats:
66 80
         except OSError:
67 81
             pass
68 82
         if not os.path.isdir(outputpath):
69
-            print('FATAL: Output path is not a directory or does not exist')
83
+            logging.fatal('Output path is not a directory or does not exist')
70 84
             sys.exit(1)
71 85
 
72 86
         if not getgnuplotversion():
73
-            print('gnuplot not found')
87
+            logging.error('gnuplot not found')
74 88
             sys.exit(1)
75 89
 
76
-        print(f'Output path: {outputpath}')
90
+        logging.info(f'Output path: {outputpath}')
77 91
         cachefile = os.path.join(outputpath, 'gitstats.cache')
78 92
 
79 93
         data = GitDataCollector(conf)
80 94
         data.loadCache(cachefile)
81 95
 
82 96
         for gitpath in paths:
83
-            print(f'Git path: {gitpath}')
97
+            logging.info(f'Git path: {gitpath}')
84 98
 
85 99
             prevdir = os.getcwd()
86 100
             os.chdir(gitpath)
87 101
 
88
-            print('Collecting data...')
102
+            logging.info('Collecting data...')
89 103
             data.collect(gitpath)
90 104
 
91 105
             os.chdir(prevdir)
92 106
 
93
-        print('Refining data...')
107
+            logging.info('Refining data...')
94 108
         data.saveCache(cachefile)
95 109
         data.refine()
96 110
 
97 111
         os.chdir(rundir)
98 112
 
99
-        print('Generating report...')
113
+        logging.info('Generating report...')
100 114
         report = HTMLReportCreator(conf)
101 115
         report.create(data, outputpath)
102 116
 
103 117
         time_end = time.time()
104 118
         calculated_exectime_internal = time_end - time_start
105
-        print(
106
-            f'Execution time {calculated_exectime_internal} secs, {exectime_external} secs ({(100.0 * exectime_external) / calculated_exectime_internal}%) in external commands)')
107
-        if sys.stdin.isatty():
108
-            print('You may now run:')
109
-            print()
110
-            print('   sensible-browser \'%s\'' % os.path.join(outputpath, 'index.html').replace("'", "'\\''"))
111
-            print()
119
+        logging.info(f'Execution time {calculated_exectime_internal} secs, {exectime_external} secs ({(100.0 * exectime_external) / calculated_exectime_internal}%) in external commands)')
120
+
121
+        print('You may now run:')
122
+        print()
123
+        print('   sensible-browser \'%s\'' % os.path.join(outputpath, 'index.html').replace("'", "'\\''"))
124
+        print()
112 125
 
113 126
 
114 127
 if __name__ == '__main__':

+ 3
- 2
gitstats/htmlreportcreator.py Voir le fichier

@@ -1,5 +1,6 @@
1 1
 import datetime
2 2
 import glob
3
+import logging
3 4
 import pkg_resources
4 5
 import os
5 6
 import shutil
@@ -36,7 +37,7 @@ class HTMLReportCreator(ReportCreator):
36 37
             if os.path.exists(resource_file):
37 38
                 shutil.copyfile(resource_file, os.path.join(path, resource))
38 39
             else:
39
-                print(f'Warning: "{resource}" not found, so not copied')
40
+                logging.warning(f'"{resource}" not found, so not copied')
40 41
 
41 42
         f = open(path + "/index.html", 'w')
42 43
         format = '%Y-%m-%d %H:%M:%S'
@@ -500,7 +501,7 @@ class HTMLReportCreator(ReportCreator):
500 501
         self.createGraphs(path)
501 502
 
502 503
     def createGraphs(self, path):
503
-        print('Generating graphs...')
504
+        logging.info('Generating graphs...')
504 505
 
505 506
         # hour of day
506 507
         f = open(path + '/hour_of_day.plot', 'w')

+ 3
- 10
gitstats/miscfuncs.py Voir le fichier

@@ -1,14 +1,11 @@
1
+import logging
1 2
 import os
2
-import platform
3 3
 import re
4 4
 import subprocess
5
-import sys
6 5
 import time
7 6
 
8 7
 os.environ['LC_ALL'] = 'C'
9 8
 
10
-ON_LINUX = (platform.system() == 'Linux')
11
-
12 9
 # By default, gnuplot is searched from path, but can be overridden with the
13 10
 # environment variable "GNUPLOT"
14 11
 gnuplot_cmd = 'gnuplot'
@@ -16,10 +13,8 @@ if 'GNUPLOT' in os.environ:
16 13
     gnuplot_cmd = os.environ['GNUPLOT']
17 14
 
18 15
 
19
-def getpipeoutput(cmds, quiet=False):
16
+def getpipeoutput(cmds):
20 17
     start = time.time()
21
-    if not quiet and ON_LINUX and os.isatty(1):
22
-        print('>> ' + ' | '.join(cmds), sys.stdout.flush())
23 18
     p = subprocess.Popen(cmds[0], stdout=subprocess.PIPE, shell=True)
24 19
     processes = [p]
25 20
     for x in cmds[1:]:
@@ -29,9 +24,7 @@ def getpipeoutput(cmds, quiet=False):
29 24
     for p in processes:
30 25
         p.wait()
31 26
     end = time.time()
32
-    if not quiet:
33
-        if ON_LINUX and os.isatty(1):
34
-            print(f'\r[{end - start}] >> {" | ".join(cmds)}')
27
+    logging.info(f'\r[{end - start:.4f}] >> {" | ".join(cmds)}')
35 28
     return output.rstrip('\n')
36 29
 
37 30
 

+ 1
- 0
setup.py Voir le fichier

@@ -37,6 +37,7 @@ setup(
37 37
         'setuptools>=18.0'
38 38
     ],
39 39
     install_requires=[
40
+        'multiprocessing_logging'
40 41
     ],
41 42
 
42 43
     extras_require={