This repository was archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
149 lines (116 loc) · 4.63 KB
/
util.py
File metadata and controls
149 lines (116 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from argparse import ArgumentParser, ArgumentError
from configparser import ConfigParser, ParsingError
from src.exceptions import DbException
from sqlite3 import connect, Error
import sys
import os
def get_cli_args() -> ArgumentParser:
"""Parse CLI args.
:return: ArgumentParser
"""
parser = ArgumentParser()
try:
parser.add_argument(
'-d', '--dev',
action='store_true',
required=False,
help='Run the development version of the script')
parser.add_argument(
'-tr', '--testrail',
action='store_true',
required=False,
help='If true, will reconcile TestRail after Trello is complete.')
except ArgumentError as err:
raise err
else:
return parser
def get_configs(fields: list, config_path: str) -> dict:
"""parse a config.ini file for specific keys
:param fields:
:param config_path:
:return res: a dict of values parsed from key-value pairs stored in the .ini file
"""
if not config_path or config_path is None or not os.path.exists(config_path):
raise FileNotFoundError("Invalid config file")
config = ConfigParser()
try:
config.read(config_path)
except ParsingError as e:
raise e
res = {}
for field_name in fields:
for membername in config.sections():
for k, v in config.items(membername):
if k == field_name and membername not in res.keys():
res[membername] = {}
if k == field_name and k not in res[membername].keys():
res[membername][k] = v
return res
def is_db_init(db_path: str) -> bool:
"""Get the count of all records in commits table.
If count is 0, return false. If .db file does not exist, return false. Else, return true.
:param db_path: path to the sqlite (.db) file
:return: True if db is initialized, otherwise False
"""
if not db_path or db_path is None:
raise DbException("Database path required for this operation")
if not os.path.exists(db_path):
return False
sql = "SELECT COUNT(*) FROM commits"
try:
conn = connect(db_path)
cursor = conn.cursor()
cursor.execute(sql)
result = cursor.fetchone()
conn.close()
except Error as sqle:
raise (sqle, "Database operation failed")
return True if result[0] > 0 else False
def git_db_setup(db_path: str) -> None:
"""Set up git database the old way."""
if not db_path or db_path is None:
raise DbException("Database path required for this operation")
# Make sure we aren't running inside task_scripts
if 'task_scripts' in os.path.join(__file__):
print(__file__, "must not be in task_scripts/")
sys.exit(-1)
dbpath = os.path.join('data', 'git_treasures.db')
# Let's start making databases!
connection = connect(dbpath)
cursor = connection.cursor()
create_tables = "CREATE TABLE IF NOT EXISTS commits (commitID int, hash text, committerDate text, mainBranch text, otherBranches text, author_name text, author_email text, commitMessage text )"
create_index = "CREATE UNIQUE INDEX IF NOT EXISTS commit_hash ON commits (hash)"
get_tables = "SELECT name FROM sqlite_master WHERE type='table'"
try:
cursor.execute(create_tables)
cursor.execute(create_index)
cursor.execute(get_tables)
connection.commit()
# Should see "commits" table
tables = cursor.fetchall()
print(tables)
except Error as sqle:
raise (sqle, "Database operation failed")
def gitlab_db_setup(db_path: str) -> None:
"""Set up git database the new way (with GitLabLog)."""
if not db_path or db_path is None:
raise DbException("Database path required for this operation")
# create the database
connection = connect(db_path)
cursor = connection.cursor()
create_tables = "CREATE TABLE IF NOT EXISTS commits (commitID int, hash text, committerDate text, mainBranch text, author_name text, author_email text, commitMessage text )"
create_index = "CREATE UNIQUE INDEX IF NOT EXISTS commit_hash ON commits (hash)"
get_tables = "SELECT name FROM sqlite_master WHERE type='table'"
try:
cursor.execute(create_tables)
cursor.execute(create_index)
cursor.execute(get_tables)
connection.commit()
# Should see "commits" table
tables = cursor.fetchall()
print(tables)
connection.close()
except Error as sqle:
raise (sqle, "Database operation failed")