cargo/publish.py

56 lines
1.4 KiB
Python
Raw Normal View History

2019-09-05 16:02:27 +00:00
#!/usr/bin/env python3
# This script is used to publish Cargo to crates.io.
import os
import re
import subprocess
2020-07-16 22:21:51 +00:00
import time
2019-09-05 16:02:27 +00:00
import urllib.request
from urllib.error import HTTPError
TO_PUBLISH = [
'crates/cargo-platform',
2021-03-20 18:28:38 +00:00
'crates/cargo-util',
2019-09-05 16:02:27 +00:00
'crates/crates-io',
'.',
]
def already_published(name, version):
try:
urllib.request.urlopen('https://crates.io/api/v1/crates/%s/%s/download' % (name, version))
except HTTPError as e:
if e.code == 404:
return False
raise
return True
def maybe_publish(path):
content = open(os.path.join(path, 'Cargo.toml')).read()
name = re.search('^name = "([^"]+)"', content, re.M).group(1)
version = re.search('^version = "([^"]+)"', content, re.M).group(1)
if already_published(name, version):
print('%s %s is already published, skipping' % (name, version))
2020-07-16 22:21:51 +00:00
return False
2019-09-05 16:02:27 +00:00
subprocess.check_call(['cargo', 'publish', '--no-verify'], cwd=path)
2020-07-16 22:21:51 +00:00
return True
2019-09-05 16:02:27 +00:00
def main():
print('Starting publish...')
2020-07-16 22:21:51 +00:00
for i, path in enumerate(TO_PUBLISH):
if maybe_publish(path):
if i < len(TO_PUBLISH)-1:
# Sleep to allow the index to update. This should probably
# check that the index is updated, or use a retry loop
# instead.
time.sleep(5)
2019-09-05 16:02:27 +00:00
print('Publish complete!')
if __name__ == '__main__':
main()