Conditions | 13 |
Total Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like console_script() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # Administration script functionality on the production system |
||
56 | def console_script(): |
||
57 | ''' |
||
58 | The main entry point for the production |
||
59 | administration script 'opensubmit-exec', |
||
60 | installed by setuptools. |
||
61 | ''' |
||
62 | if len(sys.argv) == 1: |
||
63 | print("opensubmit-exec [configcreate <server_url>|configtest|run|test <dir>|unlock|help] [-c config_file]") |
||
64 | return 0 |
||
65 | |||
66 | if "help" in sys.argv[1]: |
||
67 | print("configcreate <server_url>: Create initial config file for the OpenSubmit executor.") |
||
68 | print("configtest: Check config file for correct installation of the OpenSubmit executor.") |
||
69 | print("run: Fetch and run code to be tested from the OpenSubmit web server. Suitable for crontab.") |
||
70 | print("test <dir>: Run test script from a local folder for testing purposes.") |
||
71 | print("unlock: Break the script lock, because of crashed script.") |
||
72 | print("help: Print this help") |
||
73 | print( |
||
74 | "-c config_file Configuration file to be used (default: {0})".format(CONFIG_FILE_DEFAULT)) |
||
75 | return 0 |
||
76 | |||
77 | # Translate legacy commands |
||
78 | if sys.argv[1] == "configure": |
||
79 | sys.argv[1] = 'configtest' |
||
80 | |||
81 | config_fname = get_config_fname(sys.argv) |
||
82 | |||
83 | if "configcreate" in sys.argv[1]: |
||
84 | print("Creating config file at " + config_fname) |
||
85 | |||
86 | server_url = sys.argv[2] |
||
87 | |||
88 | if create_config(config_fname, override_url=server_url): |
||
89 | print("Config file created, fetching jobs from " + server_url) |
||
90 | return 0 |
||
91 | else: |
||
92 | return 1 |
||
93 | |||
94 | if "configtest" in sys.argv[1]: |
||
95 | print("Testing config file at " + config_fname) |
||
96 | |||
97 | if has_config(config_fname): |
||
98 | config = read_config(config_fname) |
||
99 | if not check_config(config): |
||
100 | return 1 |
||
101 | else: |
||
102 | print("ERROR: Seems like the config file %s does not exist. Call 'opensubmit-exec configcreate <server_url>' first." % |
||
103 | config_fname) |
||
104 | return 1 |
||
105 | |||
106 | print("Sending host information update to server ...") |
||
107 | send_hostinfo(config) |
||
108 | return 0 |
||
109 | |||
110 | if "unlock" in sys.argv[1]: |
||
111 | config = read_config(config_fname) |
||
112 | break_lock(config) |
||
113 | return 0 |
||
114 | |||
115 | if "run" in sys.argv[1]: |
||
116 | config = read_config(config_fname) |
||
117 | # Perform additional precautions for unattended mode in cron |
||
118 | kill_longrunning(config) |
||
119 | with ScriptLock(config): |
||
120 | download_and_run(config) |
||
121 | return 0 |
||
122 | |||
123 | if "test" in sys.argv[1]: |
||
124 | config = read_config(config_fname) |
||
125 | copy_and_run(config, sys.argv[2]) |
||
126 | return 0 |
||
127 | |||
131 |