forked from SeattleTestbed/softwareupdater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writemetainfo.py
182 lines (121 loc) · 4.44 KB
/
writemetainfo.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
"""
Author: Justin Cappos
Start Date: August 8, 2008
Description:
Writes the metainfo files used by the software updater.
Usage: ./writemetainfo.py
"""
# Armon: we use NTP, which uses some repy calls
# This way they have the functions they need
from repyportability import *
_context=locals()
add_dy_support(_context)
import sys
import os
import os.path
# Used to import repy scripts without polluting the current directory.
import shutil
import tempfile
repycachedir = tempfile.mkdtemp()
sys.path = [repycachedir] + sys.path
dy_import_module_symbols("rsa.r2py")
dy_import_module_symbols("sha.r2py")
dy_import_module_symbols("signeddata.r2py")
dy_import_module_symbols("time.r2py")
shutil.rmtree(repycachedir)
# Armon: The port that should be used to update our time using NTP
TIME_PORT = 51345
def get_file_hash(filename):
fileobj = file(filename, 'rb')
filedata = fileobj.read()
fileobj.close()
return sha_hexhash(filedata)
def generate_file_list(path):
"""
<Purpose>
The purpose of this function is to recursively generate
a list of files that are important and will be added to
the metainfo file.
"""
full_file_list = []
for root, dirname, file_list in os.walk(path):
for cur_file in file_list:
# Note that we ignore the first two characters
# of the filename because it is './'.
filename = os.path.join(root, cur_file)[2:]
# ignore pyc files...
if filename.endswith('.pyc'):
continue
# ignore swp files...
if filename.endswith('.swp'):
continue
# ignore repy preprocessed files...
if filename.endswith('_repy.py'):
continue
# ignore directories... -Brent
if os.path.isdir(filename):
continue
# ignore the metainfo file
if filename.endswith('metainfo'):
continue
full_file_list.append(filename)
return full_file_list
def get_previous_entries():
if not os.path.exists('metainfo'):
return {}
returned_dict = {}
for line in file('metainfo'):
if line.strip() == '':
continue
# comment, ignore!
if line[0] == '#':
continue
# signature, ignore
if line[0] == '!':
continue
if len(line.split()) != 3:
raise Exception, "malformed line: '"+line.strip()+"'"
# under the file name, store a hash, size tuple
returned_dict[line.split()[0]] = (line.split()[1], int(line.split()[2]))
return returned_dict
def create_metainfo_file(privatekeyfilename, publickeyfilename, new=False):
previous_entries = get_previous_entries()
outstring = ''
updatedlist = []
# Generate a list of all the files from the current directory.
filename_list = generate_file_list('.')
# Go through all the files and add the files name and hash
# to the meta file.
for filename in filename_list:
filehash = get_file_hash(filename)
filesize = os.path.getsize(filename)
if filename not in previous_entries:
if not new:
print "Warning: '"+filename+"' not in previous metainfo file!"
elif (filehash != previous_entries[filename][0] and filesize == previous_entries[filename][1]) or (filehash == previous_entries[filename][0] and filesize != previous_entries[filename][1]):
print "Warning, '"+filename+"' has only a hash or file size change but not both (how odd)."
elif (filehash != previous_entries[filename][0] and filesize != previous_entries[filename][1]):
# it was updated. We'll display output to this effect later.
updatedlist.append(filename)
outstring = outstring + filename+" "+filehash+" "+str(filesize)+"\n"
# Okay, great. We should have it all ready now. Let's sign our data
# and report what we're doing
if not new:
print "Writing a metafile with updates to:"+ ' '.join(updatedlist)
# Armon: Update our time via NTP
time_updatetime(TIME_PORT)
# timestamp now, expire in 30 days...
outsigneddata = signeddata_signdata(outstring, rsa_file_to_privatekey(privatekeyfilename), rsa_file_to_publickey(publickeyfilename), time_gettime(), time_gettime()+60*60*24*30)
outfo = file("metainfo","w")
outfo.write(outsigneddata)
outfo.close()
def main():
if len(sys.argv) < 3:
print "usage: writemetainfo.py privkeyfile publickeyfile [-n]"
sys.exit(1)
elif len(sys.argv) > 3 and sys.argv[3] == "-n":
create_metainfo_file(sys.argv[1], sys.argv[2], True)
else:
create_metainfo_file(sys.argv[1], sys.argv[2])
if __name__ == '__main__':
main()