-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbrecon.py
1183 lines (1015 loc) · 43.9 KB
/
dbrecon.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#
#As a work of the United States government, this project is in the public
#domain within the United States. Additionally, we waive copyright and related
#rights in the work worldwide through the CC0 1.0 Universal public domain
#dedication (https://creativecommons.org/publicdomain/zero/1.0/)
"""dbrecon.py
Common code and constants for the database reconstruction. part of the
replication archive for The U.S. Census Bureau's Ex Post Confidentiality
Analysis of the 2010 Census Data Publications
(https://github.com/uscensusbureau/recon_replication)
"""
import argparse
import atexit
import codecs
import csv
import datetime
import glob
import gzip
import inspect
import io
import json
import logging
import logging.handlers
import os
import os.path
import pickle
import psutil
import re
import resource
import socket
import subprocess
import sys
import tempfile
import time
import urllib.parse
import xml.etree.ElementTree as ET
import zipfile
from configparser import ConfigParser
from os.path import dirname,basename,abspath
import boto3
import botocore
from botocore.exceptions import ClientError, WaiterError
import pandas as pd
# Make sure we can read ctools, which is in ..
MY_DIR = dirname(abspath(__file__))
PARENT_DIR = dirname(MY_DIR)
if PARENT_DIR not in sys.path:
sys.path.append( PARENT_DIR )
import ctools.s3 as s3
import ctools.clogging as clogging
from ctools.dbfile import DBMySQLAuth,DBMySQL
from ctools.gzfile import GZFile
from ctools.total_size import total_size
from dfxml.writer import DFXMLWriter
global dfxml_writer
dfxml_writer = None
REIDENT = os.getenv('REIDENT')
DB_RETRIES = 10
RETRY_DELAY_TIME = 10
DEFAULT_QUIET=True
# For handling the config file
SRC_DIRECTORY = os.path.dirname( os.path.abspath(__file__))
CONFIG_FILENAME = "config.ini"
CONFIG_PATH = os.path.join(SRC_DIRECTORY, CONFIG_FILENAME) # can be changed
S3ZPUT = os.path.join( MY_DIR, 's3zput') # script that uploads a file to s3 with compression
S3ZCAT = os.path.join( MY_DIR, 's3zcat') # script that downloads and decompresses a file from s3
ZCAT = 'zcat' # regular zcat program
GZIP = 'gzip' # compressor
GZIP_OPT = '-1f' # compression options
def set_reident(reident_no_sep):
global REIDENT
import dbrecon
if reident_no_sep.endswith("_"): # remove if it was inadvertnetly provided
reident_no_sep = reident_no_sep[:-1]
os.environ['REIDENT_NO_SEP'] = reident_no_sep
dbrecon.REIDENT = REIDENT = os.environ['REIDENT'] = reident_no_sep + "_"
def reident_no_sep():
ret = REIDENT
if ret.endswith('_'):
ret = ret[:-1]
return ret
##
## Functions that return paths.
## These cannot be constants because they do substituion, and f-strings don't work as macros
###
SF1_DIR = '$ROOT/work/{stusab}/{state_code}{county}'
SF1_RACE_BINARIES = '$SRC/layouts/sf1_vars_race_binaries.csv'
GEOFILE_FILENAME_TEMPLATE = "$ROOT/work/{stusab}/geofile_{stusab}.csv"
STATE_COUNTY_FILENAME_TEMPLATE = '$ROOT/work/{stusab}/state_county_list_{state_code}.csv'
start_time = time.time()
MB=1000*1000
GB=1000*1000*1000
MiB=1024*1024
GiB=1024*1024*1024
LP='lp'
SOL='sol'
CSV='csv'
delete_on_exit = []
################################################################
### Summary Levels #############################################
################################################################
SUMLEVS = {
"State": '040',
"County": '050',
"Census Tract-Block": '101',
"Census Tract": '140'
}
SUMLEV_STATE = '040'
STATE_DATA=[
"Alabama,AL,01",
"Alaska,AK,02",
"Arizona,AZ,04",
"Arkansas,AR,05",
"California,CA,06",
"Colorado,CO,08",
"Connecticut,CT,09",
"Delaware,DE,10",
"District_of_Columbia,DC,11",
"Florida,FL,12",
"Georgia,GA,13",
"Hawaii,HI,15",
"Idaho,ID,16",
"Illinois,IL,17",
"Indiana,IN,18",
"Iowa,IA,19",
"Kansas,KS,20",
"Kentucky,KY,21",
"Louisiana,LA,22",
"Maine,ME,23",
"Maryland,MD,24",
"Massachusetts,MA,25",
"Michigan,MI,26",
"Minnesota,MN,27",
"Mississippi,MS,28",
"Missouri,MO,29",
"Montana,MT,30",
"Nebraska,NE,31",
"Nevada,NV,32",
"New_Hampshire,NH,33",
"New_Jersey,NJ,34",
"New_Mexico,NM,35",
"New_York,NY,36",
"North_Carolina,NC,37",
"North_Dakota,ND,38",
"Ohio,OH,39",
"Oklahoma,OK,40",
"Oregon,OR,41",
"Pennsylvania,PA,42",
"Rhode_Island,RI,44",
"South_Carolina,SC,45",
"South_Dakota,SD,46",
"Tennessee,TN,47",
"Texas,TX,48",
"Utah,UT,49",
"Vermont,VT,50",
"Virginia,VA,51",
"Washington,WA,53",
"West_Virginia,WV,54",
"Wisconsin,WI,55",
"Wyoming,WY,56" ]
STATES=[dict(zip("state_name,stusab,fips_state".split(","),line.split(","))) for line in STATE_DATA]
##
## For parsing the config file
##
SECTION_PATHS='paths'
SECTION_RUN='run'
OPTION_NAME='NAME'
OPTION_SRC='SRC' # the $SRC is added to the [paths] section of the config file
################################################################
### Utility Functions ##########################################
################################################################
class Memoize:
def __init__(self, fn):
self.fn = fn
self.memo = {}
def __call__(self, *args):
if args not in self.memo:
self.memo[args] = self.fn(*args)
return self.memo[args]
def hostname():
"""Hostname without domain"""
return socket.gethostname().partition('.')[0]
def filename_mtime(fname):
"""Return a file's mtime as a unix time_t"""
if fname is None:
return None
try:
return datetime.datetime.fromtimestamp(int(os.stat(fname).st_mtime))
except FileNotFoundError:
return None
################################################################
### Database management functions ##############################
################################################################
db_re = re.compile("export (.*)=(.*)")
def get_pw():
import pwd
home = pwd.getpwuid(os.getuid()).pw_dir
with open( os.path.join( home, 'dbrecon.bash')) as f:
for line in f:
m = db_re.search(line.strip())
if m:
os.environ[m.group(1)] = m.group(2)
################################################################
### The USA Geography object.
### tracks geographies. We should have created this originally.
################################################################
class USAG:
__slots__ = ['stusab','state','county','tract']
def __init__(self, stusab, county, tract, block=None):
self.stusab = stusab(stusab)
self.state = state_fips(stusab)
self.county = county
self.tract = tract
self.block = block
def __repr__(self):
v = " "+self.block if self.block is not None else ""
return f"<{self.self} {self.stusab} {self.county} {self.tract}{v}>"
def __eq__(self,a):
return (self.stusab == a.stusab) and (self.county==a.county) and (self.tract == a.tract) and (self.block==a.block)
################################################################
### Understanding LP and SOL files #############################
################################################################
def get_final_pop_for_gzfile(sol_filenamegz, requireInt=False):
count = 0
errors = 0
with dopen(sol_filenamegz,'r',download=True) as f:
for (num,line) in enumerate(f,1):
if line.startswith('C'):
line = line.strip()
if line.endswith(" 1"):
count += 1
elif line.endswith(" 0"):
pass
else:
if errors==0:
logging.error("Invalid pop count variables in "+sol_filenamegz)
logging.error("line {}: {}".format(num,line))
count += round(float(line.split()[1]))
errors += 1
if errors>0 and requireInt:
raise RuntimeError(f"errors: {errors}")
return count
def get_final_pop_from_sol(auth, stusab, county, tract, delete=True):
sol_filenamegz = SOLFILENAMEGZ(stusab=stusab,county=county,tract=tract)
count = get_final_pop_for_gzfile(sol_filenamegz)
if count==0 or count>100000:
logging.warning(f"UNSOLVE {sol_filenamegz} has a final pop of {count}. This is invalid, so deleting")
if delete:
dpath_safe_unlink(sol_filenamegz)
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts set sol_start=NULL, sol_end=NULL where stusab=%s and county=%s and tract=%s",
(stusab,county,tract))
return None
return count
################################################################
##
"""
This implements the hostlock system.
The hostlock is used by the scheduler to make sure that the same LP or SOL isn't scheduled
simulatenously on two different nodes. A host gets a lock by setting:
{what}_start = now()
{what}_host = the host
where {what} = lp or sol.
I'm not sure why we also have a hostloc and pid, because that doesn't seem to be needed.
I think that it takes advantage of the fact that you aren't building the LP and the SOL at the same time.
Under spark, we don't use db_start, just db_done.
"""
def db_lock(auth, stusab, county, tract=None, extra=''):
"""Sets the hostlock column for a stusab/county/[tract] so that the same combination won't be run on another host
:param auth: database authentication
:param stusab: state
:param county: county
:param tract: tract to lock, or None to lock all tracts
:param extra: extra SQL to add
"""
cmd = f"UPDATE {REIDENT}tracts set hostlock=%s,pid=%s WHERE stusab=%s and county=%s"
args = [hostname(),os.getpid(),stusab,county]
if tract:
cmd+=" and tract=%s"
args.append(tract)
cmd+=' '+extra
DBMySQL.csfr(auth, cmd, args)
logging.info(f"db_lock: {hostname()} {sys.argv[0]} {stusab} {county} {tract} ")
def db_unlock(auth,stusab, county, tract):
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts set hostlock=NULL,pid=NULL WHERE stusab=%s and county=%s and tract=%s",
(stusab,county,tract))
def db_unlock_all(auth, hostname):
"""Hard clear the database fields for running jobs on a given host.
This should only be called if you are assured that there are no running jobs on the host specified.
:param auth: Database authentication.
:param hostname: the host to clear hostlock, or None for all hosts.
"""
if hostname is None:
hostlock = ''
whostlock = ''
else:
hostlock = f" AND (hostlock = '{hostname}') "
whostlock = f"WHERE hostlock = '{hostname}'"
logging.warning(f"UNSOLVE where lp_start is NOT NULL and lp_end is NULL and {hostlock}")
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts SET lp_start=NULL, hostlock=NULL,lp_host=NULL WHERE (lp_start IS NOT NULL) AND (lp_end IS NULL) {hostlock}")
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts SET sol_start=NULL,hostlock=NULL,sol_host=NULL WHERE (sol_start IS NOT NULL) AND (sol_end IS NULL) {hostlock}")
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts SET hostlock=NULL {whostlock}")
def db_start(auth, what, stusab, county, tract):
assert what in [LP, SOL, CSV]
DBMySQL.csfr(auth, f"UPDATE {REIDENT}tracts set {what}_start=now(), {what}_host=%s, hostlock=%s, pid=%s where stusab=%s and county=%s and tract=%s",
(hostname(),hostname(),os.getpid(),stusab,county,tract))
logging.info(f"db_start: {hostname()} {sys.argv[0]} {what} {stusab} {county} {tract} ")
def db_done(auth, what, stusab, county, tract, *, start=None, clear_start=False):
"""
:param auth: database authorization
:param what: are we done with LP, SOL or CSV?
:param stusab: USPS state code
:param county: county code
:param tract: tract code
:param start: If provided, when the start time was
:param clear_start: if provided, clear the start time. (because database run is done but we don't know when it started.
"""
assert what in [LP, SOL, CSV]
cmd = f"UPDATE {REIDENT}tracts set hostlock=NULL, pid=NULL "
args = []
if start:
cmd += f",{what}_start=%s "
args.append(start)
if clear_start:
cmd += f",{what}_start=NULL "
else:
cmd += f",{what}_host=%s "
args.append(hostname())
if not is_db_done(auth, what, stusab, county, tract):
cmd += f",{what}_end=now() "
cmd += " WHERE stusab=%s AND county=%s AND tract=%s"
args += [stusab,county,tract]
DBMySQL.csfr(auth, cmd, args)
logging.info(f"db_done: {what} {stusab} {county} {tract} ")
def is_db_done(auth, what, stusab, county, tract):
"""Returns true if all LP, SOL, or CSV are made"""
assert what in [LP,SOL, CSV]
row = DBMySQL.csfr(auth,
f"""
SELECT {what}_end FROM {REIDENT}tracts
WHERE (stusab=%s) AND (county=%s) AND (tract=%s) and ({what}_end IS NOT NULL) AND (pop100>0) LIMIT 1
""",
(stusab,county,tract))
return len(row)==1
def db_clean(auth):
"""Clear hostlock if PID is gone. PID is the PID of the scheduler"""
rows = DBMySQL.csfr(auth,f"SELECT pid,stusab,county,tract FROM {REIDENT}tracts WHERE hostlock=%s",(hostname(),),quiet=True)
for (pid,stusab,county,tract) in rows:
if not pid:
db_unlock(auth,stusab,county,tract)
continue
try:
p = psutil.Process(pid)
except psutil.NoSuchProcess:
db_unlock(auth,stusab,county,tract)
def get_tracts_needing_lp_files(auth, stusab, county):
rows = DBMySQL.csfr(auth,
f"""
SELECT tract FROM {REIDENT}tracts
WHERE (stusab=%s) AND (county=%s) AND (lp_end IS NULL) AND (pop100>0)
""",(stusab,county))
return [row[0] for row in rows]
def tracts_in_county_ready_to_solve(auth, stusab, county):
rows = DBMySQL.csfr(auth,
f"""
SELECT tract
FROM {REIDENT}tracts
WHERE (lp_end IS NOT NULL) AND (sol_end IS NULL) AND (stusab=%s) AND (county=%s) AND (pop100>0)
""", (stusab, county))
return [row[0] for row in rows]
################################################################
### functions that return directory and file locations ########
################################################################
def STATE_COUNTY_DIR(*,root='$ROOT',stusab,county):
fips = state_fips(stusab)
return f"{root}/work/{stusab}/{fips}{county}"
def LPDIR(*,stusab,county):
"""Returns the directory where LP files for a particular state and county are stored.
dpath_expand() is not called because we may search this directory for files."""
fips = state_fips(stusab)
return f'$ROOT/work/{stusab}/{fips}{county}/lp'
def SOLDIR(*,stusab,county):
"""Returns the directory where LP files for a particular state and county are stored.
dpath_expand() is not called because we may search this directory for files.
"""
fips = state_fips(stusab)
return f'$ROOT/work/{stusab}/{fips}{county}/sol'
def SF1_ZIP_FILE(*,stusab):
return dpath_expand(f"$SF1_DIST/{stusab}2010.sf1.zip".format(stusab=stusab))
def SF1_COUNTY_DATA_FILE(*,stusab,county):
state_code = state_fips(stusab)
sf1_dir = SF1_DIR.format(state_code=state_code,county=county,stusab=stusab)
return dpath_expand(f'{sf1_dir}/sf1_county_{state_code}{county}.csv')
def SF1_BLOCK_DATA_FILE(*,stusab,county):
state_code = state_fips(stusab)
sf1_dir = SF1_DIR.format(state_code=state_code,county=county,stusab=stusab)
return dpath_expand(f'{sf1_dir}/sf1_block_{state_code}{county}.csv')
def SF1_TRACT_DATA_FILE(*,stusab,county):
state_code = state_fips(stusab)
sf1_dir = SF1_DIR.format(state_code=state_code,county=county,stusab=stusab)
return dpath_expand(f'{sf1_dir}/sf1_tract_{state_code}{county}.csv')
def LPFILENAMEGZ(*,stusab,county,tract):
geo_id = state_fips(stusab)+county+tract
lpdir = LPDIR(stusab=stusab,county=county)
return dpath_expand(f'{lpdir}/model_{geo_id}.lp.gz')
def ILPFILENAME(*,stusab,county,tract):
geo_id = state_fips(stusab)+county+tract
lpdir = LPDIR(stusab=stusab,county=county)
return dpath_expand(f'{lpdir}/model_{geo_id}.ilp')
def SOLFILENAME(*,stusab,county,tract):
soldir = SOLDIR(stusab=stusab,county=county)
fips = state_fips(stusab)
return dpath_expand(f'{soldir}/model_{fips}{county}{tract}.sol')
def SOLFILENAMEGZ(*,stusab,county,tract):
return SOLFILENAME(stusab=stusab,county=county,tract=tract)+".gz"
def COUNTY_CSV_FILENAME(*,stusab,county):
csvdir = STATE_COUNTY_DIR(root='$ROOT',stusab=stusab,county=county)
geo_id = state_fips(stusab) + county
return dpath_expand(f'{csvdir}/synth_out_{geo_id}.csv')
SET_RE = re.compile(r"[^0-9](?P<state>\d\d)(?P<county>\d\d\d)(?P<tract>\d\d\d\d\d\d)[^0-9]")
def extract_state_county_tract(fname):
m = SET_RE.search(fname)
if m:
return( stusab(m.group('state')), m.group('county'), m.group('tract'))
return None
def sf1_vars():
"""Return the pandas datafrom for reading SF1_RACE_BINARIES. Check the layouts directory first. If it is not there, we may be running under Spark. Check the current directory"""
tried = []
for fn in [SF1_RACE_BINARIES, os.path.basename(SF1_RACE_BINARIES)]:
fn = dpath_expand(fn)
if os.path.exists(fn):
return pd.read_csv(open(fn), quoting=2)
tried.append(fn)
raise FileNotFoundError(','.join(tried))
def sf1_zipfilename(stusab):
"""If the SF1 is on S3, download it to a known location and work from there.
This has a race condition if it is run in two different processs. Howeve, it's only done in steps1 and step2,
and they are threaded on state, not on county.
"""
sf1_path = dpath_expand(f"$SF1_DIST/{stusab}2010.sf1.zip")
if sf1_path.startswith("s3://"):
local_path = "/tmp/" + sf1_path.replace("/","_")
# if the file doesn't exist or if it exists and is the wrong size, download it
(bucket,key) = s3.get_bucket_key(sf1_path)
if not os.path.exists(local_path) or s3.getsize(bucket,key)!=os.path.getsize(local_path):
logging.warning(f"Downloading {sf1_path} to {local_path}")
dpath_safe_unlink(local_path)
s3.get_object(bucket, key, local_path)
return local_path
return sf1_path
def auth():
"""Returns a new, clean database connection for ctools.dbfile"""
return DBMySQLAuth.FromConfig(os.environ)
# https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class GetConfig(metaclass=Singleton):
def __init__(self):
self.config = None
def set_config(self, config):
self.config = config
def config_reload(self, path=CONFIG_PATH):
self.config = ConfigParser()
self.config.read(path)
# Add our source directory to the paths
if SECTION_PATHS not in self.config:
raise RuntimeError(f"No [{SECTION_PATHS}] section in config file {path}")
self.config[SECTION_PATHS][OPTION_SRC] = SRC_DIRECTORY
return self.config
def get_config(self, *, path=CONFIG_PATH):
if self.config is None:
self.config_reload(path=path)
return self.config
def get_config_str(section,name):
"""Like config[section][name], but looks for [name@hostname] first"""
config = GetConfig().get_config()
name_hostname = name + '@' + socket.gethostname()
if name_hostname in config[section]:
name = name_hostname
return config[section][name]
def get_config_int(section,name):
return int(get_config_str(section,name))
def state_rec(key):
"""Return the record in the state database for a key, where key is the state name, abbreviation, or FIPS code."""
assert isinstance(key,str)
for rec in STATES:
if (key.lower()==rec['state_name'].lower()
or key.lower()==rec['stusab'].lower()
or key==rec['fips_state']):
return rec
raise ValueError(f"{key}: not a valid state name, abbreviation, or FIPS code")
def state_fips(key):
"""Convert state name or abbreviation to FIPS code"""
assert isinstance(key,str)
return state_rec(key)['fips_state']
def stusab(key):
"""Convert state FIPS code to the appreviation"""
assert isinstance(key,str)
return state_rec(key)['stusab'].lower()
def all_stusabs():
# Return a list of all the states
return [rec['stusab'].lower() for rec in STATES]
def parse_stusabs(statelist):
# Turn a comman-separated list of states into an array of all state abbreviations.
# also accepts state numbers
assert isinstance(statelist,str)
return [state_rec(key)['stusab'].lower() for key in statelist.split(",")]
def counties_for_state(stusab):
"""Return a list of the the county codes (as strings) for the counties in stusab"""
rows = DBMySQL.csfr(auth(), f"SELECT county FROM {REIDENT}geo WHERE stusab=%s and sumlev='050'",(stusab,))
return [row[0] for row in rows]
def tracts_for_state_county(*,stusab,county):
"""Accessing the database, return the tracts for a given state/county.
Only return tracts with non-zero population
"""
rows = DBMySQL.csfr(auth(),
f"""
SELECT tract from {REIDENT}tracts
WHERE (stusab=%s) and (county=%s) AND (pop100>0)
""",(stusab,county))
return [row[0] for row in rows]
################################################################
### LPFile Manipulation
################################################################
MIN_LP_SIZE = 100 # smaller than this, the file must be invalid
MIN_SOL_SIZE = 1000 # smaller than this, the file is invalid
def validate_lpfile(fname):
# Small files are not valid LP files
if dgetsize(fname) < MIN_LP_SIZE:
return False
# If the lpfile is not on S3 and not compressed, we can tell if it is properly terminated
# by reading the last 3 bytes and seeing if they have an End. This is fast
if (not fname.startswith("s3:")) and (fname.endswith('.lp')):
with dopen(fname,"rb") as f:
f.seek(-4,2)
last4 = f.read(4)
return last4 in (b'End\n',b'\nEnd')
# Otherwise, scan the file
# Note: dopen() can't be used as a context manager
# This should be changed so that the temp files downloaded are automatically deleted
f = dopen(fname, 'r', download=True)
lastline = None
while True:
line = f.readline()
if line=='':
break
line = line.strip()
if line=='':
continue
lastline = line
return lastline == 'End'
def remove_lpfile(auth,stusab,county,tract):
# Remove the LP file and its solution
lpgz_filename = LPFILENAMEGZ(stusab=stusab,county=county,tract=tract)
dpath_safe_unlink(lpgz_filename)
logging.warning("UNSOLVE remove_lpfile %s %s %s",stusab, county, tract)
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts set lp_start=NULL, lp_end=NULL, lp_gb=NULL where stusab=%s and county=%s and tract=%s",
(stusab,county,tract))
remove_solfile(auth,stusab, county, tract)
################################################################
### SOLFile Manipulation
################################################################
def validate_solfile(fname):
"""Validating the solfile requires looking at all the variables and making sure each is a 0 or a 1.
That's a lot of work. This approach just looks at the log file.
Some of the logfiles were written out as compressed without a .gz, so look for both and try to decompress each
"""
for possible_log_file in [fname.replace(".sol.gz",".log.gz"),
fname.replace(".sol.gz",".log")]:
if dgetsize(possible_log_file) > 0:
data = dopen(possible_log_file,'rb').read()
if b'Optimal solution found' in data:
return True
try:
if b'Optimal solution found' in gzip.decompress(data):
return True
except OSError as e:
pass
return False
def remove_solfile(auth,stusab,county,tract):
solgz_filename = SOLFILENAMEGZ(stusab=stusab,county=county,tract=tract)
dpath_safe_unlink(solgz_filename)
logging.warning("UNSOLVE remove_solfile %s %s %s",stusab,county,tract)
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts SET sol_start=NULL, sol_end=NULL, sol_gb=NULL WHERE stusab=%s AND county=%s AND tract=%s",
(stusab,county,tract))
remove_csvfile(auth,stusab,county)
def remove_csvfile(auth,stusab,county):
csv_filename = COUNTY_CSV_FILENAME(stusab=stusab,county=county)
logging.warning("UNSOLVE remove_csvfile %s %s",stusab,county)
for fn in [csv_filename, csv_filename+'.tmp']:
dpath_safe_unlink(fn)
DBMySQL.csfr(auth,f"UPDATE {REIDENT}tracts SET csv_start=NULL, csv_end=NULL, csv_host=NULL WHERE stusab=%s AND county=%s",
(stusab,county))
################################################################
### Output Products
################################################################
def valid_state_code(code):
assert isinstance(code,str)
return len(code)==2 and all(ch.isdigit() for ch in code)
def valid_county_code(code):
assert isinstance(code,str)
return len(code)==3 and all(ch.isdigit() for ch in code)
def state_county_tract_has_file(stusab, county_code, tract_code, filetype=LP):
assert isinstance(stusab,str)
assert isinstance(county_code,str)
assert isinstance(tract_code,str)
state_code = state_fips(stusab)
files = dlistdir(f'$ROOT/{stusab}/{state_code}{county_code}/{filetype}/')
return f"model_{state_code}{county_code}{tract_code}.{filetype}" in files
def state_county_has_any_files(stusab, county_code, filetype=LP):
assert isinstance(stusab,str)
assert isinstance(county_code,str)
state_code = state_fips(stusab)
files = dlistdir(f'$ROOT/{stusab}/{state_code}{county_code}/{filetype}/')
return any([fn.endswith("."+filetype) for fn in files])
def state_has_any_files(stusab, county_code, filetype=LP):
assert isinstance(stusab,str)
assert isinstance(county_code,str)
state_code = state_fips(stusab)
counties = counties_for_state(stusab)
for county_code in counties:
if state_county_has_any_files(stusab, county_code, filetype=filetype):
return True
################################################################
### Logging. Much of this was moved to ctools.clogging
# Our generic setup routine
# https://stackoverflow.com/questions/8632354/python-argparse-custom-actions-with-additional-arguments-passed
def argparse_add_logging(parser):
clogging.add_argument(parser)
parser.add_argument("--config", help="config file")
parser.add_argument("--reident", help='set reident at command line')
parser.add_argument("--stdout", help="Also log to stdout", action='store_true')
parser.add_argument("--logmem", action='store_true',
help="enable memory debugging. Print memory usage. "
"Write output to temp file and compare with correct file.")
def setup_logging(*,config,loglevel=logging.INFO,logdir="logs",prefix='dbrecon',
stdout=None,args=None,error_alert=True):
"""Sets up loging and registers exit handlers"""
global dfxml_writer
if not prefix:
prefix = config[SECTION_RUN][OPTION_NAME]
if args and args.loglevel:
loglevel = args.loglevel
if args and args.stdout:
stdout = args.stdout
if args and args.logmem:
stdout = True
logfname = "{}/{}-{}-{:06}.log".format(logdir,prefix,datetime.datetime.now().isoformat()[0:19],os.getpid())
if not os.path.exists(logdir):
os.mkdir(logdir)
clogging.setup(level=loglevel, filename=logfname)
logger = logging.getLogger()
# Log to stdout if requested
if stdout:
print("Logging to stdout ")
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.getLevelName(loglevel))
handler.setFormatter( logging.Formatter(clogging.LOG_FORMAT) )
logger.addHandler(handler)
# Log warnings to stderr
warning_handler = logging.StreamHandler(sys.stderr)
warning_handler.setLevel(logging.WARNING)
warning_handler.setFormatter( logging.Formatter(clogging.LOG_FORMAT) )
logger.addHandler(warning_handler)
# Log to DFXML
dfxml_writer = DFXMLWriter(filename=logfname.replace(".log",".dfxml"), prettyprint=True)
dfxml_handler = dfxml_writer.logHandler()
logger.addHandler(dfxml_handler)
if error_alert:
# Log exit codes
atexit.register(tempfile_exit)
atexit.register(logging_exit)
# Finally, indicate that we have started up
logging.info(f"START {hostname()} {sys.executable} {' '.join(sys.argv)} log level: {loglevel}")
def setup_logging_and_get_config(*,args,**kwargs):
if args.reident:
set_reident(args.reident)
inspect.stack()[1].frame.f_globals['REIDENT']=os.getenv('REIDENT')
config = GetConfig().get_config()
setup_logging(config=config,**kwargs)
return config
def add_dfxml_tag(tag,text=None,attrs={}):
e = ET.SubElement(dfxml_writer.doc, tag, attrs)
if text:
e.text = text
def log_error(*,error=None, filename=None, last_value=None):
reident = os.getenv('REIDENT','').replace('_','')
DBMySQL.csfr(auth(),f"INSERT INTO errors (`host`,`error`,`argv0`,`reident`,`file`,`last_value`) VALUES (%s,%s,%s,%s,%s,%s)",
(hostname(), error, sys.argv[0], reident, filename, last_value), quiet=True)
logging.error(error)
def tempfile_exit():
"""On exit, delete any tempfiles that were created."""
for path in delete_on_exit:
dpath_safe_unlink(path)
def logging_exit():
"""Called at exit. If the exit was caused by an exception, record that"""
if hasattr(sys,'last_value'):
msg = f'PID{os.getpid()}: {sys.last_value}'
logging.error(msg)
logging.error("%s",sys.argv)
log_error(error=msg, filename=__file__, last_value=str(sys.last_value))
var_re = re.compile(r"(\$[A-Z_][A-Z_0-9]*)")
def dpath_expand(path):
"""dpath_expand is the main path expansion function. It substitutes
$VAR for variables in the [path] section of the config file. It
handles VAR@HOST and expands host automatically. It is called by
dopen() to do the expansion.
"""
# Find and replace all of the dollar variables with those in the config file
config = GetConfig().get_config()
while True:
m = var_re.search(path)
if not m:
break
varname = m.group(1)[1:]
varname_hostname = varname + "@" + socket.gethostname()
# See if the variable with my hostname is present. If so, use that one
if varname_hostname in config[SECTION_PATHS]:
varname = varname_hostname
if varname in config[SECTION_PATHS]:
val = config[SECTION_PATHS][varname]
elif varname in os.environ:
val = os.environ[varname]
else:
logging.error("varname: %s",varname)
logging.error("path: %s",path)
logging.error("keys in [%s]: %s",SECTION_PATHS,list(config[SECTION_PATHS].keys()))
raise KeyError(f"'{varname}' not in [{SECTION_PATHS}] of config file and not in global environment")
path = path[0:m.start(1)] + val + path[m.end(1):]
return path
def dpath_exists(path):
path = dpath_expand(path)
if path[0:5]=='s3://':
ret = s3.s3exists(path)
else:
ret = os.path.exists(path)
logging.info(f"dpath_exists({path})={ret}")
return ret
def dpath_exists_boto3(path, session=None):
path = dpath_expand(path)
if path[0:5]=='s3://':
#ret = s3.s3exists(path)
bucket,key = s3.get_bucket_key(path)
ret = dwait_exists_boto3(bucket, key, session, raise_error=False, wd=1, ma=6)
else:
ret = os.path.exists(path)
logging.info(f"dpath_exists({path})={ret}")
return ret
def dpath_unlink(path):
path = dpath_expand(path)
if path.startswith('s3://'):
(bucket,key) = s3.get_bucket_key(path)
r = boto3.client('s3').delete_object(Bucket=bucket, Key=key)
print(f"aws s3 rm s3://{bucket}/{key}")
else:
return os.unlink(path)
def dpath_safe_unlink(path):
if path is None: # no need to unlink None
return
try:
dpath_unlink(path)
except FileNotFoundError as e:
pass
def dlistdir(path):
path = dpath_expand(path)
url = urllib.parse.urlparse(path)
if url.scheme=='s3':
bucket = url.netloc
prefix = url.path[1:]
if not prefix.endswith('/'):
prefix += '/'
logging.info("listing objects in %s",path)
for d in s3.list_objects(bucket,prefix):
logging.info(d['Key'])
yield d['Key']
return
try:
logging.info("listing files at %s",path)
for d in os.listdir(path):
yield d
except FileNotFoundError as e:
return []
def dopen(path, mode='r', encoding='utf-8',*, zipfilename=None, download=False, fsync=False):
"""An open function that can open from S3 and from inside of zipfiles.
Don't use this for new projects; use ctools.dconfig.dopen instead"""
logging.info(" dopen('{}','{}','{}', zipfilename={})".format(path,mode,encoding,zipfilename))
# If we are writing but not writing to S3, make sure the directory exists
if mode[0]=='w' and path[0:5]!='s3://':
dmakedirs( os.path.dirname(path))
path = dpath_expand(path)
# immediate passthrough if zipfilename is None and s3 is requested
if path[0:5]=='s3://' and zipfilename is None:
(bucket,key) = s3.get_bucket_key(path)
if mode.startswith('r'):
if not s3.s3exists(path):
raise FileNotFoundError(path)
if download:
with tempfile.NamedTemporaryFile(mode='wb',delete=False) as tf:
delete_on_exit.append(tf.name) # remember to delete the file!
boto3.client('s3').download_file(bucket, key, tf.name)
if mode=='r' and path.endswith('.gz'):
return codecs.getreader(encoding)(gzip.GzipFile(tf.name,'rb'),errors='ignore')
if mode=='rb':
encoding = None
return open(tf.name, mode=mode, encoding=encoding)
if path.endswith('.gz'):
obj = boto3.resource('s3').Object(bucket,key)
f = gzip.GzipFile(fileobj=obj.get()['Body'])
if mode.endswith('b'):
return f
return codecs.getreader(encoding)(f, errors='ignore')
if mode=='rb':
return s3.S3File(path, mode=mode)
if mode.startswith('w'):
if path.endswith('.gz'):
p = subprocess.Popen([ S3ZPUT, '/dev/stdin', path], stdin=subprocess.PIPE, encoding=encoding)
return p.stdin
# fall-through: just use s3open
return s3.s3open(path, mode=mode, encoding=encoding, fsync=fsync)
if 'b' in mode:
encoding=None
# immediate passthrough if zipfilename is provided
if zipfilename:
assert mode.startswith('r') # can only read from zipfiles
filename = os.path.basename(path)
zip_file = zipfile.ZipFile(dopen(zipfilename, mode='rb'))
zf = zip_file.open(filename, 'r')
if encoding==None and ("b" not in mode):
encoding='utf-8'
logging.info("zipfilename bypass: zipfilename=%s filename=%s mode=%s encoding=%s",zipfilename,filename,mode,encoding)
return io.TextIOWrapper(io.BufferedReader(zf, buffer_size=1024*1024) , encoding=encoding) # big buffer please
# Legacy code follow
# Check for full path name
logging.info("=>open(path={},mode={},encoding={})".format(path,mode,encoding))
# if opening mode==r and the file does not exist, see if there is a file ending filename.gz,
# and if it does, open through a pipe with a decompressor.
# If opening mode==r, and the file does not exist, see if it is present in the provided ZIP file
# If a zipfile is not provided, see if we can find one in the directory
if "r" in mode and (not os.path.exists(path)):
# path does not exist; see if there is a single zip file in the directory
# If there is, see if the zipfile has the requested file in it
(dirname,filename) = os.path.split(path)
if not zipfilename:
zipnames = glob.glob(os.path.join(dirname,"*.zip"))
if len(zipnames)==1: