Guest User

Untitled

a guest
Apr 8th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. from subprocess import Popen, PIPE
  2. import sys
  3. import os.path
  4. import ConfigParser
  5. from optparse import OptionParser
  6.  
  7. docstring = """
  8. A typical .hglftp file looks like this:
  9.  
  10. [ftp]
  11. host = host
  12. user = user
  13. pass = password
  14. root =
  15. """
  16.  
  17. conf = { }
  18. debug = False
  19. steamroller = True
  20.  
  21. def parse_hglftp():
  22. config = ConfigParser.RawConfigParser()
  23. config.read('.hglftp')
  24. try:
  25. conf = dict(config.items('ftp'))
  26. except ConfigParser.NoSectionError:
  27. print "Error: .hglftp config file not found or malformed."
  28. print docstring
  29. sys.exit()
  30. return conf
  31.  
  32. def command (command):
  33. return Popen(command, shell=True, stdout=PIPE).communicate()[0]
  34.  
  35. def hg_to_update ():
  36. files = filter(lambda x: x, command ('hg st -n').split ("\n"));
  37. return [f.replace ('\\', '/') for f in files]
  38.  
  39. def lftp_command (cmd):
  40. global conf
  41. ftp_root = conf.has_key ('root') and 'cd %s && ' % conf['root'] or ''
  42. return command ('lftp -e "%s%s && quit" -u %s,%s %s' % (ftp_root, cmd, conf['user'], conf['pass'], conf['host'])).strip();
  43.  
  44. def mirror():
  45. # print lftp_command ('pwd && lpwd')
  46. print "Please wait while mirroring..."
  47. print lftp_command ('mirror -P5')
  48.  
  49. def upload():
  50. def lftpc_command_for_file (file):
  51. if os.path.isdir (file):
  52. print "%s is a directory - doing nothing" % file
  53. return 'pwd'
  54. dir = '/'.join(f.split ('/')[:-1])
  55. if os.path.exists (f):
  56. print "Putting %s at: %s" % (f, dir)
  57. if dir:
  58. return 'put -O %s %s' % (dir, f)
  59. return 'put %s' % (f)
  60. print "Removing %s" % f
  61. return 'rm %s' % f
  62.  
  63. files = hg_to_update ()
  64. if not len (files): return
  65. if debug:
  66. print lftp_command ('pwd && lpwd')
  67. queue = []
  68. for f in files:
  69. if not steamroller:
  70. print lftp_command (lftpc_command_for_file(f))
  71. else:
  72. queue.append (lftpc_command_for_file(f))
  73. if steamroller:
  74. # print " && ".join (queue)
  75. print lftp_command (" && ".join (queue))
  76. command ('hg addremove && hg ci -m "Modified files"')
  77.  
  78. def main():
  79. global conf
  80. conf = parse_hglftp()
  81. (options, args) = optargs (sys.argv)
  82. if options.mirror:
  83. return mirror()
  84. return upload()
  85.  
  86. def optargs(args):
  87. """Parses options for current command."""
  88. parser = OptionParser()
  89. parser.add_option("-m", "--mirror", dest="mirror", default=False, action="store_true",
  90. help="Create a mirror")
  91. parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true",
  92. help="Enter debug mode")
  93. (options, args) = parser.parse_args(args)
  94. return (options, args)
  95.  
  96. if __name__ == '__main__':
  97. sys.exit(main())
Add Comment
Please, Sign In to add comment