aboutsummaryrefslogtreecommitdiffstats
path: root/utils.py
blob: 0143cdf3f2c671e86ee993c6272781e1f51bf23b (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# SPDX-License-Identifier: GPL-2.0-only
'''
Created on Oct 16, 2018

__author__ = "Tracy Graydon"
__copyright__ = "Copyright 2018 - 2019, Intel Corp."
__credits__ = ["Tracy Graydon"]
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = "Tracy Graydon"
__email__ = "tracy.graydon@intel.com"
'''

import os
import os.path
import pwd
import socket
import sys
import hashlib
import glob

def where_am_i():
    hosts = ["ubuntu2004-ty-1", "ubuntu2004-ty-2", "ubuntu1804-ty-1", "ubuntu1804-ty-2", "ubuntu1804-ty-3", "ubuntu1604-ty-1",
		"centos7-ty-1", "centos7-ty-2", "centos7-ty-3", "centos7-ty-4", "centos8-ty-1", "centos8-ty-2",
		"fedora29-ty-1", "fedora30-ty-1", "fedora30-ty-2",
		"debian8-ty-1", "debian9-ty-2", "debian10-ty-1", "debian10-ty-2", "debian10-ty-3",
		"tumbleweed-ty-1", "tumbleweed-ty-2", "tumbleweed-ty-3", "opensuse151-ty-1", "opensuse150-ty-1",
		"ubuntu1804-ty-1", "ubuntu1804-ty-2", "ubuntu1804-ty-3",
		"perf-ubuntu1604", "perf-centos7",
		"ubuntu1804-arm-1"]
    abhost = socket.getfqdn()
    if "yocto.io" in abhost or abhost in hosts:
        VHOSTS = "/srv/autobuilder"
    elif "autobuilder.yoctoproject.org" in abhost:
        VHOSTS = "/srv/www/vhosts"
    else:
        print("I don't recognize this host, so defaulting to /srv/www/vhosts.")
        # Uncomment this if you want to use /srv/autobuilder as the VHOSTS dir. It's useful for testing.
        #VHOSTS = "/srv/autobuilder"
        VHOSTS = "/srv/www/vhosts"
        print("Setting VHOSTS to %s" %VHOSTS)
    AB_HOME = os.path.join(VHOSTS, "autobuilder.yoctoproject.org/pub") # uninative release (uninative.py) uses this
    AB_BASE = os.path.join(AB_HOME, "releases") # all RC candidates live here
    DL_HOME = os.path.join(VHOSTS, "downloads.yoctoproject.org/releases") # uninative release (uninative.py) uses this
    DL_BASE = os.path.join(DL_HOME, "yocto") # all other releases use this
    path_dict = {'VHOSTS': VHOSTS, 'AB_HOME': AB_HOME, 'AB_BASE': AB_BASE, 'DL_HOME': DL_HOME, 'DL_BASE': DL_BASE}
    return path_dict

def who_am_i():
    I_AM = pwd.getpwnam(os.getlogin())
    return I_AM

def signature():
   me = who_am_i()
   pwd_email = me[4]
   # There can be at least two formats for the email portion:
   # Case 1: FULL NAME <foo@somewhere.com>
   # or
   # Case 2: foo@somewhere.com
   # Account for both. Add new formats if we discover more.
   # Case 1
   email_chunks = split_thing(pwd_email, " ")
   if len(email_chunks) == 1:
       email = pwd_email
   elif len(email_chunks) > 1:
       # Case 2
       email = email_chunks[-1].replace("<", "").replace(">", "")
   #print "Email: %s" %email
   chunks = split_thing(email, "@")
   chunks = chunks[0]
   name_chunks = split_thing(chunks, ".")
   firstname = name_chunks[0]
   lastname = name_chunks.pop()
   full_name = " ".join([firstname.capitalize(), lastname.capitalize()])
   return [full_name, email]

def get_list(dirname):
    dirlist = os.listdir(dirname)
    dirlist.sort()
    return dirlist

def split_thing(thing, marker):
    filebits = thing.split(marker)
    return filebits

def rejoin_thing(thing, marker):
    filebits = marker.join(thing)
    return filebits

def sanity_check(source, target):
    if not os.path.exists(source):
       print
       print("SOURCE dir %s does NOT EXIST." %source)
       print
       sys.exit()
    if not os.listdir(source):
       print
       print("SOURCE dir %s is EMPTY" %source)
       print
    if os.path.exists(target):
       print
       print("The TARGET directory %s already exists! " %target)
       print
       sys.exit()
    return

def sync_it(source, target):
    print("Syncing %s to %s" %(source, target))
    sanity_check(source, target)
    source = source + "/"
    os.system("rsync -avrl '%s' '%s'" %(source, target))
    print
    return

def check_rc(rc_source):
    if not os.path.isdir(rc_source):
        print("I cannot find %s. Please check your RC name." %rc_source)
        print("Please use -h or --help for options.")
        found = "False"
    else:
        print("Found RC dir %s." %rc_source)
        found = "True"
    return found

def get_sha256sum(path, blocksize = 4096):
    f = open(path, 'rb')
    sha256sum = hashlib.sha256()
    buffer = f.read(blocksize)
    while len(buffer) > 0:
        sha256sum.update(buffer)
        buffer = f.read(blocksize)
    f.close()
    return sha256sum.hexdigest()

def gen_sha256sum(dirname):
    print
    print("Generating sha256sums for files in %s...." %dirname)
    for root, dirs, files in os.walk(dirname, topdown=True):
        for name in files:
            filename = (os.path.join(root, name))
            if not os.path.islink(filename):
                sha256sum = get_sha256sum(filename)
                sha256_file = ".".join([filename, 'sha256sum'])
                sha256str = sha256sum + " " + name
                print(sha256str)
                f = open(sha256_file, 'w')
                f.write(sha256str)
                f.close()
    return

def gen_rel_sha256(dirname, sha256_file):
    os.chdir(dirname)
    print("Generating master sha256sum file %s" %sha256_file)
    f = open(sha256_file, 'w')
    for root, dirs, files in os.walk(dirname, topdown=True):
        for name in files:
            filename = (os.path.join(root, name))
            ext = split_thing(name, ".")[-1]
            if not (ext == "md5sum" or ext == "sha256sum" or ext == "txt"):
                relpath = split_thing(filename, dirname)
                relpath.pop(0)
                relpath = relpath[0]
                relpath = split_thing(relpath, "/")
                relpath.pop(0)
                relpath = rejoin_thing(relpath, "/")
                relpath = "./" + relpath
                print(relpath)
                sha256sum = get_sha256sum(filename)
                print(sha256sum)
                sha256str = sha256sum + " " + relpath
                print(sha256str)
                f.write(sha256str + '\n')
    f.close()
    return

def get_hashes(rc_name):
    from rel_type import release_type 
    
    PATH_DICT = where_am_i()
    AB_BASE = PATH_DICT['AB_BASE']

    VARS = release_type(rc_name)
    RELEASE = VARS['RELEASE']
    RC_DIR = VARS['RC_DIR']
    RC_SOURCE = os.path.join(AB_BASE, RC_DIR)
    RELEASE_DIR = os.path.join(AB_BASE, RELEASE)

    HOME = os.getcwd()
    HASH_FILE = ".".join(["HASHES", RELEASE])
    outpath = os.path.join(HOME, HASH_FILE)

    os.chdir(RC_SOURCE)
    files = glob.glob('*.bz2')
    filelist = list(filter(lambda f: os.path.isfile(f), files))
    filelist.sort()
    outfile = open(outpath, 'w')
    for item in filelist:
        chunks = split_thing(item, ".")
        new_chunk = split_thing(chunks[0], '-')
        hash = new_chunk.pop()
        RELEASE_NAME = rejoin_thing(new_chunk, "-")
        outfile.write("%s: %s\n" %(RELEASE_NAME, hash))
    outfile.close()
    print("Hashes written to %s\n" %HASH_FILE)
    return HASH_FILE