Skip to content

Instantly share code, notes, and snippets.

@hanepjiv hanepjiv/cargo-deny.py
Last active Nov 29, 2018

Embed
What would you like to do?
#!/bin/env python
# -*- mode:python; coding:utf-8-unix; -*-
'''
cargo-deny.py
@author hanepjiv <hanepjiv@gmail.com>
@since 2018/06/23
@date 2018/10/03
'''
import os
import re
import sys
import subprocess
# #############################################################################
# =============================================================================
CURRENT = 1
AGE = 1
REVISION = 1
PRERELEASE = ''
BUILDMETA = ''
# -----------------------------------------------------------------------------
MAJOR = CURRENT - AGE
RELEASE = '{}.{}.{}{}{}'.format(MAJOR, AGE, REVISION, PRERELEASE, BUILDMETA)
# =============================================================================
HERE = os.path.dirname(os.path.realpath(__file__))
# -----------------------------------------------------------------------------
RUSTUP_RUN = [ os.path.join(os.environ['HOME'], '.cargo', 'bin', 'rustup'),
'run' ]
RUSTC_VERSION = ['rustc', '-V']
RUSTC_W_HELP = ['rustc', '-W', 'help']
PATTERN = re.compile(r'^ *([^ ]*) +(allow|warn|deny).*$')
IGNORES = ['warnings']
# #############################################################################
# =============================================================================
def app(args):
'''app
'''
# rustup run
rustup_run = []
rustup_run.extend(RUSTUP_RUN)
rustup_run.append(args.toolchain)
sys.stdout.write('// ')
# rustc -V
rustc_version = list(rustup_run)
rustc_version.extend(RUSTC_VERSION)
sys.stdin.flush()
sys.stdout.flush()
sys.stderr.flush()
res = subprocess.run(rustc_version,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sys.stdout.buffer.write(res.stdout) # without encode
# sys.stdout.write(res.stdout.encode('utf-8'))
# rustc -W help
rustc_w_help = list(rustup_run)
rustc_w_help.extend(RUSTC_W_HELP)
sys.stdin.flush()
sys.stdout.flush()
sys.stderr.flush()
res = subprocess.run(rustc_w_help,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(u'#![deny(')
for line in res.stdout.decode('utf-8').splitlines():
match = PATTERN.match(line)
if not match:
continue
ret = match.group(1)
if not ret or ret in IGNORES:
continue
ret = ret.replace('-', '_') + ', '
sys.stdout.write(ret)
sys.stdout.write(os.linesep)
print(')]')
# =============================================================================
def _parse_args_():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('attr', type=str)
parser.add_argument('-V', '--version', action='version', version=RELEASE)
parser.add_argument('-t', '--toolchain', default='stable')
return parser.parse_args()
# =============================================================================
if __name__ == '__main__':
app(_parse_args_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.