-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsetclubsback.py
More file actions
executable file
·67 lines (53 loc) · 2.5 KB
/
setclubsback.py
File metadata and controls
executable file
·67 lines (53 loc) · 2.5 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
#!/usr/bin/env python3
""" Single-use program to set the 'clubs' table and .CSV files back by one day """
import tmutil, sys, os
import tmglobals
myglobals = tmglobals.tmglobals()
### Insert classes and functions here. The main program begins in the "if" statement below.
if __name__ == "__main__":
import tmparms
import zipfile
import datetime
# Establish parameters
parms = tmparms.tmparms()
parms.add_argument('zipfiles', nargs='*', help='ZIP files in which to rename club.*.csv files')
parms.add_argument('--updatedatabase', dest='database', action='store_true' )
# Add other arguments here
# Do global setup
myglobals.setup(parms)
curs = myglobals.curs
conn = myglobals.conn
if len(parms.zipfiles) == 0 and not parms.database:
parms.parser.print_help()
sys.exit(1)
if parms.database:
# We have to delete and re-create the primary key to be able to do the update easily
curs.execute("START TRANSACTION")
curs.execute("ALTER TABLE loaded DROP PRIMARY KEY")
curs.execute("UPDATE loaded SET loadedfor = SUBDATE(loadedfor, 1) WHERE tablename = 'clubs'")
curs.execute("ALTER TABLE loaded ADD PRIMARY KEY (tablename, loadedfor)")
# And now we have to fix all the dates in the CLUBS table
# Again, we have a key to delete and re-create
curs.execute("ALTER TABLE clubs DROP KEY clubnumber")
curs.execute("UPDATE clubs SET firstdate = SUBDATE(firstdate, 1), lastdate = SUBDATE(lastdate, 1)")
curs.execute("ALTER TABLE clubs ADD KEY clubnumber (clubnumber, firstdate)")
curs.execute("COMMIT")
if parms.zipfiles:
for f in parms.zipfiles:
newname = f+'.new'
bakname = f+'.bak'
inzip = zipfile.ZipFile(f, 'r')
outzip = zipfile.ZipFile(newname,'w',zipfile.ZIP_DEFLATED)
items = inzip.infolist()
for item in items:
newitem = item
names = item.filename.split('.')
if names[0] == 'clubs' and names[-1] == 'csv':
# Change to yesterday
names[1] = (datetime.datetime.strptime(names[1], '%Y-%m-%d') - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
newitem.filename = '.'.join(names)
outzip.writestr(newitem,inzip.read(item))
inzip.close()
outzip.close()
os.rename(f, bakname)
os.rename(newname, f)