Skip to content

Commit 988c0f3

Browse files
committed
clean up pre-commit errors
1 parent a418923 commit 988c0f3

File tree

3 files changed

+84
-64
lines changed

3 files changed

+84
-64
lines changed

src/pb_utils.cc

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "pb_utils.h"
2828

2929
#include <sys/stat.h>
30+
3031
#include <fstream>
3132

3233
#ifdef _WIN32
@@ -39,7 +40,7 @@
3940
#endif
4041

4142
#ifndef _WIN32
42-
extern char **environ;
43+
extern char** environ;
4344
#endif
4445

4546

@@ -323,38 +324,44 @@ WrapTritonErrorInSharedPtr(TRITONSERVER_Error* error)
323324
}
324325
#endif // NOT TRITON_PB_STUB
325326

326-
bool IsValidIdentifier(const std::string& input) {
327+
bool
328+
IsValidIdentifier(const std::string& input)
329+
{
327330
if (input.empty()) {
328331
return false;
329332
}
330-
333+
331334
// Check for invalid characters
332335
if (input.find_first_of(INVALID_CHARS) != std::string::npos) {
333336
return false;
334337
}
335-
338+
336339
return true;
337340
}
338341

339-
bool IsValidPath(const std::string& path) {
342+
bool
343+
IsValidPath(const std::string& path)
344+
{
340345
if (path.empty()) {
341346
return false;
342347
}
343-
348+
344349
// Must be absolute path
345350
if (path[0] != '/') {
346351
return false;
347352
}
348-
353+
349354
return true;
350355
}
351356

352-
bool IsExecutableFile(const std::string& filepath) {
357+
bool
358+
IsExecutableFile(const std::string& filepath)
359+
{
353360
struct stat file_stat;
354361
if (stat(filepath.c_str(), &file_stat) != 0) {
355362
return false;
356363
}
357-
364+
358365
// Check if it's a regular file and executable by owner
359366
return S_ISREG(file_stat.st_mode) && (file_stat.st_mode & S_IXUSR);
360367
}
@@ -368,14 +375,15 @@ GenerateUUID()
368375
}
369376

370377
// Helper function to parse environment variables from activation script
371-
std::map<std::string, std::string>
372-
ParseActivationScript(const std::string& activate_path) {
378+
std::map<std::string, std::string>
379+
ParseActivationScript(const std::string& activate_path)
380+
{
373381
std::map<std::string, std::string> env_vars;
374-
382+
375383
// Read the current environment as baseline
376384
#ifndef _WIN32
377385
if (environ != nullptr) {
378-
for (char **env = environ; *env != nullptr; env++) {
386+
for (char** env = environ; *env != nullptr; env++) {
379387
std::string env_str(*env);
380388
size_t eq_pos = env_str.find('=');
381389
if (eq_pos != std::string::npos) {
@@ -386,33 +394,33 @@ ParseActivationScript(const std::string& activate_path) {
386394
}
387395
}
388396
#endif
389-
397+
390398
// Parse activation script for environment changes
391399
std::ifstream activate_file(activate_path);
392400
if (!activate_file.is_open()) {
393-
return env_vars; // Return current environment if can't read activation script
401+
return env_vars; // Return current environment if can't read activation
402+
// script
394403
}
395-
404+
396405
std::string line;
397406
while (std::getline(activate_file, line)) {
398407
// Look for export statements or direct assignments
399408
if (line.find("export ") == 0) {
400409
// Handle: export VAR=value
401-
line = line.substr(7); // Remove "export "
410+
line = line.substr(7); // Remove "export "
402411
}
403-
412+
404413
size_t eq_pos = line.find('=');
405414
if (eq_pos != std::string::npos && line[0] != '#') {
406415
std::string key = line.substr(0, eq_pos);
407416
std::string value = line.substr(eq_pos + 1);
408-
417+
409418
// Remove quotes if present
410-
if (value.size() >= 2 &&
411-
((value[0] == '"' && value.back() == '"') ||
412-
(value[0] == '\'' && value.back() == '\''))) {
419+
if (value.size() >= 2 && ((value[0] == '"' && value.back() == '"') ||
420+
(value[0] == '\'' && value.back() == '\''))) {
413421
value = value.substr(1, value.size() - 2);
414422
}
415-
423+
416424
// Handle variable substitution for common cases
417425
if (value.find("$PATH") != std::string::npos) {
418426
size_t pos = value.find("$PATH");
@@ -422,22 +430,23 @@ ParseActivationScript(const std::string& activate_path) {
422430
size_t pos = value.find("$LD_LIBRARY_PATH");
423431
value.replace(pos, 16, env_vars["LD_LIBRARY_PATH"]);
424432
}
425-
433+
426434
env_vars[key] = value;
427435
}
428436
}
429-
437+
430438
return env_vars;
431439
}
432440

433441
// Helper function to prepare environment array for execve
434-
std::pair<std::vector<std::string>, std::vector<char*>>
435-
PrepareEnvironment(const std::map<std::string, std::string>& env_vars,
436-
const std::string& additional_lib_path) {
437-
442+
std::pair<std::vector<std::string>, std::vector<char*>>
443+
PrepareEnvironment(
444+
const std::map<std::string, std::string>& env_vars,
445+
const std::string& additional_lib_path)
446+
{
438447
std::vector<std::string> env_strings;
439448
std::vector<char*> env_array;
440-
449+
441450
for (const auto& [key, value] : env_vars) {
442451
std::string env_string;
443452
if (key == "LD_LIBRARY_PATH" && !additional_lib_path.empty()) {
@@ -448,13 +457,13 @@ PrepareEnvironment(const std::map<std::string, std::string>& env_vars,
448457
}
449458
env_strings.push_back(env_string);
450459
}
451-
460+
452461
// Convert to char* array
453462
for (auto& env_str : env_strings) {
454463
env_array.push_back(const_cast<char*>(env_str.c_str()));
455464
}
456465
env_array.push_back(nullptr);
457-
466+
458467
return std::make_pair(std::move(env_strings), std::move(env_array));
459468
}
460469

src/pb_utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,11 @@ std::shared_ptr<TRITONSERVER_Error*> WrapTritonErrorInSharedPtr(
363363
std::string GenerateUUID();
364364

365365
// Environment handling utilities for Python activation scripts
366-
std::map<std::string, std::string> ParseActivationScript(const std::string& activate_path);
366+
std::map<std::string, std::string> ParseActivationScript(
367+
const std::string& activate_path);
367368

368369
std::pair<std::vector<std::string>, std::vector<char*>> PrepareEnvironment(
369-
const std::map<std::string, std::string>& env_vars,
370+
const std::map<std::string, std::string>& env_vars,
370371
const std::string& additional_lib_path = "");
371372

372373
}}} // namespace triton::backend::python

src/stub_launcher.cc

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828

2929
#include <filesystem>
3030

31-
#include "python_be.h"
3231
#include "pb_utils.h"
32+
#include "python_be.h"
3333

3434
#ifdef _WIN32
3535
#include <process.h> // getpid()
3636
#endif
3737

38-
extern char **environ;
38+
extern char** environ;
3939

4040
namespace triton { namespace backend { namespace python {
4141

@@ -345,13 +345,13 @@ StubLauncher::Launch()
345345
TRITONSERVER_ERROR_INVALID_ARG,
346346
"Invalid stub name: contains invalid characters");
347347
}
348-
348+
349349
if (!IsValidPath(model_path_)) {
350350
return TRITONSERVER_ErrorNew(
351351
TRITONSERVER_ERROR_INVALID_ARG,
352352
"Invalid model path: contains invalid characters or not absolute");
353353
}
354-
354+
355355
if (!IsValidIdentifier(shm_region_name_)) {
356356
return TRITONSERVER_ErrorNew(
357357
TRITONSERVER_ERROR_INVALID_ARG,
@@ -372,12 +372,12 @@ StubLauncher::Launch()
372372
// Validate the stub executable path
373373
if (!IsValidPath(python_backend_stub)) {
374374
return TRITONSERVER_ErrorNew(
375-
TRITONSERVER_ERROR_INVALID_ARG,
376-
"Invalid python backend stub path");
375+
TRITONSERVER_ERROR_INVALID_ARG, "Invalid python backend stub path");
377376
}
378377

379378
if (!IsExecutableFile(python_backend_stub)) {
380-
// Give the execute permission for the triton_python_backend_stub to the owner.
379+
// Give the execute permission for the triton_python_backend_stub to the
380+
// owner.
381381
int error = chmod(python_backend_stub.c_str(), S_IXUSR);
382382
if (error != 0) {
383383
return TRITONSERVER_ErrorNew(
@@ -398,24 +398,24 @@ StubLauncher::Launch()
398398
// revert the LD_LIBRARY_PATH changes to avoid shared library issues in
399399
// executables and libraries.
400400
ipc_control_->uses_env = false;
401-
401+
402402
if (python_execution_env_ != "") {
403-
404403
// Validate Python environment paths
405404
if (!IsValidPath(path_to_activate_) || !IsValidPath(path_to_libpython_)) {
406405
return TRITONSERVER_ErrorNew(
407-
TRITONSERVER_ERROR_INVALID_ARG,
408-
"Invalid Python environment paths");
406+
TRITONSERVER_ERROR_INVALID_ARG, "Invalid Python environment paths");
409407
}
410-
408+
411409
ipc_control_->uses_env = true;
412-
410+
413411
// Parse environment variables from activation script
414-
std::map<std::string, std::string> env_vars = ParseActivationScript(path_to_activate_);
415-
416-
// Prepare environment with additional library path
417-
auto [env_strings, custom_env] = PrepareEnvironment(env_vars, path_to_libpython_);
418-
412+
std::map<std::string, std::string> env_vars =
413+
ParseActivationScript(path_to_activate_);
414+
415+
// Prepare environment with additional library path
416+
auto [env_strings, custom_env] =
417+
PrepareEnvironment(env_vars, path_to_libpython_);
418+
419419
// Set up arguments for direct execution
420420
arg_strings.push_back(python_backend_stub);
421421
arg_strings.push_back(model_path_);
@@ -427,7 +427,7 @@ StubLauncher::Launch()
427427
arg_strings.push_back(std::to_string(ipc_control_handle_));
428428
arg_strings.push_back(stub_name);
429429
arg_strings.push_back(runtime_modeldir_);
430-
430+
431431
// Convert strings to char* array for exec
432432
for (const auto& arg : arg_strings) {
433433
exec_args.push_back(arg.c_str());
@@ -437,12 +437,15 @@ StubLauncher::Launch()
437437
// Log the command being executed
438438
std::ostringstream log_cmd;
439439
for (size_t i = 0; i < arg_strings.size(); ++i) {
440-
if (i > 0) log_cmd << " ";
440+
if (i > 0)
441+
log_cmd << " ";
441442
log_cmd << "'" << arg_strings[i] << "'";
442443
}
443444
LOG_MESSAGE(
444445
TRITONSERVER_LOG_VERBOSE,
445-
(std::string("Starting Python backend stub with custom environment: ") + log_cmd.str()).c_str());
446+
(std::string("Starting Python backend stub with custom environment: ") +
447+
log_cmd.str())
448+
.c_str());
446449

447450
pid_t pid = fork();
448451
if (pid < 0) {
@@ -451,11 +454,16 @@ StubLauncher::Launch()
451454
"Failed to fork the stub process for auto-complete.");
452455
}
453456
if (pid == 0) {
454-
// Replace this child process with the new stub process using custom environment
455-
execve(python_backend_stub.c_str(), const_cast<char**>(exec_args.data()), custom_env.data());
457+
// Replace this child process with the new stub process using custom
458+
// environment
459+
execve(
460+
python_backend_stub.c_str(), const_cast<char**>(exec_args.data()),
461+
custom_env.data());
456462
// execve() never returns if succeeded. Otherwise, an error has occurred.
457463
std::stringstream ss;
458-
ss << "Failed to run python backend stub with custom environment. Errno = " << errno << '\n'
464+
ss << "Failed to run python backend stub with custom environment. Errno "
465+
"= "
466+
<< errno << '\n'
459467
<< "Python backend stub path: " << python_backend_stub << '\n'
460468
<< "Activation script: " << path_to_activate_ << '\n'
461469
<< "Library path: " << path_to_libpython_ << '\n';
@@ -464,7 +472,7 @@ StubLauncher::Launch()
464472
} else {
465473
stub_pid_ = pid;
466474
}
467-
475+
468476
} else {
469477
arg_strings.push_back(python_backend_stub);
470478
arg_strings.push_back(model_path_);
@@ -476,7 +484,7 @@ StubLauncher::Launch()
476484
arg_strings.push_back(std::to_string(ipc_control_handle_));
477485
arg_strings.push_back(stub_name);
478486
arg_strings.push_back(runtime_modeldir_);
479-
487+
480488
// Convert strings to char* array for exec
481489
for (const auto& arg : arg_strings) {
482490
exec_args.push_back(arg.c_str());
@@ -486,12 +494,14 @@ StubLauncher::Launch()
486494
// Log the command being executed
487495
std::ostringstream log_cmd;
488496
for (size_t i = 0; i < arg_strings.size(); ++i) {
489-
if (i > 0) log_cmd << " ";
497+
if (i > 0)
498+
log_cmd << " ";
490499
log_cmd << "'" << arg_strings[i] << "'";
491500
}
492501
LOG_MESSAGE(
493502
TRITONSERVER_LOG_VERBOSE,
494-
(std::string("Starting Python backend stub: ") + log_cmd.str()).c_str());
503+
(std::string("Starting Python backend stub: ") + log_cmd.str())
504+
.c_str());
495505

496506
pid_t pid = fork();
497507
if (pid < 0) {
@@ -974,6 +984,6 @@ StubLauncher::ShareCUDAMemoryPool(
974984
if (pb_exception.what() != std::string{""}) {
975985
throw pb_exception;
976986
}
977-
}
978-
#endif // TRITON_ENABLE_GPU
987+
}
988+
#endif // TRITON_ENABLE_GPU
979989
}}}; // namespace triton::backend::python

0 commit comments

Comments
 (0)