Merge pull request #1334 from CapnBry/z-grid-dest
[clinton/Smoothieware.git] / smoothie-upload.py
CommitLineData
d4ee6ee2
JM
1#!/usr/bin/env python
2"""\
3Upload a file to Smoothie over the network
4"""
5
6from __future__ import print_function
7import sys
8import argparse
9import socket
10import os
6e20e9ec 11import re
d4ee6ee2
JM
12# Define command line argument interface
13parser = argparse.ArgumentParser(description='Upload a file to Smoothie over network.')
14parser.add_argument('file', type=argparse.FileType('r'),
15 help='filename to be uploaded')
16parser.add_argument('ipaddr',
17 help='Smoothie IP address')
18parser.add_argument('-v','--verbose',action='store_true',
19 help='Show data being uploaded')
20parser.add_argument('-o','--output',
21 help='Set output filename')
22parser.add_argument('-q','--quiet',action='store_true',
23 help='suppress all output to terminal')
6e20e9ec
JL
24parser.add_argument('-s','--space',action='store_true',
25 help='Leave whitespaces in output filename')
d4ee6ee2
JM
26
27args = parser.parse_args()
28
29f = args.file
30verbose = args.verbose
31output = args.output
32if output == None :
33 output= args.file.name
6e20e9ec
JL
34if not args.space:
35 output = re.sub("\s", "_", output)
d4ee6ee2
JM
36
37filesize= os.path.getsize(args.file.name)
38
39if not args.quiet : print("Uploading " + args.file.name + " to " + args.ipaddr + " as " + output + " size: " + str(filesize) )
40
41# make connection to sftp server
42s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
43s.settimeout(4.0)
44s.connect((args.ipaddr, 115))
45tn= s.makefile()
46
47# read startup prompt
48ln= tn.readline()
49if not ln.startswith("+") :
50 print("Failed to connect with sftp: " + ln)
51 sys.exit();
52
53if verbose: print("RSP: " + ln.strip())
54
55# Issue initial store command
56tn.write("STOR OLD /sd/" + output + "\n")
57tn.flush()
58
59ln= tn.readline()
60if not ln.startswith("+") :
61 print("Failed to create file: " + ln)
62 sys.exit();
63
64if verbose: print("RSP: " + ln.strip())
65
66# send size of file
67tn.write("SIZE " + str(filesize) + "\n")
68tn.flush()
69
70ln= tn.readline()
71if not ln.startswith("+") :
72 print("Failed: " + ln)
73 sys.exit();
74
75if verbose: print("RSP: " + ln.strip())
76
77cnt= 0
78# now send file
79for line in f:
80 tn.write(line)
81 if verbose :
82 print("SND: " + line.strip())
83 elif not args.quiet :
84 cnt += len(line)
85 print(str(cnt) + "/" + str(filesize) + "\r", end='')
86
87
88tn.flush()
89
90ln= tn.readline()
91if not ln.startswith("+") :
92 print("Failed to save file: " + ln)
93 sys.exit();
94
95if verbose: print("RSP: " + ln.strip())
96
97# exit
98tn.write("DONE\n")
99tn.flush()
100ln= tn.readline()
101tn.close()
102f.close()
103
104if not args.quiet : print("Upload complete")
105