1 | n/a | """ |
---|
2 | n/a | distutils.command.upload |
---|
3 | n/a | |
---|
4 | n/a | Implements the Distutils 'upload' subcommand (upload package to a package |
---|
5 | n/a | index). |
---|
6 | n/a | """ |
---|
7 | n/a | |
---|
8 | n/a | import os |
---|
9 | n/a | import io |
---|
10 | n/a | import platform |
---|
11 | n/a | import hashlib |
---|
12 | n/a | from base64 import standard_b64encode |
---|
13 | n/a | from urllib.request import urlopen, Request, HTTPError |
---|
14 | n/a | from urllib.parse import urlparse |
---|
15 | n/a | from distutils.errors import DistutilsError, DistutilsOptionError |
---|
16 | n/a | from distutils.core import PyPIRCCommand |
---|
17 | n/a | from distutils.spawn import spawn |
---|
18 | n/a | from distutils import log |
---|
19 | n/a | |
---|
20 | n/a | class upload(PyPIRCCommand): |
---|
21 | n/a | |
---|
22 | n/a | description = "upload binary package to PyPI" |
---|
23 | n/a | |
---|
24 | n/a | user_options = PyPIRCCommand.user_options + [ |
---|
25 | n/a | ('sign', 's', |
---|
26 | n/a | 'sign files to upload using gpg'), |
---|
27 | n/a | ('identity=', 'i', 'GPG identity used to sign files'), |
---|
28 | n/a | ] |
---|
29 | n/a | |
---|
30 | n/a | boolean_options = PyPIRCCommand.boolean_options + ['sign'] |
---|
31 | n/a | |
---|
32 | n/a | def initialize_options(self): |
---|
33 | n/a | PyPIRCCommand.initialize_options(self) |
---|
34 | n/a | self.username = '' |
---|
35 | n/a | self.password = '' |
---|
36 | n/a | self.show_response = 0 |
---|
37 | n/a | self.sign = False |
---|
38 | n/a | self.identity = None |
---|
39 | n/a | |
---|
40 | n/a | def finalize_options(self): |
---|
41 | n/a | PyPIRCCommand.finalize_options(self) |
---|
42 | n/a | if self.identity and not self.sign: |
---|
43 | n/a | raise DistutilsOptionError( |
---|
44 | n/a | "Must use --sign for --identity to have meaning" |
---|
45 | n/a | ) |
---|
46 | n/a | config = self._read_pypirc() |
---|
47 | n/a | if config != {}: |
---|
48 | n/a | self.username = config['username'] |
---|
49 | n/a | self.password = config['password'] |
---|
50 | n/a | self.repository = config['repository'] |
---|
51 | n/a | self.realm = config['realm'] |
---|
52 | n/a | |
---|
53 | n/a | # getting the password from the distribution |
---|
54 | n/a | # if previously set by the register command |
---|
55 | n/a | if not self.password and self.distribution.password: |
---|
56 | n/a | self.password = self.distribution.password |
---|
57 | n/a | |
---|
58 | n/a | def run(self): |
---|
59 | n/a | if not self.distribution.dist_files: |
---|
60 | n/a | msg = "No dist file created in earlier command" |
---|
61 | n/a | raise DistutilsOptionError(msg) |
---|
62 | n/a | for command, pyversion, filename in self.distribution.dist_files: |
---|
63 | n/a | self.upload_file(command, pyversion, filename) |
---|
64 | n/a | |
---|
65 | n/a | def upload_file(self, command, pyversion, filename): |
---|
66 | n/a | # Makes sure the repository URL is compliant |
---|
67 | n/a | schema, netloc, url, params, query, fragments = \ |
---|
68 | n/a | urlparse(self.repository) |
---|
69 | n/a | if params or query or fragments: |
---|
70 | n/a | raise AssertionError("Incompatible url %s" % self.repository) |
---|
71 | n/a | |
---|
72 | n/a | if schema not in ('http', 'https'): |
---|
73 | n/a | raise AssertionError("unsupported schema " + schema) |
---|
74 | n/a | |
---|
75 | n/a | # Sign if requested |
---|
76 | n/a | if self.sign: |
---|
77 | n/a | gpg_args = ["gpg", "--detach-sign", "-a", filename] |
---|
78 | n/a | if self.identity: |
---|
79 | n/a | gpg_args[2:2] = ["--local-user", self.identity] |
---|
80 | n/a | spawn(gpg_args, |
---|
81 | n/a | dry_run=self.dry_run) |
---|
82 | n/a | |
---|
83 | n/a | # Fill in the data - send all the meta-data in case we need to |
---|
84 | n/a | # register a new release |
---|
85 | n/a | f = open(filename,'rb') |
---|
86 | n/a | try: |
---|
87 | n/a | content = f.read() |
---|
88 | n/a | finally: |
---|
89 | n/a | f.close() |
---|
90 | n/a | meta = self.distribution.metadata |
---|
91 | n/a | data = { |
---|
92 | n/a | # action |
---|
93 | n/a | ':action': 'file_upload', |
---|
94 | n/a | 'protocol_version': '1', |
---|
95 | n/a | |
---|
96 | n/a | # identify release |
---|
97 | n/a | 'name': meta.get_name(), |
---|
98 | n/a | 'version': meta.get_version(), |
---|
99 | n/a | |
---|
100 | n/a | # file content |
---|
101 | n/a | 'content': (os.path.basename(filename),content), |
---|
102 | n/a | 'filetype': command, |
---|
103 | n/a | 'pyversion': pyversion, |
---|
104 | n/a | 'md5_digest': hashlib.md5(content).hexdigest(), |
---|
105 | n/a | |
---|
106 | n/a | # additional meta-data |
---|
107 | n/a | 'metadata_version': '1.0', |
---|
108 | n/a | 'summary': meta.get_description(), |
---|
109 | n/a | 'home_page': meta.get_url(), |
---|
110 | n/a | 'author': meta.get_contact(), |
---|
111 | n/a | 'author_email': meta.get_contact_email(), |
---|
112 | n/a | 'license': meta.get_licence(), |
---|
113 | n/a | 'description': meta.get_long_description(), |
---|
114 | n/a | 'keywords': meta.get_keywords(), |
---|
115 | n/a | 'platform': meta.get_platforms(), |
---|
116 | n/a | 'classifiers': meta.get_classifiers(), |
---|
117 | n/a | 'download_url': meta.get_download_url(), |
---|
118 | n/a | # PEP 314 |
---|
119 | n/a | 'provides': meta.get_provides(), |
---|
120 | n/a | 'requires': meta.get_requires(), |
---|
121 | n/a | 'obsoletes': meta.get_obsoletes(), |
---|
122 | n/a | } |
---|
123 | n/a | comment = '' |
---|
124 | n/a | if command == 'bdist_rpm': |
---|
125 | n/a | dist, version, id = platform.dist() |
---|
126 | n/a | if dist: |
---|
127 | n/a | comment = 'built for %s %s' % (dist, version) |
---|
128 | n/a | elif command == 'bdist_dumb': |
---|
129 | n/a | comment = 'built for %s' % platform.platform(terse=1) |
---|
130 | n/a | data['comment'] = comment |
---|
131 | n/a | |
---|
132 | n/a | if self.sign: |
---|
133 | n/a | data['gpg_signature'] = (os.path.basename(filename) + ".asc", |
---|
134 | n/a | open(filename+".asc", "rb").read()) |
---|
135 | n/a | |
---|
136 | n/a | # set up the authentication |
---|
137 | n/a | user_pass = (self.username + ":" + self.password).encode('ascii') |
---|
138 | n/a | # The exact encoding of the authentication string is debated. |
---|
139 | n/a | # Anyway PyPI only accepts ascii for both username or password. |
---|
140 | n/a | auth = "Basic " + standard_b64encode(user_pass).decode('ascii') |
---|
141 | n/a | |
---|
142 | n/a | # Build up the MIME payload for the POST data |
---|
143 | n/a | boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' |
---|
144 | n/a | sep_boundary = b'\r\n--' + boundary.encode('ascii') |
---|
145 | n/a | end_boundary = sep_boundary + b'--\r\n' |
---|
146 | n/a | body = io.BytesIO() |
---|
147 | n/a | for key, value in data.items(): |
---|
148 | n/a | title = '\r\nContent-Disposition: form-data; name="%s"' % key |
---|
149 | n/a | # handle multiple entries for the same name |
---|
150 | n/a | if not isinstance(value, list): |
---|
151 | n/a | value = [value] |
---|
152 | n/a | for value in value: |
---|
153 | n/a | if type(value) is tuple: |
---|
154 | n/a | title += '; filename="%s"' % value[0] |
---|
155 | n/a | value = value[1] |
---|
156 | n/a | else: |
---|
157 | n/a | value = str(value).encode('utf-8') |
---|
158 | n/a | body.write(sep_boundary) |
---|
159 | n/a | body.write(title.encode('utf-8')) |
---|
160 | n/a | body.write(b"\r\n\r\n") |
---|
161 | n/a | body.write(value) |
---|
162 | n/a | if value and value[-1:] == b'\r': |
---|
163 | n/a | body.write(b'\n') # write an extra newline (lurve Macs) |
---|
164 | n/a | body.write(end_boundary) |
---|
165 | n/a | body = body.getvalue() |
---|
166 | n/a | |
---|
167 | n/a | msg = "Submitting %s to %s" % (filename, self.repository) |
---|
168 | n/a | self.announce(msg, log.INFO) |
---|
169 | n/a | |
---|
170 | n/a | # build the Request |
---|
171 | n/a | headers = { |
---|
172 | n/a | 'Content-type': 'multipart/form-data; boundary=%s' % boundary, |
---|
173 | n/a | 'Content-length': str(len(body)), |
---|
174 | n/a | 'Authorization': auth, |
---|
175 | n/a | } |
---|
176 | n/a | |
---|
177 | n/a | request = Request(self.repository, data=body, |
---|
178 | n/a | headers=headers) |
---|
179 | n/a | # send the data |
---|
180 | n/a | try: |
---|
181 | n/a | result = urlopen(request) |
---|
182 | n/a | status = result.getcode() |
---|
183 | n/a | reason = result.msg |
---|
184 | n/a | except HTTPError as e: |
---|
185 | n/a | status = e.code |
---|
186 | n/a | reason = e.msg |
---|
187 | n/a | except OSError as e: |
---|
188 | n/a | self.announce(str(e), log.ERROR) |
---|
189 | n/a | raise |
---|
190 | n/a | |
---|
191 | n/a | if status == 200: |
---|
192 | n/a | self.announce('Server response (%s): %s' % (status, reason), |
---|
193 | n/a | log.INFO) |
---|
194 | n/a | if self.show_response: |
---|
195 | n/a | text = self._read_pypi_response(result) |
---|
196 | n/a | msg = '\n'.join(('-' * 75, text, '-' * 75)) |
---|
197 | n/a | self.announce(msg, log.INFO) |
---|
198 | n/a | else: |
---|
199 | n/a | msg = 'Upload failed (%s): %s' % (status, reason) |
---|
200 | n/a | self.announce(msg, log.ERROR) |
---|
201 | n/a | raise DistutilsError(msg) |
---|