unified temp files

This commit is contained in:
Pavel Muhortov 2023-06-25 20:15:44 +03:00
parent 7a12aed45b
commit 17b180bc4f

View File

@ -1,6 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# pylint: disable=C0103,C0302,C0114,W0621 # pylint: disable=C0103,C0302,W0621
"""It's the main executor of cctv-scheduler. Processes incoming parameters and calls subclasses
"""
import calendar import calendar
import base64 import base64
@ -365,7 +367,8 @@ class FFmpeg:
process = cls._bin(ffpath).split() + raw.split() process = cls._bin(ffpath).split() + raw.split()
if onlyonce and Proc.search(' '.join(process)): if onlyonce and Proc.search(' '.join(process)):
print('Process already exist, exit...') logging.info(msg='ffmpeg process already exist')
return 0
else: else:
logging.info(msg='Starting ' + ' '.join(process)) logging.info(msg='Starting ' + ' '.join(process))
with Popen(process, stdout=PIPE, stderr=STDOUT) as proc: with Popen(process, stdout=PIPE, stderr=STDOUT) as proc:
@ -2105,9 +2108,11 @@ class Sequence:
"""Sequence handling. """Sequence handling.
""" """
@staticmethod @staticmethod
# pylint: disable=W0718
def run( def run(
device: HikISAPI, sensors: dict, sequence: dict, device: HikISAPI,
sensors: dict,
sequence: dict,
temp_path: str,
records_root_path: str = None, records_root_path: str = None,
records_root_user: str = None, records_root_user: str = None,
records_root_pass: str = None records_root_pass: str = None
@ -2118,6 +2123,7 @@ class Sequence:
device (HikISAPI): HikISAPI object. device (HikISAPI): HikISAPI object.
sensors (dict): collection as key=sensorname:value=Sensor object. sensors (dict): collection as key=sensorname:value=Sensor object.
sequence (dict): sequence steps collection. sequence (dict): sequence steps collection.
temp_path (str): path to directory for temp files.
records_root_path (str, optional): path (local|smb|ftp,sftp) to records directory. records_root_path (str, optional): path (local|smb|ftp,sftp) to records directory.
Defaults to None. Defaults to None.
records_root_user (str, optional): username if path on remote host. records_root_user (str, optional): username if path on remote host.
@ -2125,6 +2131,7 @@ class Sequence:
records_root_pass (str, optional): password if path on remote host. records_root_pass (str, optional): password if path on remote host.
Defaults to None. Defaults to None.
""" """
makedirs(temp_path, exist_ok=True)
for key, value in sequence.items(): for key, value in sequence.items():
action = value.split(',')[0].strip() action = value.split(',')[0].strip()
x = value.split(',')[1].strip() x = value.split(',')[1].strip()
@ -2181,10 +2188,6 @@ class Sequence:
elif action == 'settextonosd': elif action == 'settextonosd':
response = device.settextonosd(x=int(x), y=int(y), message=m) response = device.settextonosd(x=int(x), y=int(y), message=m)
elif action == 'downloadjpeg': elif action == 'downloadjpeg':
records_root_temp = records_root_path
if records_root_temp != path.dirname(path.realpath(__file__)):
records_root_temp = path.dirname(path.realpath(__file__)) + sep + 'temp'
makedirs(records_root_temp, exist_ok=True)
dy = datetime.datetime.now().strftime('%Y') dy = datetime.datetime.now().strftime('%Y')
dm = datetime.datetime.now().strftime('%m') dm = datetime.datetime.now().strftime('%m')
dv = datetime.datetime.now().strftime('%V') dv = datetime.datetime.now().strftime('%V')
@ -2198,7 +2201,7 @@ class Sequence:
if device.downloadjpeg( if device.downloadjpeg(
x=int(x), x=int(x),
y=int(y), y=int(y),
dst_file=records_root_temp + sep + records_file_name dst_file=temp_path + sep + records_file_name
): ):
hostname = 'localhost' hostname = 'localhost'
hostport, hosttype = None, None hostport, hosttype = None, None
@ -2223,7 +2226,7 @@ class Sequence:
hostport = int(hostname.split(':')[1]) hostport = int(hostname.split(':')[1])
hostname = hostname.split(':')[0] hostname = hostname.split(':')[0]
if hosttype == 'ftp': if hosttype == 'ftp':
src_file = records_root_temp + sep + records_file_name src_file = temp_path + sep + records_file_name
dst_file = ( dst_file = (
hostpath hostpath
+ '/' + dy + '/' + dm + '/' + dv + '/' + dd + '/' + '/' + dy + '/' + dm + '/' + dv + '/' + dd + '/'
@ -2235,13 +2238,16 @@ class Sequence:
hostname=hostname, hostname=hostname,
username=username, username=username,
password=userpass password=userpass
): ):
try: try:
remove(src_file) remove(src_file)
except OSError: except OSError as error:
pass logging.debug(msg='\n' + 'error: ' + str(error))
response = True
else:
response = False
elif hosttype == 'sftp': elif hosttype == 'sftp':
src_file = records_root_temp + sep + records_file_name src_file = temp_path + sep + records_file_name
dst_file = ( dst_file = (
hostpath hostpath
+ '/' + dy + '/' + dm + '/' + dv + '/' + dd + '/' + '/' + dy + '/' + dm + '/' + dv + '/' + dd + '/'
@ -2254,13 +2260,13 @@ class Sequence:
if response != 'ERROR': if response != 'ERROR':
try: try:
remove(src_file) remove(src_file)
except OSError: except OSError as error:
pass logging.debug(msg='\n' + 'error: ' + str(error))
response = True response = True
else: else:
response = False response = False
else: else:
src_file = records_root_temp + sep + records_file_name src_file = temp_path + sep + records_file_name
dst_file = ( dst_file = (
hostpath hostpath
+ sep + dy + sep + dm + sep + dv + sep + dd + sep + sep + dy + sep + dm + sep + dv + sep + dd + sep
@ -2272,7 +2278,7 @@ class Sequence:
) )
replace(src=src_file, dst=dst_file) replace(src=src_file, dst=dst_file)
response = True response = True
except Exception as error: except OSError as error:
logging.debug(msg='' logging.debug(msg=''
+ '\n' + 'src_file: ' + src_file + '\n' + 'src_file: ' + src_file
+ '\n' + 'dst_file: ' + dst_file + '\n' + 'dst_file: ' + dst_file
@ -2287,6 +2293,10 @@ class Sequence:
logging.info(msg=' result:' + key + ' = OK') logging.info(msg=' result:' + key + ' = OK')
else: else:
logging.warning(msg='result:' + key + ' = ERROR') logging.warning(msg='result:' + key + ' = ERROR')
try:
rmdir(temp_path)
except OSError as error:
logging.debug(msg='\n' + 'error: ' + str(error))
class Convert: class Convert:
@ -3005,7 +3015,7 @@ class Do():
return result return result
@staticmethod @staticmethod
# pylint: disable=W0612,W0511 # pylint: disable=W0612
def tg_routine_media( def tg_routine_media(
tg: Telegram, tg: Telegram,
targets_media_files: dict, targets_media_files: dict,
@ -3076,7 +3086,6 @@ class Do():
for media_name, media_path in targets_media_files[period].items(): for media_name, media_path in targets_media_files[period].items():
if re.match("^(?:http://|https://|file_id=)", media_path): if re.match("^(?:http://|https://|file_id=)", media_path):
# todo: check remote file size
raise ValueError(media_name + ' is not local file') raise ValueError(media_name + ' is not local file')
else: else:
tg_limit_video_size = 50000000 tg_limit_video_size = 50000000
@ -3215,15 +3224,15 @@ if __name__ == "__main__":
logging.FileHandler( logging.FileHandler(
filename=log_root + sep + path.splitext(path.basename(__file__))[0] + '.log', filename=log_root + sep + path.splitext(path.basename(__file__))[0] + '.log',
mode='a' mode='a'
), ),
logging.StreamHandler() logging.StreamHandler()
], ],
level=log_level level=log_level
) )
logging.getLogger("paramiko").setLevel(logging.WARNING) logging.getLogger("paramiko").setLevel(logging.WARNING)
if args['broadcast']: if args['broadcast']:
logging.info(msg='Starting streaming media to destination') logging.info(msg='Streaming starts...')
broadcasts = {} broadcasts = {}
conf = Parse(parameters=args['config'], block='enable-broadcast') conf = Parse(parameters=args['config'], block='enable-broadcast')
@ -3269,11 +3278,12 @@ if __name__ == "__main__":
onlyonce=onlyonce onlyonce=onlyonce
) )
logging.info(msg='' logging.info(msg=''
+ 'Finished streaming ' + key + ' with exit code: ' + 'Streaming ' + key + ' finished with exit code: '
+ str(exit_code) + str(exit_code)
) )
elif args['sequences']: elif args['sequences']:
logging.info(msg='Starting PTZ sequences from config file') logging.info(msg='Sequence starts...')
sensors = {} sensors = {}
conf = Parse(parameters=args['config'], block='enable-sensor') conf = Parse(parameters=args['config'], block='enable-sensor')
@ -3321,11 +3331,13 @@ if __name__ == "__main__":
device=device_entity, device=device_entity,
sensors=sensors, sensors=sensors,
sequence=device_sequence, sequence=device_sequence,
temp_path=temp_path + sep + Do.random_string(8),
records_root_path=records_root_path, records_root_path=records_root_path,
records_root_user=records_root_user, records_root_user=records_root_user,
records_root_pass=records_root_pass records_root_pass=records_root_pass
) )
logging.info(msg='Finished PTZ sequence ' + key) logging.info(msg='Sequence ' + key + ' finished')
elif args['day'] or args['week'] or args['month'] or args['year']: elif args['day'] or args['week'] or args['month'] or args['year']:
period = None period = None
amount = None amount = None
@ -3344,7 +3356,7 @@ if __name__ == "__main__":
period = Do.date_calc(period=period, amount=amount) period = Do.date_calc(period=period, amount=amount)
if args['converter']: if args['converter']:
logging.info(msg='Starting convert JPEG collection to MP4') logging.info(msg='Converting starts...')
conf = Parse(parameters=args['config'], block='enable-convert') conf = Parse(parameters=args['config'], block='enable-convert')
for key, value in conf.data.items(): for key, value in conf.data.items():
@ -3455,9 +3467,9 @@ if __name__ == "__main__":
video_duration=video_duration, video_duration=video_duration,
temp_path=temp_path + sep + Do.random_string(8) temp_path=temp_path + sep + Do.random_string(8)
) )
logging.info(msg='Finished convert JPEG collection ' + key) logging.info(msg='Converting ' + key + ' finished')
if args['publisher']: if args['publisher']:
logging.info(msg='Starting publish content from templates') logging.info(msg='Publishing starts...')
conf = Parse(parameters=args['config'], block='enable-publish') conf = Parse(parameters=args['config'], block='enable-publish')
for key, value in conf.data.items(): for key, value in conf.data.items():
@ -3518,7 +3530,8 @@ if __name__ == "__main__":
tg_api_key=tg_api_key, tg_api_key=tg_api_key,
tg_chat_id=tg_chat_id tg_chat_id=tg_chat_id
) )
logging.info(msg='Finished publish content ' + key) logging.info(msg='Publishing ' + key + ' finished')
else: else:
logging.info(msg='Start arguments was not selected. Exit.') logging.info(msg='Start arguments was not selected. Exit.')