Kiln » Kiln Extensions
Clone URL:  
Pushed to 2 repositories · View In Graph Contained in tip

Update extensions for Kiln 2.7 series

Changeset 67d19149537f

Parent f0fe708a7d75

by Profile picture of User 12Benjamin Pollack <benjamin@fogcreek.com>

Changes to 5 files · Browse files at 67d19149537f Showing diff from parent f0fe708a7d75 Diff from another changeset...

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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
 
 
 
 
 
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
 
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
 '''bfiles utility code: must not import other modules in this package.'''    import os  import errno  import inspect  import shutil  import stat  import hashlib    from mercurial import cmdutil, dirstate, httpconnection, match as match_, \   url as url_, util  from mercurial.i18n import _    try:   from mercurial import scmutil  except ImportError:   pass    shortname = '.kbf'  longname = 'kilnbfiles'      # -- Portability wrappers ----------------------------------------------    if 'subrepos' in inspect.getargspec(dirstate.dirstate.status)[0]:   # for Mercurial >= 1.5   def dirstate_walk(dirstate, matcher, unknown=False, ignored=False):   return dirstate.walk(matcher, [], unknown, ignored)  else:   # for Mercurial <= 1.4   def dirstate_walk(dirstate, matcher, unknown=False, ignored=False):   return dirstate.walk(matcher, unknown, ignored)    def repo_add(repo, list):   try:   # Mercurial <= 1.5   add = repo.add   except AttributeError:   # Mercurial >= 1.6   add = repo[None].add   return add(list)    def repo_remove(repo, list, unlink=False):   try:   # Mercurial <= 1.5   remove = repo.remove   except AttributeError:   # Mercurial >= 1.6   try:   # Mercurial <= 1.8   remove = repo[None].remove   except AttributeError:   # Mercurial >= 1.9   def remove(list, unlink):   wlock = repo.wlock()   try:   if unlink:   for f in list:   try:   util.unlinkpath(repo.wjoin(f))   except OSError, inst:   if inst.errno != errno.ENOENT:   raise   repo[None].forget(list)   finally:   wlock.release()     return remove(list, unlink=unlink)    def repo_forget(repo, list):   try:   # Mercurial <= 1.5   forget = repo.forget   except AttributeError:   # Mercurial >= 1.6   forget = repo[None].forget   return forget(list)    def dirstate_normaldirty(dirstate, file):   try:   normaldirty = dirstate.normaldirty   except AttributeError:   # Mercurial >= 1.6: HAAAACK: I should not be using normaldirty()   # (now called otherparent()), and dirstate in 1.6 prevents me   # from doing so. So reimplement it here until I figure out the   # right fix.   def normaldirty(f):   dirstate._dirty = True   dirstate._addpath(f)   dirstate._map[f] = ('n', 0, -2, -1)   if f in dirstate._copymap:   del dirstate._copymap[f]   normaldirty(file)    def findoutgoing(repo, remote, force):   # First attempt is for Mercurial <= 1.5 second is for >= 1.6   try:   return repo.findoutgoing(remote)   except AttributeError:   from mercurial import discovery   try:   # Mercurial <= 1.8   return discovery.findoutgoing(repo, remote, force=force)   except AttributeError:   # Mercurial >= 1.9   common, _anyinc, _heads = discovery.findcommonincoming(repo,   remote, force=force)   return repo.changelog.findmissing(common)    # -- Private worker functions ------------------------------------------    if os.name == 'nt':   from mercurial import win32   try:   linkfn = win32.oslink   except:   linkfn = win32.os_link  else:   linkfn = os.link    def link(src, dest):   try:   linkfn(src, dest)   except OSError:   # If hardlinks fail fall back on copy   shutil.copyfile(src, dest)   os.chmod(dest, os.stat(src).st_mode)    def systemcachepath(ui, hash):   path = ui.config(longname, 'systemcache', None)   if path:   path = os.path.join(path, hash)   else:   if os.name == 'nt':   path = os.path.join(os.getenv('LOCALAPPDATA') or \   os.getenv('APPDATA'), longname, hash)   elif os.name == 'posix':   path = os.path.join(os.getenv('HOME'), '.' + longname, hash)   else:   raise util.Abort(_('Unknown operating system: %s\n') % os.name)   return path    def insystemcache(ui, hash):   return os.path.exists(systemcachepath(ui, hash))    def findfile(repo, hash):   if incache(repo, hash):   repo.ui.note(_('Found %s in cache\n') % hash)   return cachepath(repo, hash)   if insystemcache(repo.ui, hash):   repo.ui.note(_('Found %s in system cache\n') % hash)   return systemcachepath(repo.ui, hash)   return None    def openbfdirstate(ui, repo):   '''   Return a dirstate object that tracks big files: i.e. its root is the   repo root, but it is saved in .hg/bfiles/dirstate.   '''   admin = repo.join(longname)   try:   # Mercurial >= 1.9   opener = scmutil.opener(admin)   except ImportError:   # Mercurial <= 1.8   opener = util.opener(admin)   if hasattr(repo.dirstate, '_validate'):   bfdirstate = dirstate.dirstate(opener, ui, repo.root,   repo.dirstate._validate)   else:   bfdirstate = dirstate.dirstate(opener, ui, repo.root)     # If the bfiles dirstate does not exist, populate and create it. This   # ensures that we create it on the first meaningful bfiles operation in   # a new clone. It also gives us an easy way to forcibly rebuild bfiles   # state:   # rm .hg/bfiles/dirstate && hg bfstatus   # Or even, if things are really messed up:   # rm -rf .hg/bfiles && hg bfstatus   # (although that can lose data, e.g. pending big file revisions in   # .hg/bfiles/{pending,committed}).   if not os.path.exists(os.path.join(admin, 'dirstate')):   util.makedirs(admin)   matcher = getstandinmatcher(repo)   for standin in dirstate_walk(repo.dirstate, matcher):   bigfile = splitstandin(standin)   hash = readstandin(repo, standin)   try:   curhash = hashfile(bigfile)   except IOError, err:   if err.errno == errno.ENOENT:   dirstate_normaldirty(bfdirstate, bigfile)   else:   raise   else:   if curhash == hash:   bfdirstate.normal(unixpath(bigfile))   else:   dirstate_normaldirty(bfdirstate, bigfile)     bfdirstate.write()     return bfdirstate    def bfdirstate_status(bfdirstate, repo, rev):   wlock = repo.wlock()   try:   match = match_.always(repo.root, repo.getcwd())   s = bfdirstate.status(match, [], False, False, False)   unsure, modified, added, removed, missing, unknown, ignored, clean = s   for bfile in unsure:   if repo[rev][standin(bfile)].data().strip() != \   hashfile(repo.wjoin(bfile)):   modified.append(bfile)   else:   clean.append(bfile)   bfdirstate.normal(unixpath(bfile))   bfdirstate.write()   finally:   wlock.release()   return (modified, added, removed, missing, unknown, ignored, clean)    def listbfiles(repo, rev=None, matcher=None):   '''list big files in the working copy or specified changeset'''     if matcher is None:   matcher = getstandinmatcher(repo)     bfiles = []   if rev is not None:   cctx = repo[rev]   for standin in cctx.walk(matcher):   filename = splitstandin(standin)   bfiles.append(filename)   else:   for standin in sorted(dirstate_walk(repo.dirstate, matcher)):   filename = splitstandin(standin)   bfiles.append(filename)   return bfiles    def incache(repo, hash):   return os.path.exists(cachepath(repo, hash))    def createdir(dir):   if not os.path.exists(dir):   os.makedirs(dir)    def cachepath(repo, hash):   return repo.join(os.path.join(longname, hash))    def copytocache(repo, rev, file, uploaded=False):   hash = readstandin(repo, standin(file))   if incache(repo, hash):   return   copytocacheabsolute(repo, repo.wjoin(file), hash)    def copytocacheabsolute(repo, file, hash):   createdir(os.path.dirname(cachepath(repo, hash)))   if insystemcache(repo.ui, hash):   link(systemcachepath(repo.ui, hash), cachepath(repo, hash))   else:   shutil.copyfile(file, cachepath(repo, hash))   os.chmod(cachepath(repo, hash), os.stat(file).st_mode)   createdir(os.path.dirname(systemcachepath(repo.ui, hash)))   link(cachepath(repo, hash), systemcachepath(repo.ui, hash))    def getstandinmatcher(repo, pats=[], opts={}):   '''Return a match object that applies pats to <repo>/.kbf.'''   standindir = repo.pathto(shortname)   if pats:   # patterns supplied: search .hgbfiles relative to current dir   cwd = repo.getcwd()   if os.path.isabs(cwd):   # cwd is an absolute path for hg -R <reponame>   # work relative to the repository root in this case   cwd = ''   pats = [os.path.join(standindir, cwd, pat) for pat in pats]   elif os.path.isdir(standindir):   # no patterns: relative to repo root   pats = [standindir]   else:   # no patterns and no .hgbfiles dir: return matcher that matches nothing   match = match_.match(repo.root, None, [], exact=True)   match.matchfn = lambda f: False   return match   return getmatcher(repo, pats, opts, showbad=False)    def getmatcher(repo, pats=[], opts={}, showbad=True):   '''Wrapper around scmutil.match() that adds showbad: if false, neuter   the match object\'s bad() method so it does not print any warnings   about missing files or directories.'''   try:   # Mercurial >= 1.9   match = scmutil.match(repo[None], pats, opts)   except ImportError:   # Mercurial <= 1.8   match = cmdutil.match(repo, pats, opts)     if not showbad:   match.bad = lambda f, msg: None   return match    def composestandinmatcher(repo, rmatcher):   '''Return a matcher that accepts standins corresponding to the files   accepted by rmatcher. Pass the list of files in the matcher as the   paths specified by the user.'''   smatcher = getstandinmatcher(repo, rmatcher.files())   isstandin = smatcher.matchfn   def composed_matchfn(f):   return isstandin(f) and rmatcher.matchfn(splitstandin(f))   smatcher.matchfn = composed_matchfn     return smatcher    def standin(filename):   '''Return the repo-relative path to the standin for the specified big   file.'''   # Notes:   # 1) Most callers want an absolute path, but _create_standin() needs   # it repo-relative so bfadd() can pass it to repo_add(). So leave   # it up to the caller to use repo.wjoin() to get an absolute path.   # 2) Join with '/' because that's what dirstate always uses, even on   # Windows. Change existing separator to '/' first in case we are   # passed filenames from an external source (like the command line).   return shortname + '/' + filename.replace(os.sep, '/')    def isstandin(filename):   '''Return true if filename is a big file standin. filename must   be in Mercurial\'s internal form (slash-separated).'''   return filename.startswith(shortname + '/')    def splitstandin(filename):   # Split on / because that's what dirstate always uses, even on Windows.   # Change local separator to / first just in case we are passed filenames   # from an external source (like the command line).   bits = filename.replace(os.sep, '/').split('/', 1)   if len(bits) == 2 and bits[0] == shortname:   return bits[1]   else:   return None    def updatestandin(repo, standin):   file = repo.wjoin(splitstandin(standin))   if os.path.exists(file):   hash = hashfile(file)   executable = getexecutable(file)   writestandin(repo, standin, hash, executable)    def readstandin(repo, standin):   '''read hex hash from <repo.root>/<standin>'''   return readhash(repo.wjoin(standin))    def writestandin(repo, standin, hash, executable):   '''write hhash to <repo.root>/<standin>'''   writehash(hash, repo.wjoin(standin), executable)    def copyandhash(instream, outfile):   '''Read bytes from instream (iterable) and write them to outfile,   computing the SHA-1 hash of the data along the way. Close outfile   when done and return the binary hash.'''   hasher = util.sha1('')   for data in instream:   hasher.update(data)   outfile.write(data)     # Blecch: closing a file that somebody else opened is rude and   # wrong. But it's so darn convenient and practical! After all,   # outfile was opened just to copy and hash.   outfile.close()     return hasher.digest()    def hashrepofile(repo, file):   return hashfile(repo.wjoin(file))    def hashfile(file):   if not os.path.exists(file):   return ''   hasher = util.sha1('')   fd = open(file, 'rb')   for data in blockstream(fd):   hasher.update(data)   fd.close()   return hasher.hexdigest()    class limitreader(object):   def __init__(self, f, limit):   self.f = f   self.limit = limit     def read(self, length):   if self.limit == 0:   return ''   length = length > self.limit and self.limit or length   self.limit -= length   return self.f.read(length)     def close(self):   pass    def blockstream(infile, blocksize=128 * 1024):   """Generator that yields blocks of data from infile and closes infile."""   while True:   data = infile.read(blocksize)   if not data:   break   yield data   # Same blecch as above.   infile.close()    def readhash(filename):   rfile = open(filename, 'rb')   hash = rfile.read(40)   rfile.close()   if len(hash) < 40:   raise util.Abort(_('bad hash in \'%s\' (only %d bytes long)')   % (filename, len(hash)))   return hash    def writehash(hash, filename, executable):   util.makedirs(os.path.dirname(filename))   if os.path.exists(filename):   os.unlink(filename)   if os.name == 'posix':   # Yuck: on Unix, go through open(2) to ensure that the caller's mode is   # filtered by umask() in the kernel, where it's supposed to be done.   wfile = os.fdopen(os.open(filename, os.O_WRONLY|os.O_CREAT,   getmode(executable)), 'wb')   else:   # But on Windows, use open() directly, since passing mode='wb' to   # os.fdopen() does not work. (Python bug?)   wfile = open(filename, 'wb')     try:   wfile.write(hash)   wfile.write('\n')   finally:   wfile.close()    def getexecutable(filename):   mode = os.stat(filename).st_mode   return (mode & stat.S_IXUSR) and (mode & stat.S_IXGRP) and (mode & \   stat.S_IXOTH)    def getmode(executable):   if executable:   return 0755   else:   return 0644    def urljoin(first, second, *arg):   def join(left, right):   if not left.endswith('/'):   left += '/'   if right.startswith('/'):   right = right[1:]   return left + right     url = join(first, second)   for a in arg:   url = join(url, a)   return url    def hexsha1(data):   """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like   object data"""   h = hashlib.sha1()   for chunk in util.filechunkiter(data):   h.update(chunk)   return h.hexdigest()    def httpsendfile(ui, filename):   try:   # Mercurial >= 1.9 - return httpconnection.httpsendfile(ui, filename, 'rb') + sendfile = httpconnection.httpsendfile(ui, filename, 'rb') + if getattr(sendfile, '__len__', None) is None: + # Mercurial 1.9.3 removes httpsendfile's __len__. Hack it back in. + setattr(sendfile.__class__, '__len__', lambda self: self.length) + return sendfile   except ImportError:   if 'ui' in inspect.getargspec(url_.httpsendfile.__init__)[0]:   # Mercurial == 1.8   return url_.httpsendfile(ui, filename, 'rb')   else:   # Mercurial <= 1.7   return url_.httpsendfile(filename, 'rb')    # Convert a path to a unix style path. This is used to give a  # canonical path to the bfdirstate.  def unixpath(path):   return os.path.normpath(path).replace(os.sep, '/')    def iskbfilesrepo(repo):   return 'kbfiles' in repo.requirements and any_('.kbf/' in f[0] for f in   repo.store.datafiles())    def any_(gen):   for x in gen:   if x:   return True   return False
Change 1 of 1 Show Entire File big-push.py Stacked
 
19
20
21
22
23
24
25
26
27
28
29
30
31
 
32
33
34
 
19
20
21
 
 
 
 
 
22
23
24
25
26
27
28
29
30
@@ -19,16 +19,12 @@
 from mercurial import cmdutil, commands, hg, extensions  from mercurial.i18n import _   -try: - from mercurial import discovery -except ImportError: - pass -  max_push_size = 1000    def findoutgoing(repo, other):   try:   # Mercurial 1.6 through 1.8 + from mercurial import discovery   return discovery.findoutgoing(repo, other, force=False)   except AttributeError:   # Mercurial 1.9 and higher
Change 1 of 14 Show Entire File kiln.py Stacked
 
34
35
36
 
37
38
 
39
40
41
 
42
43
44
45
46
 
 
47
48
49
 
137
138
139
 
 
140
141
142
 
144
145
146
147
148
149
 
 
150
151
152
 
198
199
200
201
 
202
203
204
 
222
223
224
225
 
 
226
227
228
 
355
356
357
358
359
 
 
 
 
 
360
361
362
 
378
379
380
381
382
383
 
 
 
 
 
 
384
385
386
 
389
390
391
392
 
393
394
395
 
454
455
456
457
458
459
460
461
462
 
463
464
465
466
467
468
469
470
471
472
 
473
474
475
 
491
492
493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
495
496
 
499
500
501
502
 
503
504
505
 
506
507
508
 
565
566
567
 
 
 
568
569
570
 
574
575
576
577
578
579
 
 
 
580
581
582
583
 
584
585
586
 
609
610
611
 
612
613
 
614
 
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
 
48
49
50
51
52
 
140
141
142
143
144
145
146
147
 
149
150
151
 
 
 
152
153
154
155
156
 
202
203
204
 
205
206
207
208
 
226
227
228
 
229
230
231
232
233
 
360
361
362
 
 
363
364
365
366
367
368
369
370
 
386
387
388
 
 
 
389
390
391
392
393
394
395
396
397
 
400
401
402
 
403
404
405
406
 
465
466
467
 
 
 
 
 
 
468
469
470
 
 
 
 
 
 
 
 
471
472
473
474
 
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
 
723
724
725
 
726
727
728
 
729
730
731
732
 
789
790
791
792
793
794
795
796
797
 
801
802
803
 
804
805
806
807
808
809
810
811
812
813
814
815
816
 
839
840
841
842
843
 
844
845
@@ -34,16 +34,19 @@
 following line in the [kiln] section of your hgrc:   ignoreversion = X.Y.Z  ''' +import httplib  import os  import re +import unicodedata  import urllib  import urllib2  import sys +import traceback    from cookielib import MozillaCookieJar  from hashlib import md5 -from mercurial import commands, demandimport, extensions, hg, httprepo, \ - localrepo, match, util +from mercurial import commands, cmdutil, demandimport, error, extensions, hg, \ + httprepo, localrepo, match, util  from mercurial import ui as hgui  from mercurial import url as hgurl  from mercurial.error import RepoError @@ -137,6 +140,8 @@
  '''   url = baseurl + urlsuffix   data = urllib.urlencode(params, doseq=True) + ui.debug(_('calling %s\n') % url, + _(' with parameters %s\n') % params)   try:   if post:   fd = urllib2.urlopen(url, data) @@ -144,9 +149,8 @@
  fd = urllib2.urlopen(url + '?' + data)   obj = json.load(fd)   except Exception: - raise util.Abort(_('Path guessing requires Fog Creek Kiln 2.0. If you' - ' are running Kiln 2.0 and continue to experience' - ' problems, please contact Fog Creek Software.')) + ui.debug(_('kiln: traceback: %s\n') % traceback.format_exc()) + raise util.Abort(_('kiln: an error occurred while trying to reach %s\n') % url)     if isinstance(obj, dict) and 'errors' in obj:   if 'token' in params and obj['errors'][0]['codeError'] == 'InvalidToken': @@ -198,7 +202,7 @@
   def _upgrade(ui, repo):   ext_dir = os.path.dirname(os.path.abspath(__file__)) - ui.debug('kiln: checking for extensions upgrade for %s\n' % ext_dir) + ui.debug(_('kiln: checking for extensions upgrade for %s\n') % ext_dir)     try:   r = localrepo.localrepository(hgui.ui(), ext_dir) @@ -222,7 +226,8 @@
  else:   ui.write(_('complete\n'))   except Exception, e: - ui.debug(_('kiln: error updating Kiln Extensions: %s\n') % e) + ui.debug(_('kiln: error updating extensions: %s\n') % e) + ui.debug(_('kiln: traceback: %s\n') % traceback.format_exc())    def is_dest_a_path(ui, dest):   paths = ui.configitems('paths') @@ -355,8 +360,11 @@
  audit_path('hgrc')   audit_path('hgrc.backup')   base = repo.opener.base - util.copyfile(os.path.join(base, 'hgrc'), - os.path.join(base, 'hgrc.backup')) + + hgrc, backup = [os.path.join(base, x) for x in 'hgrc', 'hgrc.backup'] + if os.path.exists(hgrc): + util.copyfile(hgrc, backup) +   ui.setconfig('paths', path, value)     try: @@ -378,9 +386,12 @@
  audit_path('hgrc')   audit_path('hgrc.backup')   base = repo.opener.base - if os.path.exists(os.path.join(base, 'hgrc')): - util.copyfile(os.path.join(base, 'hgrc.backup'), - os.path.join(base, 'hgrc')) + + hgrc, backup = [os.path.join(base, x) for x in 'hgrc', 'hgrc.backup'] + if os.path.exists(backup): + util.copyfile(backup, hgrc) + else: + os.remove(hgrc)    def guess_kilnpath(orig, ui, repo, dest=None, **opts):   if not dest: @@ -389,7 +400,7 @@
  if os.path.exists(dest) or is_dest_a_path(ui, dest) or is_dest_a_scheme(ui, dest):   return orig(ui, repo, dest, **opts)   else: - targets = get_targets(repo); + targets = get_targets(repo)   matches = []   prefixmatches = []   @@ -454,22 +465,10 @@
  kilnschemes = repo.ui.configitems('kiln_scheme')   for scheme in kilnschemes:   url = scheme[1] - if url.lower().find('/kiln/') != -1: - baseurl = url[:url.lower().find('/kiln/') + len("/kiln/")] - elif url.lower().find('kilnhg.com/') != -1: - baseurl = url[:url.lower().find('kilnhg.com/') + len("kilnhg.com/")] - else: - continue + baseurl = get_api_url(url)     tails = get_tails(repo) - - token = check_kilnapi_token(repo.ui, baseurl) - if not token: - token = check_kilnauth_token(repo.ui, baseurl) - add_kilnapi_token(repo.ui, baseurl, token) - if not token: - token = login(repo.ui, baseurl) - add_kilnapi_token(repo.ui, baseurl, token) + token = get_token(repo.ui, baseurl)     # We have an token at this point   params = dict(revTails=tails, token=token) @@ -491,6 +490,231 @@
  alias_text = ''   repo.ui.write(' %s/%s/%s/%s%s\n' % (target[0], target[1], target[2], target[3], alias_text))   +def get_token(ui, url): + '''Checks for an existing API token. If none, returns a new valid token.''' + token = check_kilnapi_token(ui, url) + if not token: + token = check_kilnauth_token(ui, url) + add_kilnapi_token(ui, url, token) + if not token: + token = login(ui, url) + add_kilnapi_token(ui, url, token) + return token + +def get_api_url(url): + '''Given a URL, returns the URL of the Kiln installation.''' + if '/kiln/' in url.lower(): + baseurl = url[:url.lower().find('/kiln/') + 6] + elif 'kilnhg.com/' in url.lower(): + baseurl = url[:url.lower().find('kilnhg.com/') + 11] + else: + baseurl = url + return baseurl + +class HTTPNoRedirectHandler(urllib2.HTTPRedirectHandler): + def http_error_302(self, req, fp, code, msg, headers): + # Doesn't allow multiple redirects so repo alias URLs will not + # eventually get redirected to the unhelpful login page + return fp + + http_error_301 = http_error_303 = http_error_307 = http_error_302 + +def get_repo_record(repo, url, token=None): + '''Returns a Kiln repository record that corresponds to the given repo.''' + baseurl = get_api_url(url) + if not token: + token = get_token(repo.ui, baseurl) + + try: + data = urllib.urlencode({ 'token': token }, doseq=True) + opener = urllib2.build_opener(HTTPNoRedirectHandler) + urllib2.install_opener(opener) + fd = urllib2.urlopen(url + '?' + data) + + # Get redirected URL + if 'location' in fd.headers: + url = fd.headers.getheaders('location')[0] + elif 'uri' in fd.headers: + url = fd.headers.getheaders('uri')[0] + except HTTPError as e: + raise util.Abort(_('Invalid URL: %s' % url)) + + def find_slug(slug, l, attr=None): + if not l: + return None + for candidate in l.get(attr) if attr else l: + if candidate['sSlug'] == slug or (slug == 'Group' and candidate['sSlug'] == ''): + return candidate + return None + + paths = url.split('/') + kiln_projects = call_api(repo.ui, baseurl, 'Api/1.0/Project/', dict(token=token)) + project, group, repo = paths[-3:] + project = find_slug(project, kiln_projects) + group = find_slug(group, project, 'repoGroups') + repo = find_slug(repo, group, 'repos') + return repo + +def new_branch(repo, url, name): + '''Creates a new, decentralized branch off of the specified repo.''' + baseurl = get_api_url(url) + token = get_token(repo.ui, baseurl) + kiln_repo = get_repo_record(repo, url, token) + params = {'sName': name, + 'ixRepoGroup': kiln_repo['ixRepoGroup'], + 'ixParent': kiln_repo['ixRepo'], + 'fCentral': False, + 'sDefaultPermission': 'inherit', + 'token': token} + repo.ui.write('branching from %s' % url) + return call_api(repo.ui, baseurl, 'Api/1.0/Repo/Create', params, post=True) + +def normalize_user(s): + '''Takes a Unicode string and returns an ASCII string.''' + return unicodedata.normalize('NFKD', s).encode('ASCII', 'ignore') + +def encode_out(s): + '''Takes a Unicode string and returns a string encoded for output.''' + return s.encode(sys.stdout.encoding, 'ignore') + +def record_base(ui, repo, node, **kwargs): + '''Stores the first changeset committed in the repo UI so we do not need to expensively recalculate.''' + repo.ui.setconfig('kiln', 'node', node) + +def walk(repo, revs): + '''Returns revisions in repo specified by the string revs''' + return cmdutil.walkchangerevs(repo, match.always(repo.root, None), {'rev': [revs.encode('ascii', 'ignore')]}, lambda *args: None) + +def print_list(ui, l, header): + '''Prints a list l to ui using list notation, with header being the first line''' + ui.write(_('%s\n' % header)) + for item in l: + ui.write(_('- %s\n') % item) + +def wrap_push(orig, ui, repo, dest=None, **opts): + '''Wraps `hg push' so a review will be created after path guessing and a successful push.''' + guess_kilnpath(orig, ui, repo, dest, **opts) + review(ui, repo, dest, opts) + +def add_unique_reviewer(ui, reviewer, reviewers, name_to_ix, ix_to_name): + '''Adds a reviewer to reviewers if it is not already added. Otherwise, print an error.''' + if name_to_ix[reviewer] in reviewers: + ui.write(_('user already added: %s\n') % ix_to_name[name_to_ix[reviewer]]) + else: + reviewers.append(name_to_ix[reviewer]) + print_list(ui, [ix_to_name[r] for r in reviewers], 'reviewers:') + +def review(ui, repo, pats, opts): + '''Associates the pushed changesets with a new or existing Kiln review.''' + if not opts['review'] or not repo.ui.config('kiln', 'node'): + return + + url = repo.ui.expandpath(pats[0] if pats else 'default-push', default='default') + baseurl = get_api_url(url) + token = get_token(ui, baseurl) + kiln_repo = get_repo_record(repo, url, token) + + review_lists = call_api(repo.ui, baseurl, 'Api/1.0/Reviews', dict(token=token)) + reviews = filter(lambda r: r['ixRepo'] == kiln_repo['ixRepo'], review_lists['reviewsOpenedByMe'] + review_lists['reviewsReviewedByMe']) + reviews = sorted(reviews, key=lambda r: r['ixReview']) + choices = [] + ui.write(_('\n')) + for r in reviews: + ui.write(encode_out(_('%d - %s\n') % (r['ixReview'], r['sTitle']))) + choices.append(str(r['ixReview'])) + + choices.extend(['n', 'q', '?']) + while True: + choice = ui.prompt(_('add to review? [nq?]')).lower() + if choice not in choices: + ui.write(_('unrecognized response\n\n')) + elif choice == 'q': + return + elif choice == '?': + exist_review = _(' - enter an existing review number to add changeset(s).\n') if reviews else '' + ui.write(exist_review) + ui.write(_('n - new, create a new review.\n'), + _('q - quit, do not associate changeset(s).\n'), + _('? - display help.\n\n')) + else: + # Create new review or associate changeset(s) to existing + break + + node = repo.ui.config('kiln', 'node') + heads = opts['rev'] + # If no specified revisions to push, default to getting revision numbers (not ancestors/descendants) between node and tip. + sets = ['%s::%s' % (node, r) for r in heads] if heads else ['%s:tip' % node] + revset = ' or '.join(sets) + revs = [r.hex() for r in walk(repo, revset)] + + if choice == 'n': + # Associate changeset(s) with a new review. + people_records = call_api(repo.ui, baseurl, 'Api/1.0/Person', dict(token=token)) + # If two user names normalize to the same string, then name_to_ix will only store the second person. This will + # also affect user input if the user enters the first user's unstored name, then the user will add the wrong + # reviewer. If this edge case becomes an issue, I wish thee happy pondering. + name_to_ix = dict([(normalize_user(p['sName']).lower(), p['ixPerson']) for p in people_records]) + ix_to_name = dict([(p['ixPerson'], encode_out(p['sName'])) for p in people_records]) + + reviewers = [] + while True: + reviewer = ui.prompt(_('\nchoose reviewer(s) [dlq?]'), default='') + reviewer = normalize_user(unicode(reviewer, sys.stdin.encoding)).lower() + if reviewer == 'q': + return + elif reviewer == '?': + ui.write(_(' - type a user\'s full name to add that user. a partial name displays\n'), + _(' a list of matching users.\n'), + _('d - done, create a new review.\n'), + _('l - list, list current reviewers.\n'), + _('q - quit, do not create a new review.\n'), + _('? - display help.\n')) + elif reviewer == 'l': + if reviewers: + print_list(ui, [ix_to_name[r] for r in reviewers], 'reviewers:') + else: + ui.write(_('no users selected.\n')) + elif reviewer == 'd': + if reviewers: + break + else: + ui.write(_('no users selected.\n')) + elif reviewer in name_to_ix.keys(): + add_unique_reviewer(ui, reviewer, reviewers, name_to_ix, ix_to_name) + else: + options = filter(lambda name: reviewer in name, name_to_ix.keys()) + options = [ix_to_name[name_to_ix[name]] for name in options] + options = sorted(options, key=lambda n: n.lower()) + if options: + if len(options) == 1: + # If one user matches search, just add him/her + option = normalize_user(unicode(options[0], sys.stdout.encoding)).lower() + add_unique_reviewer(ui, option, reviewers, name_to_ix, ix_to_name) + else: + print_list(ui, options, 'user names (%d) that match:' % len(options)) + else: + ui.write(_('no matching users.\n')) + + params = { + 'token': token, + 'ixRepo': kiln_repo['ixRepo'], + 'revs': revs, + 'ixReviewers': reviewers, + 'sTitle': '(Multiple changesets)' if len(revs) > 1 else repo[revs[0]].description(), + 'sDescription': 'Review created from push.' + } + r = call_api(repo.ui, baseurl, 'Api/1.0/Review/Create', params, post=True) + ui.write(_('new review created: %s\n' % urljoin(baseurl, 'Review', str(r['ixReview'])))) + else: + # Associate changeset(s) with an existing review. + params = { + 'token': token, + 'ixBug': int(choice), + 'revs': revs + } + call_api(repo.ui, baseurl, 'Api/1.0/Repo/%d/CaseAssociation/Create' % kiln_repo['ixRepo'], params, post=True) + ui.write(_('updated review: %s\n' % urljoin(baseurl, 'Review', choice))) +  def dummy_command(ui, repo, dest=None, **opts):   '''dummy command to pass to guess_path() for hg kiln   @@ -499,10 +723,10 @@
  '''   return opts['path'] != dest and dest or None   -def kiln(ui, repo, *pats, **opts): +def kiln(ui, repo, **opts):   '''show the relevant page of the repository in Kiln   - This command allows you to navigate straight the Kiln page for a + This command allows you to navigate straight to the Kiln page for a   repository, including directly to settings, file annotation, and   file & changeset viewing.   @@ -565,6 +789,9 @@
  if opts['targets']:   default = False   display_targets(repo) + if opts['new_branch']: + default = False + new_branch(repo, url, opts['new_branch'])   if opts['logout']:   default = False   delete_kilnapi_tokens() @@ -574,13 +801,16 @@
   def uisetup(ui):   extensions.wrapcommand(commands.table, 'outgoing', guess_kilnpath) - extensions.wrapcommand(commands.table, 'push', guess_kilnpath)   extensions.wrapcommand(commands.table, 'pull', guess_kilnpath)   extensions.wrapcommand(commands.table, 'incoming', guess_kilnpath) + push_cmd = extensions.wrapcommand(commands.table, 'push', wrap_push) + # Add --review as a valid flag to push's command table + push_cmd[1].extend([('', 'review', None, 'associate changesets with Kiln review')])    def reposetup(ui, repo):   if issubclass(repo.__class__, httprepo.httprepository):   _upgradecheck(ui, repo) + repo.ui.setconfig('hooks', 'outgoing.kilnreview', 'python:kiln.record_base')    def extsetup(ui):   try: @@ -609,6 +839,7 @@
  ('p', 'path', '', _('select which Kiln branch of the repository to use')),   ('r', 'rev', [], _('view the specified changeset in Kiln')),   ('t', 'targets', None, _('view the repository\'s targets')), + ('n', 'new-branch', '', _('asynchronously create a new branch from the current repository')),   ('', 'logout', None, _('log out of Kiln sessions'))], - _('hg kiln [-p url] [-r rev|-a file|-f file|-c|-o|-s|-t|--logout]')) + _('hg kiln [-p url] [-r rev|-a file|-f file|-c|-o|-s|-t|-n branchName|--logout]'))   }
Change 1 of 3 Show Entire File kilnauth.py Stacked
 
53
54
55
 
56
57
58
 
205
206
207
208
 
209
210
211
 
220
221
222
 
 
223
224
225
 
53
54
55
56
57
58
59
 
206
207
208
 
209
210
211
212
 
221
222
223
224
225
226
227
228
@@ -53,6 +53,7 @@
   from mercurial.i18n import _  import mercurial.url +from mercurial import commands    current_user = None   @@ -205,7 +206,7 @@
  return urlopener   mercurial.url.opener = opener   -def logout(ui, repo, domain=None): +def logout(ui, domain=None):   """log out of http repositories     Clears the cookies stored for HTTP repositories. If [domain] is @@ -220,6 +221,8 @@
  except KeyError:   ui.write("Not logged in to '%s'\n" % (domain,))   +commands.norepo += ' logout' +  cmdtable = {   'logout': (logout, [], '[domain]')  }
Change 1 of 2 Show Entire File setup.py Stacked
 
1
2
3
4
5
6
7
8
 
9
10
11
 
42
43
44
45
 
46
47
48
 
1
2
 
3
4
5
6
 
7
8
9
10
 
41
42
43
 
44
45
46
47
@@ -1,11 +1,10 @@
 import compileall  import os -import win32api  import zipfile    folders = ['bfiles', '_custom']  extensions = ['.py'] -excludes = ['\\setup.py'] +excludes = ['setup.py']    def compile_extensions():   compileall.compile_dir(os.path.dirname(__file__), force=1) @@ -42,7 +41,7 @@
  files = list_files(absdir, '.')     print 'Creating ZIP archive...' - zip = zipfile.ZipFile(absdir + '\kiln_extensions.zip', 'w') + zip = zipfile.ZipFile(os.path.join(absdir, 'kiln_extensions.zip'), 'w')   for file in files:   zip.write(file[0], file[1])   zip.close()