azfilebackup/azfilebak/naming.py

126 строки
4.6 KiB
Python
Исходник Обычный вид История

2018-09-03 19:11:17 +03:00
# coding=utf-8
2018-09-12 11:08:22 +03:00
"""Timing module."""
2018-09-03 19:11:17 +03:00
import os
import re
2018-09-12 11:08:22 +03:00
class Naming(object):
"""Utility functions to generate file and blob names."""
2018-09-03 19:11:17 +03:00
@staticmethod
def backup_type_str(is_full):
"""
>>> Naming.backup_type_str(is_full=True)
'full'
>>> Naming.backup_type_str(is_full=False)
'tran'
"""
return ({True:"full", False:"tran"})[is_full]
@staticmethod
def type_str_is_full(type_str):
"""
>>> Naming.type_str_is_full('full')
True
>>> Naming.type_str_is_full('tran')
False
"""
return ({"full":True, "tran":False})[type_str]
@staticmethod
2018-09-12 11:40:33 +03:00
def construct_filename(fileset, is_full, start_timestamp):
2018-09-03 19:11:17 +03:00
"""
2018-09-12 11:40:33 +03:00
>>> Naming.construct_filename(fileset="test1fs", is_full=True, start_timestamp="20180601_112429")
2018-09-13 15:37:04 +03:00
'test1fs_full_20180601_112429.tar.gz'
2018-09-12 11:40:33 +03:00
>>> Naming.construct_filename(fileset="test1fs", is_full=False, start_timestamp="20180601_112429")
2018-09-13 15:37:04 +03:00
'test1fs_tran_20180601_112429.tar.gz'
2018-09-03 19:11:17 +03:00
"""
2018-09-13 15:37:04 +03:00
return "{fileset}_{type}_{start_timestamp}.tar.gz".format(
2018-09-12 11:08:22 +03:00
fileset=fileset,
type=Naming.backup_type_str(is_full),
2018-09-12 11:40:33 +03:00
start_timestamp=start_timestamp)
2018-09-03 19:11:17 +03:00
@staticmethod
2018-09-12 11:40:33 +03:00
def local_filesystem_name(directory, fileset, is_full, start_timestamp):
2018-09-12 11:08:22 +03:00
"""
Construct file name with directory.
2018-09-12 11:40:33 +03:00
>>> Naming.local_filesystem_name(directory="/tmp", fileset="test1fs", is_full=True, start_timestamp="20180601_112429")
2018-09-13 15:37:04 +03:00
'/tmp/test1fs_full_20180601_112429.tar.gz'
2018-09-12 11:08:22 +03:00
"""
2018-09-03 19:11:17 +03:00
file_name = Naming.construct_filename(
2018-09-12 11:40:33 +03:00
fileset, is_full, start_timestamp)
2018-09-03 19:11:17 +03:00
return os.path.join(directory, file_name)
@staticmethod
2018-09-12 11:08:22 +03:00
def construct_blobname_prefix(fileset, is_full):
2018-09-03 19:11:17 +03:00
"""
2018-09-12 11:08:22 +03:00
>>> Naming.construct_blobname_prefix(fileset="test1fs", is_full=True)
'test1fs_full_'
2018-09-03 19:11:17 +03:00
"""
2018-09-12 11:08:22 +03:00
return "{fileset}_{type}_".format(fileset=fileset, type=Naming.backup_type_str(is_full))
2018-09-03 19:11:17 +03:00
@staticmethod
2018-09-12 11:40:33 +03:00
def construct_blobname(fileset, is_full, start_timestamp, end_timestamp):
2018-09-03 19:11:17 +03:00
"""
2018-09-12 11:40:33 +03:00
>>> Naming.construct_blobname(fileset="test1fs", is_full=True, start_timestamp="20180601_112429", end_timestamp="20180601_131234")
2018-09-13 15:37:04 +03:00
'test1fs_full_20180601_112429--20180601_131234.tar.gz'
2018-09-03 19:11:17 +03:00
"""
2018-09-13 15:37:04 +03:00
return "{fileset}_{type}_{start}--{end}.tar.gz".format(
2018-09-12 11:08:22 +03:00
fileset=fileset,
type=Naming.backup_type_str(is_full),
2018-09-12 11:40:33 +03:00
start=start_timestamp, end=end_timestamp)
2018-09-03 19:11:17 +03:00
@staticmethod
def parse_filename(filename):
"""
2018-09-13 15:37:04 +03:00
>>> Naming.parse_filename('test1fs_full_20180601_112429.tar.gz')
2018-09-12 11:40:33 +03:00
('test1fs', True, '20180601_112429')
2018-09-13 15:37:04 +03:00
>>> Naming.parse_filename('test1fs_tran_20180601_112429.tar.gz')
2018-09-12 11:40:33 +03:00
('test1fs', False, '20180601_112429')
2018-09-12 11:08:22 +03:00
>>> Naming.parse_filename('bad_input') == None
True
2018-09-03 19:11:17 +03:00
"""
2018-09-13 15:37:04 +03:00
m = re.search(r'(?P<fileset>\S+?)_(?P<type>full|tran)_(?P<start>\d{8}_\d{6})\.tar.gz', filename)
2018-09-12 11:08:22 +03:00
if m is None:
2018-09-03 19:11:17 +03:00
return None
2018-09-12 11:40:33 +03:00
(fileset, is_full, start_timestamp) = (m.group('fileset'), Naming.type_str_is_full(m.group('type')), m.group('start'))
2018-09-03 19:11:17 +03:00
2018-09-12 11:40:33 +03:00
return fileset, is_full, start_timestamp
2018-09-03 19:11:17 +03:00
@staticmethod
def parse_blobname(filename):
"""
2018-09-13 15:37:04 +03:00
>>> Naming.parse_blobname('test1fs_full_20180601_112429--20180601_131234.tar.gz')
2018-09-12 11:40:33 +03:00
('test1fs', True, '20180601_112429', '20180601_131234')
2018-09-13 15:37:04 +03:00
>>> Naming.parse_blobname('test1fs_tran_20180601_112429--20180601_131234.tar.gz')
2018-09-12 11:40:33 +03:00
('test1fs', False, '20180601_112429', '20180601_131234')
2018-09-12 11:08:22 +03:00
>>> Naming.parse_filename('bad_input') == None
True
2018-09-03 19:11:17 +03:00
"""
2018-09-13 15:37:04 +03:00
m = re.search(r'(?P<fileset>\S+?)_(?P<type>full|tran)_(?P<start>\d{8}_\d{6})--(?P<end>\d{8}_\d{6})\.tar.gz', filename)
2018-09-12 11:08:22 +03:00
if m is None:
2018-09-03 19:11:17 +03:00
return None
2018-09-12 11:40:33 +03:00
(fileset, is_full, start_timestamp, end_timestamp) = (m.group('fileset'), Naming.type_str_is_full(m.group('type')), m.group('start'), m.group('end'))
2018-09-03 19:11:17 +03:00
2018-09-12 11:40:33 +03:00
return fileset, is_full, start_timestamp, end_timestamp
2018-09-03 19:11:17 +03:00
@staticmethod
def blobname_to_filename(blobname):
2018-09-12 11:08:22 +03:00
"""Convert blob name to file name."""
2018-09-03 19:11:17 +03:00
parts = Naming.parse_blobname(blobname)
return Naming.construct_filename(
2018-09-12 11:08:22 +03:00
fileset=parts[0],
is_full=parts[1],
2018-09-12 11:40:33 +03:00
start_timestamp=parts[2])
2018-09-12 14:39:00 +03:00
@staticmethod
def temp_container_name(fileset, start_timestamp):
"""Temporary container name to upload the blob to."""
return "tmp-{fileset}-{start_timestamp}".format(
fileset=fileset,
start_timestamp=start_timestamp
).replace("_", "-").lower()