Passed
Push — master ( ab513a...1713a6 )
by Peter
05:21 queued 03:17
created

package_builder.package_builder()   F

Complexity

Conditions 11

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 29
nop 3
dl 0
loc 37
rs 3.1764
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like package_builder.package_builder() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from SCons.Script import * 
2
from contextlib import contextmanager
3
import os, tarfile
4
5
def package_builder(target, source, env):
6
    '''
7
    	Performs packaging.
8
    '''    
9
    # Remove first directory part of target name,
10
    # so "dist/foo_v1.0" becomes "foo_v1.0"
11
    target_name = str(target[0]).split('/',1)[1]
12
    inclusions = [[str(s), str(s)] for s in source]
13
    exclusion_suffix=[".pyc", ".sqlite", ".orig"]
14
    exclusion_files = []
15
    exclusion_dirs = [".git"]
16
17
    def tarfilter(tarinfo):
18
        fname = tarinfo.name
19
        if tarinfo.isdir():
20
            for dirname in exclusion_dirs:
21
                # if the filter is "/static", we still want to add "/static-release"
22
                if fname.endswith(dirname) or dirname+os.sep in fname:
23
                    print "Skipping directory "+fname
24
                    return None
25
            print "Adding directory "+fname
26
            return tarinfo
27
        elif tarinfo.isfile():
28
            if fname in exclusion_files:
29
                print "Skipping file "+fname
30
                return None
31
            for suffix in exclusion_suffix:
32
                if fname.endswith(suffix):
33
                    print "Skipping file "+fname
34
                    return None
35
            print "Adding file "+fname
36
            return tarinfo
37
38
    tar = tarfile.open(str(target[0])+".tar.gz","w:gz")
39
    for src, dest in inclusions:
40
        tar.add(src, dest, filter=tarfilter)
41
    tar.close()
42
43
packagebuilder = Builder(action=package_builder)
44