summaryrefslogtreecommitdiffstats
path: root/linux_dpdk/cov.py
blob: 27578f4b3dcb56e9043d58740d5fe8292cca4fc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# hhaim 2015
import sys 
import os
import argparse;
import uuid
import urllib2



H_COV_VER = "0.0.1"

class cov_driver(object):
     args=None;


BUILD_NUM_FILE  = "../VERSION" 

COV_FILE_OUT = 'trex-64.bz2'

#'http://www.python.org/'
def check_url_is_valid (url):
    try:
      f = urllib2.urlopen(url)
      f.read()
      return 0
    except :
       return -1


def get_build_num ():
 s='';
 if os.path.isfile(BUILD_NUM_FILE):
     f=open(BUILD_NUM_FILE,'r');
     s+=f.readline().rstrip();
     f.close();
 return s;

def get_build_num_dis ():
     return get_build_num ()+ "-"+str(uuid.uuid1())


def process_options ():
    parser = argparse.ArgumentParser(usage=""" 
    cov -b # build  sa
    cov -u  #upload sa
    """,
   description="sa utility ",
    epilog=" written by hhaim");


    parser.add_argument('-b', action='store_true',
                        help='build ')
    parser.add_argument('-u', action='store_true',
                        help='upload  ')
    parser.add_argument('-nc', action='store_true',
                        help='build without clean  ')

    parser.add_argument('--version', action='version',
                        version=H_COV_VER )

    cov_driver.args = parser.parse_args();




def run_cmd (cmd,is_exception=True):
    print "run cmd '%s'" % (cmd)
    res=os.system(cmd);
    if is_exception and (res !=0):
        s= "ERORR cmd return error !";
        raise Exception(s);
    else:
        print  "OK"


def run_build (is_clean):
    clean_str = ""
    if is_clean :
        clean_str = "clean"
    cov_build_cmd = cov_driver.tool_path+"cov-analysis-linux64-7.7.0.4/bin/cov-build --dir cov-int ./b %s build --target=_t-rex-64" % (clean_str);
    run_cmd(cov_build_cmd);
    if os.path.isfile(COV_FILE_OUT) :
        run_cmd(('rm %s' % COV_FILE_OUT));
    run_cmd("tar caf %s cov-int" % COV_FILE_OUT);

def upload ():
    if not os.path.isfile(COV_FILE_OUT) :
        s="ERROR file %s does not exit try to build it " % (COV_FILE_OUT);
        raise Exception (s)
    if check_url_is_valid ('http://www.google.com/')<0:
        s="ERROR, You are under firewall, try from another build server";
        raise Exception (s)

    ver=get_build_num_dis ()
    cmd='curl --form token=fRIZZCAGD9TnkSiuxXiEAQ --form email='+cov_driver.user_name+'@cisco.com --form file=@./'+COV_FILE_OUT+'  --form version="'+ver+'" --form description="'+ver+'" https://scan.coverity.com/builds?project=cisco-system-traffic-generator%2Ftrex-core'
    run_cmd(cmd);
    print "You should get an email with the results"  
    print "or visit http://scan.coverity.com/projects/cisco-system-traffic-generator-trex-core?tab=overview"  

def check_env (env,err):
    if  os.environ.has_key(env) == False :
        s= "ERROR you should define %s, %s" % (env,err)
        raise Exception(s);


def main_cov ():
    args=cov_driver.args

    # default nothing was given 
    if args.b == False and args.u == False :
        run_build (True)
        upload ()

    if args.b :
        is_clean = not args.nc 
        run_build (is_clean)

    if args.u:
        upload ()


def main ():
    try:
        check_env ('NBAR_ENV',"should defined to the tools path")
        check_env ('USER',"should be defined as your user name")

        cov_driver.tool_path=os.environ['NBAR_ENV']
        cov_driver.user_name=os.environ['USER']
        process_options ()
        main_cov ()
        exit(0);
    except Exception, e:
        print str(e);
        exit(-1);



if __name__ == "__main__":
    main();