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