Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from subprocess import Popen, PIPE
- import sys
- import os.path
- import ConfigParser
- from optparse import OptionParser
- docstring = """
- A typical .hglftp file looks like this:
- [ftp]
- host = host
- user = user
- pass = password
- root =
- """
- conf = { }
- debug = False
- steamroller = True
- def parse_hglftp():
- config = ConfigParser.RawConfigParser()
- config.read('.hglftp')
- try:
- conf = dict(config.items('ftp'))
- except ConfigParser.NoSectionError:
- print "Error: .hglftp config file not found or malformed."
- print docstring
- sys.exit()
- return conf
- def command (command):
- return Popen(command, shell=True, stdout=PIPE).communicate()[0]
- def hg_to_update ():
- files = filter(lambda x: x, command ('hg st -n').split ("\n"));
- return [f.replace ('\\', '/') for f in files]
- def lftp_command (cmd):
- global conf
- ftp_root = conf.has_key ('root') and 'cd %s && ' % conf['root'] or ''
- return command ('lftp -e "%s%s && quit" -u %s,%s %s' % (ftp_root, cmd, conf['user'], conf['pass'], conf['host'])).strip();
- def mirror():
- # print lftp_command ('pwd && lpwd')
- print "Please wait while mirroring..."
- print lftp_command ('mirror -P5')
- def upload():
- def lftpc_command_for_file (file):
- if os.path.isdir (file):
- print "%s is a directory - doing nothing" % file
- return 'pwd'
- dir = '/'.join(f.split ('/')[:-1])
- if os.path.exists (f):
- print "Putting %s at: %s" % (f, dir)
- if dir:
- return 'put -O %s %s' % (dir, f)
- return 'put %s' % (f)
- print "Removing %s" % f
- return 'rm %s' % f
- files = hg_to_update ()
- if not len (files): return
- if debug:
- print lftp_command ('pwd && lpwd')
- queue = []
- for f in files:
- if not steamroller:
- print lftp_command (lftpc_command_for_file(f))
- else:
- queue.append (lftpc_command_for_file(f))
- if steamroller:
- # print " && ".join (queue)
- print lftp_command (" && ".join (queue))
- command ('hg addremove && hg ci -m "Modified files"')
- def main():
- global conf
- conf = parse_hglftp()
- (options, args) = optargs (sys.argv)
- if options.mirror:
- return mirror()
- return upload()
- def optargs(args):
- """Parses options for current command."""
- parser = OptionParser()
- parser.add_option("-m", "--mirror", dest="mirror", default=False, action="store_true",
- help="Create a mirror")
- parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true",
- help="Enter debug mode")
- (options, args) = parser.parse_args(args)
- return (options, args)
- if __name__ == '__main__':
- sys.exit(main())
Add Comment
Please, Sign In to add comment