Skip to content

Commit 4ff5110

Browse files
author
Thomas Preud'homme
committed
[LNT] Python 3 support: Use absolute import by default
Forces module import to be absolute by default on Python 2 since this is the default for Python 3. Adapts relative imports to use the relative import symtax 'from . import foo' or 'from .pkg import foo'. Also remove unnecessary circular import dependency between testsuite and testsuitedb by moving functions used by testsuite but currently in testsuitedb into module testsuite. Finally adds type hints into lnt.formats to avoid warnings from mypy due to change to relative import syntax. Changes to adapt syntax of relative import was produced by running futurize's stage1 libfuturize.fixes.fix_absolute_import, while breaking of circular dependency was done manually. Reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls Reviewed By: hubert.reinterpretcast Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D67811 llvm-svn: 372551
1 parent 0bac267 commit 4ff5110

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

lnt/formats/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"""
99

1010
from __future__ import absolute_import
11+
from typing import List, Dict
1112
from .PlistFormat import format as plist
1213
from .JSONFormat import format as json
1314

14-
formats = [plist, json]
15-
formats_by_name = dict((f['name'], f) for f in formats)
16-
format_names = list(formats_by_name.keys())
15+
formats = [plist, json] # type: List[Dict]
16+
formats_by_name = dict((f['name'], f) for f in formats) # type: Dict[str, Dict]
17+
format_names = list(formats_by_name.keys()) # type: List[str]
1718

1819

1920
def get_format(name):

lnt/server/db/testsuite.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sqlalchemy
1111
import sqlalchemy.ext.declarative
1212
import sqlalchemy.orm
13-
from sqlalchemy import Column, Integer, ForeignKey, String, Binary
13+
from sqlalchemy import Column, Integer, ForeignKey, String, Binary, Float
1414
from sqlalchemy.orm import relation
1515
from lnt.util import logger
1616

@@ -59,6 +59,35 @@ def __repr__(self):
5959
return '%s%r' % (self.__class__.__name__, (self.name,))
6060

6161

62+
_sample_type_to_sql = {
63+
'Real': Float,
64+
'Hash': String,
65+
'Status': Integer,
66+
}
67+
68+
69+
def is_known_sample_type(name):
70+
return name in _sample_type_to_sql
71+
72+
73+
def make_sample_column(name, type):
74+
sqltype = _sample_type_to_sql.get(type)
75+
if sqltype is None:
76+
raise ValueError("test suite defines unknown sample type %r" % type)
77+
options = []
78+
if type == 'Status':
79+
options.append(ForeignKey(StatusKind.id))
80+
return Column(name, sqltype, *options)
81+
82+
83+
def make_run_column(name):
84+
return Column(name, String(256))
85+
86+
87+
def make_machine_column(name):
88+
return Column(name, String(256))
89+
90+
6291
class _MigrationError(Exception):
6392
def __init__(self, message):
6493
full_message = \
@@ -114,7 +143,6 @@ def __repr__(self):
114143

115144
@staticmethod
116145
def from_json(data):
117-
from . import testsuitedb
118146
if data.get('format_version') != '2':
119147
raise ValueError("Expected \"format_version\": \"2\" in schema")
120148
name = data['name']
@@ -150,7 +178,7 @@ def from_json(data):
150178
display_name = metric_desc.get('display_name')
151179
unit = metric_desc.get('unit')
152180
unit_abbrev = metric_desc.get('unit_abbrev')
153-
if not testsuitedb.is_known_sample_type(metric_type_name):
181+
if not is_known_sample_type(metric_type_name):
154182
raise ValueError("Unknown metric type '%s'" %
155183
metric_type_name)
156184
metric_type = SampleType(metric_type_name)
@@ -350,7 +378,6 @@ def copy_info(self, other):
350378

351379

352380
def _upgrade_to(connectable, tsschema, new_schema, dry_run=False):
353-
from . import testsuitedb
354381
new = json.loads(new_schema.jsonschema)
355382
old = json.loads(tsschema.jsonschema)
356383
ts_name = new['name']
@@ -371,7 +398,7 @@ def _upgrade_to(connectable, tsschema, new_schema, dry_run=False):
371398
name)
372399
elif not dry_run:
373400
# Add missing columns
374-
column = testsuitedb.make_sample_column(name, type)
401+
column = make_sample_column(name, type)
375402
util.add_column(connectable, '%s_Sample' % ts_name, column)
376403

377404
if len(old_metrics) != 0:
@@ -398,7 +425,7 @@ def _upgrade_to(connectable, tsschema, new_schema, dry_run=False):
398425
old_field = old_run_fields.pop(name, None)
399426
# Add missing columns
400427
if old_field is None and not dry_run:
401-
column = testsuitedb.make_run_column(name)
428+
column = make_run_column(name)
402429
util.add_column(connectable, '%s_Run' % ts_name, column)
403430

404431
if len(old_run_fields) > 0:
@@ -418,7 +445,7 @@ def _upgrade_to(connectable, tsschema, new_schema, dry_run=False):
418445
old_field = old_machine_fields.pop(name, None)
419446
# Add missing columns
420447
if old_field is None and not dry_run:
421-
column = testsuitedb.make_machine_column(name)
448+
column = make_machine_column(name)
422449
util.add_column(connectable, '%s_Machine' % ts_name, column)
423450

424451
if len(old_machine_fields) > 0:

lnt/server/db/testsuitedb.py

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,6 @@ def _dict_update_abort_on_duplicates(base_dict, to_merge):
3333
base_dict[key] = value
3434

3535

36-
_sample_type_to_sql = {
37-
'Real': Float,
38-
'Hash': String,
39-
'Status': Integer,
40-
}
41-
42-
43-
def is_known_sample_type(name):
44-
return name in _sample_type_to_sql
45-
46-
47-
def make_sample_column(name, type):
48-
sqltype = _sample_type_to_sql.get(type)
49-
if sqltype is None:
50-
raise ValueError("test suite defines unknown sample type %r" % type)
51-
options = []
52-
if type == 'Status':
53-
options.append(ForeignKey(testsuite.StatusKind.id))
54-
return Column(name, sqltype, *options)
55-
56-
57-
def make_run_column(name):
58-
return Column(name, String(256))
59-
60-
61-
def make_machine_column(name):
62-
return Column(name, String(256))
63-
64-
6536
class MachineInfoChanged(ValueError):
6637
pass
6738

@@ -153,7 +124,8 @@ class Machine(self.base, ParameterizedMixin):
153124
raise ValueError("test suite defines reserved key %r" % (
154125
iname))
155126

156-
class_dict[iname] = item.column = make_machine_column(iname)
127+
item.column = testsuite.make_machine_column(iname)
128+
class_dict[iname] = item.column
157129

158130
def __init__(self, name_value):
159131
self.id = None
@@ -356,7 +328,8 @@ class Run(self.base, ParameterizedMixin):
356328
raise ValueError("test suite defines reserved key %r" %
357329
(iname,))
358330

359-
class_dict[iname] = item.column = make_run_column(iname)
331+
item.column = testsuite.make_run_column(iname)
332+
class_dict[iname] = item.column
360333

361334
def __init__(self, new_id, machine, order, start_time, end_time):
362335
self.id = new_id
@@ -537,7 +510,8 @@ def get_hash_of_binary_field():
537510
raise ValueError("test suite defines reserved key %r" %
538511
(iname,))
539512

540-
item.column = make_sample_column(iname, item.type.name)
513+
item.column = testsuite.make_sample_column(iname,
514+
item.type.name)
541515
class_dict[iname] = item.column
542516

543517
def __init__(self, run, test, **kwargs):

lnt/tests/nt.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""LLVM test-suite compile and execution tests"""
2+
from __future__ import absolute_import
23
from __future__ import print_function
34
import csv
45
import os
@@ -33,7 +34,7 @@
3334
from lnt.server.reporting.analysis import REGRESSED, IMPROVED
3435
from lnt.util import logger
3536
from lnt.lnttool.common import submit_options
36-
import builtintest
37+
from . import builtintest
3738

3839

3940
class TestModule(object):

0 commit comments

Comments
 (0)