Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

support.py 5.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. import sys
  2. import os
  3. from pathlib import Path
  4. import subprocess as sp
  5. from invoke import task
  6. import psycopg2
  7. import logging
  8. _log = logging.getLogger(__name__)
  9. data_dir = Path('data')
  10. tgt_dir = Path('target')
  11. bin_dir = tgt_dir / 'release'
  12. numspaces = dict(work=100000000, edition=200000000, rec=300000000,
  13. gr_work=400000000, gr_book=500000000,
  14. isbn=900000000)
  15. def db_url():
  16. if 'DB_URL' in os.environ:
  17. return os.environ['DB_URL']
  18. host = os.environ.get('PGHOST', 'localhost')
  19. port = os.environ.get('PGPORT', None)
  20. db = os.environ['PGDATABASE']
  21. user = os.environ.get('PGUSER', None)
  22. pw = os.environ.get('PGPASSWORD', None)
  23. url = 'postgresql://'
  24. if user:
  25. url += user
  26. if pw:
  27. url += ':' + pw
  28. url += '@'
  29. url += host
  30. if port:
  31. url += ':' + port
  32. url += '/' + db
  33. return url
  34. def psql(c, script):
  35. c.run(f'psql -v ON_ERROR_STOP=on -f {script}')
  36. @task
  37. def init(c):
  38. "Make sure initial database structure are in place"
  39. if start('init', fail=False):
  40. psql(c, 'common-schema.sql')
  41. finish('init')
  42. @task
  43. def build(c, debug=False):
  44. "Compile the Rust support executables"
  45. global bin_dir
  46. if debug:
  47. _log.info('compiling support executables in debug mode')
  48. c.run('cargo build')
  49. bin_dir = tgt_dir / 'debug'
  50. else:
  51. _log.info('compiling support executables')
  52. c.run('cargo build --release')
  53. @task
  54. def clean(c):
  55. "Clean up intermediate & generated files"
  56. _log.info('cleaning Rust build')
  57. c.run('cargo clean')
  58. _log.info('cleaning cluster CSV')
  59. for f in data_dir.glob('*clusters.csv'):
  60. _log.debug('rm %s', f)
  61. f.unlink()
  62. for f in data_dir.glob('*-edges.csv.gz'):
  63. _log.debug('rm %s', f)
  64. f.unlink()
  65. for f in data_dir.glob('*-isbns.csv.gz'):
  66. _log.debug('rm %s', f)
  67. f.unlink()
  68. @task
  69. def test(c, debug=False):
  70. "Run tests on the import & support code."
  71. global bin_dir
  72. if debug:
  73. _log.info('testing support executables in debug mode')
  74. c.run('cargo test')
  75. else:
  76. _log.info('testing support executables')
  77. c.run('cargo test --release')
  78. def pipeline(steps, outfile=None):
  79. last = sp.DEVNULL
  80. if outfile is not None:
  81. outfd = os.open(outfile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o666)
  82. else:
  83. outfd = None
  84. procs = []
  85. for step in steps[:-1]:
  86. _log.debug('running %s', step)
  87. proc = sp.Popen(step, stdin=last, stdout=sp.PIPE)
  88. last = proc.stdout
  89. procs.append(proc)
  90. proc = sp.Popen(steps[-1], stdin=last, stdout=outfd)
  91. procs.append(proc)
  92. for p, s in zip(procs, steps):
  93. rc = p.wait()
  94. if rc != 0:
  95. _log.error(f'{s[0]} exited with code {rc}')
  96. raise RuntimeError('subprocess failed')
  97. class database:
  98. def __init__(self, autocommit = False, dbc=None):
  99. self.autocommit = autocommit
  100. self.connection = dbc
  101. self.need_close = False
  102. def __enter__(self):
  103. if self.connection is None:
  104. _log.debug('connecting to database')
  105. self.connection = psycopg2.connect("")
  106. self.need_close = True
  107. if self.autocommit:
  108. self.connection.set_session(autocommit=True)
  109. return self.connection
  110. def __exit__(self, *args):
  111. if self.need_close:
  112. _log.debug('closing DB connection')
  113. self.connection.close()
  114. self.need_close = False
  115. def check_prereq(step, dbc=None):
  116. _log.debug('checking prereq %s', step)
  117. with database(dbc=dbc, autocommit=True) as db:
  118. with db.cursor() as cur:
  119. cur.execute('''
  120. SELECT finished_at FROM import_status
  121. WHERE step = %s AND finished_at IS NOT NULL
  122. ''', [step])
  123. res = cur.fetchone()
  124. if not res:
  125. _log.error('prerequisite step %s not completed', step)
  126. raise RuntimeError('prerequisites not met')
  127. def start(step, force=False, fail=True, dbc=None):
  128. _log.debug('starting step %s', step)
  129. with database(dbc=dbc, autocommit=True) as db:
  130. with db.cursor() as cur:
  131. cur.execute('''
  132. SELECT finished_at FROM import_status
  133. WHERE step = %s
  134. ''', [step])
  135. res = cur.fetchone()
  136. if res:
  137. date, = res
  138. if date:
  139. if force:
  140. _log.warning('step %s already completed at %s, continuing anyway', step, date)
  141. elif fail:
  142. _log.error('step %s already completed at %s', step, date)
  143. raise RuntimeError('step {} already completed'.format(step))
  144. else:
  145. _log.info('step %s already completed at %s', step, date)
  146. return False
  147. else:
  148. _log.warning('step %s already started, did it fail?', step)
  149. cur.execute('''
  150. INSERT INTO import_status (step)
  151. VALUES (%s)
  152. ON CONFLICT (step)
  153. DO UPDATE SET started_at = now(), finished_at = NULL
  154. ''', [step])
  155. return True
  156. def finish(step, dbc=None):
  157. _log.debug('finishing step %s')
  158. with database(dbc=dbc, autocommit=True) as db:
  159. with db.cursor() as cur:
  160. cur.execute('''
  161. UPDATE import_status
  162. SET finished_at = now()
  163. WHERE step = %s
  164. ''', [step])
  165. ct = cur.rowcount
  166. if ct != 1:
  167. raise RuntimeError("couldn't update step!")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...