-
Notifications
You must be signed in to change notification settings - Fork 0
/
seattleinstaller.py
2315 lines (1863 loc) · 86.4 KB
/
seattleinstaller.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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
<Program Name>
seattleinstaller.py
<Started>
February 10, 2009
Amended June 11, 2009
Amended July 30, 2009
Amended September 9, 2009
<Author>
Carter Butaud
Amended by Zachary Boka
<Purpose>
Installs seattle on any supported system. This means setting up the computer
to run seattle at startup, generating node keys, customizing configuration
files, and starting seattle running (not necessarily in that order, except
that seattle must always be started running last).
"""
# Let's make sure the version of python is supported.
import checkpythonversion
checkpythonversion.ensure_python_version_is_supported()
import os
import shutil
import platform
import sys
import getopt
import tempfile
import time
import getpass
# Python should do this by default, but doesn't on Windows CE.
sys.path.append(os.getcwd())
import servicelogger
import nonportable
import createnodekeys
import repy_constants
import persist # Armon: Need to modify the NM config file
import benchmark_resources
# Anthony - traceback is imported so that benchmarking can be logged
# before the vessel state has been created (servicelogger does not work
# without the v2 directory).
import traceback
SILENT_MODE = False
KEYBITSIZE = 1024
DISABLE_STARTUP_SCRIPT = False
OS = nonportable.ostype
SUPPORTED_OSES = ["Windows", "WindowsCE", "Linux", "Darwin"]
# Supported Windows Versions: XP, Vista, 7
# NOTE:
# To support newer versions of Windows (or when changing the Python version
# included with the Windows installer package), ammend the function
# get_filepath_of_win_startup_folder_with_link_to_seattle() below.
RESOURCE_PERCENTAGE = 10
# Armon: DISABLE_INSTALL: Special flag for testing purposes that can be
# accessed from the command-line argument "--onlynetwork". All
# pre-install actions are performed, but the actual install is disabled.
DISABLE_INSTALL = False
# Specify the directory containing all seattle files.
SEATTLE_FILES_DIR = os.path.realpath(".")
# Import subprocess if not in WindowsCE
subprocess = None
if OS != "WindowsCE":
import subprocess
# Import windows_api if in Windows or WindowsCE
windows_api = None
if OS == "WindowsCE":
import windows_api
# Import _winreg if in Windows or WindowsCE
_winreg = None
if OS == "Windows" or OS == "WindowsCE":
import _winreg
IS_ANDROID = False
class CronAccessibilityFilesPermissionDeniedError(Exception):
pass
class CronAccessibilityFilesNotFoundError(Exception):
pass
class CannotDetermineCronStatusError(Exception):
pass
class DetectUserError(Exception):
pass
class UnsupportedOSError(Exception):
pass
class AlreadyInstalledError(Exception):
pass
def _output(text):
"""
For internal use.
If the program is not in silent mode, prints the input text.
"""
if not SILENT_MODE:
print text
def find_substring_in_a_file_line(search_absolute_filepath,substring):
"""
<Purpose>
Determine if the given substring exists in at least one line in the given
file by opening a file object for the file in search_absolute_filepath.
<Arguments>
search_absolute_filepath:
The absolute file path to the file that will be searched for the given
substring.
substring:
The substring that will be searched for in file named by
search_absolute_filepath.
<Exceptions>
IOError if the supplied file path does not exist.
<Side Effects>
None.
<Return>
True if the substring is found in at least one line in the file specified
by search_absolute_filepath,
False otherwise.
"""
file_obj = open(search_absolute_filepath,"r")
for line in file_obj:
if substring in line:
file_obj.close()
return True
file_obj.close
return False
def preprocess_file(absolute_filepath, substitute_dict, comment="#"):
"""
<Purpose>
Looks through the given file and makes all substitutions indicated in lines
the do not begin with a comment.
<Arguments>
absolute_filepath:
The absolute path to the file that should be preprocessed.
substitute_dict:
Map of words to be substituted to their replacements, e.g.,
{"word1_in_file": "replacement1", "word2_in_file": "replacement2"}
comment:
A string which indicates commented lines; lines that start with this will
be ignored, but lines that contain this symbol somewhere else in the line
will be preprocessed up to the first instance of the symbol. Defaults to
"#". To preprocess all lines in a file, set as the empty string.
<Exceptions>
IOError on bad file names.
<Side Effects>
None.
<Returns>
None.
"""
edited_lines = []
base_fileobj = open(absolute_filepath, "r")
for fileline in base_fileobj:
commentedOutString = ""
if comment == "" or not fileline.startswith(comment):
# Substitute the replacement string into the file line.
# First, test whether there is an in-line comment.
if comment != "" and comment in fileline:
splitLine = fileline.split(comment,1)
fileline = splitLine[0]
commentedOutString = comment + splitLine[1]
for substitute in substitute_dict:
fileline = fileline.replace(substitute, substitute_dict[substitute])
edited_lines.append(fileline + commentedOutString)
base_fileobj.close()
# Now, write those modified lines to the actual starter file location.
final_fileobj = open(absolute_filepath, "w")
final_fileobj.writelines(edited_lines)
final_fileobj.close()
def get_filepath_of_win_startup_folder_with_link_to_seattle():
"""
<Purpose>
Gets what the full filepath would be to a link to the seattle starter script
in the Windows startup folder. Also tests whether or not that filepath
exists (i.e., whether or not there is currently a link in the startup folder
to run seattle at boot).
<Arguments>
None.
<Exceptions>
UnsupportedOSException if the operating system is not Windows or WindowsCE.
IOError may be thrown if an error occurs while accessing a file.
<Side Effects>
None.
<Returns>
A tuple is returned with the first value being the filepath to the link in
the startup folder that will run seattle at boot. The second value is a
boolean value: True indicates the link currently exists in the startup
folder, and False if it does not.
"""
if OS == "WindowsCE":
startup_path = "\\Windows\\Startup" + os.sep \
+ get_starter_shortucut_file_name()
return (startup_path, os.path.exists(startup_path))
elif OS != "Windows":
raise UnsupportedOSError("The startup folder only exists on Windows.")
# The startup_path is the same for Vista and Windows 7.
#
# As discussed per ticket #1059, different Python versions return
# different names for Windows 7 (see also http://bugs.python.org/issue7863).
# Testing on Windows 7 Professional, 64 bits, German localization,
# platform.release() returns
# "Vista" for Python versions 2.5.2 and 2.5.4,
# "post2008Server" for versions 2.6.2 to 2.6.5, and
# "7" for versions 2.6.6 and 2.7.0 to 2.7.3.
# Please adapt this once new Python/Windows versions become available.
release = platform.release()
if release == "Vista" or release == "post2008Server" or release == "7" or release == "8":
startup_path = os.environ.get("HOMEDRIVE") + os.environ.get("HOMEPATH") \
+ "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs" \
+ "\\Startup" + os.sep + get_starter_shortcut_file_name()
return (startup_path, os.path.exists(startup_path))
elif release == "XP":
startup_path = os.environ.get("HOMEDRIVE") + os.environ.get("HOMEPATH") \
+ "\\Start Menu\\Programs\\Startup" + os.sep \
+ get_starter_shortcut_file_name()
return (startup_path, os.path.exists(startup_path))
else:
raise UnsupportedOSError("""
Sorry, we couldn't detect your Windows version.
Please contact the Seattle development team at
to resolve this issue. Version details:
Python version: """ + str(platform.python_version()) +
"\nPlatform arch: " + str(platform.architecture()) +
"\nPlatform release: " + str(platform.release()) +
"\nPlatform version string: " + str(platform.version()))
def get_starter_file_name():
"""
<Purpose>
Returns the name of the starter file on the current operating system.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the operating system requested is not supported.
<Side Effects>
None.
<Returns>
A string containing the name of the starter file.
"""
if OS == "Windows":
return "start_seattle.bat"
elif OS == "WindowsCE":
return "start_seattle.py"
elif OS == "Linux" or OS == "Darwin":
return "start_seattle.sh"
else:
raise UnsupportedOSError("This operating system is not supported. " \
+ "Currently, only the following operating " \
+ "systems are supported: " + SUPPORTED_OSES)
def get_starter_shortcut_file_name():
"""
<Purpose>
Returns the name of the starter shortcut file on the current operating
system.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the operating system requested is not supported.
<Side Effects>
None.
<Returns>
A string containing the name of the starter shortcut file.
"""
if OS == "Windows":
return "start_seattle_shortcut.bat"
else:
raise UnsupportedOSError("Only the Windows installer contains a shortcut " \
+ "for the seattle starter batch file.")
def get_stopper_file_name():
"""
<Purpose>
Returns the name of the stopper file on the current operating system.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the operating system requested is not supported.
<Side Effects>
None.
<Returns>
A string containing the name of the stopper file. Returns an empty string
if the supported operating system does not contain a stopper file.
"""
if OS == "Windows":
return "stop_seattle.bat"
elif OS == "WindowsCE":
return ""
elif OS == "Linux" or OS == "Darwin":
return "stop_seattle.sh"
else:
raise UnsupportedOSError("This operating system is not supported. " \
+ "Currently, only the following operating " \
+ "systems are supported: " + SUPPORTED_OSES)
def get_uninstaller_file_name():
"""
<Purpose>
Returns the name of the uninstaller file on the current operating
system.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the operating system requested is not supported.
<Side Effects>
None.
<Returns>
The name of the uninstaller file.
"""
if OS == "Windows":
return "uninstall.bat"
elif OS == "WindowsCE":
return "uninstall.py"
elif OS == "Linux" or OS == "Darwin":
return "uninstall.sh"
else:
raise UnsupportedOSError("This operating system is not supported. " \
+ "Currently, only the following operating " \
+ "systems are supported: " + SUPPORTED_OSES)
def search_value_in_win_registry_key(opened_key,seeking_value_name):
"""
<Purpose>
Searches a given key to see if a given value exists for that key.
<Arguments>
opened_key:
An already opened key that will be searched for the given value. For a
key to be opened, it must have had either the _winreg.OpenKey(...) or
_winreg.CreateKey(...) function performed on it.
seeking_value_name:
A string containing the name of the value to search for within the
opened_key.
<Exceptions>
UnsupportedOSError if the operating system is not Windows or WindowsCE.
WindowsError if opened_key has not yet been opened.
<Side Effects>
None.
<Returns>
True if seeking_value_name is found within opened_key.
False otherwise.
"""
if OS != "Windows" and OS != "WindowsCE":
raise UnsupportedOSError("This operating system must be Windows or " \
+ "WindowsCE in order to manipulate registry " \
+ "keys.")
# Test to make sure that opened_key was actually opened by obtaining
# information about that key.
# Raises a WindowsError if opened_key has not been opened.
# subkeycount: the number of subkeys opened_key contains. (not used).
# valuescount: the number of values opened_key has.
# modification_info: long integer stating when the key was last modified.
# (not used)
subkeycount, valuescount, modification_info = _winreg.QueryInfoKey(opened_key)
if valuescount == 0:
return False
try:
value_data,value_type = _winreg.QueryValueEx(opened_key,seeking_value_name)
# No exception was raised, so seeking_value_name was found.
return True
except WindowsError:
return False
def remove_seattle_from_win_startup_folder():
"""
<Purpose>
Removes the seattle startup script from the Windows startup folder if it
exists.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the os is not supported (i.e., a Windows machine).
IOError may be raised if an error occurs during file and filepath
manipulation.
<Side Effects>
Removes the seattle startup script from the Windows startup folder if it
exists.
<Returns>
True if the function removed the link to the startup script, meaning it
previously existed.
False otherwise, meaning that a link to the startup script did not
previously exist.
"""
if OS != "Windows" and OS != "WindowsCE":
raise UnsupportedOSError("This must be a Windows operating system to " \
+ "access the startup folder.")
# Getting the startup path in order to see if a link to seattle has been
# installed there.
full_startup_file_path,file_path_exists = \
get_filepath_of_win_startup_folder_with_link_to_seattle()
if file_path_exists:
os.remove(full_startup_file_path)
return True
else:
return False
def add_seattle_to_win_startup_folder():
"""
<Purpose>
Add the seattle startup script to the Windows startup folder.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the os is not supported (i.e., a Windows machine).
IOError may be raised if an error occurs during file and filepath
manipulation.
<Side Effects>
Adds the seattle startup script to the Windows startup folder.
<Returns>
None.
"""
if OS != "Windows" and OS != "WindowsCE":
raise UnsupportedOSError("This must be a Windows operating system to " \
+ "access the startup folder.")
# Getting the startup path in order to copy the startup file there which will
# make seattle start when the user logs in.
full_startup_file_path,file_path_exists = \
get_filepath_of_win_startup_folder_with_link_to_seattle()
if file_path_exists:
raise AlreadyInstalledError("seattle was already installed in the " \
+ "startup folder.")
else:
shutil.copy(SEATTLE_FILES_DIR + os.sep + get_starter_shortcut_file_name(),
full_startup_file_path)
def add_to_win_registry_Local_Machine_key():
"""
<Purpose>
Adds seattle to the Windows registry key Local_Machine which runs programs
at machine startup (regardless of which user logs in).
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the os is not supported (i.e., a Windows machine).
AlreadyInstalledError if seattle has already been installed on the system.
<Side Effects>
Adds a value named "seattle", which contains the absolute file path to the
seattle starter script, to the startup registry key.
<Returns>
True if succeeded in adding seattle to the registry,
False otherwise.
"""
if OS != "Windows" and OS != "WindowsCE":
raise UnsupportedOSError("This machine must be running Windows in order " \
+ "to access the Windows registry.")
# The following entire try: block attempts to add seattle to the Windows
# registry to run seattle at machine startup regardless of user login.
try:
# The startup key must first be opened before any operations, including
# searching its values, may be performed on it.
# ARGUMENTS:
# _winreg.HKEY_LOCAL_MACHINE: specifies the key containing the subkey used
# to run programs at machine startup
# (independent of user login).
# "Software\\Microsoft\\Windows\\CurrentVersion\\Run": specifies the subkey
# that runs programs on
# machine startup.
# 0: a reserved integer that must be zero.
# _winreg.KEY_ALL_ACCESS: an integer that acts as an access map that
# describes desired security access for this key.
# In this case, we want all access to the key so it
# can be modified. (Default: _winreg.KEY_READ)
startup_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
0, _winreg.KEY_ALL_ACCESS)
except WindowsError:
return False
else:
# The key was successfully opened. Now check to see if seattle was
# previously installed in this key. *Note that the key should be closed in
# this else: block when it is no longer needed.
if search_value_in_win_registry_key(startup_key, "seattle"):
# Close the key before raising AlreadyInstalledError.
_winreg.CloseKey(startup_key)
raise AlreadyInstalledError("seattle is already installed in the " \
+ "Windows registry startup key.")
try:
# seattle has not been detected in the registry from a previous
# installation, so attempting to add the value now.
# _winreg.SetValueEx(...) creates the value "seattle", if it does not
# already exist, and simultaneously adds the given
# data to the value.
# ARGUMENTS:
# startup_key: the opened subkey that runs programs on startup.
# "seattle": the name of the new value to be created under startup_key
# that will make seattle run at machine startup.
# 0: A reserved value that can be anything, though zero is always passed
# to the API according to python documentation for this function.
# _winreg.REG_SZ: Specifies the integer constant REG_SZ which indicates
# that the type of the data to be stored in the value is a
# null-terminated string.
# SEATTLE_FILES_DIR + os.sep + get_starter_file_name(): The data of the
# new value being created
# containing the full path
# to seattle's startup
# script.
_winreg.SetValueEx(startup_key, "seattle", 0, _winreg.REG_SZ,
SEATTLE_FILES_DIR + os.sep + get_starter_file_name())
servicelogger.log(" seattle was successfully added to the Windows " \
+ "registry key to run at startup: " \
+ "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows" \
+ "\\CurrentVersion\\Run")
# Close the key before returning.
_winreg.CloseKey(startup_key)
return True
except WindowsError:
# Close the key before falling through the try: block.
_winreg.CloseKey(startup_key)
return False
def add_to_win_registry_Current_User_key():
"""
<Purpose>
Sets up seattle to run at user login on this Windows machine.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the os is not supported (i.e., a Windows machine).
AlreadyInstalledError if seattle has already been installed on the system.
<Side Effects>
Adds a value named "seattle", which contains the absolute file path to the
seattle starter script, to the user login registry key.
<Returns>
True if succeeded in adding seattle to the registry,
False otherwise.
"""
if OS != "Windows" and OS != "WindowsCE":
raise UnsupportedOSError("This machine must be running Windows in order " \
+ "to access the Windows registry.")
# The following entire try: block attempts to add seattle to the Windows
# registry to run seattle at user login.
try:
# The startup key must first be opened before any operations, including
# searching its values, may be performed on it.
# ARGUMENTS:
# _winreg.HKEY_CURRENT_MACHINE: specifies the key containing the subkey used
# to run programs at user login.
# "Software\\Microsoft\\Windows\\CurrentVersion\\Run": specifies the subkey
# that runs programs on
# user login.
# 0: a reserved integer that must be zero.
# _winreg.KEY_ALL_ACCESS: an integer that acts as an access map that
# describes desired security access for this key.
# In this case, we want all access to the key so it
# can be modified. (Default: _winreg.KEY_READ)
startup_key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
0, _winreg.KEY_ALL_ACCESS)
except WindowsError:
return False
else:
# The key was successfully opened. Now check to see if seattle was
# previously installed in this key. *Note that the key should be closed in
# this else: block when it is no longer needed.
if search_value_in_win_registry_key(startup_key, "seattle"):
# Close the key before raising AlreadyInstalledError.
_winreg.CloseKey(startup_key)
raise AlreadyInstalledError("seattle is already installed in the " \
+ "Windows registry startup key.")
try:
# seattle has not been detected in the registry from a previous
# installation, so attempting to add the value now.
# _winreg.SetValueEx(...) creates the value "seattle", if it does not
# already exist, and simultaneously adds the given
# data to the value.
# ARGUMENTS:
# startup_key: the opened subkey that runs programs on user login.
# "seattle": the name of the new value to be created under startup_key
# that will make seattle run at user login.
# 0: A reserved value that can be anything, though zero is always passed
# to the API according to python documentation for this function.
# _winreg.REG_SZ: Specifies the integer constant REG_SZ which indicates
# that the type of the data to be stored in the value is a
# null-terminated string.
# SEATTLE_FILES_DIR + os.sep + get_starter_file_name(): The data of the
# new value being created
# containing the full path
# to seattle's startup
# script.
_winreg.SetValueEx(startup_key, "seattle", 0, _winreg.REG_SZ,
SEATTLE_FILES_DIR + os.sep + get_starter_file_name())
servicelogger.log(" seattle was successfully added to the Windows " \
+ "registry key to run at user login: " \
+ "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows" \
+ "\\CurrentVersion\\Run")
# Close the key before returning.
_winreg.CloseKey(startup_key)
return True
except WindowsError:
# Close the key before falling through the try: block.
_winreg.CloseKey(startup_key)
return False
def setup_win_startup():
"""
<Purpose>
Sets up seattle to run at startup on this Windows machine. First, this means
adding a value, with absolute file path to the seattle starter script, to
the machine startup and user login registry keys (HKEY_LOCAL_MACHINE and
HKEY_CURRENT_USER) which will run seattle at startup regardless of which
user logs in and when the current user logs in (in the case where a machine
is not shut down between users logging in and out). Second, if that fails,
this method attempts to add a link to the Windows startup folder which will
only run seattle when this user logs in.
<Arguments>
None.
<Exceptions>
UnsupportedOSError if the os is not supported (i.e., a Windows machine).
AlreadyInstalledError if seattle has already been installed on the system.
IOError may be raised if an error occurs during file and filepath
manipulation in one of the sub-functions called by this method.
<Side Effects>
Adds a value named "seattle", which contains the absolute file path to the
seattle starter script, to the startup registry key, or adds seattle to the
startup folder if adding to the registry key fails.
If an entry is successfully made to the registry key and a pre-existing link
to seattle exists in the startup folder, the entry in the startup folder is
removed.
<Returns>
None.
"""
# Check to make sure the OS is supported
if OS != "Windows" and OS != "WindowsCE":
raise UnsupportedOSError("This operating system must be Windows or " \
+ "WindowsCE in order to modify a registry " \
+ "or startup folder.")
try:
added_to_CU_key = add_to_win_registry_Current_User_key()
added_to_LM_key = add_to_win_registry_Local_Machine_key()
except Exception,e:
# Fall through try: block to setup seattle in the startup folder.
_output("seattle could not be installed in the Windows registry for the " \
+ "following reason: " + str(e))
servicelogger.log(" seattle was NOT setup in the Windows registry " \
+ "for the following reason: " + str(e))
else:
if added_to_CU_key or added_to_CU_key:
# Succeeded in adding seattle to the registry key, so now remove seattle
# from the startup folder if there is currently a link there from a
# previous installation.
if remove_seattle_from_win_startup_folder():
_output("seattle was detected in the startup folder.")
_output("Now that seattle has been successfully added to the " \
+ "Windows registry key, the link to run seattle has been " \
+ "deleted from the startup folder.")
servicelogger.log(" A link to the seattle starter file from a " \
+ "previous installation was removed from the " \
+ "startup folder during the current installation.")
# Since seattle was successfully installed in the registry, the job of
# this function is finished.
return
else:
_output("This user does not have permission to access the user registry.")
# Reaching this point means modifying the registry key failed, so add seattle
# to the startup folder.
_output("Attempting to add seattle to the startup folder as an " \
+ "alternative method for running seattle at startup.")
add_seattle_to_win_startup_folder()
servicelogger.log(" A link to the seattle starter script was installed in " \
+ "the Windows startup folder rather than in the " \
+ "registry.")
def test_cron_is_running():
"""
<Purpose>
Try to find out if cron is installed and running on this system. This is not
a straight-forward process because many operating systems install cron in
different locations. Further, not all the current known locations of
where cron may be installed will allow for the status (whether or not cron
is actually running) to be checked. As a result, the most general method of
trying to find if cron is running is performed first (grep the list of
current processes looking for cron), then if that fails, the following list
of possible cron file locations where the cron status can be checked are
searched. If all else fails, a CannotDetermineCronStatusError is raised.
Current list of possible cron locations where the status of cron can be
checked:
DEBIAN AND UBUNTU: /etc/init.d/cron
DEBIAN AND UBUNTU: /etc/init.d/crond
FREEBSD and others?: /etc/rc.d/cron
unknown: /etc/rc.d/init.d/cron
<Arguments>
None.
<Exceptions>
UnsupportedOSError if this is not a Linux or Mac box.
CannotDetermineCronStatusError if cron is installed but it cannot be
determined whether or not it is running.
<Side Effects>
None.
<Return>
True if cron is running on this machine,
False otherwise.
"""
if not OS == "Linux" and not OS == "Darwin":
raise UnsupportedOSError("This must be a Linux or Macintosh machine to " \
+ "test if cron is running.")
# First, try the most general way of seeing if cron is running.
# Due to the pipes in the subprocess.Popen command, it makes more sense to
# send the command as one string rather than breaking up the cammand into
# three separate subprocess.Popen processes and piping the output of one into
# the input of the next.
grep_cron_stdout,grep_cron_stderr = \
subprocess.Popen("ps -ef | grep cron | grep -v grep",shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
if "cron" in grep_cron_stdout:
return (True,None)
else:
grep_crond_stdout,grep_crond_stderr = \
subprocess.Popen("ps -ef | grep crond | grep -v grep",shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
if "crond" in grep_crond_stdout:
return (True,None)
# Reaching this point means cron was not detected using the most general
# method. Cron may still be running or installed, but may not be easily
# accessible. For example, FreeBSD seems to have some trouble running the
# command "ps -ef | grep cron | grep -v grep".
# Try to get the status of cron if possible.
# Depending on the system and distribution, the cron file that allows the cron
# status to be checked could appear in a variety of places.
cron_file_paths_list = ["/etc/init.d/cron","/etc/init.d/crond",
"/etc/rc.d/init.d/cron","/etc/rc.d/cron"]
cron_status_path = None
for possible_cron_path in cron_file_paths_list:
# Test if possible_cron_path exists and is executable.
if os.access(possible_cron_path,os.X_OK):
cron_status_path = possible_cron_path
break
if cron_status_path == None:
# Not able to detect cron on this machine. Because the cron_file_paths_list
# may be incomplete, this function cannot return false but must instead
# raise a CannotDetermineCronStatusError.
raise CannotDetermineCronStatusError("Cannot determine if cron is " \
+ "installed, and thus cannot " \
+ "test if cron is running.")
else:
# Try to get the status of cron from the found cron_status_path.
try:
cron_status_stdout,cron_status_stderr = \
subprocess.Popen([cron_status_path,"status"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
except Exception:
raise CannotDetermineCronStatusError("User cannot access the cron " \
+ "status.")
else:
# Because there will be various outputs depending on the OS and whether
# or not cron is installed, many conditions are needed to attempt to
# capture the cron status.
if not cron_status_stdout and not cron_status_stderr:
# If there is no output, then the status cannot be determined.
raise CannotDetermineCronStatusError("No output produced from the " \
+ "cron status command.")
elif "not running" in cron_status_stdout:
# If "not running" appears in the stdout output, return False.
return (False,cron_status_path)
elif cron_status_stdout and not cron_status_stderr:
# After those tests, if there is stdout output and no stderr output,
# return True to indicate that cron is running.
return (True,cron_status_path)
else:
# For any other unpredicted conditions, raise a
# CannotDetermineCronStatusError
raise CannotDetermineCronStatusError("The output produced by the " \
+ "cron status command could " \
+ "not be interpreted.")
def test_cron_accessibility():
"""
<Purpose>
Find out if the user has access to use cron by examining the allow and deny
files for cron. Depending on the operating system, these files may be
located in various locations. Below is a list of probable locations
depending on the system (this list may not be complete).
DEBIAN: /etc/cron.allow
SuSE: /var/spool/cron/allow
MAC: /usr/lib/cron/cron.allow
UNIX?: /etc/cron.d/cron.allow
BSD: /var/cron/allow
Because there are so many options, this function searches for any of these
files on the system. If any of them exist, then we have found the location
of the accessibility files on this machine. If this function cannot find
any of these files, then we assume the accessibility files do not exist on
this machine, meaning that the user may or may not have access to cron
depending on the system on its specifications with cron (this cannot be
determined by us, so a CronAccessibilityFilesNotFoundError is raised).
<Arguments>
None.
<Exceptions>
UnsupportedOSError if this not a Linux or Mac box.
CronAccessibilityFilesNotFoundError if the allow or deny files cannot be
found on this system.
DectectUserError if cron accessibility files are found but the user name
cannot be determined.
CronAccessibilityFilesPermissionDeniedError when the cron accessibility
files are found but the user does not have permission to read them.
<Side Effects>
None.
<Return>
True if the user has access to use cron,
False otherwise.
"""