##// END OF EJS Templates
vcsserver: modernize code for python3
super-admin -
r1130:d68a72e0 python3
parent child Browse files
Show More
@@ -72,7 +72,7 b' class SettingsMaker(object):'
72 os.makedirs(input_val, mode=mode, exist_ok=True)
72 os.makedirs(input_val, mode=mode, exist_ok=True)
73
73
74 if not os.path.isdir(input_val):
74 if not os.path.isdir(input_val):
75 raise Exception('Dir at {} does not exist'.format(input_val))
75 raise Exception(f'Dir at {input_val} does not exist')
76 return input_val
76 return input_val
77
77
78 @classmethod
78 @classmethod
@@ -212,7 +212,7 b' def lfs_objects_verify(request):'
212
212
213 store_size = store.size_oid()
213 store_size = store.size_oid()
214 if store_size != size:
214 if store_size != size:
215 msg = 'requested file size mismatch store size:%s requested:%s' % (
215 msg = 'requested file size mismatch store size:{} requested:{}'.format(
216 store_size, size)
216 store_size, size)
217 return write_response_error(
217 return write_response_error(
218 HTTPUnprocessableEntity, msg)
218 HTTPUnprocessableEntity, msg)
@@ -51,7 +51,7 b' class OidHandler(object):'
51
51
52 if not store.has_oid():
52 if not store.has_oid():
53 # error reply back to client that something is wrong with dl
53 # error reply back to client that something is wrong with dl
54 err_msg = 'object: {} does not exist in store'.format(store.oid)
54 err_msg = f'object: {store.oid} does not exist in store'
55 has_errors = OrderedDict(
55 has_errors = OrderedDict(
56 error=OrderedDict(
56 error=OrderedDict(
57 code=404,
57 code=404,
@@ -75,7 +75,7 b' def install_git_hooks(repo_path, bare, e'
75 template = template.replace(b'_PATH_', safe_bytes(path))
75 template = template.replace(b'_PATH_', safe_bytes(path))
76 f.write(template)
76 f.write(template)
77 os.chmod(_hook_file, 0o755)
77 os.chmod(_hook_file, 0o755)
78 except IOError:
78 except OSError:
79 log.exception('error writing hook file %s', _hook_file)
79 log.exception('error writing hook file %s', _hook_file)
80 else:
80 else:
81 log.debug('skipping writing hook file')
81 log.debug('skipping writing hook file')
@@ -129,7 +129,7 b' def install_svn_hooks(repo_path, executa'
129
129
130 f.write(template)
130 f.write(template)
131 os.chmod(_hook_file, 0o755)
131 os.chmod(_hook_file, 0o755)
132 except IOError:
132 except OSError:
133 log.exception('error writing hook file %s', _hook_file)
133 log.exception('error writing hook file %s', _hook_file)
134 else:
134 else:
135 log.debug('skipping writing hook file')
135 log.debug('skipping writing hook file')
@@ -84,7 +84,7 b' class JsonEncoder(json.JSONEncoder):'
84 return str(obj)
84 return str(obj)
85
85
86 try:
86 try:
87 return super(JsonEncoder, self).default(obj)
87 return super().default(obj)
88
88
89 except TypeError:
89 except TypeError:
90 try:
90 try:
@@ -200,7 +200,7 b' class JsonFormatter(ExceptionAwareFormat'
200
200
201 def serialize_log_record(self, log_record):
201 def serialize_log_record(self, log_record):
202 """Returns the final representation of the log record."""
202 """Returns the final representation of the log record."""
203 return "%s%s" % (self.prefix, self.jsonify_log_record(log_record))
203 return "{}{}".format(self.prefix, self.jsonify_log_record(log_record))
204
204
205 def format(self, record):
205 def format(self, record):
206 """Formats a log record and serializes to json"""
206 """Formats a log record and serializes to json"""
@@ -219,7 +219,7 b' class Lock(object):'
219
219
220 if self._held:
220 if self._held:
221 owner_id = self.get_owner_id()
221 owner_id = self.get_owner_id()
222 raise AlreadyAcquired("Already acquired from this Lock instance. Lock id: {}".format(owner_id))
222 raise AlreadyAcquired(f"Already acquired from this Lock instance. Lock id: {owner_id}")
223
223
224 if not blocking and timeout is not None:
224 if not blocking and timeout is not None:
225 raise TimeoutNotUsable("Timeout cannot be used if blocking=False")
225 raise TimeoutNotUsable("Timeout cannot be used if blocking=False")
@@ -1,5 +1,3 b''
1
2
3 import logging
1 import logging
4
2
5 from .stream import TCPStatsClient, UnixSocketStatsClient # noqa
3 from .stream import TCPStatsClient, UnixSocketStatsClient # noqa
@@ -1,5 +1,3 b''
1
2
3 import re
1 import re
4 import random
2 import random
5 from collections import deque
3 from collections import deque
@@ -1,5 +1,3 b''
1
2
3 import socket
1 import socket
4
2
5 from .base import StatsClientBase, PipelineBase
3 from .base import StatsClientBase, PipelineBase
@@ -1,5 +1,3 b''
1
2
3 import functools
1 import functools
4 from time import perf_counter as time_now
2 from time import perf_counter as time_now
5
3
@@ -1,5 +1,3 b''
1
2
3 import socket
1 import socket
4
2
5 from .base import StatsClientBase, PipelineBase
3 from .base import StatsClientBase, PipelineBase
@@ -8,7 +6,7 b' from .base import StatsClientBase, Pipel'
8 class Pipeline(PipelineBase):
6 class Pipeline(PipelineBase):
9
7
10 def __init__(self, client):
8 def __init__(self, client):
11 super(Pipeline, self).__init__(client)
9 super().__init__(client)
12 self._maxudpsize = client._maxudpsize
10 self._maxudpsize = client._maxudpsize
13
11
14 def _send(self):
12 def _send(self):
@@ -43,7 +43,7 b' class ColorFormatter(logging.Formatter):'
43 """
43 """
44 Change record's levelname to use with COLORS enum
44 Change record's levelname to use with COLORS enum
45 """
45 """
46 def_record = super(ColorFormatter, self).format(record)
46 def_record = super().format(record)
47
47
48 levelname = record.levelname
48 levelname = record.levelname
49 start = COLOR_SEQ % (COLORS[levelname])
49 start = COLOR_SEQ % (COLORS[levelname])
@@ -51,11 +51,11 b' class LRUDictDebug(LRUDict):'
51 Wrapper to provide some debug options
51 Wrapper to provide some debug options
52 """
52 """
53 def _report_keys(self):
53 def _report_keys(self):
54 elems_cnt = '{}/{}'.format(len(list(self.keys())), self.size)
54 elems_cnt = f'{len(list(self.keys()))}/{self.size}'
55 # trick for pformat print it more nicely
55 # trick for pformat print it more nicely
56 fmt = '\n'
56 fmt = '\n'
57 for cnt, elem in enumerate(self.keys()):
57 for cnt, elem in enumerate(self.keys()):
58 fmt += '{} - {}\n'.format(cnt+1, safe_str(elem))
58 fmt += f'{cnt+1} - {safe_str(elem)}\n'
59 log.debug('current LRU keys (%s):%s', elems_cnt, fmt)
59 log.debug('current LRU keys (%s):%s', elems_cnt, fmt)
60
60
61 def __getitem__(self, key):
61 def __getitem__(self, key):
@@ -173,7 +173,7 b' def get_or_create_region(region_name, re'
173 region_obj = region_meta.dogpile_cache_regions.get(region_name)
173 region_obj = region_meta.dogpile_cache_regions.get(region_name)
174 if not region_obj:
174 if not region_obj:
175 reg_keys = list(region_meta.dogpile_cache_regions.keys())
175 reg_keys = list(region_meta.dogpile_cache_regions.keys())
176 raise EnvironmentError(f'Region `{region_name}` not in configured: {reg_keys}.')
176 raise OSError(f'Region `{region_name}` not in configured: {reg_keys}.')
177
177
178 region_uid_name = f'{region_name}:{region_namespace}'
178 region_uid_name = f'{region_name}:{region_namespace}'
179
179
@@ -483,7 +483,7 b' class GitRemote(RemoteBase):'
483
483
484 q = {"service": 'git-upload-pack'}
484 q = {"service": 'git-upload-pack'}
485 qs = '?%s' % urllib.parse.urlencode(q)
485 qs = '?%s' % urllib.parse.urlencode(q)
486 cu = "{}{}".format(test_uri, qs)
486 cu = f"{test_uri}{qs}"
487 req = urllib.request.Request(cu, None, {})
487 req = urllib.request.Request(cu, None, {})
488
488
489 try:
489 try:
@@ -494,7 +494,7 b' class GitRemote(RemoteBase):'
494 except Exception as e:
494 except Exception as e:
495 log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True)
495 log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True)
496 # means it cannot be cloned
496 # means it cannot be cloned
497 raise exceptions.URLError(e)("[{}] org_exc: {}".format(obfuscated_uri, e))
497 raise exceptions.URLError(e)(f"[{obfuscated_uri}] org_exc: {e}")
498
498
499 # now detect if it's proper git repo
499 # now detect if it's proper git repo
500 gitdata: bytes = resp.read()
500 gitdata: bytes = resp.read()
@@ -464,7 +464,7 b' class HgRemote(RemoteBase):'
464 q = {"cmd": 'between'}
464 q = {"cmd": 'between'}
465 q.update({'pairs': "{}-{}".format('0' * 40, '0' * 40)})
465 q.update({'pairs': "{}-{}".format('0' * 40, '0' * 40)})
466 qs = '?%s' % urllib.parse.urlencode(q)
466 qs = '?%s' % urllib.parse.urlencode(q)
467 cu = "{}{}".format(test_uri, qs)
467 cu = f"{test_uri}{qs}"
468 req = urllib.request.Request(cu, None, {})
468 req = urllib.request.Request(cu, None, {})
469
469
470 try:
470 try:
@@ -475,7 +475,7 b' class HgRemote(RemoteBase):'
475 except Exception as e:
475 except Exception as e:
476 log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True)
476 log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True)
477 # means it cannot be cloned
477 # means it cannot be cloned
478 raise exceptions.URLError(e)("[{}] org_exc: {}".format(obfuscated_uri, e))
478 raise exceptions.URLError(e)(f"[{obfuscated_uri}] org_exc: {e}")
479
479
480 # now check if it's a proper hg repo, but don't do it for svn
480 # now check if it's a proper hg repo, but don't do it for svn
481 try:
481 try:
@@ -42,11 +42,11 b' class StreamFeeder(threading.Thread):'
42 """
42 """
43
43
44 def __init__(self, source):
44 def __init__(self, source):
45 super(StreamFeeder, self).__init__()
45 super().__init__()
46 self.daemon = True
46 self.daemon = True
47 filelike = False
47 filelike = False
48 self.bytes = bytes()
48 self.bytes = b''
49 if type(source) in (type(''), bytes, bytearray): # string-like
49 if type(source) in (str, bytes, bytearray): # string-like
50 self.bytes = bytes(source)
50 self.bytes = bytes(source)
51 else: # can be either file pointer or file-like
51 else: # can be either file pointer or file-like
52 if isinstance(source, int): # file pointer it is
52 if isinstance(source, int): # file pointer it is
@@ -85,7 +85,7 b' class StreamFeeder(threading.Thread):'
85 class InputStreamChunker(threading.Thread):
85 class InputStreamChunker(threading.Thread):
86 def __init__(self, source, target, buffer_size, chunk_size):
86 def __init__(self, source, target, buffer_size, chunk_size):
87
87
88 super(InputStreamChunker, self).__init__()
88 super().__init__()
89
89
90 self.daemon = True # die die die.
90 self.daemon = True # die die die.
91
91
@@ -440,7 +440,7 b' class SubprocessIOChunker(object):'
440 elif err and fail_on_stderr:
440 elif err and fail_on_stderr:
441 text_err = err.decode()
441 text_err = err.decode()
442 raise OSError(
442 raise OSError(
443 "Subprocess exited due to an error:\n{}".format(text_err))
443 f"Subprocess exited due to an error:\n{text_err}")
444
444
445 if ret_code_fail and fail_on_return_code:
445 if ret_code_fail and fail_on_return_code:
446 text_err = err.decode()
446 text_err = err.decode()
@@ -449,7 +449,7 b' class SubprocessIOChunker(object):'
449 # in many cases git reports the errors on stdout too
449 # in many cases git reports the errors on stdout too
450 text_err = out.decode()
450 text_err = out.decode()
451 raise OSError(
451 raise OSError(
452 "Subprocess exited with non 0 ret code:{}: stderr:{}".format(return_code, text_err))
452 f"Subprocess exited with non 0 ret code:{return_code}: stderr:{text_err}")
453
453
454 self.stdout = bg_out
454 self.stdout = bg_out
455 self.stderr = bg_err
455 self.stderr = bg_err
@@ -482,7 +482,7 b' class SubprocessIOChunker(object):'
482 self.stop_streams()
482 self.stop_streams()
483 err = self.get_stderr()
483 err = self.get_stderr()
484 raise OSError(
484 raise OSError(
485 "Subprocess exited (exit_code:{}) due to an error during iteration:\n{}".format(return_code, err))
485 f"Subprocess exited (exit_code:{return_code}) due to an error during iteration:\n{err}")
486
486
487 if stop_iteration:
487 if stop_iteration:
488 raise stop_iteration
488 raise stop_iteration
@@ -103,15 +103,15 b' class RequestWrapperTween(object):'
103 statsd.timing(
103 statsd.timing(
104 "vcsserver_req_timing.histogram", elapsed_time_ms,
104 "vcsserver_req_timing.histogram", elapsed_time_ms,
105 tags=[
105 tags=[
106 "view_name:{}".format(match_route),
106 f"view_name:{match_route}",
107 "code:{}".format(resp_code)
107 f"code:{resp_code}"
108 ],
108 ],
109 use_decimals=False
109 use_decimals=False
110 )
110 )
111 statsd.incr(
111 statsd.incr(
112 "vcsserver_req_total", tags=[
112 "vcsserver_req_total", tags=[
113 "view_name:{}".format(match_route),
113 f"view_name:{match_route}",
114 "code:{}".format(resp_code)
114 f"code:{resp_code}"
115 ])
115 ])
116
116
117 return response
117 return response
General Comments 0
You need to be logged in to leave comments. Login now