11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33
4- # from __future__ import annotations
5-
64import os
75import sys
86import yaml
1412
1513class SyncplayBoot :
1614 """ Handle Syncplay bootstrap arguments. """
17- def __debug (self , msg : str ) -> None :
15+ def __debug (self , prefix : str , message : Any ) -> None :
1816 """ Print out debug information. """
1917 if self .__debug_mode :
20- print (f'\033 [90m{ msg } \033 [0m' , file = sys .stderr )
18+ print (f'\033 [33m{ prefix } \033 [0m -> \033 [90m{ message } \033 [0m' , file = sys .stderr )
19+
20+ def __temp_file (self , file : str , content : str ) -> str :
21+ """ Create and save content to temporary files. """
22+ file = os .path .join (self .__temp_dir , file )
23+ with open (file , 'w' , encoding = 'utf-8' ) as fp :
24+ fp .write (content )
25+ return file
2126
2227 def __build_parser (self ) -> Generator :
2328 """ Build arguments parser for Syncplay bootstrap. """
@@ -48,33 +53,32 @@ def __build_options(self) -> Generator:
4853 yield action .dest , opt_type , is_list
4954
5055 def __init__ (self , args : list [str ], config : dict [str , Any ],
51- work_dir : str , cert_dir : str , debug_mode : bool = False ):
52- self .__work_dir = work_dir
53- self .__cert_dir = cert_dir
56+ cert_dir : str , temp_dir : str , work_dir : str , debug_mode : bool = False ):
5457 self .__debug_mode = debug_mode
58+ self .__cert_dir , self .__temp_dir , self .__work_dir = cert_dir , temp_dir , work_dir
5559 self .__options = [x for x in self .__build_options ()] # list[(NAME, TYPE, IS_LIST)]
56- self .__debug (f 'Bootstrap options -> { self .__options } \n ' )
60+ self .__debug ('Bootstrap options' , self .__options )
5761
5862 env_opts = self .__load_from_env ()
59- self .__debug (f 'Environment options -> { env_opts } \n ' )
63+ self .__debug ('Environment options' , env_opts )
6064 cfg_opts = self .__load_from_config (config )
61- self .__debug (f 'Configure file options -> { cfg_opts } \n ' )
65+ self .__debug ('Configure file options' , cfg_opts )
6266 cli_opts = self .__load_from_args (args )
63- self .__debug (f 'Command line options -> { cli_opts } \n ' )
67+ self .__debug ('Command line options' , cli_opts )
6468
6569 self .__opts = env_opts | cfg_opts | cli_opts
66- self .__debug (f 'Bootstrap final options -> { self .__opts } \n ' )
70+ self .__debug ('Bootstrap final options' , self .__opts )
6771
6872 def __load_from_args (self , raw_args : list [str ]) -> dict [str , Any ]:
6973 """ Loading options from command line arguments. """
7074 args = self .__parser .parse_args (raw_args )
71- self .__debug (f 'Command line arguments -> { args } ' )
75+ self .__debug ('Command line arguments' , args )
7276 arg_filter = lambda x : x is not None and x is not False
7377 return {x : y for x , y in vars (args ).items () if arg_filter (y )}
7478
7579 def __load_from_config (self , config : dict [str , Any ]) -> dict [str , Any ]:
7680 """ Loading options from configure file. """
77- self .__debug (f 'Configure file -> { config } ' )
81+ self .__debug ('Configure file' , config )
7882 options = {x [0 ].replace ('_' , '-' ): x [0 ] for x in self .__options }
7983 return {options [x ]: config [x ] for x in options if x in config }
8084
@@ -88,25 +92,17 @@ def __convert(opt_raw: str, opt_field: str, opt_type: type) -> tuple[str, Any]:
8892 elif opt_type is bool :
8993 return opt_field , opt_raw .upper () in ['ON' , 'TRUE' ]
9094
91- self .__debug (f 'Environment variables -> { os .environ } ' )
95+ self .__debug ('Environment variables' , os .environ )
9296 options = {x .upper (): (x , t ) for x , t , is_list in self .__options if not is_list } # filter non-list options
9397 return dict ([__convert (os .environ [x ], * y ) for x , y in options .items () if x in os .environ ])
9498
95- @staticmethod
96- def __temp_file (file : str , content : str ) -> str :
97- """ Create and save content to temporary files. """
98- file = os .path .join ('/tmp/' , file )
99- with open (file , 'w' ) as fp :
100- fp .write (content )
101- return file
102-
10399 def release (self ) -> list [str ]:
104100 """ Construct the startup arguments for syncplay server. """
105101 args = ['--port' , str (self .__opts .get ('port' , 8999 ))]
106102 if 'password' in self .__opts :
107103 args += ['--password' , self .__opts ['password' ]]
108104 if 'motd' in self .__opts :
109- args += ['--motd-file' , SyncplayBoot .__temp_file ('motd.data' , self .__opts ['motd' ])]
105+ args += ['--motd-file' , self .__temp_file ('motd.data' , self .__opts ['motd' ])]
110106
111107 salt = self .__opts .get ('salt' , None if 'random_salt' in self .__opts else '' )
112108 if salt is not None :
@@ -128,7 +124,7 @@ def release(self) -> list[str]:
128124 args += ['--max-chat-message-length' , str (self .__opts ['max_chat_message' ])]
129125 if 'permanent_rooms' in self .__opts :
130126 rooms = '\n ' .join (self .__opts ['permanent_rooms' ])
131- args += ['--permanent-rooms-file' , SyncplayBoot .__temp_file ('rooms.list' , rooms )]
127+ args += ['--permanent-rooms-file' , self .__temp_file ('rooms.list' , rooms )]
132128
133129 if 'listen_ipv4' in self .__opts and 'listen_ipv6' in self .__opts :
134130 args += ['--interface-ipv4' , self .__opts ['listen_ipv4' ]]
@@ -138,19 +134,20 @@ def release(self) -> list[str]:
138134 elif 'listen_ipv6' in self .__opts :
139135 args += ['--ipv6-only' , '--interface-ipv6' , self .__opts ['listen_ipv6' ]]
140136
141- self .__debug (f 'Syncplay startup arguments -> { args } ' )
137+ self .__debug ('Syncplay startup arguments' , args )
142138 return args
143139
144140
145141def syncplay_boot () -> None :
146142 """ Bootstrap the syncplay server. """
143+ temp_dir = os .environ .get ('TEMP_DIR' , '/tmp' )
147144 work_dir = os .environ .get ('WORK_DIR' , '/data' )
148145 cert_dir = os .environ .get ('CERT_DIR' , '/certs' )
149146 config_file = os .environ .get ('CONFIG' , 'config.yml' )
150147 debug_mode = os .environ .get ('DEBUG' , '' ).upper () in ['ON' , 'TRUE' ]
151148
152149 config = yaml .safe_load (open (config_file ).read ()) if os .path .exists (config_file ) else {}
153- bootstrap = SyncplayBoot (sys .argv [1 :], config , work_dir , cert_dir , debug_mode )
150+ bootstrap = SyncplayBoot (sys .argv [1 :], config , cert_dir , temp_dir , work_dir , debug_mode )
154151 sys .argv = ['syncplay' ] + bootstrap .release ()
155152
156153
0 commit comments