-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_tar_file.py
81 lines (62 loc) · 2.62 KB
/
extract_tar_file.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
#!/usr/bin/python -tt
#########################################################################################################
# Developer: Rajarshi Pain
# Version: 1.0
# Date:07/07/2016
# Description: This will read and extract specific file from a TAR file from a list of components
#
#########################################################################################################
############################################### MODULES #################################################
import tarfile
import datetime
import sys
import re
import os
############################################### VARIABLES ###############################################
input_file = 'components.txt'
tar_file_name = ''
failed_files = ''
current_path=os.getcwd()
############################################### METHODS #################################################
def take_input():
global tar_file_name
tar_file_name = raw_input('Enter the source TAR file name: ')
tar_file_name = tar_file_name.strip()
while not tar_file_name:
print '##########\nInvalid input\n##########'
tar_file_name = raw_input('Enter the source TAR file name: ')
tar_file_name = tar_file_name.strip()
def extracrTAR():
global tar_file_name, failed_files
print 'Extracting files from ',tar_file_name
try:
inputfile = open(input_file)
tar_file = tarfile.open(tar_file_name)
tar_list = tar_file.getmembers()
for component in inputfile:
is_extracted = 'false'
component = component.strip()
component = component.strip('\n')
if component == '' or len(component) == 0: continue
print 'Extracting ', component
for item in tar_list:
item1 = '"'+str(item)+'"'
match = re.search(component, item1)
if match:
tar_file.extract(item)
is_extracted = 'true'
break
if is_extracted == 'false':
failed_files = failed_files + '\nFailed to extract ' + component
except Exception, e:
print 'ERROR: ', e.message
############################################### MAIN METHOD #############################################
def main():
take_input()
start_time=datetime.datetime.now().replace(microsecond=0)
extracrTAR()
end_time = datetime.datetime.now().replace(microsecond=0)
print failed_files
print '\nTime taken for this operation :', (end_time-start_time)
if __name__=='__main__':
main()