Passed
Push — master ( 477ce1...9e8f04 )
by Matěj
01:16 queued 10s
created

data.utils.create_tarball()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
import os
2
import os.path
3
import tarfile
4
5
_DIR = os.path.dirname(__file__)
6
_TAR_BASENAME = 'data.tar'
7
_SSG_PREFIX = 'xccdf_org.ssgproject.content_'
8
9
10
def iterate_over_rules():
11
    """Iterate over directories beginning with "rule_".
12
13
    Returns:
14
    dir_name -- absolute path to the rule directory
15
    rule -- full rule id as it is present in datastream
16
    files -- list of files in the directory
17
    """
18
    for dir_name, directories, files in os.walk(_DIR):
19
        leaf_dir = os.path.basename(dir_name)
20
        rel_dir = '.' + dir_name[len(_DIR):]
21
        if leaf_dir.startswith('rule_'):
22
            yield rel_dir, _SSG_PREFIX + leaf_dir, files
23
24
25
def _exclude_utils(file_name):
26
    if file_name in ['./__init__.py', './utils.py']:
27
        return True
28
    if file_name.endswith('pyc'):
29
        return True
30
    if file_name.endswith('swp'):
31
        return True
32
    return False
33
34
35
def create_tarball(tar_dir):
36
    archive_path = os.path.join(tar_dir, _TAR_BASENAME)
37
    with tarfile.TarFile.open(archive_path, 'w') as tarball:
38
        tarball.add(_DIR, arcname='.', exclude=_exclude_utils)
39
    return archive_path
40