-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimport.py
More file actions
executable file
·124 lines (91 loc) · 3.04 KB
/
import.py
File metadata and controls
executable file
·124 lines (91 loc) · 3.04 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
119
120
121
122
123
124
#!/usr/bin/python
# Import, a program to archive single message files, by Zoe Blade
# For Python 3
# See README.md for more information
import csv, glob, hashlib, mesg, os, string, sys
configFile = '~/.arcmesgrc'
messageDir = '~/message-archive'
deleteDuplicates = False
verbosity = 'Normal'
if not os.path.exists(os.path.expanduser(configFile)):
print('Please create configuration file', configFile)
exit()
# Let's begin!
config = open(os.path.expanduser(configFile))
for line in csv.reader(config, delimiter='\t'):
if len(line) == 0 or line[0][0] == '#':
continue
command = line[0]
if command == 'DocumentRoot':
if len(line) != 2:
continue
messageDir = line[1]
# Pop all the input filenames into one easy-to-manage list (array)
inputFilenames = []
for argument in sys.argv:
if argument[-3:] == '.py':
continue
if argument == '--delete-duplicates':
deleteDuplicates = True
continue
if argument == '--terse':
verbosity = 'Terse'
continue
if argument == '--verbose':
verbosity = 'Verbose'
continue
filenames = glob.glob(argument)
for filename in filenames:
inputFilenames.append(filename)
# Try to import each file in turn
for inputFilename in inputFilenames:
file = open(inputFilename, 'r', 1, 'iso-8859-1')
message = file.readlines()
messageID = mesg.getMessageID(message)
if verbosity == 'Verbose':
print('File: ' + inputFilename)
print('Message ID: ' + messageID)
if verbosity == 'Terse':
sys.stdout.flush() # Really, this should be at the end of the loop, but I don't want to duplicate it before each continue
if mesg.getArchivable(message) == False:
if verbosity == 'Terse':
sys.stdout.write('X')
else:
print('Deleting ' + inputFilename + '; not archivable (x-no-archive: yes)')
os.remove(inputFilename)
continue
if messageID:
hash = mesg.hashMessageID(messageID)
else:
hash = mesg.hashMessage(message)
if verbosity == 'Verbose':
print('Hash: ' + hash)
if mesg.messageAlreadyArchived(messageDir, hash):
if verbosity == 'Terse':
sys.stdout.write('D') # Duplicate
else:
if deleteDuplicates == True:
print('Deleting ' + inputFilename + '; already in collection')
else:
print('Skipping ' + inputFilename + '; already in collection')
if deleteDuplicates == True:
os.remove(inputFilename)
continue
# This duplicates some of mesg.py, which is bad practice
hashDir = hash[:2]
hashSubdir = hash[2:4]
hashFile = hash[4:]
messageFilename = os.path.expanduser(messageDir+'/'+hashDir+'/'+hashSubdir+'/'+hashFile)
if not os.path.exists(os.path.expanduser(messageDir)):
os.mkdir(os.path.expanduser(messageDir), 0o755)
if not os.path.exists(os.path.expanduser(messageDir+'/'+hashDir)):
os.mkdir(os.path.expanduser(messageDir+'/'+hashDir), 0o755)
if not os.path.exists(os.path.expanduser(messageDir+'/'+hashDir+'/'+hashSubdir)):
os.mkdir(os.path.expanduser(messageDir+'/'+hashDir+'/'+hashSubdir), 0o755)
os.rename(inputFilename, messageFilename)
if verbosity == 'Terse':
sys.stdout.write('.')
else:
print('Imported ' + inputFilename)
if verbosity == 'Terse':
print()