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