Initialize invert_probe
[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
11# Define command line argument interface
12parser = argparse.ArgumentParser(description='Upload a file to Smoothie over network.')
13parser.add_argument('file', type=argparse.FileType('r'),
14 help='filename to be uploaded')
15parser.add_argument('ipaddr',
16 help='Smoothie IP address')
17parser.add_argument('-v','--verbose',action='store_true',
18 help='Show data being uploaded')
19parser.add_argument('-o','--output',
20 help='Set output filename')
21parser.add_argument('-q','--quiet',action='store_true',
22 help='suppress all output to terminal')
23
24args = parser.parse_args()
25
26f = args.file
27verbose = args.verbose
28output = args.output
29if output == None :
30 output= args.file.name
31
32filesize= os.path.getsize(args.file.name)
33
34if not args.quiet : print("Uploading " + args.file.name + " to " + args.ipaddr + " as " + output + " size: " + str(filesize) )
35
36# make connection to sftp server
37s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38s.settimeout(4.0)
39s.connect((args.ipaddr, 115))
40tn= s.makefile()
41
42# read startup prompt
43ln= tn.readline()
44if not ln.startswith("+") :
45 print("Failed to connect with sftp: " + ln)
46 sys.exit();
47
48if verbose: print("RSP: " + ln.strip())
49
50# Issue initial store command
51tn.write("STOR OLD /sd/" + output + "\n")
52tn.flush()
53
54ln= tn.readline()
55if not ln.startswith("+") :
56 print("Failed to create file: " + ln)
57 sys.exit();
58
59if verbose: print("RSP: " + ln.strip())
60
61# send size of file
62tn.write("SIZE " + str(filesize) + "\n")
63tn.flush()
64
65ln= tn.readline()
66if not ln.startswith("+") :
67 print("Failed: " + ln)
68 sys.exit();
69
70if verbose: print("RSP: " + ln.strip())
71
72cnt= 0
73# now send file
74for line in f:
75 tn.write(line)
76 if verbose :
77 print("SND: " + line.strip())
78 elif not args.quiet :
79 cnt += len(line)
80 print(str(cnt) + "/" + str(filesize) + "\r", end='')
81
82
83tn.flush()
84
85ln= tn.readline()
86if not ln.startswith("+") :
87 print("Failed to save file: " + ln)
88 sys.exit();
89
90if verbose: print("RSP: " + ln.strip())
91
92# exit
93tn.write("DONE\n")
94tn.flush()
95ln= tn.readline()
96tn.close()
97f.close()
98
99if not args.quiet : print("Upload complete")
100