forked from RT-Thread/env
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.py
More file actions
118 lines (101 loc) · 4.03 KB
/
archive.py
File metadata and controls
118 lines (101 loc) · 4.03 KB
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
# -*- 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
#
import tarfile
import zipfile
import os
import pkgsdb
def unpack(archive_fn, path, pkg, pkgs_name_in_json):
flag = True
if ".tar.bz2" in archive_fn:
arch = tarfile.open(archive_fn, "r:bz2")
for tarinfo in arch:
arch.extract(tarinfo, path)
a = tarinfo.name
if not os.path.isdir(os.path.join(path, a)):
right_path = a.replace('/', '\\')
a = os.path.join(os.path.split(right_path)[0], os.path.split(right_path)[1])
pkgsdb.savetodb(a, archive_fn)
arch.close()
if ".tar.gz" in archive_fn:
arch = tarfile.open(archive_fn, "r:gz")
for tarinfo in arch:
arch.extract(tarinfo, path)
a = tarinfo.name
if not os.path.isdir(os.path.join(path, a)):
right_path = a.replace('/', '\\')
a = os.path.join(os.path.split(right_path)[0], os.path.split(right_path)[1])
pkgsdb.savetodb(a, archive_fn)
arch.close()
if ".zip" in archive_fn:
arch = zipfile.ZipFile(archive_fn, "r")
for item in arch.namelist():
arch.extract(item, path)
if not os.path.isdir(os.path.join(path, item)):
right_path = item.replace('/', '\\')
# Gets the folder name and change_dirname only once
if flag:
dir_name = os.path.split(right_path)[0]
change_dirname = pkgs_name_in_json + '-' + pkg['ver']
flag = False
right_name_to_db = right_path.replace(dir_name, change_dirname, 1)
pkgsdb.savetodb(right_name_to_db, archive_fn, right_path)
arch.close()
# Change the folder name
change_dirname = pkgs_name_in_json + '-' + pkg['ver']
if os.path.isdir(os.path.join(path, change_dirname)):
cmd = 'rd /s /q ' + os.path.join(path, change_dirname)
os.system(cmd)
os.rename(os.path.join(path, dir_name),os.path.join(path, change_dirname))
def packtest(path):
ret = True
if ".zip" in path:
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, e:
print('packtest e.message:%s\t' % e.message)
# arch.close()
print("The archive package is broken. \n")
ret = False
if ".tar.bz2" in path:
try:
if not tarfile.is_tarfile(path):
ret = False
except Exception, e:
ret = False
if ".tar.gz" in path:
try:
if not tarfile.is_tarfile(path):
ret = False
except Exception, e:
ret = False
return ret