comparison hg_status.py @ 17:a4778c56cc03

update deps and k8s setup
author drewp@bigasterisk.com
date Sun, 12 Dec 2021 22:32:25 -0800
parents db4037285592
children
comparison
equal deleted inserted replaced
16:db4037285592 17:a4778c56cc03
1 from dataclasses import dataclass, field
2 import datetime 1 import datetime
3 import json 2 import json
4 import logging
5 from pathlib import Path
6 import time 3 import time
7 import traceback 4 import traceback
5 from dataclasses import dataclass, field
6 from pathlib import Path
8 from typing import Dict, Optional, Tuple 7 from typing import Dict, Optional, Tuple
9 8
10 import cyclone.httpserver 9 import cyclone.httpserver
11 import cyclone.sse 10 import cyclone.sse
12 import cyclone.web 11 import cyclone.web
12 import docopt
13 import treq
14 import tzlocal
13 from cycloneerr import PrettyErrorHandler 15 from cycloneerr import PrettyErrorHandler
14 from dateutil.parser import parse 16 from dateutil.parser import parse
15 from dateutil.tz import tzlocal 17 from dateutil.tz import tzlocal
16 import docopt 18 from prometheus_client.exposition import generate_latest
19 from prometheus_client.registry import REGISTRY
17 from ruamel.yaml import YAML 20 from ruamel.yaml import YAML
18 from standardservice.logsetup import log, verboseLogging 21 from standardservice.logsetup import log, verboseLogging
19 import treq
20 from twisted.internet import reactor 22 from twisted.internet import reactor
21 from twisted.internet.defer import inlineCallbacks, returnValue 23 from twisted.internet.defer import inlineCallbacks, returnValue
22 from twisted.internet.utils import getProcessOutput, _UnexpectedErrorOutput 24 from twisted.internet.utils import _UnexpectedErrorOutput, getProcessOutput
23 import tzlocal 25
24
25 local = tzlocal.get_localzone()
26 githubOwner = 'drewp' 26 githubOwner = 'drewp'
27 27
28 28
29 @inlineCallbacks 29 @inlineCallbacks
30 def runHg(cwd, args): 30 def runHg(cwd, args):
74 def getLatestHgCommit(self): 74 def getLatestHgCommit(self):
75 if self._isStale('log'): 75 if self._isStale('log'):
76 rows = yield runHg(self.path, ['log', '--limit', '1']) 76 rows = yield runHg(self.path, ['log', '--limit', '1'])
77 commit = rows[0] 77 commit = rows[0]
78 sec = commit['date'][0] 78 sec = commit['date'][0]
79 t = datetime.datetime.fromtimestamp(sec, local) 79 t = datetime.datetime.fromtimestamp(sec, tzlocal())
80 self._save('log', {'email': commit['user'], 't': t.isoformat(), 'message': commit['desc']}) 80 self._save('log', {'email': commit['user'], 't': t.isoformat(), 'message': commit['desc']})
81 returnValue(self._get('log')) 81 returnValue(self._get('log'))
82 82
83 @inlineCallbacks 83 @inlineCallbacks
84 def getLatestGithubCommit(self): 84 def getLatestGithubCommit(self):
89 'User-agent': 'reposync by github.com/drewp', 89 'User-agent': 'reposync by github.com/drewp',
90 'Accept': 'application/vnd.github.v3+json' 90 'Accept': 'application/vnd.github.v3+json'
91 }) 91 })
92 ret = yield treq.json_content(resp) 92 ret = yield treq.json_content(resp)
93 commit = ret[0]['commit'] 93 commit = ret[0]['commit']
94 t = parse(commit['committer']['date']).astimezone(local).isoformat() 94 t = parse(commit['committer']['date']).astimezone(tzlocal()).isoformat()
95 self._save('github', {'email': commit['committer']['email'], 't': t, 'message': commit['message']}) 95 self._save('github', {'email': commit['committer']['email'], 't': t, 'message': commit['message']})
96 returnValue(self._get('github')) 96 returnValue(self._get('github'))
97 97
98 @inlineCallbacks 98 @inlineCallbacks
99 def clearGithubMaster(self): 99 def clearGithubMaster(self):
107 maybe --set-upstream origin 107 maybe --set-upstream origin
108 bang(pts/13):/tmp/reset% git remote set-branches origin master 108 bang(pts/13):/tmp/reset% git remote set-branches origin master
109 ? 109 ?
110 then push 110 then push
111 then github setdefault to master 111 then github setdefault to master
112 then github delete clearing 112 then github delete clearing
113 ''' 113 '''
114 114
115 @inlineCallbacks 115 @inlineCallbacks
116 def pushToGithub(self): 116 def pushToGithub(self):
117 if not self.github: 117 if not self.github:
159 log.warn(f'not reporting on {repo}') 159 log.warn(f'not reporting on {repo}')
160 traceback.print_exc() 160 traceback.print_exc()
161 reactor.callLater(0, self.runOne) 161 reactor.callLater(0, self.runOne)
162 162
163 163
164 class Metrics(cyclone.web.RequestHandler):
165
166 def get(self):
167 self.add_header('content-type', 'text/plain')
168 self.write(generate_latest(REGISTRY))
169
170
164 def main(): 171 def main():
165 args = docopt.docopt(''' 172 args = docopt.docopt('''
166 Usage: 173 Usage:
167 hg_status.py [options] 174 hg_status.py [options]
168 175
169 Options: 176 Options:
170 -v, --verbose more logging 177 -v, --verbose more logging
171 ''') 178 ''')
172 verboseLogging(args['--verbose']) 179 verboseLogging(args['--verbose'])
173 180
174 import sys 181 # import sys
175 sys.path.append('/usr/lib/python3/dist-packages') 182 # sys.path.append('/usr/lib/python3/dist-packages')
176 import OpenSSL 183 # import OpenSSL
177 184
178 yaml = YAML(typ='safe') 185 yaml = YAML(typ='safe')
179 config = yaml.load(open('config.yaml')) 186 config = yaml.load(open('config.yaml'))
180 repos = [Repo(Path(row['dir']), row['github']) for row in config['hg_repos']] 187 repos = [Repo(Path(row['dir']), row['github']) for row in config['hg_repos']]
181 188
190 (r'/build/(bundle\.js)', cyclone.web.StaticFileHandler, { 197 (r'/build/(bundle\.js)', cyclone.web.StaticFileHandler, {
191 'path': './build/' 198 'path': './build/'
192 }), 199 }),
193 (r'/status/events', Statuses), 200 (r'/status/events', Statuses),
194 (r'/githubSync', GithubSync), 201 (r'/githubSync', GithubSync),
202 (r'/metrics', Metrics),
195 ] 203 ]
196 cyclone.web.Application.__init__( 204 cyclone.web.Application.__init__(
197 self, 205 self,
198 handlers, 206 handlers,
199 repos=repos, 207 repos=repos,