|
1 | 1 | from __future__ import print_function, absolute_import
|
2 | 2 | import os
|
3 | 3 | import re
|
4 |
| -import sys |
5 | 4 | from distutils.errors import DistutilsSetupError
|
6 | 5 | from .utils import Binding, Strip
|
7 | 6 |
|
8 |
| -try: |
9 |
| - import configparser |
10 |
| -except ImportError: |
11 |
| - import ConfigParser as configparser |
12 |
| - |
13 |
| - |
14 | 7 | import semantic_version
|
15 | 8 |
|
16 | 9 |
|
@@ -99,18 +92,28 @@ def __init__(self, target, path,
|
99 | 92 | self.path = path
|
100 | 93 |
|
101 | 94 | def get_lib_name(self):
|
102 |
| - cfg = configparser.ConfigParser() |
103 |
| - cfg.read(self.path) |
104 |
| - section = 'lib' if cfg.has_option('lib', 'name') else 'package' |
105 |
| - name = cfg.get(section, 'name').strip('\'\"').strip() |
106 |
| - return re.sub(r"[./\\-]", "_", name) |
| 95 | + """ Parse Cargo.toml to get the name of the shared library. """ |
| 96 | + # We import in here to make sure the the setup_requires are already installed |
| 97 | + import toml |
| 98 | + |
| 99 | + cfg = toml.load(self.path) |
| 100 | + name = cfg.get('lib', {}).get('name') |
| 101 | + if name is None: |
| 102 | + name = cfg.get('package', {}).get('name') |
| 103 | + if name is None: |
| 104 | + raise Exception( |
| 105 | + "Can not parse library name from Cargo.toml. " |
| 106 | + "Cargo.toml missing value for 'name' key " |
| 107 | + "in both the [package] section and the [lib] section") |
| 108 | + name = re.sub(r"[./\\-]", "_", name) |
| 109 | + return name |
107 | 110 |
|
108 | 111 | def get_rust_version(self):
|
109 | 112 | if self.rust_version is None:
|
110 | 113 | return None
|
111 | 114 | try:
|
112 | 115 | return semantic_version.Spec(self.rust_version)
|
113 |
| - except: |
| 116 | + except ValueError: |
114 | 117 | raise DistutilsSetupError(
|
115 | 118 | 'Can not parse rust compiler version: %s', self.rust_version)
|
116 | 119 |
|
|
0 commit comments