helpers.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. from __future__ import with_statement
  2. import argparse
  3. from datetime import datetime, timedelta
  4. from fnmatch import fnmatchcase
  5. import grp
  6. import os
  7. import pwd
  8. import re
  9. import stat
  10. import struct
  11. import sys
  12. import time
  13. import urllib
  14. class Statistics(object):
  15. def __init__(self):
  16. self.osize = self.csize = self.usize = self.nfiles = 0
  17. def update(self, size, csize, unique):
  18. self.osize += size
  19. self.csize += csize
  20. if unique:
  21. self.usize += csize
  22. def print_(self):
  23. print 'Number of files: %d' % self.nfiles
  24. print 'Original size: %d (%s)' % (self.osize, format_file_size(self.osize))
  25. print 'Compressed size: %s (%s)'% (self.csize, format_file_size(self.csize))
  26. print 'Unique data: %d (%s)' % (self.usize, format_file_size(self.usize))
  27. def day_of_year(d):
  28. """Calculate the "day of year" from a date object"""
  29. return int(d.strftime('%j'))
  30. # OSX filenames are UTF-8 Only so any non-utf8 filenames are url encoded
  31. if sys.platform == 'darwin':
  32. def encode_filename(name):
  33. try:
  34. name.decode('utf-8')
  35. return name
  36. except UnicodeDecodeError:
  37. return urllib.quote(name)
  38. else:
  39. encode_filename = str
  40. class Counter(object):
  41. __slots__ = ('v',)
  42. def __init__(self, value=0):
  43. self.v = value
  44. def inc(self, amount=1):
  45. self.v += amount
  46. def dec(self, amount=1):
  47. self.v -= amount
  48. def __cmp__(self, x):
  49. return cmp(self.v, x)
  50. def __repr__(self):
  51. return '<Counter(%r)>' % self.v
  52. def get_keys_dir():
  53. """Determine where to store keys and cache"""
  54. return os.environ.get('DARC_KEYS_DIR',
  55. os.path.join(os.path.expanduser('~'), '.darc', 'keys'))
  56. def get_cache_dir():
  57. """Determine where to store keys and cache"""
  58. return os.environ.get('DARC_CACHE_DIR',
  59. os.path.join(os.path.expanduser('~'), '.darc', 'cache'))
  60. def deferrable(f):
  61. def wrapper(*args, **kw):
  62. callback = kw.pop('callback', None)
  63. if callback:
  64. data = kw.pop('callback_data', None)
  65. try:
  66. res = f(*args, **kw)
  67. except Exception, e:
  68. callback(None, e, data)
  69. else:
  70. callback(res, None, data)
  71. else:
  72. return f(*args, **kw)
  73. return wrapper
  74. def error_callback(res, error, data):
  75. if res:
  76. raise res
  77. def to_localtime(ts):
  78. """Convert datetime object from UTC to local time zone"""
  79. return ts - timedelta(seconds=time.altzone)
  80. def read_set(path):
  81. """Read set from disk (as int32s)
  82. """
  83. with open(path, 'rb') as fd:
  84. data = fd.read()
  85. return set(struct.unpack('<%di' % (len(data) / 4), data))
  86. def write_set(s, path):
  87. """Write set to disk (as int32s)
  88. """
  89. with open(path, 'wb') as fd:
  90. fd.write(struct.pack('<%di' % len(s), *s))
  91. def encode_long(v):
  92. bytes = []
  93. while True:
  94. if v > 0x7f:
  95. bytes.append(0x80 | (v % 0x80))
  96. v >>= 7
  97. else:
  98. bytes.append(v)
  99. return ''.join(chr(x) for x in bytes)
  100. def decode_long(bytes):
  101. v = 0
  102. base = 0
  103. for x in bytes:
  104. b = ord(x)
  105. if b & 0x80:
  106. v += (b & 0x7f) << base
  107. base += 7
  108. else:
  109. return v + (b << base)
  110. def exclude_path(path, patterns):
  111. """Used by create and extract sub-commands to determine
  112. if an item should be processed or not
  113. """
  114. for pattern in (patterns or []):
  115. if pattern.match(path):
  116. return isinstance(pattern, ExcludePattern)
  117. return False
  118. class IncludePattern(object):
  119. """--include PATTERN
  120. >>> py = IncludePattern('*.py')
  121. >>> foo = IncludePattern('/foo')
  122. >>> py.match('/foo/foo.py')
  123. True
  124. >>> py.match('/bar/foo.java')
  125. False
  126. >>> foo.match('/foo/foo.py')
  127. True
  128. >>> foo.match('/bar/foo.java')
  129. False
  130. >>> foo.match('/foobar/foo.py')
  131. False
  132. >>> foo.match('/foo')
  133. True
  134. """
  135. def __init__(self, pattern):
  136. self.pattern = self.dirpattern = pattern
  137. if not pattern.endswith(os.path.sep):
  138. self.dirpattern += os.path.sep
  139. def match(self, path):
  140. dir, name = os.path.split(path)
  141. return (path == self.pattern
  142. or (dir + os.path.sep).startswith(self.dirpattern)
  143. or fnmatchcase(name, self.pattern))
  144. def __repr__(self):
  145. return '%s(%s)' % (type(self), self.pattern)
  146. class ExcludePattern(IncludePattern):
  147. """
  148. """
  149. def walk_path(path, skip_inodes=None):
  150. st = os.lstat(path)
  151. if skip_inodes and (st.st_ino, st.st_dev) in skip_inodes:
  152. return
  153. yield path, st
  154. if stat.S_ISDIR(st.st_mode):
  155. for f in os.listdir(path):
  156. for x in walk_path(os.path.join(path, f), skip_inodes):
  157. yield x
  158. def format_time(t):
  159. """Format datetime suitable for fixed length list output
  160. """
  161. if (datetime.now() - t).days < 365:
  162. return t.strftime('%b %d %H:%M')
  163. else:
  164. return t.strftime('%b %d %Y')
  165. def format_timedelta(td):
  166. """Format timedelta in a human friendly format"""
  167. ts = td.total_seconds()
  168. s = ts % 60
  169. m = int(ts / 60) % 60
  170. h = int(ts / 3600) % 24
  171. txt = '%.2f seconds' % s
  172. if m:
  173. txt = '%d minutes %s' % (m, txt)
  174. if h:
  175. txt = '%d hours %s' % (h, txt)
  176. if td.days:
  177. txt = '%d days %s' % (td.days, txt)
  178. return txt
  179. def format_file_mode(mod):
  180. """Format file mode bits for list output
  181. """
  182. def x(v):
  183. return ''.join(v & m and s or '-'
  184. for m, s in ((4, 'r'), (2, 'w'), (1, 'x')))
  185. return '%s%s%s' % (x(mod / 64), x(mod / 8), x(mod))
  186. def format_file_size(v):
  187. """Format file size into a human friendly format
  188. """
  189. if v > 1024 * 1024 * 1024:
  190. return '%.2f GB' % (v / 1024. / 1024. / 1024.)
  191. elif v > 1024 * 1024:
  192. return '%.2f MB' % (v / 1024. / 1024.)
  193. elif v > 1024:
  194. return '%.2f kB' % (v / 1024.)
  195. else:
  196. return str(v)
  197. class IntegrityError(Exception):
  198. """
  199. """
  200. def memoize(function):
  201. cache = {}
  202. def decorated_function(*args):
  203. try:
  204. return cache[args]
  205. except KeyError:
  206. val = function(*args)
  207. cache[args] = val
  208. return val
  209. return decorated_function
  210. @memoize
  211. def uid2user(uid):
  212. try:
  213. return pwd.getpwuid(uid).pw_name
  214. except KeyError:
  215. return None
  216. @memoize
  217. def user2uid(user):
  218. try:
  219. return pwd.getpwnam(user).pw_uid
  220. except KeyError:
  221. return None
  222. @memoize
  223. def gid2group(gid):
  224. try:
  225. return grp.getgrgid(gid).gr_name
  226. except KeyError:
  227. return None
  228. @memoize
  229. def group2gid(group):
  230. try:
  231. return grp.getgrnam(group).gr_gid
  232. except KeyError:
  233. return None
  234. class Location(object):
  235. """Object representing a store / archive location
  236. >>> Location('ssh://user@host:1234/some/path::archive')
  237. Location(proto='ssh', user='user', host='host', port=1234, path='/some/path', archive='archive')
  238. >>> Location('file:///some/path::archive')
  239. Location(proto='file', user=None, host=None, port=None, path='/some/path', archive='archive')
  240. >>> Location('user@host:/some/path::archive')
  241. Location(proto='ssh', user='user', host='host', port=22, path='/some/path', archive='archive')
  242. >>> Location('/some/path::archive')
  243. Location(proto='file', user=None, host=None, port=None, path='/some/path', archive='archive')
  244. """
  245. proto = user = host = port = path = archive = None
  246. ssh_re = re.compile(r'(?P<proto>ssh)://(?:(?P<user>[^@]+)@)?'
  247. r'(?P<host>[^:/#]+)(?::(?P<port>\d+))?'
  248. r'(?P<path>[^:]*)(?:::(?P<archive>.+))?')
  249. file_re = re.compile(r'(?P<proto>file)://'
  250. r'(?P<path>[^:]*)(?:::(?P<archive>.+))?')
  251. scp_re = re.compile(r'((?:(?P<user>[^@]+)@)?(?P<host>[^:/]+):)?'
  252. r'(?P<path>[^:]*)(?:::(?P<archive>.+))?')
  253. def __init__(self, text):
  254. if not self.parse(text):
  255. raise ValueError
  256. def parse(self, text):
  257. m = self.ssh_re.match(text)
  258. if m:
  259. self.proto = m.group('proto')
  260. self.user = m.group('user')
  261. self.host = m.group('host')
  262. self.port = m.group('port') and int(m.group('port')) or 22
  263. self.path = m.group('path')
  264. self.archive = m.group('archive')
  265. return True
  266. m = self.file_re.match(text)
  267. if m:
  268. self.proto = m.group('proto')
  269. self.path = m.group('path')
  270. self.archive = m.group('archive')
  271. return True
  272. m = self.scp_re.match(text)
  273. if m:
  274. self.user = m.group('user')
  275. self.host = m.group('host')
  276. self.path = m.group('path')
  277. self.archive = m.group('archive')
  278. self.proto = self.host and 'ssh' or 'file'
  279. if self.proto == 'ssh':
  280. self.port = 22
  281. return True
  282. return False
  283. def __str__(self):
  284. items = []
  285. items.append('proto=%r' % self.proto)
  286. items.append('user=%r' % self.user)
  287. items.append('host=%r' % self.host)
  288. items.append('port=%r' % self.port)
  289. items.append('path=%r'% self.path)
  290. items.append('archive=%r' % self.archive)
  291. return ', '.join(items)
  292. def to_key_filename(self):
  293. name = re.sub('[^\w]', '_', self.path).strip('_')
  294. if self.proto != 'file':
  295. name = self.host + '__' + name
  296. return os.path.join(get_keys_dir(), name)
  297. def __repr__(self):
  298. return "Location(%s)" % self
  299. def location_validator(archive=None):
  300. def validator(text):
  301. try:
  302. loc = Location(text)
  303. except ValueError:
  304. raise argparse.ArgumentTypeError('Invalid location format: "%s"' % text)
  305. if archive is True and not loc.archive:
  306. raise argparse.ArgumentTypeError('"%s": No archive specified' % text)
  307. elif archive is False and loc.archive:
  308. raise argparse.ArgumentTypeError('"%s" No archive can be specified' % text)
  309. return loc
  310. return validator