Welcome to mirror list, hosted at ThFree Co, Russian Federation.

master_unpack.py « buildbot « build_files - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 157e244e210135a7d69e8c9dc0b25065382f46b6 (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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# Runs on Buildbot master, to unpack incoming unload.zip into latest
# builds directory and remove older builds.

# <pep8 compliant>

import os
import shutil
import sys
import zipfile


# extension stripping
def strip_extension(filename):
    extensions = '.zip', '.tar', '.bz2', '.gz', '.tgz', '.tbz', '.exe'

    for ext in extensions:
        if filename.endswith(ext):
            filename = filename[:-len(ext)]

    return filename


# extract platform from package name
def get_platform(filename):
    # name is blender-version-platform.extension. we want to get the
    # platform out, but there may be some variations, so we fiddle a
    # bit to handle current and hopefully future names
    filename = strip_extension(filename)
    filename = strip_extension(filename)

    tokens = filename.split("-")
    platforms = ('osx', 'mac', 'bsd',
                 'win', 'linux', 'source',
                 'irix', 'solaris')
    platform_tokens = []
    found = False

    for i, token in enumerate(tokens):
        if not found:
            for platform in platforms:
                if platform in token.lower():
                    found = True
                    break

        if found:
            platform_tokens += [token]

    return '-'.join(platform_tokens)


def get_branch(filename):
    if filename.startswith("blender-2.8"):
        return "blender2.8"

    tokens = filename.split("-")
    branch = ""

    for token in tokens:
        if token == "blender":
            return branch

        if branch == "":
            branch = token
        else:
            branch = branch + "-" + token

    return ""

# get filename
if len(sys.argv) < 2:
    sys.stderr.write("Not enough arguments, expecting file to unpack\n")
    sys.exit(1)

filename = sys.argv[1]

# open zip file
if not os.path.exists(filename):
    sys.stderr.write("File %r not found.\n" % filename)
    sys.exit(1)

try:
    z = zipfile.ZipFile(filename, "r")
except Exception as ex:
    sys.stderr.write('Failed to open zip file: %s\n' % str(ex))
    sys.exit(1)

if len(z.namelist()) != 1:
    sys.stderr.write("Expected one file in %r." % filename)
    sys.exit(1)

package = z.namelist()[0]
packagename = os.path.basename(package)

# detect platform and branch
platform = get_platform(packagename)
branch = get_branch(packagename)

if platform == '':
    sys.stderr.write('Failed to detect platform ' +
        'from package: %r\n' % packagename)
    sys.exit(1)

# extract
if not branch or branch == 'master':
    directory = 'public_html/download'
elif branch == 'experimental-build':
    directory = 'public_html/download/experimental'
else:
    directory = 'public_html/download'

try:
    filename = os.path.join(directory, packagename)
    zf = z.open(package)
    f = file(filename, "wb")

    shutil.copyfileobj(zf, f)
    os.chmod(filename, 0644)

    zf.close()
    z.close()
except Exception as ex:
    sys.stderr.write('Failed to unzip package: %s\n' % str(ex))
    sys.exit(1)

# remove other files from the same platform and branch
try:
    for f in os.listdir(directory):
        if get_platform(f) == platform and get_branch(f) == branch:
            if f != packagename:
                os.remove(os.path.join(directory, f))
except Exception as ex:
    sys.stderr.write('Failed to remove old packages: %s\n' % str(ex))
    sys.exit(1)