‘dput.methods.ftp’: Convert ‘print’ statements to ‘sys.stdout.write’ calls.
[dput.git] / dput / methods / ftp.py
blobf40b56f35ec50fc6b275c37e9f1c21198a72e2f2
1 # -*- coding: utf-8; -*-
3 # dput/methods/ftp.py
4 # Part of ‘dput’, a Debian package upload toolkit.
6 # Copyright © 2015 Ben Finney <ben+python@benfinney.id.au>
7 # Copyright © 2008–2013 Y Giridhar Appaji Nag <appaji@debian.org>
8 # Copyright © 2007 Thomas Viehmann <tv@beamnet.de>
10 # This is free software: you may copy, modify, and/or distribute this work
11 # under the terms of the GNU General Public License as published by the
12 # Free Software Foundation; version 2 of that license or any later version.
13 # No warranty expressed or implied. See the file ‘LICENSE.GPL-2’ for details.
15 """ Implementation for FTP upload method. """
17 from __future__ import absolute_import
19 import os
20 import sys
21 import ftplib
22 import getpass
24 from ..helper import dputhelper
27 def upload(
28 fqdn, login, incoming, files_to_upload, debug, ftp_mode,
29 progress=0, port=21):
30 """ Upload the files via FTP.
32 (Could need a bit more error-checking.)
34 """
36 try:
37 ftp_connection = ftplib.FTP()
38 ftp_connection.connect(fqdn, port)
39 if debug:
40 sys.stdout.write("D: FTP-Connection to host: %s\n" % fqdn)
41 except ftplib.all_errors as e:
42 sys.stdout.write(
43 "Connection failed, aborting. Check your network\n"
44 "%s\n" % e)
45 sys.exit(1)
46 prompt = login + "@" + fqdn + " password: "
47 if login == 'anonymous':
48 password = 'dput@packages.debian.org'
49 else:
50 password = getpass.getpass(prompt)
51 try:
52 ftp_connection.login(login, password)
53 except ftplib.error_perm:
54 sys.stdout.write("Wrong Password\n")
55 sys.exit(1)
56 except EOFError:
57 sys.stdout.write("Server closed the connection\n")
58 sys.exit(1)
59 ftp_connection.set_pasv(ftp_mode == 1)
60 try:
61 ftp_connection.cwd(incoming)
62 except ftplib.error_perm as e:
63 if e.args and e.args[0][:3] == '550':
64 sys.stdout.write("Directory to upload to does not exist.\n")
65 sys.exit(1)
66 else:
67 raise
68 if debug:
69 sys.stdout.write("D: Directory to upload to: %s\n" % incoming)
70 for afile in files_to_upload:
71 path_to_package, package_name = os.path.split(afile)
72 try:
73 if debug:
74 sys.stdout.write("D: Uploading File: %s\n" % afile)
75 if progress:
76 try:
77 size = os.stat(afile).st_size
78 except:
79 size = -1
80 if debug:
81 sys.stdout.write(
82 "D: Determining size of file '%s' failed\n"
83 % afile)
84 f = open(afile, 'rb')
85 if progress:
86 f = dputhelper.FileWithProgress(
87 f, ptype=progress,
88 progressf=sys.stdout,
89 size=size)
90 sys.stdout.write(" Uploading %s: " % package_name)
91 sys.stdout.flush()
92 ftp_connection.storbinary(
93 'STOR ' + package_name,
94 f, 1024)
95 f.close()
96 sys.stdout.write("done.\n")
97 sys.stdout.flush()
98 except ftplib.all_errors as e:
99 sys.stdout.write("%s\n" % e)
100 if (
101 isinstance(e, ftplib.Error)
102 and e.args and e.args[0][:3] == '553'):
103 sys.stdout.write(
104 "Leaving existing %s on the server and continuing\n"
105 % (package_name))
106 sys.stdout.write(
107 "NOTE: This existing file may have been"
108 " previously uploaded partially.\n"
109 "For official Debian upload queues,"
110 " the dcut(1) utility can be\n"
111 "used to remove this file, and after"
112 " an acknowledgement mail is\n"
113 "received in response to dcut,"
114 " the upload can be re-initiated.\n")
115 continue
116 elif (
117 isinstance(e, ftplib.Error)
118 and e.args and e.args[0][:1] == '5'):
119 sys.stdout.write(
120 "Note: This error might indicate a problem with"
121 " your passive_ftp setting.\n"
122 "Please consult dput.cf(5) for details on"
123 " this configuration option.\n")
124 if debug:
125 sys.stdout.write(
126 "D: Should exit silently now, but"
127 " will throw exception for debug.\n")
128 raise
129 sys.exit(1)
130 try:
131 ftp_connection.quit()
132 except Exception as e:
133 if debug:
134 sys.stdout.write(
135 "D: Exception %s while attempting to quit ftp session.\n"
136 "D: Throwing an exception for debugging purposes.\n" % e)
137 raise
140 # Local variables:
141 # coding: utf-8
142 # mode: python
143 # End:
144 # vim: fileencoding=utf-8 filetype=python :