]> git.proxmox.com Git - cargo.git/blob - publish.py
Fix self-publish script.
[cargo.git] / publish.py
1 #!/usr/bin/env python3
2
3 # This script is used to publish Cargo to crates.io.
4
5 import os
6 import re
7 import subprocess
8 import time
9 import urllib.request
10 from urllib.error import HTTPError
11
12
13 TO_PUBLISH = [
14 'crates/cargo-platform',
15 'crates/crates-io',
16 '.',
17 ]
18
19
20 def already_published(name, version):
21 try:
22 urllib.request.urlopen('https://crates.io/api/v1/crates/%s/%s/download' % (name, version))
23 except HTTPError as e:
24 if e.code == 404:
25 return False
26 raise
27 return True
28
29
30 def maybe_publish(path):
31 content = open(os.path.join(path, 'Cargo.toml')).read()
32 name = re.search('^name = "([^"]+)"', content, re.M).group(1)
33 version = re.search('^version = "([^"]+)"', content, re.M).group(1)
34 if already_published(name, version):
35 print('%s %s is already published, skipping' % (name, version))
36 return False
37 subprocess.check_call(['cargo', 'publish', '--no-verify'], cwd=path)
38 return True
39
40
41 def main():
42 print('Starting publish...')
43 for i, path in enumerate(TO_PUBLISH):
44 if maybe_publish(path):
45 if i < len(TO_PUBLISH)-1:
46 # Sleep to allow the index to update. This should probably
47 # check that the index is updated, or use a retry loop
48 # instead.
49 time.sleep(5)
50 print('Publish complete!')
51
52
53 if __name__ == '__main__':
54 main()