Add adding data feature
This commit is contained in:
parent
7eb7a495e8
commit
ec29a3d956
3
.gitignore
vendored
3
.gitignore
vendored
@ -51,6 +51,9 @@ doc/build
|
||||
AUTHORS
|
||||
ChangeLog
|
||||
|
||||
# config files
|
||||
etc/*.conf
|
||||
|
||||
# Editors
|
||||
*~
|
||||
.*.swp
|
||||
|
@ -16,7 +16,7 @@ import collections
|
||||
import datetime
|
||||
|
||||
from oslo_config import cfg
|
||||
#from oslo_db.sqlalchemy import session as db_session
|
||||
from oslo_db.sqlalchemy import session as db_session
|
||||
import six
|
||||
import sqlalchemy
|
||||
from sqlalchemy.engine.url import make_url
|
||||
@ -25,7 +25,6 @@ import logging
|
||||
|
||||
from coverage2sql.db import models
|
||||
#from coverage2sql import exceptions
|
||||
#from coverage2sql import read_coverage
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.register_cli_opt(cfg.BoolOpt('verbose', short='v', default=False,
|
||||
@ -68,3 +67,35 @@ def get_session(autocommit=True, expire_on_commit=False):
|
||||
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
|
||||
|
||||
return session
|
||||
|
||||
|
||||
def create_coverage(project_name, coverage_rate=0.0, report_time=None,
|
||||
session=None):
|
||||
"""Create a new coverage record in the database.
|
||||
|
||||
This method is used to add a new coverage in the database.
|
||||
It tracks the coverage history.
|
||||
|
||||
:param str project_name: project_name e.g. openstack/tempest
|
||||
:param float coverage_rate: coverage_rate defaults to 0
|
||||
:param datetime.Datetime report_time: when the coverage was collected
|
||||
defaults to None
|
||||
:param session: optional session object if one isn't provided a new session
|
||||
will be acquired for the duration of this operation
|
||||
:return: The coverage object stored in the DB
|
||||
:rtype: coverage2sql.models.Coverage
|
||||
"""
|
||||
coverage = models.Coverage()
|
||||
coverage.project_name = project_name
|
||||
coverage.coverage_rate = coverage_rate
|
||||
if report_time:
|
||||
report_time = report_time.replace(tzinfo=None)
|
||||
report_time_microsecond = report_time.microsecond
|
||||
else:
|
||||
report_time_microsecond = None
|
||||
coverage.report_time = report_time
|
||||
coverage.report_time_microsecond = report_time_microsecond
|
||||
session = session or get_session()
|
||||
with session.begin():
|
||||
session.add(coverage)
|
||||
return coverage
|
||||
|
@ -54,5 +54,5 @@ class Coverage(BASE, CoverageBase):
|
||||
project_name = sa.Column(sa.String(256),
|
||||
nullable=False)
|
||||
coverage_rate = sa.Column(sa.Float())
|
||||
report_time = sa.Column(sa.DateTime())
|
||||
report_time = sa.Column(sa.DateTime(), default=datetime.datetime.now())
|
||||
report_time_microsecond = sa.Column(sa.Integer(), default=0)
|
||||
|
37
coverage2sql/read_coverage.py
Normal file
37
coverage2sql/read_coverage.py
Normal file
@ -0,0 +1,37 @@
|
||||
# Copyright 2016 Hewlett Packard Enterprise Development LP
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import coverage
|
||||
|
||||
|
||||
class DevNull(object):
|
||||
"""
|
||||
A file like '/dev/null'
|
||||
"""
|
||||
def write(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class ReadCoverage(object):
|
||||
|
||||
def __init__(self, coverage_file=None):
|
||||
self.cov = coverage.Coverage(data_file=coverage_file)
|
||||
self.cov.load()
|
||||
self.cov_pct = self.cov.report(file=DevNull())
|
||||
|
||||
def get_data(self):
|
||||
return self.cov.get_data()
|
||||
|
||||
def get_coverage_rate(self):
|
||||
return self.cov_pct / 100
|
@ -17,20 +17,23 @@ import copy
|
||||
import sys
|
||||
|
||||
from oslo_config import cfg
|
||||
# from oslo_db import options
|
||||
from oslo_db import options
|
||||
from pbr import version
|
||||
from stevedore import enabled
|
||||
|
||||
from coverage2sql.db import api
|
||||
# from coverage2sql import exceptions
|
||||
# from coverage2sql import read_subunit as subunit
|
||||
from coverage2sql import read_coverage as coverage
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.import_opt('verbose', 'coverage2sql.db.api')
|
||||
|
||||
SHELL_OPTS = [
|
||||
cfg.MultiStrOpt('coverage_files', positional=True,
|
||||
help='list of coverage files to put into the database'),
|
||||
cfg.StrOpt('project_name', positional=False,
|
||||
help='project name of the coverage files'),
|
||||
cfg.StrOpt('coverage_file', positional=False,
|
||||
help='A coverage file to put into the database'),
|
||||
cfg.StrOpt('connection'),
|
||||
]
|
||||
|
||||
_version_ = version.VersionInfo('coverage2sql').version_string()
|
||||
@ -51,25 +54,25 @@ def list_opts():
|
||||
|
||||
|
||||
def parse_args(argv, default_config_files=None):
|
||||
# cfg.CONF.register_cli_opts(options.database_opts, group='database')
|
||||
cfg.CONF.register_cli_opts(options.database_opts, group='database')
|
||||
cfg.CONF(argv[1:], project='coverage2sql', version=_version_,
|
||||
default_config_files=default_config_files)
|
||||
|
||||
|
||||
def process_results(results):
|
||||
print(results)
|
||||
def process_results(project_name=".", coverage_rate=0.0):
|
||||
session = api.get_session()
|
||||
api.create_coverage(project_name, coverage_rate)
|
||||
session.close()
|
||||
|
||||
|
||||
def main():
|
||||
cli_opts()
|
||||
|
||||
parse_args(sys.argv)
|
||||
if CONF.coverage_files:
|
||||
print("From file:")
|
||||
process_results("FIXME") # FIXME
|
||||
else:
|
||||
print("From stdin:")
|
||||
process_results("FIXME") # FIXME
|
||||
project_name = CONF.project_name
|
||||
cov = coverage.ReadCoverage(CONF.coverage_file)
|
||||
coverage_rate = cov.get_coverage_rate()
|
||||
process_results(project_name, coverage_rate)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -6,3 +6,5 @@ pbr>=1.6
|
||||
SQLAlchemy>=0.8.2
|
||||
alembic>=0.4.1
|
||||
oslo.config>=1.4.0.0a3
|
||||
oslo.db!=1.12.0
|
||||
Babel>=1.3,!=2.3.0,!=2.3.1,!=2.3.2,!=2.3.3 # BSD
|
||||
|
Loading…
x
Reference in New Issue
Block a user