feisty meow concerns codebase 2.140
generate_aliases-new.py
Go to the documentation of this file.
1#! /usr/bin/env python3
2
3"""
4
5Name : generate_aliases
6Author : Chris Koeritz
7Rights : Copyright (C) 1996-$now by Author
8
9Purpose:
10
11 This script generates feisty meow script alias files. Alias files
12contain a list of definitions for command aliases that are written in the
13specified shell dialect (such as bash or perl) and which are additionally
14tailored for the operating system to be used.
15
16author: chris koeritz
17
18####
19This program is free software; you can redistribute it and/or modify it
20under the terms of the GNU General Public License as published by the Free
21Software Foundation; either version 2 of the License or (at your option)
22any later version. See: "http://www.gruntose.com/Info/GNU/GPL.html" for a
23version of the License. Please send any updates to "fred@gruntose.com".
24"""
25
26import inspect
27import os
28import re
29import sys
30
31# locate where our scripts live, so we can find local library files.
32THIS_SCRIPT_DIR = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
33FILE_PROCESSING_LIBRARY = THIS_SCRIPT_DIR + '/../files/'
34sys.path.append(FILE_PROCESSING_LIBRARY)
35
36import filename_helper
37
38# returns true if the environment variable to enable debugging noise is set.
40 return len(DEBUG_FEISTY_MEOW) > 0
41
42# given a possible aliasable filename, this will decide whether to create a perl
43# or bash alias for it. it needs the filename of the possible alias and the
44# directory where that file resides.
45def make_alias(file: str, dir: str) -> None:
46 # we'll set the shorter alias name if we find a match for the file extension.
47 aliasname = None
48 # the method we'll call once we figure out what type of alias to build.
49 funky = None
50
51 alias_handling_methods = {'py':'make_python_alias', 'sh':'make_bash_alias', 'pl':'make_perl_alias'}
52
53 for extension, method in alias_handling_methods.items():
54 found = re.search('^.*\.' + extension, file, re.IGNORECASE)
55 if found:
56 aliasname = re.sub('^.*\.' + extension, "", file, re.IGNORECASE)
57 funky = method
58 break
59
60 if aliasname is not None:
61 if is_debugging():
62 print("aliasname is " + aliasname + " and funky is " + funky)
63 # evaluate a function call with the chosen method.
64 return eval(funky+'(' + aliasname + ',' + dir + ')');
65 else:
66 print('could not find a matching extension for the file: ' + file)
67 return None
68
69
70
71# makes an alias for a bash script given the alias name.
72def make_bash_alias(aliasname: str, dir: str) -> str:
73 full_alias = dir + "/" + aliasname
74 if is_debugging():
75 print("bash full alias " + full_alias)
76#huh? aliasname = re.sub(r'^.*/([^/]*)', r'\1')
77#from: $aliasname =~ s/^.*\/([^\/]*)/\1/;
78# print "alias became: " + aliasname
79 return "define_yeti_alias: " + aliasname+ '="bash "' + full_alias + '".sh"';
80# print she "define_yeti_alias $aliasname=\"bash $full_alias.sh\"";
81
82# makes an alias for a python script given the alias name.
83def make_python_alias(aliasname: str, dir: str) -> str:
84 full_alias = dir + "/" + aliasname
85 if is_debugging():
86 print("python full alias: " + full_alias)
87#hmmm: don't love that we're hardcoding python3 in here, but apparently some systems don't have a 'python' command despite having python installed.
88 return "define_yeti_alias " + aliasname+ '="python3 "' + full_alias + '".py"';
89
90# makes an alias for a perl script given the alias name.
91def make_perl_alias(aliasname: str, dir: str) -> str:
92 full_alias = dir + "/" + aliasname
93 if is_debugging():
94 print("perl full alias: " + full_alias)
95 return "define_yeti_alias " + aliasname+ '="perl "' + full_alias + '".py"';
96
97
98
99# The "common.alias" file is used in the generated aliases file as a base
100# set of generally useful aliases. We also add aliases for any script files
101# (perl, bash, python, etc) that we find in the feisty meow script hierarchy.
102# Any *.alias files found in the $FEISTY_MEOW_LOADING_DOCK/custom folder are
103# loaded also.
105
106 if is_debugging():
107 print("rebuilding generated aliases file...")
108
109 # create our generated shells directory if it's not already.
110 if not os.path.isdir(FEISTY_MEOW_LOADING_DOCK):
111 os.mkdirs(FEISTY_MEOW_LOADING_DOCK)
112 if is_debugging():
113 print("made FEISTY_MEOW_LOADING_DOCK at '" + FEISTY_MEOW_LOADING_DOCK + "'")
114
115#hmmm: not sure why this bit was removed from the perl code--maybe it blew up or made noise or didn't work right?
116 # test if we can use color in ls...
117# $test_color=` ls --help 2>&1 | grep -i color `;
118
119 # this is an array of files from which to draw alias definitions.
120 ALIAS_DEFINITION_FILES = [ FEISTY_MEOW_SCRIPTS + "/core/common.alias" ];
121
122 # if custom aliases files exist, add them to the list.
123#hmmm: would be nice to have this name in a symbol somewhere instead of having "custom" or "customize" everywhere.
124 for filename in glob_list(FEISTY_MEOW_LOADING_DOCK + "/custom/*.alias"):
125 if os.path.isfile(filename): ALIAS_DEFINITION_FILES.append(filename)
126 if is_debugging():
127 print("using these alias files:")
128 for filename in ALIAS_DEFINITION_FILES:
129 base_of_dir = os.path.basename(os.path.dirname(filename))
130 basename = os.path.basename(filename)
131 print(" " + base_of_dir + "/" + basename)
132
133 # write the aliases for sh and bash scripts.
134 GENERATED_ALIAS_FILE = FEISTY_MEOW_LOADING_DOCK + "/fmc_core_and_custom_aliases.sh"
135 if is_debugging():
136 print("writing generated aliases in " + GENERATED_ALIAS_FILE + "...")
137
138#hmmm: perhaps a good place for a function to create the header,
139# given the appropriate comment code.
140
141 try:
142 with open(GENERATED_ALIAS_FILE, "w") as GENOUT:
143 GENOUT.write("##")
144 GENOUT.write("## generated file: " + GENERATED_ALIAS_FILE)
145 GENOUT.write("## please do not edit.")
146 GENOUT.write("##")
147
148#hmmm: old handling for the color addition.
149# starting to remember that maybe i hated where this code was being added? and that's why it was removed? maybe?
150# if (length($test_color)) {
151# print GENOUT "export color_add='--color=auto'";
152# } else {
153# print GENOUT "export color_add=";
154# }
155
156 # plow in the full set of aliases into the file.
157 for filename in ALIAS_DEFINITION_FILES:
158 try:
159 with open(filename, "r") as CURR_ALIASER:
160 for line in CURR_ALIASER:
161 GENOUT.write(line)
162 except:
163 print("cannot open current alias file: " + filename + "; skipping it.")
164
165 except:
166 print("cannot open generated aliases in " + GENERATED_ALIAS_FILE)
167 exit(1)
168
169 if is_debugging():
170 print("done rebuilding generated aliases file.");
171
172
173
174#hmmm: move this to filename helpers
175def add_permission(filename: str, perm_adjustment: int) -> None:
176 """ Adds a permission mask into the existing permissions for the file. Uses the stat values for file permissions. """
177 # get the existing permissions.
178 stats = os.stat(filename)
179 # add in the requested new items.
180 stats |= perm_adjustment
181 # save the results back to the file.
182 os.chmod(filename, stats)
183
184
185
186def not_customize(filename: str):
187 """
188 returns true if the filename string does not have 'customize' in it.
189 this indicates that the file is not located under our customization hierarchy.
190 """
191 return not re.search("customize", str, re.IGNORECASE)
192
193
194
195def main() -> None:
196 """ the main driver of activities for this app. """
197
198 # load some variables from the environment, if we can.
199 HOME = os.environ['HOME']
200 FEISTY_MEOW_BINARIES = os.environ['FEISTY_MEOW_BINARIES']
201 BUILD_TOP = os.environ['BUILD_TOP']
202 FEISTY_MEOW_APEX = os.environ['FEISTY_MEOW_APEX']
203 FEISTY_MEOW_LOADING_DOCK = os.environ['FEISTY_MEOW_LOADING_DOCK']
204 FEISTY_MEOW_SCRIPTS = os.environ['FEISTY_MEOW_SCRIPTS']
205 DEBUG_FEISTY_MEOW = os.environ['DEBUG_FEISTY_MEOW']
206
207 if is_debugging():
208 print("home is " + HOME)
209
210 # make sure we know where to store the files we're creating.
211 if len(FEISTY_MEOW_LOADING_DOCK) == 0:
212 print("\
213The FEISTY_MEOW_LOADING_DOCK variable is not defined. This must point to\
214the location where the generated scripts are stored. You may still need to\
215configure the feisty meow script system with something like:\
216 bash /opt/feistymeow.org/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\
217Please see http://feistymeow.org for more details.")
218 exit(1)
219
220
221
222 # replace any backslashes with forward thinking ones.
223 FEISTY_MEOW_LOADING_DOCK = re.sub('\\', '/', FEISTY_MEOW_LOADING_DOCK)
224 FEISTY_MEOW_SCRIPTS = re.sub('\\', '/', FEISTY_MEOW_SCRIPTS)
225 FEISTY_MEOW_APEX = re.sub('\\', '/', FEISTY_MEOW_APEX)
226
227
228
229 # create our generated shells directory if it's not already there.
230 if not os.path.isdir(FEISTY_MEOW_LOADING_DOCK):
231 os.mkdirs(FEISTY_MEOW_LOADING_DOCK)
232
233
234
235 # set the executable bit for binaries for just this current user.
236 if os.path.isdir(FEISTY_MEOW_BINARIES):
237 for filename in os.listdir(FEISTY_MEOW_BINARIES):
238 if is_debugging():
239 print("adjusting permission on " + filename)
240 # plop in executable permission for just the owner.
241 add_permission(filename, stat.S_IXUSR)
242
243
244
245 # generate the first set of alias files that are defined in the core
246 # and custom scripts directories.
248
249
250
251 SCRIPT_ALIAS_FILENAME = FEISTY_MEOW_LOADING_DOCK + "/fmc_aliases_for_scripts.sh"
252
253 # trash the old versions.
254 os.unlink(SCRIPT_ALIAS_FILENAME)
255
256 if is_debugging():
257 print("writing " + SCRIPT_ALIAS_FILENAME)
258
259
260
261 # open the alias files to be created.
262 try:
263 with open(SCRIPT_ALIAS_FILENAME) as she:
264
265 # find the list of files in the scripts directory.
266 shell_files = [ find_files(recursive_find_directories(FEISTY_MEOW_SCRIPTS)),
267 find_files(FEISTY_MEOW_LOADING_DOCK + "/custom/scripts"),
268 find_files(recursive_find_directories(FEISTY_MEOW_LOADING_DOCK + "/custom/scripts")) ]
269
270 # strip out the customization files, since they are added in only for particular users.
271 print("before filtering list: " + shell_files)
272 shell_files = list(filter(not_customize, shell_files))
273 print("after filtering list: " + shell_files)
274
275 print("found all these files in main script dirs:")
276 print(" " + shell_files)
277
278 # construct aliases for items in the scripts directory.
279 for file in shell_files:
280 # test to see what type of item we got.
281 if (re.search('^\.$', file) # skip bare current directory.
282 or re.search('^\.\.$', file) # skip bare parent directory.
283 or re.search('^.svn$', file, re.IGNORECASE) # skip bare svn state directory.
284 or re.search('^.git$', file, re.IGNORECASE) # skip bare git state directory.
285 or re.search('/\/\.$/', file, re.IGNORECASE) # skip relative current directory.
286 or re.search('/\/\.\.$/', file, re.IGNORECASE) # skip relative parent directory.
287 or re.search('/\/\.svn$/', file, re.IGNORECASE) # skip relative svn directory.
288 or re.search('/\/\.git$/', file, re.IGNORECASE) # skip relative git directory.
289 ):
290#hmmm: one could combine some of those above with a more complex regex if one wanted a project.
291 # just skip this item; it's a special directory or a file we don't want to include.
292 print("skipping name: " + file)
293 else:
294 to_add = make_alias(file, "");
295 she.write(to_add)
296
297 except:
298 print("an error occurred while writing the shell aliases file " + SCRIPT_ALIAS_FILENAME)
299 exit(1)
300
301
302
303 # prepare the finalizer chunk that is the last thing to load.
304 SENTINEL_FILENAME = FEISTY_MEOW_LOADING_DOCK + "/fmc_ending_sentinel.sh"
305 try:
306 with open(SENTINEL_FILENAME) as sentinel:
307 # write in our sentinel alias that says the alias loading process was handled.
308 sentinel.write("define_yeti_alias CORE_ALIASES_LOADED=true")
309 except:
310 print("an error occurred while writing the sentinel file " + SENTINEL_FILENAME)
311 exit(1)
312
313
314
315if __name__ == "__main__":
316 main()
317
#define open
Definition Xos2defs.h:36
None add_permission(str filename, int perm_adjustment)
str make_perl_alias(str aliasname, str dir)
str make_python_alias(str aliasname, str dir)
None make_alias(str file, str dir)
str make_bash_alias(str aliasname, str dir)