1
|
|
|
#!/usr/bin/env python3 |
2
|
5 |
|
"""This module is used for dealing with SQL databases, including CSV export.""" |
3
|
|
|
|
4
|
5 |
|
import csv # write to csv |
5
|
5 |
|
import operator # for sorting |
6
|
5 |
|
import os # paths |
7
|
5 |
|
import sqlite3 # the sql library |
8
|
5 |
|
import time # current date |
9
|
|
|
|
10
|
5 |
|
from bbarchivist import decorators # sql handlers |
11
|
5 |
|
from bbarchivist import iniconfig # config directory |
12
|
|
|
|
13
|
5 |
|
__author__ = "Thurask" |
14
|
5 |
|
__license__ = "WTFPL v2" |
15
|
5 |
|
__copyright__ = "2015-2019 Thurask" |
16
|
|
|
|
17
|
|
|
|
18
|
5 |
|
def prepare_path(): |
19
|
|
|
""" |
20
|
|
|
Figure out where the path is. |
21
|
|
|
""" |
22
|
5 |
|
sqlpath = os.path.join(iniconfig.config_homepath(None), "bbarchivist.db") |
23
|
5 |
|
return sqlpath |
24
|
|
|
|
25
|
|
|
|
26
|
5 |
|
@decorators.sql_excepthandler("False") |
27
|
|
|
def prepare_sw_db(): |
28
|
|
|
""" |
29
|
|
|
Create SQLite database, if not already existing. |
30
|
|
|
""" |
31
|
5 |
|
cnxn = sqlite3.connect(prepare_path()) |
32
|
5 |
|
with cnxn: |
33
|
5 |
|
crs = cnxn.cursor() |
34
|
5 |
|
reqid = "INTEGER PRIMARY KEY" |
35
|
5 |
|
reqs = "TEXT NOT NULL UNIQUE COLLATE NOCASE" |
36
|
5 |
|
reqs2 = "TEXT NOT NULL" |
37
|
5 |
|
table = "Swrelease(Id {0}, Os {1}, Software {1}, Available {2}, Date {2})".format( |
38
|
|
|
reqid, reqs, reqs2) |
39
|
5 |
|
crs.execute("CREATE TABLE IF NOT EXISTS " + table) |
40
|
|
|
|
41
|
|
|
|
42
|
5 |
|
@decorators.sql_excepthandler("True") |
43
|
5 |
|
def insert(osversion, swrelease, available, curdate=None): |
44
|
|
|
""" |
45
|
|
|
Insert values into main SQLite database. |
46
|
|
|
|
47
|
|
|
:param osversion: OS version. |
48
|
|
|
:type osversion: str |
49
|
|
|
|
50
|
|
|
:param swrelease: Software release. |
51
|
|
|
:type swrelease: str |
52
|
|
|
|
53
|
|
|
:param servers: If release is available. String converted boolean. |
54
|
|
|
:type servers: str |
55
|
|
|
|
56
|
|
|
:param curdate: If None, today. For manual dates, specify this. |
57
|
|
|
:type curdate: str |
58
|
|
|
""" |
59
|
5 |
|
if curdate is None: |
60
|
5 |
|
curdate = time.strftime("%Y %B %d") |
61
|
5 |
|
cnxn = sqlite3.connect(prepare_path()) |
62
|
5 |
|
with cnxn: |
63
|
5 |
|
crs = cnxn.cursor() |
64
|
5 |
|
try: # insert if new |
65
|
5 |
|
crs.execute( |
66
|
|
|
"INSERT INTO Swrelease(Os, Software, Available, Date) VALUES (?,?,?,?)", |
67
|
|
|
(osversion, |
68
|
|
|
swrelease, |
69
|
|
|
available, |
70
|
|
|
curdate)) |
71
|
5 |
|
except sqlite3.IntegrityError: # update if not new |
72
|
5 |
|
crs.execute("UPDATE Swrelease SET Available=? WHERE Os=? AND Software=?", |
73
|
|
|
(available, osversion, swrelease)) |
74
|
|
|
|
75
|
|
|
|
76
|
5 |
|
@decorators.sql_excepthandler("False") |
77
|
|
|
def pop_sw_release(osversion, swrelease): |
78
|
|
|
""" |
79
|
|
|
Remove given entry from database. |
80
|
|
|
|
81
|
|
|
:param osversion: OS version. |
82
|
|
|
:type osversion: str |
83
|
|
|
|
84
|
|
|
:param swrelease: Software release. |
85
|
|
|
:type swrelease: str |
86
|
|
|
""" |
87
|
5 |
|
cnxn = sqlite3.connect(prepare_path()) |
88
|
5 |
|
with cnxn: |
89
|
5 |
|
crs = cnxn.cursor() |
90
|
5 |
|
crs.execute("DELETE FROM Swrelease WHERE Os=? AND Software=?", (osversion, swrelease)) |
91
|
|
|
|
92
|
|
|
|
93
|
5 |
|
@decorators.sql_excepthandler("False") |
94
|
|
|
def check_exists(osversion, swrelease): |
95
|
|
|
""" |
96
|
|
|
Check if we did this one already. |
97
|
|
|
|
98
|
|
|
:param osversion: OS version. |
99
|
|
|
:type osversion: str |
100
|
|
|
|
101
|
|
|
:param swrelease: Software release. |
102
|
|
|
:type swrelease: str |
103
|
|
|
""" |
104
|
5 |
|
cnxn = sqlite3.connect(prepare_path()) |
105
|
5 |
|
with cnxn: # check if exists |
106
|
5 |
|
crs = cnxn.cursor() |
107
|
5 |
|
exis = crs.execute( |
108
|
|
|
"SELECT EXISTS (SELECT 1 FROM Swrelease WHERE Os=? AND Software=?)", |
109
|
|
|
(osversion, |
110
|
|
|
swrelease)).fetchone()[0] |
111
|
5 |
|
return bool(exis) |
112
|
|
|
|
113
|
|
|
|
114
|
5 |
|
@decorators.sql_excepthandler("False") |
115
|
5 |
|
@decorators.sql_existhandler(prepare_path()) |
116
|
|
|
def export_sql_db(): |
117
|
|
|
""" |
118
|
|
|
Export main SQL database into a CSV file. |
119
|
|
|
""" |
120
|
5 |
|
cnxn = sqlite3.connect(prepare_path()) |
121
|
5 |
|
with cnxn: |
122
|
5 |
|
csvpath = os.path.join(os.path.expanduser("~"), "swrelease.csv") |
123
|
5 |
|
csvw = csv.writer(open(csvpath, "w"), dialect='excel') |
124
|
5 |
|
crs = cnxn.cursor() |
125
|
5 |
|
crs.execute("SELECT Os, Software, Available, Date FROM Swrelease") |
126
|
5 |
|
rows = crs.fetchall() |
127
|
5 |
|
sortedrows = sorted(rows, key=operator.itemgetter(0)) |
128
|
5 |
|
csvw.writerow(('OS Version', 'Software Release', 'Available', 'Date Modified')) |
129
|
5 |
|
csvw.writerows(sortedrows) |
130
|
5 |
|
print("EXPORTED!\n{0}".format(csvpath)) |
131
|
|
|
|
132
|
|
|
|
133
|
5 |
|
@decorators.sql_excepthandler("False") |
134
|
5 |
|
@decorators.sql_existhandler(prepare_path()) |
135
|
5 |
|
def list_sw_releases(avail=False): |
136
|
|
|
""" |
137
|
|
|
Return every SW/OS pair in the database. |
138
|
|
|
|
139
|
|
|
:param avail: If we filter out non-available results. Default is false. |
140
|
|
|
:type avail: bool |
141
|
|
|
""" |
142
|
5 |
|
cnxn = sqlite3.connect(prepare_path()) |
143
|
5 |
|
with cnxn: |
144
|
5 |
|
crs = cnxn.cursor() |
145
|
5 |
|
query = "SELECT Os, Software, Available, Date FROM Swrelease" |
146
|
5 |
|
if avail: |
147
|
5 |
|
query += " WHERE Available= 'available'" |
148
|
5 |
|
crs.execute(query) |
149
|
5 |
|
rows = crs.fetchall() |
150
|
|
|
return rows |
151
|
|
|
|