azfilebackup/azfilebak/naming.py

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

2018-09-03 19:11:17 +03:00
# coding=utf-8
2018-10-25 13:14:10 +03:00
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
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)
2018-11-21 13:40:26 +03:00
'incr'
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
return ({True:"full", False:"incr"})[is_full]
2018-09-03 19:11:17 +03:00
@staticmethod
def type_str_is_full(type_str):
"""
>>> Naming.type_str_is_full('full')
True
2018-11-21 13:40:26 +03:00
>>> Naming.type_str_is_full('incr')
2018-09-03 19:11:17 +03:00
False
"""
2018-11-21 13:40:26 +03:00
return ({"full":True, "incr":False})[type_str]
2018-09-03 19:11:17 +03:00
@staticmethod
2018-11-21 13:40:26 +03:00
def construct_filename(fileset, is_full, start_timestamp, vmname):
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
>>> Naming.construct_filename(fileset="test1fs", is_full=True, start_timestamp="20180601_112429", vmname="vm1")
'test1fs_vm1_full_20180601_112429.tar.gz'
>>> Naming.construct_filename(fileset="test1fs", is_full=False, start_timestamp="20180601_112429", vmname="vm1")
'test1fs_vm1_incr_20180601_112429.tar.gz'
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
return "{fileset}_{vmname}_{type}_{start_timestamp}.tar.gz".format(
2018-09-12 11:08:22 +03:00
fileset=fileset,
2018-11-21 13:40:26 +03:00
vmname=vmname,
2018-09-12 11:08:22 +03:00
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-11-21 13:40:26 +03:00
def local_filesystem_name(directory, fileset, is_full, start_timestamp, vmname):
2018-09-12 11:08:22 +03:00
"""
Construct file name with directory.
2018-11-21 13:40:26 +03:00
>>> Naming.local_filesystem_name(directory="/tmp", fileset="test1fs", is_full=True, start_timestamp="20180601_112429", vmname="vm1")
'/tmp/test1fs_vm1_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-11-21 13:40:26 +03:00
fileset, is_full, start_timestamp, vmname)
2018-09-03 19:11:17 +03:00
return os.path.join(directory, file_name)
@staticmethod
2018-11-21 13:40:26 +03:00
def construct_blobname_prefix(fileset, is_full, vmname):
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
>>> Naming.construct_blobname_prefix(fileset="test1fs", is_full=True, vmname="vm1")
'test1fs_vm1_full_'
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
return "{fileset}_{vmname}_{type}_".format(fileset=fileset, vmname=vmname, type=Naming.backup_type_str(is_full))
2018-09-03 19:11:17 +03:00
@staticmethod
2018-11-21 13:40:26 +03:00
def construct_blobname(fileset, is_full, start_timestamp, vmname):
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
>>> Naming.construct_blobname(fileset="test1fs", is_full=True, start_timestamp="20180601_112429", vmname="vm1")
'test1fs_vm1_full_20180601_112429.tar.gz'
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
return "{fileset}_{vmname}_{type}_{start}.tar.gz".format(
2018-09-12 11:08:22 +03:00
fileset=fileset,
2018-11-21 13:40:26 +03:00
vmname=vmname,
2018-09-12 11:08:22 +03:00
type=Naming.backup_type_str(is_full),
2018-09-20 13:30:03 +03:00
start=start_timestamp)
2018-09-03 19:11:17 +03:00
@staticmethod
def parse_filename(filename):
"""
2018-11-21 13:40:26 +03:00
>>> Naming.parse_filename('test1fs_vm1_full_20180601_112429.tar.gz')
('test1fs', True, '20180601_112429', 'vm1')
>>> Naming.parse_filename('test1fs_vm1_incr_20180601_112429.tar.gz')
('test1fs', False, '20180601_112429', 'vm1')
2018-09-12 11:08:22 +03:00
>>> Naming.parse_filename('bad_input') == None
True
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
match = re.search(r'(?P<fileset>\S+?)_(?P<vmname>\S+?)_(?P<type>full|incr)_(?P<start>\d{8}_\d{6})\.tar.gz', filename)
if match is None:
2018-09-03 19:11:17 +03:00
return None
2018-11-21 13:40:26 +03:00
(fileset, is_full, start_timestamp, vmname) = (
match.group('fileset'),
Naming.type_str_is_full(match.group('type')),
match.group('start'),
match.group('vmname'))
2018-09-03 19:11:17 +03:00
2018-11-21 13:40:26 +03:00
return fileset, is_full, start_timestamp, vmname
2018-09-03 19:11:17 +03:00
@staticmethod
def parse_blobname(filename):
"""
2018-11-21 13:40:26 +03:00
>>> Naming.parse_blobname('test1fs_vm1_full_20180601_112429.tar.gz')
('test1fs', True, '20180601_112429', 'vm1')
>>> Naming.parse_blobname('test1fs_vm1_incr_20180601_112429.tar.gz')
('test1fs', False, '20180601_112429', 'vm1')
2018-09-12 11:08:22 +03:00
>>> Naming.parse_filename('bad_input') == None
True
2018-09-03 19:11:17 +03:00
"""
2018-11-21 13:40:26 +03:00
match = re.search(r'(?P<fileset>\S+?)_(?P<vmname>\S+?)_(?P<type>full|incr)_(?P<start>\d{8}_\d{6})\.tar.gz', filename)
if match is None:
2018-09-03 19:11:17 +03:00
return None
2018-11-21 13:40:26 +03:00
(fileset, is_full, start_timestamp, vmname) = (
match.group('fileset'),
Naming.type_str_is_full(match.group('type')),
match.group('start'),
match.group('vmname'))
2018-09-03 19:11:17 +03:00
2018-11-21 13:40:26 +03:00
return fileset, is_full, start_timestamp, vmname
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-11-21 13:40:26 +03:00
start_timestamp=parts[2],
vmname=parts[3])
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()