forked from RT-Thread/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.py
210 lines (177 loc) · 7.92 KB
/
archive.py
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
206
207
208
209
210
# -*- coding:utf-8 -*-
#
# File : archive.py
# This file is part of RT-Thread RTOS
# COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
#
# 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.
#
# Change Logs:
# Date Author Notes
# 2018-5-28 SummerGift Add copyright information
# 2020-4-10 SummerGift Code clear up
#
import logging
import os
import shutil
import tarfile
import zipfile
import pkgsdb
from cmds.cmd_package.cmd_package_utils import is_windows, remove_folder
from tqdm import tqdm
def unpack(archive_filename, bsp_package_path, package_info, package_name):
if archive_filename.endswith(".zip"):
return handle_zip_package(archive_filename, bsp_package_path, package_name, package_info)
if ".tar." in archive_filename:
return handle_tar_package(archive_filename, bsp_package_path, package_name, package_info)
return True
def handle_tar_package(archive_filename, bsp_package_path, package_name, package_info):
package_version = package_info['ver']
package_temp_path = os.path.join(bsp_package_path, "package_temp")
try:
if remove_folder(package_temp_path):
os.makedirs(package_temp_path)
except Exception as e:
logging.warning('Error message : {0}'.format(e))
logging.info("BSP packages path {0}".format(bsp_package_path))
logging.info("BSP package temp path: {0}".format(package_temp_path))
logging.info("archive filename : {0}".format(archive_filename))
try:
flag = True
package_folder_name = ""
package_name_with_version = ""
arch = tarfile.open(archive_filename, "r")
for item in tqdm(arch.getnames()):
arch.extract(item, package_temp_path)
if not os.path.isdir(os.path.join(package_temp_path, item)):
# Gets the folder name and changed folder name only once
if flag:
package_folder_name = item.split('/')[0]
package_name_with_version = package_name + '-' + package_version
flag = False
if is_windows():
right_path = item.replace('/', '\\')
else:
right_path = item
right_name_to_db = right_path.replace(package_folder_name, package_name_with_version, 1)
right_path = os.path.join("package_temp", right_path)
pkgsdb.save_to_database(right_name_to_db, archive_filename, right_path)
arch.close()
if not move_package_to_bsp_packages(
package_folder_name, package_name, package_temp_path, package_version, bsp_package_path
):
return False
except Exception as e:
logging.warning('unpack error message : {0}'.format(e))
logging.warning('unpack {0} failed'.format(os.path.basename(archive_filename)))
# remove temp folder and archive file
remove_folder(package_temp_path)
os.remove(archive_filename)
return False
return True
def handle_zip_package(archive_filename, bsp_package_path, package_name, package_info):
package_version = package_info['ver']
package_temp_path = os.path.join(bsp_package_path, "package_temp")
try:
if remove_folder(package_temp_path):
os.makedirs(package_temp_path)
except Exception as e:
logging.warning('Error message : {0}'.format(e))
logging.info("BSP packages path {0}".format(bsp_package_path))
logging.info("BSP package temp path: {0}".format(package_temp_path))
logging.info("archive filename : {0}".format(archive_filename))
try:
flag = True
package_folder_name = ""
package_name_with_version = ""
arch = zipfile.ZipFile(archive_filename, "r")
for item in tqdm(arch.namelist()):
arch.extract(item, package_temp_path)
if not os.path.isdir(os.path.join(package_temp_path, item)):
# Gets the folder name and changed folder name only once
if flag:
package_folder_name = item.split('/')[0]
package_name_with_version = package_name + '-' + package_version
flag = False
if is_windows():
right_path = item.replace('/', '\\')
else:
right_path = item
right_name_to_db = right_path.replace(package_folder_name, package_name_with_version, 1)
right_path = os.path.join("package_temp", right_path)
pkgsdb.save_to_database(right_name_to_db, archive_filename, right_path)
arch.close()
if not move_package_to_bsp_packages(
package_folder_name, package_name, package_temp_path, package_version, bsp_package_path
):
return False
except Exception as e:
logging.warning('unpack error message : {0}'.format(e))
logging.warning('unpack {0} failed'.format(os.path.basename(archive_filename)))
# remove temp folder and archive file
remove_folder(package_temp_path)
os.remove(archive_filename)
return False
return True
def move_package_to_bsp_packages(package_folder_name, package_name, package_temp_path, package_version, bsp_packages_path):
"""move package in temp folder to bsp packages folder."""
origin_package_folder_path = os.path.join(package_temp_path, package_folder_name)
package_name_with_version = package_name + '-' + package_version
package_folder_in_temp = os.path.join(package_temp_path, package_name_with_version)
bsp_package_path = os.path.join(bsp_packages_path, package_name_with_version)
logging.info("origin name: {0}".format(origin_package_folder_path))
logging.info("rename name: {0}".format(package_folder_in_temp))
result = True
try:
# rename package folder name to package name with version
os.rename(origin_package_folder_path, package_folder_in_temp)
# if there is no specified version package in the bsp package path,
# then move package from package_folder_in_temp to bsp_package_path
if not os.path.isdir(bsp_package_path):
shutil.move(package_folder_in_temp, bsp_package_path)
except Exception as e:
logging.warning('{0}'.format(e))
result = False
finally:
# must remove temp folder
remove_folder(package_temp_path)
return result
def package_integrity_test(path):
ret = True
if path.endswith(".zip"):
try:
if zipfile.is_zipfile(path):
# Test zip again to make sure it's a right zip file.
arch = zipfile.ZipFile(path, "r")
if arch.testzip():
ret = False
arch.close()
else:
ret = False
print('package check error. \n')
except Exception as e:
print('Package test error message:%s\t' % e)
print("The archive package is broken. \n")
arch.close()
ret = False
# if ".tar.*" in path:.
if path.endswith(".tar.bz2") or path.endswith(".tar.gz") or path.endswith(".tar.xz"):
try:
if not tarfile.is_tarfile(path):
ret = False
except Exception as e:
print('Error message:%s' % e)
ret = False
return ret