Advertisement
Guest User

Untitled

a guest
Oct 9th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. '''
  2. Subversion SCM
  3. '''
  4.  
  5. # Import python libs
  6. import re
  7. import shlex
  8. import subprocess
  9.  
  10. # Import salt libs
  11. from salt import utils, exceptions
  12.  
  13. _INI_RE = re.compile(r"^([^:]+):\s+(\S.*)$", re.M)
  14.  
  15.  
  16. def __virtual__():
  17. '''
  18. Only load if svn is installed
  19. '''
  20. if utils.which('svn'):
  21. return 'svn'
  22. return False
  23.  
  24.  
  25. def _check_svn():
  26. '''
  27. Check for svn on this node.
  28. '''
  29. utils.check_or_die('svn')
  30.  
  31.  
  32. def _run_svn(cmd, cwd, user, username, opts, **kwargs):
  33. '''
  34. Execute svn
  35. return the output of the command
  36.  
  37. cmd
  38. The command to run.
  39.  
  40. cwd
  41. The path to the Subversion repository
  42.  
  43. user
  44. Run svn as a user other than what the minion runs as
  45.  
  46. username
  47. Connect to the Subversion server as another user
  48.  
  49. opts
  50. Any additional options to add to the command line
  51.  
  52. kwargs
  53. Additional options to pass to the run-cmd
  54. '''
  55. cmd = 'svn --non-interactive {0} '.format(cmd)
  56. if username:
  57. opts += ('--username', username)
  58. if opts:
  59. cmd += subprocess.list2cmdline(opts)
  60.  
  61. result = __salt__['cmd.run_all'](cmd, cwd=cwd, runas=user, **kwargs)
  62.  
  63. retcode = result['retcode']
  64.  
  65. if retcode == 0:
  66. return result['stdout']
  67. else:
  68. raise exceptions.CommandExecutionError(result['stderr'] + '\n\n' + cmd)
  69.  
  70.  
  71. def info(cwd, targets=None, user=None, username=None, fmt='str'):
  72. '''
  73. Display the Subversion information from the checkout.
  74.  
  75. cwd
  76. The path to the Subversion repository
  77.  
  78. targets : None
  79. files, directories, and URLs to pass to the command as arguments
  80. svn uses '.' by default
  81.  
  82. user : None
  83. Run svn as a user other than what the minion runs as
  84.  
  85. username : None
  86. Connect to the Subversion server as another user
  87.  
  88. fmt : str
  89. How to fmt the output from info.
  90. (str, xml, list, dict)
  91.  
  92. CLI Example::
  93.  
  94. salt '*' svn.info /path/to/svn/repo
  95. '''
  96. opts = list()
  97. if fmt == 'xml':
  98. opts.append('--xml')
  99. if targets:
  100. opts += shlex.split(targets)
  101. infos = _run_svn('info', cwd, user, username, opts)
  102.  
  103. if fmt in ('str', 'xml'):
  104. return infos
  105.  
  106. info_list = []
  107. for infosplit in infos.split('\n\n'):
  108. info_list.append(_INI_RE.findall(infosplit))
  109.  
  110. if fmt == 'list':
  111. return info_list
  112. if fmt == 'dict':
  113. return [dict(tmp) for tmp in info_list]
  114.  
  115.  
  116. def checkout(cwd, remote, target=None, user=None, username=None, *opts):
  117. '''
  118. Download a working copy of the remote Subversion repository
  119. directory or file
  120.  
  121. cwd
  122. The path to the Subversion repository
  123.  
  124. remote : None
  125. URL to checkout
  126.  
  127. target : None
  128. The name to give the file or directory working copy
  129. Default: svn uses the remote basename
  130.  
  131. user : None
  132. Run svn as a user other than what the minion runs as
  133.  
  134. username : None
  135. Connect to the Subversion server as another user
  136.  
  137. CLI Example::
  138.  
  139. salt '*' svn.checkout /path/to/repo svn://remote/repo
  140. '''
  141. opts += (remote,)
  142. if target:
  143. opts += (target,)
  144. return _run_svn('checkout', cwd, user, username, opts)
  145.  
  146.  
  147. def switch(cwd, remote, target=None, user=None, username=None, *opts):
  148. '''
  149. Switch a working copy of a remote Subversion repository
  150. directory
  151.  
  152. cwd
  153. The path to the Subversion repository
  154.  
  155. remote : None
  156. URL to switch
  157.  
  158. target : None
  159. The name to give the file or directory working copy
  160. Default: svn uses the remote basename
  161.  
  162. user : None
  163. Run svn as a user other than what the minion runs as
  164.  
  165. username : None
  166. Connect to the Subversion server as another user
  167.  
  168. CLI Example::
  169.  
  170. salt '*' svn.switch /path/to/repo svn://remote/repo
  171. '''
  172. opts += (remote,)
  173. if target:
  174. opts += (target,)
  175. return _run_svn('switch', cwd, user, username, opts)
  176.  
  177.  
  178. def update(cwd, targets=None, user=None, *opts):
  179. '''
  180. Update the current directory, files, or directories from
  181. the remote Subversion repository
  182.  
  183. cwd
  184. The path to the Subversion repository
  185.  
  186. targets : None
  187. files and directories to pass to the command as arguments
  188. Default: svn uses '.'
  189.  
  190. user : None
  191. Run svn as a user other than what the minion runs as
  192.  
  193. username : None
  194. Connect to the Subversion server as another user
  195.  
  196. CLI Example::
  197.  
  198. salt '*' svn.update /path/to/repo
  199. '''
  200. if targets:
  201. opts += tuple(shlex.split(targets))
  202. return _run_svn('update', cwd, user, None, opts)
  203.  
  204.  
  205. def diff(cwd, targets=None, user=None, username=None, *opts):
  206. '''
  207. Return the diff of the current directory, files, or directories from
  208. the remote Subversion repository
  209.  
  210. cwd
  211. The path to the Subversion repository
  212.  
  213. targets : None
  214. files and directories to pass to the command as arguments
  215. Default: svn uses '.'
  216.  
  217. user : None
  218. Run svn as a user other than what the minion runs as
  219.  
  220. username : None
  221. Connect to the Subversion server as another user
  222.  
  223. CLI Example::
  224.  
  225. salt '*' svn.diff /path/to/repo
  226. '''
  227. if targets:
  228. opts += tuple(shlex.split(targets))
  229. return _run_svn('diff', cwd, user, username, opts)
  230.  
  231.  
  232. def commit(cwd, targets=None, msg=None, user=None, username=None, *opts):
  233. '''
  234. Commit the current directory, files, or directories to
  235. the remote Subversion repository
  236.  
  237. cwd
  238. The path to the Subversion repository
  239.  
  240. targets : None
  241. files and directories to pass to the command as arguments
  242. Default: svn uses '.'
  243.  
  244. msg : None
  245. Message to attach to the commit log
  246.  
  247. user : None
  248. Run svn as a user other than what the minion runs as
  249.  
  250. username : None
  251. Connect to the Subversion server as another user
  252.  
  253. CLI Example::
  254.  
  255. salt '*' svn.commit /path/to/repo
  256. '''
  257. if msg:
  258. opts += ('-m', msg)
  259. if targets:
  260. opts += tuple(shlex.split(targets))
  261. return _run_svn('commit', cwd, user, username, opts)
  262.  
  263.  
  264. def add(cwd, targets, user=None, *opts):
  265. '''
  266. Add files to be tracked by the Subversion working-copy checkout
  267.  
  268. cwd
  269. The path to the Subversion repository
  270.  
  271. targets : None
  272. files and directories to pass to the command as arguments
  273.  
  274. user : None
  275. Run svn as a user other than what the minion runs as
  276.  
  277. CLI Example::
  278.  
  279. salt '*' svn.add /path/to/repo /path/to/new/file
  280. '''
  281. if targets:
  282. opts += tuple(shlex.split(targets))
  283. return _run_svn('add', cwd, user, None, opts)
  284.  
  285.  
  286. def remove(cwd, targets, msg=None, user=None, username=None, *opts):
  287. '''
  288. Remove files and directories from the Subversion repository
  289.  
  290. cwd
  291. The path to the Subversion repository
  292.  
  293. targets : None
  294. files, directories, and URLs to pass to the command as arguments
  295.  
  296. msg : None
  297. Message to attach to the commit log
  298.  
  299. user : None
  300. Run svn as a user other than what the minion runs as
  301.  
  302. username : None
  303. Connect to the Subversion server as another user
  304.  
  305. CLI Example::
  306.  
  307. salt '*' svn.remove /path/to/repo /path/to/repo/remove
  308. '''
  309. if msg:
  310. opts += ('-m', msg)
  311. if targets:
  312. opts += tuple(shlex.split(targets))
  313. return _run_svn('remove', cwd, user, username, opts)
  314.  
  315.  
  316. def status(cwd, targets=None, user=None, username=None, *opts):
  317. '''
  318. Display the status of the current directory, files, or
  319. directories in the Subversion repository
  320.  
  321. cwd
  322. The path to the Subversion repository
  323.  
  324. targets : None
  325. files, directories, and URLs to pass to the command as arguments
  326. Default: svn uses '.'
  327.  
  328. user : None
  329. Run svn as a user other than what the minion runs as
  330.  
  331. username : None
  332. Connect to the Subversion server as another user
  333.  
  334. CLI Example::
  335.  
  336. salt '*' svn.status /path/to/repo
  337. '''
  338. if targets:
  339. opts += tuple(shlex.split(targets))
  340. return _run_svn('status', cwd, user, username, opts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement