Attachment 'qtimes.py'

Download

   1 # qtimes.py - save or restore modification times of patched files
   2 #
   3 # Copyright 2008 Andrei Vermel <andrei.vermel@gmail.com>
   4 #
   5 # This software may be used and distributed according to the terms
   6 # of the GNU General Public License, incorporated herein by reference.
   7 
   8 """save or restore modification times of patched files"""
   9 
  10 from mercurial.i18n import _
  11 from mercurial.node import *
  12 from mercurial.error import RepoError
  13 from mercurial import commands, scmutil, hg, node, util
  14 import os
  15 
  16 _sha1 = util.sha1
  17 
  18 def times(ui, repo, *pats, **opts):
  19     """save or restore modification times of patched files"""
  20     try:
  21       node1, node2 = scmutil.revpair(repo, ['qparent', 'qtip'])
  22     except RepoError:
  23       node1, node2 = 0, 0
  24     if node1 == node2:
  25         ui.warn(_('no patches applied - nothing to do'))
  26         return True
  27     matchfn = scmutil.match(repo[None], pats, opts)
  28     cwd = (pats and repo.getcwd()) or ''
  29     modified, added, removed, deleted, unknown, ignored, clean = [
  30         n for n in repo.status(node1=node1, node2=node2, match=matchfn)]
  31     if opts['save']:
  32         pm = file(repo.path+'/patched_mtimes', 'w')
  33         patched = modified + added
  34         for f in patched:        
  35             fname = repo.wjoin(f)
  36             try:
  37                 mtime = os.stat(fname).st_mtime
  38             except OSError:
  39                 continue
  40             checksum = _sha1("")
  41             checksum.update(file(fname, 'r').read())
  42             pm.write("%s,%s,%s\n" % (fname, checksum.hexdigest(), mtime))
  43     elif opts['restore']:
  44         pm = file(repo.path+'/patched_mtimes', 'r')
  45         for line in pm.readlines():
  46             fname, checksum, mtime = line.strip().split(',')
  47             if not os.path.exists(fname):
  48                 continue
  49             cs = _sha1("")
  50             cs.update(file(fname, 'r').read())
  51             if cs.hexdigest() != checksum:
  52                 ui.warn(_('Not restoring timestamp of file %s\n') % fname)
  53             else:
  54                 st = os.stat(fname)
  55                 os.utime(fname, (st.st_atime, type(st.st_mtime)(mtime)))
  56     else:
  57         ui.warn(_("hg qtimes: either -r or -s option is needed\n"))
  58         commands.help_(ui, 'qtimes')
  59 
  60     return True
  61 
  62 cmdtable = {
  63     "qtimes": 
  64         (times, 
  65          [('s', 'save', None, _('save modification times')),
  66           ('r', 'restore', None, _('restore modification times'))], 
  67               _('hg qtimes (-s | -r)')),
  68 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-08-14 08:41:33, 2.4 KB) [[attachment:qtimes.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.