Skip to content

Commit 547a58a

Browse files
committed
RDBC-790 Embed RavenDB on package data + code style
1 parent a17358e commit 547a58a

11 files changed

+238
-110
lines changed

MANIFEST.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
graft ravendb_embedded/target/nuget
2-
include README.rst
2+
include README.rst
3+
recursive-exclude tests *

ravendb_embedded/embedded_server.py

+52-16
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,27 @@ def start_server(self, options_param: ServerOptions = None) -> None:
6060

6161
start_server = Lazy(lambda: self._run_server(options))
6262

63-
if self.server_task and self.server_task.created and self.server_task != start_server:
63+
if (
64+
self.server_task
65+
and self.server_task.created
66+
and self.server_task != start_server
67+
):
6468
raise RuntimeError("The server was already started")
6569

6670
self.server_task = start_server
6771

6872
if options.security is not None:
69-
self.client_pem_certificate_path = options.security.client_pem_certificate_path
73+
self.client_pem_certificate_path = (
74+
options.security.client_pem_certificate_path
75+
)
7076
self.trust_store_path = options.security.ca_certificate_path
7177

7278
start_server.get_value()
7379

7480
def get_document_store(self, database: str) -> DocumentStore:
75-
return self.get_document_store_from_options(DatabaseOptions.from_database_name(database))
81+
return self.get_document_store_from_options(
82+
DatabaseOptions.from_database_name(database)
83+
)
7684

7785
def _initialize_document_store(self, database_name, options):
7886
server_url = self.get_server_uri()
@@ -92,7 +100,9 @@ def _initialize_document_store(self, database_name, options):
92100

93101
return store
94102

95-
def get_document_store_from_options(self, options: DatabaseOptions) -> DocumentStore:
103+
def get_document_store_from_options(
104+
self, options: DatabaseOptions
105+
) -> DocumentStore:
96106
database_name = options.database_record.database_name
97107

98108
if not database_name or database_name.isspace():
@@ -104,20 +114,30 @@ def get_document_store_from_options(self, options: DatabaseOptions) -> DocumentS
104114

105115
return self.document_stores.setdefault(database_name, lazy).get_value()
106116

107-
def _try_create_database(self, options: DatabaseOptions, store: DocumentStore) -> None:
117+
def _try_create_database(
118+
self, options: DatabaseOptions, store: DocumentStore
119+
) -> None:
108120
try:
109-
store.maintenance.server.send(CreateDatabaseOperation(options.database_record))
121+
store.maintenance.server.send(
122+
CreateDatabaseOperation(options.database_record)
123+
)
110124
except Exception as e:
111125
# Expected behavior when the database already exists
112-
if "conflict" in e.args[0]: # todo: change exc type when python client will implement conflict handling
113-
self._log_debug(f"{options.database_record.database_name} already exists.")
126+
if (
127+
"conflict" in e.args[0]
128+
): # todo: change exc type when python client will implement conflict handling
129+
self._log_debug(
130+
f"{options.database_record.database_name} already exists."
131+
)
114132
else:
115133
raise e
116134

117135
def get_server_uri(self) -> str:
118136
server = self.server_task
119137
if server is None:
120-
raise RuntimeError("Please run start_server() before trying to use the server.")
138+
raise RuntimeError(
139+
"Please run start_server() before trying to use the server."
140+
)
121141

122142
return server.get_value()[0]
123143

@@ -126,7 +146,9 @@ def _shutdown_server_process(self, process: subprocess.Popen) -> None:
126146
return
127147

128148
with process:
129-
if process.poll() is not None: # Check if the process has already terminated
149+
if (
150+
process.poll() is not None
151+
): # Check if the process has already terminated
130152
return
131153

132154
try:
@@ -172,13 +194,19 @@ def _run_server(self, options: ServerOptions) -> Tuple[str, subprocess.Popen]:
172194
process.stdout,
173195
startup_duration,
174196
options,
175-
lambda line, builder: self.online(line, builder, url_ref, process, startup_duration, options),
197+
lambda line, builder: self.online(
198+
line, builder, url_ref, process, startup_duration, options
199+
),
176200
)
177201

178202
if url_ref["value"] is None:
179-
error_string = self.read_output(process.stderr, startup_duration, options, None)
203+
error_string = self.read_output(
204+
process.stderr, startup_duration, options, None
205+
)
180206
self._shutdown_server_process(process)
181-
raise RuntimeError(self.build_startup_exception_message(output_string, error_string))
207+
raise RuntimeError(
208+
self.build_startup_exception_message(output_string, error_string)
209+
)
182210

183211
return url_ref["value"], process
184212

@@ -210,9 +238,13 @@ def online(
210238
options: ServerOptions,
211239
):
212240
if line is None:
213-
error_string = self.read_output(process.stderr, startup_duration, options, None)
241+
error_string = self.read_output(
242+
process.stderr, startup_duration, options, None
243+
)
214244
self._shutdown_server_process(process)
215-
raise RuntimeError(self.build_startup_exception_message("".join(builder), error_string))
245+
raise RuntimeError(
246+
self.build_startup_exception_message("".join(builder), error_string)
247+
)
216248

217249
prefix = "Server available on: "
218250
if line.startswith(prefix):
@@ -234,7 +266,11 @@ def read_output_line() -> Optional[str]:
234266
line_ = output_queue.get_nowait()
235267
return line_
236268
except queue.Empty:
237-
if options.max_server_startup_time_duration - startup_duration.elapsed() <= timedelta(seconds=0):
269+
if (
270+
options.max_server_startup_time_duration
271+
- startup_duration.elapsed()
272+
<= timedelta(seconds=0)
273+
):
238274
return None
239275
time.sleep(1)
240276

ravendb_embedded/options.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
from ravendb.exceptions.raven_exceptions import RavenException
99
from ravendb.serverwide.database_record import DatabaseRecord
1010

11-
from ravendb_embedded.provide import ProvideRavenDBServer, ExtractFromPkgResourceServerProvider, ExternalServerProvider
11+
from ravendb_embedded.provide import (
12+
ProvideRavenDBServer,
13+
ExtractFromPkgResourceServerProvider,
14+
ExternalServerProvider,
15+
)
1216

1317

1418
class DatabaseOptions:
@@ -72,12 +76,16 @@ def secured(
7276
raise ValueError("certificate cannot be None")
7377

7478
if self.security is not None:
75-
raise RuntimeError("The security has already been set up for this ServerOptions object")
79+
raise RuntimeError(
80+
"The security has already been set up for this ServerOptions object"
81+
)
7682

7783
try:
7884
self.security = SecurityOptions()
7985
self.security.server_pfx_certificate_path = server_pfx_certificate_path
80-
self.security.server_pfx_certificate_password = server_pfx_certificate_password
86+
self.security.server_pfx_certificate_password = (
87+
server_pfx_certificate_password
88+
)
8189
self.security.client_pem_certificate_path = client_pem_certificate_path
8290
if ca_certificate_path:
8391
self.security.ca_certificate_path = ca_certificate_path

ravendb_embedded/raven_server_runner.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from ravendb.exceptions.raven_exceptions import RavenException
88

99
from ravendb_embedded.options import ServerOptions
10-
from ravendb_embedded.runtime_framework_version_matcher import RuntimeFrameworkVersionMatcher
10+
from ravendb_embedded.runtime_framework_version_matcher import (
11+
RuntimeFrameworkVersionMatcher,
12+
)
1113

1214

1315
class CommandLineArgumentEscaper:
@@ -43,7 +45,9 @@ def run(options: ServerOptions) -> subprocess.Popen:
4345
break
4446

4547
if server_dll_path is None:
46-
raise RavenException("Server file was not found in any of the expected locations.")
48+
raise RavenException(
49+
"Server file was not found in any of the expected locations."
50+
)
4751

4852
if not options.dot_net_path.strip():
4953
raise ValueError("dot_net_path cannot be None or whitespace")
@@ -86,7 +90,9 @@ def run(options: ServerOptions) -> subprocess.Popen:
8690
]
8791
)
8892
if options.security.client_pem_certificate_path:
89-
with open(options.security.client_pem_certificate_path, "rb") as cert_file:
93+
with open(
94+
options.security.client_pem_certificate_path, "rb"
95+
) as cert_file:
9096
cert_data = cert_file.read()
9197

9298
cert = x509.load_pem_x509_certificate(cert_data, default_backend())
@@ -112,7 +118,9 @@ def run(options: ServerOptions) -> subprocess.Popen:
112118
command_line_args.insert(1, framework_version)
113119
command_line_args.insert(1, "--fx-version")
114120

115-
process_builder = subprocess.Popen(command_line_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
121+
process_builder = subprocess.Popen(
122+
command_line_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
123+
)
116124
process = process_builder
117125

118126
return process

ravendb_embedded/runtime_framework_version_matcher.py

+54-15
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ def match(cls, options: ServerOptions) -> str:
2626
return cls.match_runtime(runtime, runtimes)
2727

2828
@staticmethod
29-
def match_runtime(runtime: RuntimeFrameworkVersion, runtimes: List[RuntimeFrameworkVersion]) -> str:
30-
sorted_runtimes = sorted(runtimes, key=lambda x: (x.major, x.minor, x.patch), reverse=True)
29+
def match_runtime(
30+
runtime: RuntimeFrameworkVersion, runtimes: List[RuntimeFrameworkVersion]
31+
) -> str:
32+
sorted_runtimes = sorted(
33+
runtimes, key=lambda x: (x.major, x.minor, x.patch), reverse=True
34+
)
3135

3236
for version in sorted_runtimes:
3337
if runtime.match(version):
@@ -44,7 +48,10 @@ def needs_match(cls, options: ServerOptions) -> bool:
4448
return False
4549

4650
framework_version = options.framework_version.lower()
47-
if cls.WILDCARD not in framework_version and cls.GREATER_OR_EQUAL not in framework_version:
51+
if (
52+
cls.WILDCARD not in framework_version
53+
and cls.GREATER_OR_EQUAL not in framework_version
54+
):
4855
return False
4956

5057
return True
@@ -59,14 +66,19 @@ def get_framework_versions(options: ServerOptions):
5966

6067
try:
6168
with subprocess.Popen(
62-
process_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
69+
process_command,
70+
stdout=subprocess.PIPE,
71+
stderr=subprocess.PIPE,
72+
text=True,
6373
) as process:
6474
inside_runtimes = False
6575
runtime_lines = []
6676

6777
for line in process.stdout:
6878
line = line.strip()
69-
if line.startswith(".NET runtimes installed:") or line.startswith(".NET Core runtimes installed:"):
79+
if line.startswith(".NET runtimes installed:") or line.startswith(
80+
".NET Core runtimes installed:"
81+
):
7082
inside_runtimes = True
7183
continue
7284
if inside_runtimes and line.startswith("Microsoft.NETCore.App"):
@@ -81,7 +93,9 @@ def get_framework_versions(options: ServerOptions):
8193
runtimes.append(RuntimeFrameworkVersion(values[1]))
8294

8395
except Exception as e:
84-
raise RuntimeError("Unable to execute dotnet to retrieve list of installed runtimes") from e
96+
raise RuntimeError(
97+
"Unable to execute dotnet to retrieve list of installed runtimes"
98+
) from e
8599
finally:
86100
if process:
87101
process.kill()
@@ -100,22 +114,28 @@ def __init__(self, framework_version: str):
100114

101115
framework_version = framework_version.lower()
102116

103-
suffixes = [s for s in framework_version.split(self.SUFFIX_SEPARATOR) if s.strip()]
117+
suffixes = [
118+
s for s in framework_version.split(self.SUFFIX_SEPARATOR) if s.strip()
119+
]
104120
if len(suffixes) != 1:
105121
framework_version = suffixes[0]
106122
self.suffix = self.SUFFIX_SEPARATOR.join(suffixes[1:])
107123
else:
108124
self.suffix = None
109125

110-
versions = [v.strip() for v in framework_version.split(self.SEPARATORS) if v.strip()]
126+
versions = [
127+
v.strip() for v in framework_version.split(self.SEPARATORS) if v.strip()
128+
]
111129
for i, version in enumerate(versions):
112130
if RuntimeFrameworkVersionMatcher.WILDCARD not in version:
113131
tuple_values = self.parse(version)
114132
self._set(i, version, *tuple_values)
115133
continue
116134

117135
if version != RuntimeFrameworkVersionMatcher.WILDCARD:
118-
raise RuntimeError(f"Wildcard character must be a sole part of the version string, but was '{version}'")
136+
raise RuntimeError(
137+
f"Wildcard character must be a sole part of the version string, but was '{version}'"
138+
)
119139

120140
self._set(i, None, None, MatchingType.EQUAL)
121141

@@ -161,12 +181,22 @@ def parse(value: str) -> Tuple[int, MatchingType]:
161181
value_as_int = int(value_to_parse)
162182
return value_as_int, matching_type
163183

164-
def _set(self, i: int, value_as_string: Optional[str], value: Optional[int], matching_type: MatchingType) -> None:
184+
def _set(
185+
self,
186+
i: int,
187+
value_as_string: Optional[str],
188+
value: Optional[int],
189+
matching_type: MatchingType,
190+
) -> None:
165191
if i == 0:
166-
self.assert_matching_type("major", value_as_string, MatchingType.EQUAL, matching_type)
192+
self.assert_matching_type(
193+
"major", value_as_string, MatchingType.EQUAL, matching_type
194+
)
167195
self.major = value
168196
elif i == 1:
169-
self.assert_matching_type("minor", value_as_string, MatchingType.EQUAL, matching_type)
197+
self.assert_matching_type(
198+
"minor", value_as_string, MatchingType.EQUAL, matching_type
199+
)
170200
self.minor = value
171201
elif i == 2:
172202
self.assert_matching_type("patch", value_as_string, None, matching_type)
@@ -189,7 +219,10 @@ def assert_matching_type(
189219
f"is not allowed when suffix ('{self.suffix}') is set."
190220
)
191221

192-
if expected_matching_type is not None and expected_matching_type != matching_type:
222+
if (
223+
expected_matching_type is not None
224+
and expected_matching_type != matching_type
225+
):
193226
raise RuntimeError(
194227
f"Cannot set '{field_name}' with value '{value_as_string}' "
195228
f"because '{self.matching_type_to_string(matching_type)}' "
@@ -213,9 +246,15 @@ def match(self, version: RuntimeFrameworkVersion) -> bool:
213246
return False
214247

215248
if self.patch is not None:
216-
if self.patch_matching_type == MatchingType.EQUAL and self.patch != version.patch:
249+
if (
250+
self.patch_matching_type == MatchingType.EQUAL
251+
and self.patch != version.patch
252+
):
217253
return False
218-
elif self.patch_matching_type == MatchingType.GREATER_OR_EQUAL and self.patch > version.patch:
254+
elif (
255+
self.patch_matching_type == MatchingType.GREATER_OR_EQUAL
256+
and self.patch > version.patch
257+
):
219258
return False
220259

221260
return self.suffix == version.suffix

requirements.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
setuptools~=68.2.0
22
ravendb>=5.2.4
3-
cryptography~=42.0.0
4-
tqdm~=4.66.1
3+
cryptography~=42.0.0

0 commit comments

Comments
 (0)