PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/hgext/inotify/server.py

https://bitbucket.org/mirror/mercurial/
Python | 492 lines | 454 code | 23 blank | 15 comment | 40 complexity | f275985ed0d437b54acea8871a531dc5 MD5 | raw file
Possible License(s): GPL-2.0
  1. # server.py - common entry point for inotify status server
  2. #
  3. # Copyright 2009 Nicolas Dumazet <nicdumz@gmail.com>
  4. #
  5. # This software may be used and distributed according to the terms of the
  6. # GNU General Public License version 2 or any later version.
  7. from mercurial.i18n import _
  8. from mercurial import cmdutil, osutil, util
  9. import common
  10. import errno
  11. import os
  12. import socket
  13. import stat
  14. import struct
  15. import sys
  16. import tempfile
  17. class AlreadyStartedException(Exception):
  18. pass
  19. class TimeoutException(Exception):
  20. pass
  21. def join(a, b):
  22. if a:
  23. if a[-1] == '/':
  24. return a + b
  25. return a + '/' + b
  26. return b
  27. def split(path):
  28. c = path.rfind('/')
  29. if c == -1:
  30. return '', path
  31. return path[:c], path[c + 1:]
  32. walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG)
  33. def walk(dirstate, absroot, root):
  34. '''Like os.walk, but only yields regular files.'''
  35. # This function is critical to performance during startup.
  36. def walkit(root, reporoot):
  37. files, dirs = [], []
  38. try:
  39. fullpath = join(absroot, root)
  40. for name, kind in osutil.listdir(fullpath):
  41. if kind == stat.S_IFDIR:
  42. if name == '.hg':
  43. if not reporoot:
  44. return
  45. else:
  46. dirs.append(name)
  47. path = join(root, name)
  48. if dirstate._ignore(path):
  49. continue
  50. for result in walkit(path, False):
  51. yield result
  52. elif kind in (stat.S_IFREG, stat.S_IFLNK):
  53. files.append(name)
  54. yield fullpath, dirs, files
  55. except OSError, err:
  56. if err.errno == errno.ENOTDIR:
  57. # fullpath was a directory, but has since been replaced
  58. # by a file.
  59. yield fullpath, dirs, files
  60. elif err.errno not in walk_ignored_errors:
  61. raise
  62. return walkit(root, root == '')
  63. class directory(object):
  64. """
  65. Representing a directory
  66. * path is the relative path from repo root to this directory
  67. * files is a dict listing the files in this directory
  68. - keys are file names
  69. - values are file status
  70. * dirs is a dict listing the subdirectories
  71. - key are subdirectories names
  72. - values are directory objects
  73. """
  74. def __init__(self, relpath=''):
  75. self.path = relpath
  76. self.files = {}
  77. self.dirs = {}
  78. def dir(self, relpath):
  79. """
  80. Returns the directory contained at the relative path relpath.
  81. Creates the intermediate directories if necessary.
  82. """
  83. if not relpath:
  84. return self
  85. l = relpath.split('/')
  86. ret = self
  87. while l:
  88. next = l.pop(0)
  89. try:
  90. ret = ret.dirs[next]
  91. except KeyError:
  92. d = directory(join(ret.path, next))
  93. ret.dirs[next] = d
  94. ret = d
  95. return ret
  96. def walk(self, states, visited=None):
  97. """
  98. yield (filename, status) pairs for items in the trees
  99. that have status in states.
  100. filenames are relative to the repo root
  101. """
  102. for file, st in self.files.iteritems():
  103. if st in states:
  104. yield join(self.path, file), st
  105. for dir in self.dirs.itervalues():
  106. if visited is not None:
  107. visited.add(dir.path)
  108. for e in dir.walk(states):
  109. yield e
  110. def lookup(self, states, path, visited):
  111. """
  112. yield root-relative filenames that match path, and whose
  113. status are in states:
  114. * if path is a file, yield path
  115. * if path is a directory, yield directory files
  116. * if path is not tracked, yield nothing
  117. """
  118. if path[-1] == '/':
  119. path = path[:-1]
  120. paths = path.split('/')
  121. # we need to check separately for last node
  122. last = paths.pop()
  123. tree = self
  124. try:
  125. for dir in paths:
  126. tree = tree.dirs[dir]
  127. except KeyError:
  128. # path is not tracked
  129. visited.add(tree.path)
  130. return
  131. try:
  132. # if path is a directory, walk it
  133. target = tree.dirs[last]
  134. visited.add(target.path)
  135. for file, st in target.walk(states, visited):
  136. yield file
  137. except KeyError:
  138. try:
  139. if tree.files[last] in states:
  140. # path is a file
  141. visited.add(tree.path)
  142. yield path
  143. except KeyError:
  144. # path is not tracked
  145. pass
  146. class repowatcher(object):
  147. """
  148. Watches inotify events
  149. """
  150. statuskeys = 'almr!?'
  151. def __init__(self, ui, dirstate, root):
  152. self.ui = ui
  153. self.dirstate = dirstate
  154. self.wprefix = join(root, '')
  155. self.prefixlen = len(self.wprefix)
  156. self.tree = directory()
  157. self.statcache = {}
  158. self.statustrees = dict([(s, directory()) for s in self.statuskeys])
  159. self.ds_info = self.dirstate_info()
  160. self.last_event = None
  161. def handle_timeout(self):
  162. pass
  163. def dirstate_info(self):
  164. try:
  165. st = os.lstat(self.wprefix + '.hg/dirstate')
  166. return st.st_mtime, st.st_ino
  167. except OSError, err:
  168. if err.errno != errno.ENOENT:
  169. raise
  170. return 0, 0
  171. def filestatus(self, fn, st):
  172. try:
  173. type_, mode, size, time = self.dirstate._map[fn][:4]
  174. except KeyError:
  175. type_ = '?'
  176. if type_ == 'n':
  177. st_mode, st_size, st_mtime = st
  178. if size == -1:
  179. return 'l'
  180. if size and (size != st_size or (mode ^ st_mode) & 0100):
  181. return 'm'
  182. if time != int(st_mtime):
  183. return 'l'
  184. return 'n'
  185. if type_ == '?' and self.dirstate._dirignore(fn):
  186. # we must check not only if the file is ignored, but if any part
  187. # of its path match an ignore pattern
  188. return 'i'
  189. return type_
  190. def updatefile(self, wfn, osstat):
  191. '''
  192. update the file entry of an existing file.
  193. osstat: (mode, size, time) tuple, as returned by os.lstat(wfn)
  194. '''
  195. self._updatestatus(wfn, self.filestatus(wfn, osstat))
  196. def deletefile(self, wfn, oldstatus):
  197. '''
  198. update the entry of a file which has been deleted.
  199. oldstatus: char in statuskeys, status of the file before deletion
  200. '''
  201. if oldstatus == 'r':
  202. newstatus = 'r'
  203. elif oldstatus in 'almn':
  204. newstatus = '!'
  205. else:
  206. newstatus = None
  207. self.statcache.pop(wfn, None)
  208. self._updatestatus(wfn, newstatus)
  209. def _updatestatus(self, wfn, newstatus):
  210. '''
  211. Update the stored status of a file.
  212. newstatus: - char in (statuskeys + 'ni'), new status to apply.
  213. - or None, to stop tracking wfn
  214. '''
  215. root, fn = split(wfn)
  216. d = self.tree.dir(root)
  217. oldstatus = d.files.get(fn)
  218. # oldstatus can be either:
  219. # - None : fn is new
  220. # - a char in statuskeys: fn is a (tracked) file
  221. if self.ui.debugflag and oldstatus != newstatus:
  222. self.ui.note(_('status: %r %s -> %s\n') %
  223. (wfn, oldstatus, newstatus))
  224. if oldstatus and oldstatus in self.statuskeys \
  225. and oldstatus != newstatus:
  226. del self.statustrees[oldstatus].dir(root).files[fn]
  227. if newstatus in (None, 'i'):
  228. d.files.pop(fn, None)
  229. elif oldstatus != newstatus:
  230. d.files[fn] = newstatus
  231. if newstatus != 'n':
  232. self.statustrees[newstatus].dir(root).files[fn] = newstatus
  233. def check_deleted(self, key):
  234. # Files that had been deleted but were present in the dirstate
  235. # may have vanished from the dirstate; we must clean them up.
  236. nuke = []
  237. for wfn, ignore in self.statustrees[key].walk(key):
  238. if wfn not in self.dirstate:
  239. nuke.append(wfn)
  240. for wfn in nuke:
  241. root, fn = split(wfn)
  242. del self.statustrees[key].dir(root).files[fn]
  243. del self.tree.dir(root).files[fn]
  244. def update_hgignore(self):
  245. # An update of the ignore file can potentially change the
  246. # states of all unknown and ignored files.
  247. # XXX If the user has other ignore files outside the repo, or
  248. # changes their list of ignore files at run time, we'll
  249. # potentially never see changes to them. We could get the
  250. # client to report to us what ignore data they're using.
  251. # But it's easier to do nothing than to open that can of
  252. # worms.
  253. if '_ignore' in self.dirstate.__dict__:
  254. delattr(self.dirstate, '_ignore')
  255. self.ui.note(_('rescanning due to .hgignore change\n'))
  256. self.handle_timeout()
  257. self.scan()
  258. def getstat(self, wpath):
  259. try:
  260. return self.statcache[wpath]
  261. except KeyError:
  262. try:
  263. return self.stat(wpath)
  264. except OSError, err:
  265. if err.errno != errno.ENOENT:
  266. raise
  267. def stat(self, wpath):
  268. try:
  269. st = os.lstat(join(self.wprefix, wpath))
  270. ret = st.st_mode, st.st_size, st.st_mtime
  271. self.statcache[wpath] = ret
  272. return ret
  273. except OSError:
  274. self.statcache.pop(wpath, None)
  275. raise
  276. class socketlistener(object):
  277. """
  278. Listens for client queries on unix socket inotify.sock
  279. """
  280. def __init__(self, ui, root, repowatcher, timeout):
  281. self.ui = ui
  282. self.repowatcher = repowatcher
  283. self.sock = socket.socket(socket.AF_UNIX)
  284. self.sockpath = join(root, '.hg/inotify.sock')
  285. self.realsockpath = self.sockpath
  286. if os.path.islink(self.sockpath):
  287. if os.path.exists(self.sockpath):
  288. self.realsockpath = os.readlink(self.sockpath)
  289. else:
  290. raise util.Abort('inotify-server: cannot start: '
  291. '.hg/inotify.sock is a broken symlink')
  292. try:
  293. self.sock.bind(self.realsockpath)
  294. except socket.error, err:
  295. if err.args[0] == errno.EADDRINUSE:
  296. raise AlreadyStartedException(_('cannot start: socket is '
  297. 'already bound'))
  298. if err.args[0] == "AF_UNIX path too long":
  299. tempdir = tempfile.mkdtemp(prefix="hg-inotify-")
  300. self.realsockpath = os.path.join(tempdir, "inotify.sock")
  301. try:
  302. self.sock.bind(self.realsockpath)
  303. os.symlink(self.realsockpath, self.sockpath)
  304. except (OSError, socket.error), inst:
  305. try:
  306. os.unlink(self.realsockpath)
  307. except:
  308. pass
  309. os.rmdir(tempdir)
  310. if inst.errno == errno.EEXIST:
  311. raise AlreadyStartedException(_('cannot start: tried '
  312. 'linking .hg/inotify.sock to a temporary socket but'
  313. ' .hg/inotify.sock already exists'))
  314. raise
  315. else:
  316. raise
  317. self.sock.listen(5)
  318. self.fileno = self.sock.fileno
  319. def answer_stat_query(self, cs):
  320. names = cs.read().split('\0')
  321. states = names.pop()
  322. self.ui.note(_('answering query for %r\n') % states)
  323. visited = set()
  324. if not names:
  325. def genresult(states, tree):
  326. for fn, state in tree.walk(states):
  327. yield fn
  328. else:
  329. def genresult(states, tree):
  330. for fn in names:
  331. for f in tree.lookup(states, fn, visited):
  332. yield f
  333. return ['\0'.join(r) for r in [
  334. genresult('l', self.repowatcher.statustrees['l']),
  335. genresult('m', self.repowatcher.statustrees['m']),
  336. genresult('a', self.repowatcher.statustrees['a']),
  337. genresult('r', self.repowatcher.statustrees['r']),
  338. genresult('!', self.repowatcher.statustrees['!']),
  339. '?' in states
  340. and genresult('?', self.repowatcher.statustrees['?'])
  341. or [],
  342. [],
  343. 'c' in states and genresult('n', self.repowatcher.tree) or [],
  344. visited
  345. ]]
  346. def answer_dbug_query(self):
  347. return ['\0'.join(self.repowatcher.debug())]
  348. def accept_connection(self):
  349. sock, addr = self.sock.accept()
  350. cs = common.recvcs(sock)
  351. version = ord(cs.read(1))
  352. if version != common.version:
  353. self.ui.warn(_('received query from incompatible client '
  354. 'version %d\n') % version)
  355. try:
  356. # try to send back our version to the client
  357. # this way, the client too is informed of the mismatch
  358. sock.sendall(chr(common.version))
  359. except:
  360. pass
  361. return
  362. type = cs.read(4)
  363. if type == 'STAT':
  364. results = self.answer_stat_query(cs)
  365. elif type == 'DBUG':
  366. results = self.answer_dbug_query()
  367. else:
  368. self.ui.warn(_('unrecognized query type: %s\n') % type)
  369. return
  370. try:
  371. try:
  372. v = chr(common.version)
  373. sock.sendall(v + type + struct.pack(common.resphdrfmts[type],
  374. *map(len, results)))
  375. sock.sendall(''.join(results))
  376. finally:
  377. sock.shutdown(socket.SHUT_WR)
  378. except socket.error, err:
  379. if err.args[0] != errno.EPIPE:
  380. raise
  381. if sys.platform.startswith('linux'):
  382. import linuxserver as _server
  383. else:
  384. raise ImportError
  385. master = _server.master
  386. def start(ui, dirstate, root, opts):
  387. timeout = opts.get('idle_timeout')
  388. if timeout:
  389. timeout = float(timeout) * 60000
  390. else:
  391. timeout = None
  392. class service(object):
  393. def init(self):
  394. try:
  395. self.master = master(ui, dirstate, root, timeout)
  396. except AlreadyStartedException, inst:
  397. raise util.Abort("inotify-server: %s" % inst)
  398. def run(self):
  399. try:
  400. try:
  401. self.master.run()
  402. except TimeoutException:
  403. pass
  404. finally:
  405. self.master.shutdown()
  406. if 'inserve' not in sys.argv:
  407. runargs = util.hgcmd() + ['inserve', '-R', root]
  408. else:
  409. runargs = util.hgcmd() + sys.argv[1:]
  410. pidfile = ui.config('inotify', 'pidfile')
  411. if opts['daemon'] and pidfile is not None and 'pid-file' not in runargs:
  412. runargs.append("--pid-file=%s" % pidfile)
  413. service = service()
  414. logfile = ui.config('inotify', 'log')
  415. appendpid = ui.configbool('inotify', 'appendpid', False)
  416. ui.debug('starting inotify server: %s\n' % ' '.join(runargs))
  417. cmdutil.service(opts, initfn=service.init, runfn=service.run,
  418. logfile=logfile, runargs=runargs, appendpid=appendpid)