Skip to content

Commit 09d9a1c

Browse files
authored
Merge pull request #61 from SummerGGift/archive_check
【完善】:压缩包有效性检查
2 parents c678be2 + 08b57c9 commit 09d9a1c

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

archive.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,14 @@ def unpack(archive_fn, path, pkg, pkgs_name_in_json):
3333

3434
flag = True
3535

36-
#print(pkg)
37-
3836
if ".tar.bz2" in archive_fn:
3937
arch = tarfile.open(archive_fn, "r:bz2")
4038
for tarinfo in arch:
4139
arch.extract(tarinfo, path)
4240
a = tarinfo.name
4341
if not os.path.isdir(os.path.join(path, a)):
4442
right_path = a.replace('/', '\\')
45-
a = os.path.join(os.path.split(right_path)[
46-
0], os.path.split(right_path)[1])
43+
a = os.path.join(os.path.split(right_path)[0], os.path.split(right_path)[1])
4744
pkgsdb.savetodb(a, archive_fn)
4845
arch.close()
4946

@@ -54,8 +51,7 @@ def unpack(archive_fn, path, pkg, pkgs_name_in_json):
5451
a = tarinfo.name
5552
if not os.path.isdir(os.path.join(path, a)):
5653
right_path = a.replace('/', '\\')
57-
a = os.path.join(os.path.split(right_path)[
58-
0], os.path.split(right_path)[1])
54+
a = os.path.join(os.path.split(right_path)[0], os.path.split(right_path)[1])
5955
pkgsdb.savetodb(a, archive_fn)
6056
arch.close()
6157

@@ -71,19 +67,14 @@ def unpack(archive_fn, path, pkg, pkgs_name_in_json):
7167
change_dirname = pkgs_name_in_json + '-' + pkg['ver']
7268
flag = False
7369

74-
right_name_to_db = right_path.replace(
75-
dir_name, change_dirname, 1)
76-
#print(right_name_to_db)
70+
right_name_to_db = right_path.replace(dir_name, change_dirname, 1)
7771
pkgsdb.savetodb(right_name_to_db, archive_fn, right_path)
7872
arch.close()
7973

80-
#Change the folder name
74+
# Change the folder name
8175

8276
change_dirname = pkgs_name_in_json + '-' + pkg['ver']
83-
#print(os.path.join(path, dir_name))
84-
#print(os.path.join(path, change_dirname))
85-
#print(pkgs_name_in_json)
86-
77+
8778
if os.path.isdir(os.path.join(path, change_dirname)):
8879
cmd = 'rd /s /q ' + os.path.join(path, change_dirname)
8980
os.system(cmd)
@@ -96,28 +87,32 @@ def packtest(path):
9687
if ".zip" in path:
9788
try:
9889
if zipfile.is_zipfile(path):
99-
# test zip again to make sure it's a right zip file.
90+
# Test zip again to make sure it's a right zip file.
10091
arch = zipfile.ZipFile(path, "r")
10192
if arch.testzip():
10293
ret = False
103-
arch.close()
94+
arch.close()
95+
else:
96+
ret = False
97+
print('package check error. \n')
10498
except Exception, e:
105-
print('e.message:%s\t'%e.message)
106-
arch.close()
99+
print('packtest e.message:%s\t' % e.message)
100+
# arch.close()
101+
print("The archive package is broken. \n")
107102
ret = False
108-
103+
109104
if ".tar.bz2" in path:
110105
try:
111106
if not tarfile.is_tarfile(path):
112-
ret = False
107+
ret = False
113108
except Exception, e:
114109
ret = False
115110

116111
if ".tar.gz" in path:
117112
try:
118113
if not tarfile.is_tarfile(path):
119-
ret = False
114+
ret = False
120115
except Exception, e:
121116
ret = False
122-
117+
123118
return ret

cmds/cmd_package.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import subprocess
3333
import time
3434
import logging
35+
import archive
3536
import sys
3637

3738
try:
@@ -334,20 +335,24 @@ def install_pkg(env_root, bsp_root, pkg):
334335
else:
335336
# Download a package of compressed package type.
336337
if not package.download(pkg['ver'], local_pkgs_path, package_url):
337-
ret = False
338-
return ret
338+
return False
339339

340340
pkg_dir = package.get_filename(pkg['ver'])
341341
pkg_dir = os.path.splitext(pkg_dir)[0]
342+
pkg_fullpath = os.path.join(local_pkgs_path, package.get_filename(pkg['ver']))
342343

343-
pkg_fullpath = os.path.join(
344-
local_pkgs_path, package.get_filename(pkg['ver']))
345-
#print("pkg_fullpath: %s"%pkg_fullpath)
346-
344+
if not archive.packtest(pkg_fullpath):
345+
return False
346+
347347
# unpack package
348348
if not os.path.exists(pkg_dir):
349-
package.unpack(pkg_fullpath, bsp_pkgs_path, pkg, pkgs_name_in_json)
350-
ret = True
349+
try:
350+
if not package.unpack(pkg_fullpath, bsp_pkgs_path, pkg, pkgs_name_in_json):
351+
ret = False
352+
except Exception, e:
353+
os.remove(pkg_fullpath)
354+
ret = False
355+
print('e.message: %s\t' % e.message)
351356

352357
return ret
353358

@@ -625,7 +630,7 @@ def error_packages_handle(error_packages_list, read_back_pkgs_json, pkgs_fn):
625630

626631
for pkg in error_packages_list: # Redownloaded the packages in error_packages_list
627632
if install_pkg(env_root, bsp_root, pkg):
628-
print("==============================> %s %s is redownloaded successfully. \n" % (
633+
print("==============================> %s %s is redownloaded successfully. \n" % (
629634
pkg['name'], pkg['ver']))
630635
else:
631636
error_packages_redownload_error_list.append(pkg)
@@ -634,8 +639,8 @@ def error_packages_handle(error_packages_list, read_back_pkgs_json, pkgs_fn):
634639

635640
if len(error_packages_redownload_error_list):
636641
print("%s" % error_packages_redownload_error_list)
637-
print ("Packages:%s,%s redownloed error,you need to use <pkgs --update> command again to redownload them." %
638-
pkg['name'], pkg['ver'])
642+
print ("Packages:%s,%s redownloed error, you need to use <pkgs --update> command again to redownload them." %
643+
(pkg['name'], pkg['ver']))
639644
write_back_pkgs_json = sub_list(
640645
read_back_pkgs_json, error_packages_redownload_error_list)
641646
read_back_pkgs_json = write_back_pkgs_json
@@ -950,12 +955,6 @@ def package_wizard():
950955
f.write(package)
951956
f.close()
952957

953-
s = Template(Sconscript_file)
954-
sconscript = s.substitute(name=name)
955-
f = file(os.path.join(pkg_path, 'SConscript'), 'wb')
956-
f.write(sconscript)
957-
f.close()
958-
959958
print ('\nThe package index was created successfully.')
960959

961960

package.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ def download(self, ver, path, url_from_srv):
223223

224224
def unpack(self, fullpkg_path, path, pkg, pkgs_name_in_json):
225225
try:
226+
# ignore the return value
226227
archive.unpack(fullpkg_path, path, pkg, pkgs_name_in_json)
228+
return True
227229
except Exception, e:
228-
print('e.message:%s\t' % e.message)
230+
print('unpack e.message:%s\t' % e.message)
229231
print('unpack %s failed' % os.path.basename(fullpkg_path))
232+
os.remove(fullpkg_path)
233+
return False

0 commit comments

Comments
 (0)