forked from sleuthkit/autopsy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_versions.py
executable file
·971 lines (897 loc) · 35.3 KB
/
update_versions.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
#
# Autopsy Forensic Browser
#
# Copyright 2012-2013 Basis Technology Corp.
# Contact: carrier <at> sleuthkit <dot> org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#######################
# This script exists to help us determine update the library
# versions appropriately. See this page for version details.
#
# http://wiki.sleuthkit.org/index.php?title=Autopsy_3_Module_Versions
#
# The basic idea is that this script uses javadoc/jdiff to
# compare the current state of the source code to the last
# tag and identifies if APIs were removed, added, etc.
#
# When run from the Autopsy build script, this script will:
# - Clone Autopsy and checkout to the previous release tag
# as found in the NEWS.txt file
# - Auto-discover all modules and packages
# - Run jdiff, comparing the current and previous modules
# - Use jdiff's output to determine if each module
# a) has no changes
# b) has backwards compatible changes
# c) has backwards incompatible changes
# - Based off it's compatibility, updates each module's
# a) Major version
# b) Specification version
# c) Implementation version
# - Updates the dependencies on each module depending on the
# updated version numbers
#
# Optionally, when run from the command line, one can provide the
# desired tag to compare the current version to, the directory for
# the current version of Autopsy, and whether to automatically
# update the version numbers and dependencies.
# ------------------------------------------------------------
import errno
import os
import shutil
import stat
import subprocess
import sys
import traceback
from os import remove, close
from shutil import move
from tempfile import mkstemp
from xml.dom.minidom import parse, parseString
# Jdiff return codes. Described in more detail further on
NO_CHANGES = 100
COMPATIBLE = 101
NON_COMPATIBLE = 102
ERROR = 1
# Set this to true when developing the script. It does not delete
# the cloned repo each time - making it much faster to run.
TESTING = False
# An Autopsy module object
class Module:
# Initialize it with a name, return code, and version numbers
def __init__(self, name=None, ret=None, versions=None):
self.name = name
self.ret = ret
self.versions = versions
# As a string, the module should be it's name
def __str__(self):
return self.name
def __repr__(self):
return self.name
# When compared to another module, the two are equal if the names are the same
def __cmp__(self, other):
if isinstance(other, Module):
if self.name == other.name:
return 0
elif self.name < other.name:
return -1
else:
return 1
return 1
def __eq__(self, other):
if isinstance(other, Module):
if self.name == other.name:
return True
return False
def set_name(self, name):
self.name = name
def set_ret(self, ret):
self.ret = ret
def set_versions(self, versions):
self.versions = versions
def spec(self):
return self.versions[0]
def impl(self):
return self.versions[1]
def release(self):
return self.versions[2]
# Representation of the Specification version number
class Spec:
# Initialize specification number, where num is a string like x.y
def __init__(self, num):
self.third = None
spec_nums = num.split(".")
if len(spec_nums) == 3:
self.final = spec_nums[2]
self.third = int(self.final)
l, r = spec_nums[0], spec_nums[1]
self.left = int(l)
self.right = int(r)
def __str__(self):
return self.get()
def __cmp__(self, other):
if isinstance(other, Spec):
if self.left == other.left:
if self.right == other.right:
return 0
if self.right < other.right:
return -1
return 1
if self.left < other.left:
return -1
return 1
elif isinstance(other, str):
l, r = other.split(".")
if self.left == int(l):
if self.right == int(r):
return 0
if self.right < int(r):
return -1
return 1
if self.left < int(l):
return -1
return 1
return -1
def incrementIncompat(self):
return str(self.left + 1) + ".0"
def incrementCompat(self):
return str(self.left) + "." + str(self.right + 1)
def get(self):
spec_str = str(self.left) + "." + str(self.right)
if self.third is not None:
spec_str += "." + str(self.final)
return spec_str
def set(self, num):
if isinstance(num, str):
l, r = num.split(".")
self.left = int(l)
self.right = int(r)
elif isinstance(num, Spec):
self.left = num.left
self.right = num.right
return self
# ================================ #
# Core Functions #
# ================================ #
# Given a list of modules and the names for each version, compare
# the generated jdiff XML for each module and output the jdiff
# JavaDocs.
#
# modules: the list of all modules both versions have in common
# apiname_tag: the api name of the previous version, most likely the tag
# apiname_cur: the api name of the current version, most likely "Current"
#
# returns the exit code from the modified jdiff.jar
# return code 1 = error in jdiff
# return code 100 = no changes
# return code 101 = compatible changes
# return code 102 = incompatible changes
def compare_xml(module, apiname_tag, apiname_cur):
global docdir
make_dir(docdir)
null_file = fix_path(os.path.abspath("./thirdparty/jdiff/v-custom/lib/Null.java"))
jdiff = fix_path(os.path.abspath("./thirdparty/jdiff/v-custom/jdiff.jar"))
oldapi = fix_path("build/jdiff-xml/" + apiname_tag + "-" + module.name)
newapi = fix_path("build/jdiff-xml/" + apiname_cur + "-" + module.name)
docs = fix_path(docdir + "/" + module.name)
# Comments are strange. They look for a file with additional user comments in a
# directory like docs/user_comments_for_xyz. The problem being that xyz is the
# path to the new/old api. So xyz turns into multiple directories for us.
# i.e. user_comments_for_build/jdiff-xml/[tag name]-[module name]_to_build/jdiff-xml
comments = fix_path(docs + "/user_comments_for_build")
jdiff_com = fix_path(comments + "/jdiff-xml")
tag_comments = fix_path(jdiff_com + "/" + apiname_tag + "-" + module.name + "_to_build")
jdiff_tag_com = fix_path(tag_comments + "/jdiff-xml")
if not os.path.exists(jdiff):
print("JDIFF doesn't exist.")
make_dir(docs)
make_dir(comments)
make_dir(jdiff_com)
make_dir(tag_comments)
make_dir(jdiff_tag_com)
make_dir("jdiff-logs")
log = open("jdiff-logs/COMPARE-" + module.name + ".log", "w")
cmd = ["javadoc",
"-doclet", "jdiff.JDiff",
"-docletpath", jdiff,
"-d", docs,
"-oldapi", oldapi,
"-newapi", newapi,
"-script",
null_file]
try:
jdiff = subprocess.Popen(cmd, stdout=log, stderr=log)
jdiff.wait()
code = jdiff.returncode
except Exception:
printt("Error executing javadoc. Exiting...")
exit(1)
log.close()
print("Compared XML for " + module.name)
if code == NO_CHANGES:
print(" No API changes")
elif code == COMPATIBLE:
print(" API Changes are backwards compatible")
elif code == NON_COMPATIBLE:
print(" API Changes are not backwards compatible")
else:
print(" *Error in XML, most likely an empty module")
sys.stdout.flush()
return code
# Generate the jdiff xml for the given module
# path: path to the autopsy source
# module: Module object
# name: api name for jdiff
def gen_xml(path, modules, name):
for module in modules:
# If its the regression test, the source is in the "test" dir
if module.name == "Testing":
src = os.path.join(path, module.name, "test", "qa-functional", "src")
else:
src = os.path.join(path, module.name, "src")
# xerces = os.path.abspath("./lib/xerces.jar")
xml_out = fix_path(os.path.abspath("./build/jdiff-xml/" + name + "-" + module.name))
jdiff = fix_path(os.path.abspath("./thirdparty/jdiff/v-custom/jdiff.jar"))
make_dir("build/jdiff-xml")
make_dir("jdiff-logs")
log = open("jdiff-logs/GEN_XML-" + name + "-" + module.name + ".log", "w")
cmd = ["javadoc",
"-doclet", "jdiff.JDiff",
"-docletpath", jdiff, # ;" + xerces, <-- previous problems required this
"-apiname", xml_out, # leaving it in just in case it's needed once again
"-sourcepath", fix_path(src)]
cmd = cmd + get_packages(src)
try:
jdiff = subprocess.Popen(cmd, stdout=log, stderr=log)
jdiff.wait()
except Exception:
printt("Error executing javadoc. Exiting...")
exit(1)
log.close()
print("Generated XML for " + name + " " + module.name)
sys.stdout.flush()
# Find all the modules in the given path
def find_modules(path):
modules = []
# Step into each folder in the given path and
# see if it has manifest.mf - if so, it's a module
for dir in os.listdir(path):
directory = os.path.join(path, dir)
if os.path.isdir(directory):
for file in os.listdir(directory):
if file == "manifest.mf":
modules.append(Module(dir, None, None))
return modules
# Detects the differences between the source and tag modules
def module_diff(source_modules, tag_modules):
added_modules = [x for x in source_modules if x not in tag_modules]
removed_modules = [x for x in tag_modules if x not in source_modules]
similar_modules = [x for x in source_modules if x in tag_modules]
added_modules = (added_modules if added_modules else [])
removed_modules = (removed_modules if removed_modules else [])
similar_modules = (similar_modules if similar_modules else [])
return similar_modules, added_modules, removed_modules
# Reads the previous tag from NEWS.txt
def get_tag(sourcepath):
news = open(sourcepath + "/NEWS.txt", "r")
second_instance = False
for line in news:
if "----------------" in line:
if second_instance:
ver = line.split("VERSION ")[1]
ver = ver.split(" -")[0]
return ("autopsy-" + ver).strip()
else:
second_instance = True
continue
news.close()
# ========================================== #
# Dependency Functions #
# ========================================== #
# Write a new XML file, copying all the lines from projectxml
# and replacing the specification version for the code-name-base base
# with the supplied specification version spec
def set_dep_spec(projectxml, base, spec):
print(" Updating Specification version..")
orig = open(projectxml, "r")
f, abs_path = mkstemp()
new_file = open(abs_path, "w")
found_base = False
spacing = " "
sopen = "<specification-version>"
sclose = "</specification-version>\n"
for line in orig:
if base in line:
found_base = True
if found_base and sopen in line:
update = spacing + sopen + str(spec) + sclose
new_file.write(update)
else:
new_file.write(line)
new_file.close()
close(f)
orig.close()
remove(projectxml)
move(abs_path, projectxml)
# Write a new XML file, copying all the lines from projectxml
# and replacing the release version for the code-name-base base
# with the supplied release version
def set_dep_release(projectxml, base, release):
print(" Updating Release version..")
orig = open(projectxml, "r")
f, abs_path = mkstemp()
new_file = open(abs_path, "w")
found_base = False
spacing = " "
ropen = "<release-version>"
rclose = "</release-version>\n"
for line in orig:
if base in line:
found_base = True
if found_base and ropen in line:
update = spacing + ropen + str(release) + rclose
new_file.write(update)
else:
new_file.write(line)
new_file.close()
close(f)
orig.close()
remove(projectxml)
move(abs_path, projectxml)
# Return the dependency versions in the XML dependency node
def get_dep_versions(dep):
run_dependency = dep.getElementsByTagName("run-dependency")[0]
release_version = run_dependency.getElementsByTagName("release-version")
if release_version:
release_version = getTagText(release_version[0].childNodes)
specification_version = run_dependency.getElementsByTagName("specification-version")
if specification_version:
specification_version = getTagText(specification_version[0].childNodes)
return int(release_version), Spec(specification_version)
# Given a code-name-base, see if it corresponds with any of our modules
def get_module_from_base(modules, code_name_base):
for module in modules:
if "org.sleuthkit.autopsy." + module.name.lower() == code_name_base:
return module
return None # If it didn't match one of our modules
# Check the text between two XML tags
def getTagText(nodelist):
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
return node.data
# Check the projectxml for a dependency on any module in modules
def check_for_dependencies(projectxml, modules):
dom = parse(projectxml)
dep_list = dom.getElementsByTagName("dependency")
for dep in dep_list:
code_name_base = dep.getElementsByTagName("code-name-base")[0]
code_name_base = getTagText(code_name_base.childNodes)
module = get_module_from_base(modules, code_name_base)
if module:
print(" Found dependency on " + module.name)
release, spec = get_dep_versions(dep)
if release != module.release() and module.release() is not None:
set_dep_release(projectxml, code_name_base, module.release())
else: print(" Release version is correct")
if spec != module.spec() and module.spec() is not None:
set_dep_spec(projectxml, code_name_base, module.spec())
else: print(" Specification version is correct")
# Given the module and the source directory, return
# the paths to the manifest and project properties files
def get_dependency_file(module, source):
projectxml = os.path.join(source, module.name, "nbproject", "project.xml")
if os.path.isfile(projectxml):
return projectxml
# Verify/Update the dependencies for each module, basing the dependency
# version number off the versions in each module
def update_dependencies(modules, source):
for module in modules:
print("Checking the dependencies for " + module.name + "...")
projectxml = get_dependency_file(module, source)
if projectxml == None:
print(" Error finding project xml file")
else:
other = [x for x in modules]
check_for_dependencies(projectxml, other)
sys.stdout.flush()
# ======================================== #
# Versioning Functions #
# ======================================== #
# Return the specification version in the given project.properties/manifest.mf file
def get_specification(project, manifest):
try:
# Try to find it in the project file
# it will be there if impl version is set to append automatically
f = open(project, 'r')
for line in f:
if "spec.version.base" in line:
return Spec(line.split("=")[1].strip())
f.close()
# If not found there, try the manifest file
f = open(manifest, 'r')
for line in f:
if "OpenIDE-Module-Specification-Version:" in line:
return Spec(line.split(": ")[1].strip())
except Exception as e:
print("Error parsing Specification version for")
print(project)
print(e)
# Set the specification version in the given project properties file
# but if it can't be found there, set it in the manifest file
def set_specification(project, manifest, num):
try:
# First try the project file
f = open(project, 'r')
for line in f:
if "spec.version.base" in line:
f.close()
replace(project, line, "spec.version.base=" + str(num) + "\n")
return
f.close()
# If it's not there, try the manifest file
f = open(manifest, 'r')
for line in f:
if "OpenIDE-Module-Specification-Version:" in line:
f.close()
replace(manifest, line, "OpenIDE-Module-Specification-Version: " + str(num) + "\n")
return
# Otherwise we're out of luck
print(" Error finding the Specification version to update")
print(" " + manifest)
f.close()
except:
print(" Error incrementing Specification version for")
print(" " + project)
# Return the implementation version in the given manifest.mf file
def get_implementation(manifest):
try:
f = open(manifest, 'r')
for line in f:
if "OpenIDE-Module-Implementation-Version" in line:
return int(line.split(": ")[1].strip())
f.close()
except:
print("Error parsing Implementation version for")
print(manifest)
# Set the implementation version in the given manifest file
def set_implementation(manifest, num):
try:
f = open(manifest, 'r')
for line in f:
if "OpenIDE-Module-Implementation-Version" in line:
f.close()
replace(manifest, line, "OpenIDE-Module-Implementation-Version: " + str(num) + "\n")
return
# If it isn't there, add it
f.close()
write_implementation(manifest, num)
except:
print(" Error incrementing Implementation version for")
print(" " + manifest)
# Rewrite the manifest file to include the implementation version
def write_implementation(manifest, num):
f = open(manifest, "r")
contents = f.read()
contents = contents[:-2] + "OpenIDE-Module-Implementation-Version: " + str(num) + "\n\n"
f.close()
f = open(manifest, "w")
f.write(contents)
f.close()
# Return the release version in the given manifest.mf file
def get_release(manifest):
try:
f = open(manifest, 'r')
for line in f:
if "OpenIDE-Module:" in line:
return int(line.split("/")[1].strip())
f.close()
except:
#print("Error parsing Release version for")
#print(manifest)
return 0
# Set the release version in the given manifest file
def set_release(manifest, num):
try:
f = open(manifest, 'r')
for line in f:
if "OpenIDE-Module:" in line:
f.close()
index = line.index('/') - len(line) + 1
newline = line[:index] + str(num)
replace(manifest, line, newline + "\n")
return
print(" Error finding the release version to update")
print(" " + manifest)
f.close()
except:
print(" Error incrementing release version for")
print(" " + manifest)
# Given the module and the source directory, return
# the paths to the manifest and project properties files
def get_version_files(module, source):
manifest = os.path.join(source, module.name, "manifest.mf")
project = os.path.join(source, module.name, "nbproject", "project.properties")
if os.path.isfile(manifest) and os.path.isfile(project):
return manifest, project
# Returns a the current version numbers for the module in source
def get_versions(module, source):
manifest, project = get_version_files(module, source)
if manifest == None or project == None:
print(" Error finding manifeset and project properties files")
return
spec = get_specification(project, manifest)
impl = get_implementation(manifest)
release = get_release(manifest)
return [spec, impl, release]
# Update the version numbers for every module in modules
def update_versions(modules, source):
for module in modules:
versions = module.versions
manifest, project = get_version_files(module, source)
print("Updating " + module.name + "...")
if manifest == None or project == None:
print(" Error finding manifeset and project properties files")
return
if module.ret == COMPATIBLE:
versions = [versions[0].set(versions[0].incrementCompat()), versions[1] + 1, versions[2]]
set_specification(project, manifest, versions[0])
set_implementation(manifest, versions[1])
module.set_versions(versions)
elif module.ret == NON_COMPATIBLE:
versions = [versions[0].set(versions[0].incrementIncompat()), versions[1] + 1, versions[2] + 1]
set_specification(project, manifest, versions[0])
set_implementation(manifest, versions[1])
set_release(manifest, versions[2])
module.set_versions(versions)
elif module.ret == NO_CHANGES:
versions = [versions[0], versions[1] + 1, versions[2]]
set_implementation(manifest, versions[1])
module.set_versions(versions)
elif module.ret == None:
versions = [Spec("1.0"), 1, 1]
set_specification(project, manifest, versions[0])
set_implementation(manifest, versions[1])
set_release(manifest, versions[2])
module.set_versions(versions)
sys.stdout.flush()
# Given a list of the added modules, remove the modules
# which have the correct 'new module default' version number
def remove_correct_added(modules):
correct = [x for x in modules]
for module in modules:
if module.spec() == "1.0" or module.spec() == "0.0":
if module.impl() == 1:
if module.release() == 1 or module.release() == 0:
correct.remove(module)
return correct
# ==================================== #
# Helper Functions #
# ==================================== #
# Replace pattern with subst in given file
def replace(file, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(file)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(file)
#Move new file
move(abs_path, file)
# Given a list of modules print the version numbers that need changing
def print_version_updates(modules):
f = open("gen_version.txt", "a")
for module in modules:
versions = module.versions
if module.ret == COMPATIBLE:
output = (module.name + ":\n")
output += ("\tMajor Release:\tNo Change.\n")
output += ("\tSpecification:\t" + str(versions[0]) + "\t->\t" + str(versions[0].incrementCompat()) + "\n")
if versions[1] is None:
output += ("\tImplementation: Not defined\n")
else:
output += ("\tImplementation:\t" + str(versions[1]) + "\t->\t" + str(versions[1] + 1) + "\n")
output += ("\n")
print(output)
sys.stdout.flush()
f.write(output)
elif module.ret == NON_COMPATIBLE:
output = (module.name + ":\n")
output += ("\tMajor Release:\t" + str(versions[2]) + "\t->\t" + str(versions[2] + 1) + "\n")
output += ("\tSpecification:\t" + str(versions[0]) + "\t->\t" + str(versions[0].incrementIncompat()) + "\n")
if versions[1] is None:
output += ("\tImplementation: Not defined\n")
else:
output += ("\tImplementation:\t" + str(versions[1]) + "\t->\t" + str(versions[1] + 1) + "\n")
output += ("\n")
print(output)
sys.stdout.flush()
f.write(output)
elif module.ret == ERROR:
output = (module.name + ":\n")
output += ("\t*Unable to detect necessary changes\n")
output += ("\tMajor Release:\t\t" + str(versions[2]) + "\n")
output += ("\tSpecification:\t" + str(versions[0]) + "\n")
if versions[1] is None:
output += ("\tImplementation: Not defined\n")
else:
output += ("\tImplementation:\t" + str(versions[1]) + "\n")
output += ("\n")
print(output)
f.write(output)
sys.stdout.flush()
elif module.ret == NO_CHANGES:
output = (module.name + ":\n")
output += ("\tMajor Release:\tNo Change.\n")
output += ("\tSpecification:\tNo Change.\n")
if versions[1] is None:
output += ("\tImplementation: Not defined\n")
else:
output += ("\tImplementation:\t" + str(versions[1]) + "\t->\t" + str(versions[1] + 1) + "\n")
output += ("\n")
print(output)
sys.stdout.flush()
f.write(output)
elif module.ret is None:
output = ("Added " + module.name + ":\n")
if module.release() != 1 and module.release() != 0:
output += ("\tMajor Release:\t\t" + str(module.release()) + "\t->\t" + "1\n")
output += ("\n")
if module.spec() != "1.0" and module.spec() != "0.0":
output += ("\tSpecification:\t" + str(module.spec()) + "\t->\t" + "1.0\n")
output += ("\n")
if module.impl() != 1:
output += ("\tImplementation:\t" + str(module.impl()) + "\t->\t" + "1\n")
output += ("\n")
print(output)
sys.stdout.flush()
f.write(output)
sys.stdout.flush()
f.close()
# Changes cygwin paths to Windows
def fix_path(path):
if "cygdrive" in path:
new_path = path[11:]
return "C:/" + new_path
else:
return path
# Print a 'title'
def printt(title):
print("\n" + title)
lines = ""
for letter in title:
lines += "-"
print(lines)
sys.stdout.flush()
# Get a list of package names in the given path
# The path is expected to be of the form {base}/module/src
#
# NOTE: We currently only check for packages of the form
# org.sleuthkit.autopsy.x
# If we add other namespaces for commercial modules we will
# have to add a check here
def get_packages(path):
packages = []
package_path = os.path.join(path, "org", "sleuthkit", "autopsy")
for folder in os.listdir(package_path):
package_string = "org.sleuthkit.autopsy."
packages.append(package_string + folder)
return packages
# Create the given directory, if it doesn't already exist
def make_dir(dir):
try:
if not os.path.isdir(dir):
os.mkdir(dir)
if os.path.isdir(dir):
return True
return False
except:
print("Exception thrown when creating directory")
return False
# Delete the given directory, and make sure it is deleted
def del_dir(dir):
try:
if os.path.isdir(dir):
shutil.rmtree(dir, ignore_errors=False, onerror=handleRemoveReadonly)
if os.path.isdir(dir):
return False
else:
return True
return True
except:
print("Exception thrown when deleting directory")
traceback.print_exc()
return False
# Handle any permisson errors thrown by shutil.rmtree
def handleRemoveReadonly(func, path, exc):
excvalue = exc[1]
if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
func(path)
else:
raise
# Run git clone and git checkout for the tag
def do_git(tag, tag_dir):
try:
printt("Cloning Autopsy tag " + tag + " into dir " + tag_dir + " (this could take a while)...")
subprocess.call(["git", "clone", "https://github.com/sleuthkit/autopsy.git", tag_dir],
stdout=subprocess.PIPE)
printt("Checking out tag " + tag + "...")
subprocess.call(["git", "checkout", tag],
stdout=subprocess.PIPE,
cwd=tag_dir)
return True
except Exception as ex:
print("Error cloning and checking out Autopsy: ", sys.exc_info()[0])
print(str(ex))
print("The terminal you are using most likely does not recognize git commands.")
return False
# Get the flags from argv
def args():
try:
sys.argv.pop(0)
while sys.argv:
arg = sys.argv.pop(0)
if arg == "-h" or arg == "--help":
return 1
elif arg == "-t" or arg == "--tag":
global tag
tag = sys.argv.pop(0)
elif arg == "-s" or arg == "--source":
global source
source = sys.argv.pop(0)
elif arg == "-d" or arg == "--dir":
global docdir
docdir = sys.argv.pop(0)
elif arg == "-a" or arg == "--auto":
global dry
dry = False
else:
raise Exception()
except:
pass
# Print script run info
def printinfo():
global tag
global source
global docdir
global dry
printt("Release script information:")
if source is None:
source = fix_path(os.path.abspath("."))
print("Using source directory:\n " + source)
if tag is None:
tag = get_tag(source)
print("Checking out to tag:\n " + tag)
if docdir is None:
docdir = fix_path(os.path.abspath("./jdiff-javadocs"))
print("Generating jdiff JavaDocs in:\n " + docdir)
if dry is True:
print("Dry run: will not auto-update version numbers")
sys.stdout.flush()
# Print the script's usage/help
def usage():
return \
"""
USAGE:
Compares the API of the current Autopsy source code with a previous
tagged version. By default, it will detect the previous tag from
the NEWS file and will not update the versions in the source code.
OPTIONAL FLAGS:
-t --tag Specify a previous tag to compare to.
Otherwise the NEWS file will be used.
-d --dir The output directory for the jdiff JavaDocs. If no
directory is given, the default is jdiff-javadocs/{module}.
-s --source The directory containing Autopsy's source code.
-a --auto Automatically update version numbers (not dry).
-h --help Prints this usage.
"""
# ==================================== #
# Main Functionality #
# ==================================== #
# Where the magic happens
def main():
global tag; global source; global docdir; global dry
tag = None; source = None; docdir = None; dry = True
ret = args()
if ret:
print(usage())
return 0
printinfo()
# Check if javadoc and jdiff are present.
jdiff = fix_path(os.path.abspath("./thirdparty/jdiff/v-custom/jdiff.jar"))
if(not os.path.isfile(jdiff)):
printt("jdiff not found. Exiting...")
return 1
try:
subprocess.call(["javadoc"], stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT)
except Exception:
printt("javadoc not found in path. Exiting...")
return 1
# -----------------------------------------------
# 1) Clone Autopsy, checkout to given tag/commit
# 2) Get the modules in the clone and the source
# 3) Generate the xml comparison
# -----------------------------------------------
if (not TESTING) and (not del_dir("./build/" + tag)):
print("\n\n=========================================")
print(" Failed to delete previous Autopsy clone.")
print(" Unable to continue...")
print("=========================================")
return 1
tag_dir = os.path.abspath("./build/" + tag)
if not do_git(tag, tag_dir):
return 1
sys.stdout.flush()
tag_modules = find_modules(tag_dir)
source_modules = find_modules(source)
printt("Generating jdiff XML reports...")
apiname_tag = tag
apiname_cur = "current"
gen_xml(tag_dir, tag_modules, apiname_tag)
gen_xml(source, source_modules, apiname_cur)
if not TESTING:
printt("Deleting cloned Autopsy directory...")
print("Clone successfully deleted" if del_dir(tag_dir) else "Failed to delete clone")
sys.stdout.flush()
# -----------------------------------------------------
# 1) Seperate modules into added, similar, and removed
# 2) Compare XML for each module
# -----------------------------------------------------
printt("Comparing modules found...")
similar_modules, added_modules, removed_modules = module_diff(source_modules, tag_modules)
if added_modules or removed_modules:
for m in added_modules:
print("+ Added " + m.name)
sys.stdout.flush()
for m in removed_modules:
print("- Removed " + m.name)
sys.stdout.flush()
else:
print("No added or removed modules")
sys.stdout.flush()
printt("Comparing jdiff outputs...")
for module in similar_modules:
module.set_ret(compare_xml(module, apiname_tag, apiname_cur))
print("Refer to the jdiff-javadocs folder for more details")
# ------------------------------------------------------------
# 1) Do versioning
# 2) Auto-update version numbers in files and the_modules list
# 3) Auto-update dependencies
# ------------------------------------------------------------
printt("Auto-detecting version numbers and changes...")
for module in added_modules:
module.set_versions(get_versions(module, source))
for module in similar_modules:
module.set_versions(get_versions(module, source))
added_modules = remove_correct_added(added_modules)
the_modules = similar_modules + added_modules
print_version_updates(the_modules)
if not dry:
printt("Auto-updating version numbers...")
update_versions(the_modules, source)
print("All auto-updates complete")
printt("Detecting and auto-updating dependencies...")
update_dependencies(the_modules, source)
printt("Deleting jdiff XML...")
xml_dir = os.path.abspath("./build/jdiff-xml")
print("XML successfully deleted" if del_dir(xml_dir) else "Failed to delete XML")
print("\n--- Script completed successfully ---")
return 0
# Start off the script
if __name__ == "__main__":
sys.exit(main())