Last active
December 10, 2018 06:52
-
-
Save bennyyip/2e13fb78af91e10685b7fe6037e4b4bc to your computer and use it in GitHub Desktop.
crates2pkgbuild
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| import urllib.request | |
| import sys | |
| template = '''\ | |
| # Maintainer: BennyYip <yebenmy@protonmail.com> | |
| pkgname={pkgname} | |
| _pkgname={name} | |
| pkgver={max_version} | |
| pkgrel=1 | |
| pkgdesc="{description}" | |
| arch=('x86_64') | |
| url="{homepage}" | |
| license=('{license}') | |
| makedepends=('rust' 'cargo') | |
| depends=() | |
| provides=("$_pkgname") | |
| source=($_pkgname.tar.gz::"https://crates.io//api/v1/crates/$_pkgname/$pkgver/download") | |
| sha256sums=('SKIP') | |
| build() {{ | |
| cd $srcdir/$_pkgname-$pkgver | |
| CARGO_INCREMENTAL=0 cargo build --release | |
| }} | |
| package() {{ | |
| cd $srcdir/$_pkgname-$pkgver | |
| install -Dm 755 "target/release/$pkgname" "$pkgdir/usr/bin/$pkgname" | |
| }} | |
| # vim:set sw=2 et: | |
| ''' | |
| def get_pkgbuild(j): | |
| info = j['crate'] | |
| info['name'] = info['name'].lower() | |
| if info['homepage'] is None: | |
| info['homepage'] = info['repository'] | |
| try: | |
| info['license'] = j['versions'][0]['license'].split()[0] | |
| except IndexError: | |
| info['license'] = '(FIXME)' | |
| s = template.format_map(info) | |
| return s | |
| def get_json(name): | |
| url = f'https://crates.io//api/v1/crates/{name}' | |
| res = urllib.request.urlopen(url) | |
| data = res.read().decode('utf-8') | |
| j = json.loads(data) | |
| return j | |
| def main(): | |
| if len(sys.argv) < 2: | |
| sys.exit('Crate name?') | |
| name = sys.argv[1] | |
| if len(sys.argv) > 2: | |
| pkgname = sys.argv[2] | |
| else: | |
| pkgname = name | |
| j = get_json(name) | |
| j['crate']['pkgname'] = pkgname | |
| pkg = get_pkgbuild(j) | |
| sys.stdout.write(pkg) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment