Browse Source

Switch from print to logging

Dan Rapp 7 years ago
parent
commit
e5fdbf36f6

+ 3
- 2
gitstats/datacollector.py View File

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

+ 8
- 7
gitstats/gitdatacollector.py View File

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

+ 39
- 26
gitstats/gitstats.py View File

1
 #!/usr/bin/python
1
 #!/usr/bin/python
2
 # Copyright (c) 2007-2014 Heikki Hokkanen <hoxu@users.sf.net> & others (see doc/AUTHOR)
2
 # Copyright (c) 2007-2014 Heikki Hokkanen <hoxu@users.sf.net> & others (see doc/AUTHOR)
3
 # GPLv2 / GPLv3
3
 # GPLv2 / GPLv3
4
-import argparse
4
+import getopt
5
+import logging
5
 import os
6
 import os
6
 import sys
7
 import sys
7
 import time
8
 import time
8
 
9
 
10
+import multiprocessing_logging
11
+
9
 from .gitdatacollector import GitDataCollector
12
 from .gitdatacollector import GitDataCollector
10
 from .htmlreportcreator import HTMLReportCreator
13
 from .htmlreportcreator import HTMLReportCreator
11
 from .miscfuncs import getgnuplotversion
14
 from .miscfuncs import getgnuplotversion
24
     'linear_linestats': 1,
27
     'linear_linestats': 1,
25
     'project_name': '',
28
     'project_name': '',
26
     'processes': 8,
29
     'processes': 8,
27
-    'start_date': ''
30
+    'start_date': '',
31
+    'logging': logging.INFO
28
 }
32
 }
29
 
33
 
30
 class GitStats:
34
 class GitStats:
42
     """)
46
     """)
43
 
47
 
44
     def run(self):
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
             self._usage()
64
             self._usage()
47
             sys.exit(0)
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
         time_start = time.time()
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
         rundir = os.getcwd()
76
         rundir = os.getcwd()
63
 
77
 
66
         except OSError:
80
         except OSError:
67
             pass
81
             pass
68
         if not os.path.isdir(outputpath):
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
             sys.exit(1)
84
             sys.exit(1)
71
 
85
 
72
         if not getgnuplotversion():
86
         if not getgnuplotversion():
73
-            print('gnuplot not found')
87
+            logging.error('gnuplot not found')
74
             sys.exit(1)
88
             sys.exit(1)
75
 
89
 
76
-        print(f'Output path: {outputpath}')
90
+        logging.info(f'Output path: {outputpath}')
77
         cachefile = os.path.join(outputpath, 'gitstats.cache')
91
         cachefile = os.path.join(outputpath, 'gitstats.cache')
78
 
92
 
79
         data = GitDataCollector(conf)
93
         data = GitDataCollector(conf)
80
         data.loadCache(cachefile)
94
         data.loadCache(cachefile)
81
 
95
 
82
         for gitpath in paths:
96
         for gitpath in paths:
83
-            print(f'Git path: {gitpath}')
97
+            logging.info(f'Git path: {gitpath}')
84
 
98
 
85
             prevdir = os.getcwd()
99
             prevdir = os.getcwd()
86
             os.chdir(gitpath)
100
             os.chdir(gitpath)
87
 
101
 
88
-            print('Collecting data...')
102
+            logging.info('Collecting data...')
89
             data.collect(gitpath)
103
             data.collect(gitpath)
90
 
104
 
91
             os.chdir(prevdir)
105
             os.chdir(prevdir)
92
 
106
 
93
-        print('Refining data...')
107
+            logging.info('Refining data...')
94
         data.saveCache(cachefile)
108
         data.saveCache(cachefile)
95
         data.refine()
109
         data.refine()
96
 
110
 
97
         os.chdir(rundir)
111
         os.chdir(rundir)
98
 
112
 
99
-        print('Generating report...')
113
+        logging.info('Generating report...')
100
         report = HTMLReportCreator(conf)
114
         report = HTMLReportCreator(conf)
101
         report.create(data, outputpath)
115
         report.create(data, outputpath)
102
 
116
 
103
         time_end = time.time()
117
         time_end = time.time()
104
         calculated_exectime_internal = time_end - time_start
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
 if __name__ == '__main__':
127
 if __name__ == '__main__':

+ 3
- 2
gitstats/htmlreportcreator.py View File

1
 import datetime
1
 import datetime
2
 import glob
2
 import glob
3
+import logging
3
 import pkg_resources
4
 import pkg_resources
4
 import os
5
 import os
5
 import shutil
6
 import shutil
36
             if os.path.exists(resource_file):
37
             if os.path.exists(resource_file):
37
                 shutil.copyfile(resource_file, os.path.join(path, resource))
38
                 shutil.copyfile(resource_file, os.path.join(path, resource))
38
             else:
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
         f = open(path + "/index.html", 'w')
42
         f = open(path + "/index.html", 'w')
42
         format = '%Y-%m-%d %H:%M:%S'
43
         format = '%Y-%m-%d %H:%M:%S'
500
         self.createGraphs(path)
501
         self.createGraphs(path)
501
 
502
 
502
     def createGraphs(self, path):
503
     def createGraphs(self, path):
503
-        print('Generating graphs...')
504
+        logging.info('Generating graphs...')
504
 
505
 
505
         # hour of day
506
         # hour of day
506
         f = open(path + '/hour_of_day.plot', 'w')
507
         f = open(path + '/hour_of_day.plot', 'w')

+ 3
- 10
gitstats/miscfuncs.py View File

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

+ 1
- 0
setup.py View File

37
         'setuptools>=18.0'
37
         'setuptools>=18.0'
38
     ],
38
     ],
39
     install_requires=[
39
     install_requires=[
40
+        'multiprocessing_logging'
40
     ],
41
     ],
41
 
42
 
42
     extras_require={
43
     extras_require={