Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

#!/usr/bin/env python 

# coding: utf-8 

 

""" 

This is the 'keepcool' module. 

 

This module is a git repository inspection package 

that aims to avoid developper's burnout. 

""" 

 

from datetime import datetime, time 

from optparse import OptionParser 

from git import Git 

 

 

class Date(object): 

""" 

Class that handle date from a unix timestamp. 

 

This class is used to determine if a date 

belongs or not to the working hours 

 

Please note that datetime is an immuatable object : 

it is complicated to inherit from it... 

""" 

 

def __init__(self, unix_timestamp): 

""" 

It is complicated to inherit from datetime 

wich is an immuatable object. 

""" 

self.date = datetime.fromtimestamp(unix_timestamp) 

 

def __repr__(self): 

return self.date.strftime("Date(%s)") 

 

def __str__(self): 

ret = self.date.strftime("%A %Y-%m-%d %H:%M:%S") 

 

extra = [] 

if not self.belongs_to_workin_hours(): 

 

if self.belongs_to_week_end(): 

extra.append("week-end") 

 

if self.belongs_to_early_morning(): 

extra.append("early morning") 

 

if self.belongs_to_late_evening(): 

extra.append("late in the evening") 

 

ret = ret + " -> " + str(extra) 

 

return ret 

 

def belongs_to_week_end(self): 

""" 

Return True if date on saterday or sunday 

""" 

return self.date.weekday() >= 5 

 

def belongs_to_early_morning(self): 

""" 

Return True if date is before 8:00 am 

""" 

return self.date.time() < time(hour=8) 

 

def belongs_to_late_evening(self): 

""" 

Return True if date is after 20:00 

""" 

return self.date.time() > time(hour=20) 

 

def belongs_to_workin_hours(self): 

""" 

Return TRUE if date belongs to workings hours. 

 

Working hours include 8 am to 8 pm from monday to friday. 

""" 

ret = True 

if (self.belongs_to_week_end() 

or self.belongs_to_early_morning() 

or self.belongs_to_late_evening()): 

ret = False 

return ret 

 

 

class Commit(Date): 

""" 

This class is used to store GIT each commit object 

""" 

def __init__(self, uuid, user, unix_timestamp): 

Date.__init__(self, unix_timestamp) 

self.uuid = uuid 

self.user = user 

 

def __repr__(self): 

return "Commit('%s', %s, %s)" % ( 

self.uuid, repr(self.user), self.date.strftime("%s")) 

 

def __str__(self): 

return Date.__str__(self) 

 

 

class User(object): 

""" 

This class is used to store GIT committers 

""" 

def __init__(self, name): 

self.name = name 

self.nb_in = 0 

self.nb_out = 0 

self.commits = {} 

 

def __repr__(self): 

return "User('%s')" % self.name 

 

def __str__(self): 

# still not very well understand 

return (self.name).encode('utf-8') 

 

def add_commit(self, commit): 

""" 

Add a commit object if not already there. 

 

Throw KeyError if already there. 

""" 

if commit.uuid in self.commits: 

raise IndexError( 

"'%s' commit's key is already recorded for '%s'" % ( 

commit.uuid, self.name)) 

self.commits[commit.uuid] = commit 

 

def compute_status(self): 

""" 

Compute commits done by the user 

""" 

self.nb_in = 0 

self.nb_out = 0 

 

for key in self.commits: 

if self.commits[key].belongs_to_workin_hours(): 

self.nb_in = self.nb_in + 1 

else: 

self.nb_out = self.nb_out + 1 

 

def print_status(self, trace=False): 

""" 

Display commits done by the user 

""" 

 

nb_sum = self.nb_in + self.nb_out 

ratio = 100 * self.nb_out / nb_sum if nb_sum else 0 

ret = "" 

 

if trace is True: 

 

def get_date(item): 

"""sort commits by date""" 

return item[1].date 

 

ret = ret + ".......................................\n" 

items = sorted(self.commits.items(), key=get_date) 

 

for asso in items: 

ret = ret + str(asso[1]) + "\n" 

 

ret = ret + "%25s: %4i / %4i%%\n" % (str(self), nb_sum, ratio) 

return ret 

 

 

class KeepCool(object): 

""" is the module's class. 

 

Unique namespace to import : 

>>> from keepcoll import KeepCool 

""" 

 

def __init__(self, repo, **kwargs): 

self.repo = repo 

self.options = kwargs 

self.users = {} 

self.commits = {} 

self.nb_in = 0 

self.nb_out = 0 

 

def __repr__(self): 

return "KeepCool('%s')" % self.repo 

 

def parse_logs(self, **kwargs): 

""" 

Load logs into objects. 

""" 

git = Git(self.repo) 

log_args = ["--encoding=UTF-8", "--pretty=%H; %cN; %ct"] 

only_user = None 

after = None 

before = None 

 

# get context 

if 'user' in kwargs: 

only_user = kwargs['user'] 

elif 'user' in self.options: 

only_user = self.options['user'] 

 

if 'after' in kwargs: 

after = kwargs['after'] 

elif 'after' in self.options: 

after = self.options['after'] 

 

if 'before' in kwargs: 

before = kwargs['before'] 

elif 'before' in self.options: 

before = self.options['before'] 

 

# add command line options 

if only_user is not None: 

log_args.append("--committer=" + only_user) 

if after is not None: 

log_args.append("--after=" + after) 

if before is not None: 

log_args.append("--before=" + before) 

 

# query, parse and record git logs 

string = git.log(*log_args) 

for log in string.splitlines(): 

(uuid, name, unix_timestamp_string) = log.split(';') 

self.add_commit(uuid, name, int(unix_timestamp_string)) 

 

def get_user(self, name): 

""" 

Find and return an already recorded user. 

 

Return None if not already there 

""" 

ret = None 

if name in self.users: 

ret = self.users[name] 

return ret 

 

def add_user(self, name): 

""" 

Add a new user object if not already there. 

 

Return the user object. 

""" 

ret = self.get_user(name) 

if ret is None: 

ret = self.users[name] = User(name) 

return ret 

 

def add_commit(self, uuid, name, unix_timestamp): 

""" 

Add a new commit object if not already there. 

 

Return the commit objet, 

or throw KeyError if already there. 

""" 

if uuid in self.commits: 

raise IndexError("'%s' commit's key is already recorded" % uuid) 

 

user = self.add_user(name) 

ret = self.commits[uuid] = Commit(uuid, user, unix_timestamp) 

user.add_commit(ret) 

return ret 

 

def compute_status(self): 

""" 

Compute commits done by users 

""" 

self.nb_in = 0 

self.nb_out = 0 

 

for key in self.users: 

self.users[key].compute_status() 

self.nb_in = self.nb_in + self.users[key].nb_in 

self.nb_out = self.nb_out + self.users[key].nb_out 

 

def print_status(self, **kwargs): 

""" 

Display commits done by all user 

""" 

 

def get_name(item): 

"""sort users by name""" 

return item[1].name 

 

def get_sum(item): 

"""sort users by sum""" 

return item[1].nb_in + item[1].nb_out 

 

def get_ratio(item): 

"""sort users by ratio""" 

nb_sum = item[1].nb_in + item[1].nb_out 

return 100 * item[1].nb_out / nb_sum if nb_sum else 0 

 

trace = False 

sort_field = 'name' 

only_user = None 

 

# get context 

if 'trace' in kwargs: 

trace = kwargs['trace'] 

elif 'trace' in self.options: 

trace = self.options['trace'] 

 

if 'sort_field' in kwargs: 

sort_field = kwargs['sort_field'] 

elif 'sort_field' in self.options: 

sort_field = self.options['sort_field'] 

 

if 'user' in kwargs: 

only_user = kwargs['user'] 

elif 'user' in self.options: 

only_user = self.options['user'] 

 

nb_sum = self.nb_in + self.nb_out 

ratio = 100 * self.nb_out / nb_sum if nb_sum else 0 

ret = "" 

 

ret = ret + "---------------------------------------\n" 

ret = ret + "%25s: %4s / %4s\n" % ( 

self.repo + "'s commiter", "sum", "ratio") 

ret = ret + "---------------------------------------\n" 

 

# sort users 

if sort_field == 'sum': 

key_func = get_sum 

elif sort_field == 'ratio': 

key_func = get_ratio 

else: 

key_func = get_name 

 

# loop on user 

items = sorted(self.users.items(), key=key_func) 

for asso in items: 

ret = ret + asso[1].print_status(trace) 

 

ret = ret + "---------------------------------------\n" 

 

if only_user is None: 

ret = ret + "%25s: %4i / %4i%%\n" % ("all", nb_sum, ratio) 

ret = ret + "---------------------------------------\n" 

 

return ret 

 

 

def main(): 

""" 

Entry point if called as an executable 

 

Note: optparse is deprecated, but fails to use argparse 

within Debian Stretch using official packages. 

""" 

parser = OptionParser(usage="usage: %prog [options] [git-path]", 

version="%prog 0.1") 

 

# get options and parameters 

parser.add_option( 

"-v", "--verbose", action="store_true", dest="trace", 

default="False", help="print commits too") 

parser.add_option( 

"-u", "--user", dest="user", default=None, 

help="limit search to a USER", metavar="USER") 

parser.add_option( 

"-a", "--after", dest="after", default=None, 

help="limit search from DATE", metavar="DATE") 

parser.add_option( 

"-b", "--before", dest="before", default=None, 

help="limit search to DATE", metavar="DATE") 

parser.add_option( 

"-s", "--sort", dest="sort_field", default='name', 

help="sort on FIELD: 'name' (default), 'sum', or 'ratio'", 

metavar="FIELD") 

 

(options, args) = parser.parse_args() 

repo = args[0] if len(args) > 0 else "repo" 

 

# call main API 

keepcool = KeepCool(repo, user=options.user) 

keepcool.parse_logs(after=options.after, before=options.before) 

keepcool.compute_status() 

print keepcool.print_status(sort_field=options.sort_field, 

trace=options.trace) 

 

 

if __name__ == "__main__": 

main()