Skip to content

Instantly share code, notes, and snippets.

@Ginhing
Created April 16, 2015 07:00
Show Gist options
  • Save Ginhing/9600beb0ada187e363c4 to your computer and use it in GitHub Desktop.
Save Ginhing/9600beb0ada187e363c4 to your computer and use it in GitHub Desktop.
Edit remote file and save it back
#!/usr/bin/env python
from subprocess import call
from time import time
from argparse import ArgumentParser
def pull(path, fname):
if call(['scp', path, fname]) != 0:
raise Exception(path + ' :File path or host seems wrong')
call(['cp', fname, fname + '.bak'])
def push(fname, path):
call(['scp', fname, path])
def edit(fname, editor):
if 0 != call([editor, fname]):
raise Exception('The editor seems wrong.')
def main(*args):
from os.path import basename
from tempfile import gettempdir
parser = ArgumentParser()
parser.add_argument('path',
help='remote path in ssh format.')
parser.add_argument('-e',
help='special what editor or vim to open.',
dest='editor',
default='vim')
args = parser.parse_args(args) if len(args) else parser.parse_args()
path = args.path
editor = args.editor
fname = '{}/{}_redit{}'.format(
gettempdir(),
basename(path),
int(time() * 100)
)
pull(path, fname)
edit(fname, editor)
push(fname, path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment