7 Rights : Copyright (C) 1996-$now by Author
11 Functions that manipulate filenames in various helpful ways.
14 This program is free software; you can redistribute it and/or modify it
15 the terms of the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License or (at your option)
17 any later version. See: "http://www.gruntose.com/Info/GNU/GPL.html" for a
18 version of the License. Please send any updates to "fred@gruntose.com".
48 takes a set of filenames that may be relative (or really arcane) and globs them into a normal list of filenames.
52 print(
"temp list is: " + original_names)
55 for entry
in original_names:
56 print(
"entry is: " + entry)
59 print(
"chopped 0=" + chopped_filename[0])
60 print(
"chopped 1=" + chopped_filename[1])
62 if chopped_filename[0] ==
"." or chopped_filename[0] ==
"..":
64 to_return.append(chopped_filename[0])
67 if chopped_filename[1] ==
".":
69 to_return.append(chopped_filename[0])
73 files_found = os.listdir(chopped_filename[0])
76 replacement_patterns = {
91 for possible_name
in files_found:
92 match = chopped_filename[1]
94 for seek, replacer
in replacement_patterns:
95 match = re.sub(seek, replacer, match)
98 match =
"^" + match +
"\$"
99 print(
"possibname is '" + possible_name +
"':")
100 if re.search(match, possible_name):
102 to_return.append(chopped_filename[0] + possible_name)
103 print(
"got a match on:" + chopped_filename)
113 f1_stat =
stat(file1)
114 f2_stat =
stat(file2)
115 return (f1_stat.ST_INO == f2_stat.ST_INO)
and (f1_stat.ST_DEV == f2_stat.ST_DEV)
125 chewed_name = patch_name_for_pc(chewed_name)
126 if re.search(
r'/', chewed_name):
128 directory_part = os.path.dirname(chewed_name)
129 file_part = os.path.basename(chewed_name)
130 if len(file_part) == 0:
133 return directory_part, file_part
134 elif chewed_name ==
'.':
137 elif chewed_name ==
"..":
143 return "./", chewed_name
164 for i
in range(len(base) - 1, -1, -1):
171 return base[found : len(base) - found]
178 full = patch_name_for_pc(full)
181 return full[0 : len(full) - to_remove]
187 directory_name = pathname
189 inspection_point = len(directory_name) - 1;
190 while inspection_point > 0:
192 final_char = directoryname[inspection_point:inspection_point]
194 if not final_char ==
'/' &&
not final_char ==
"\\":
196 directory_name = directory_name[0 : len(directory_name) - 1]
198 return directory_name
206 if OS ==
"Windows_NT" or OS ==
"Windows_95" or OS ==
"DOS" or OS ==
"OS2":
234 print(
"old dir name is " + directory_name)
237 if re.search(
"win", OS, re.IGNORE_CASE):
243 directory_name = re.sub(
'^(.):[\\\/](.*)$',
'\/\1\/\2')
247 directory_name = re.sub(
'^(.):[\\\/](.*)$',
'\/cygdrive\/\1\/\2/')
251 for j
in range(0, len(directory_name)):
253 if directory_name[j, j+1] ==
"\\" or directory_name[j, j+1] ==
"/":
254 directory_name[j] = dirsep
259 for i
in range(1, len(directory_name)):
262 if (substr($directory_name, $i, 1) eq $dirsep) {
266 local($head, $tail) = (substr($directory_name, 0, $i - 1),
267 substr($directory_name, $i, length($directory_name) - 1));
268 $directory_name = $head . $tail;
280 if ($directory_name =~ /^.:$/) {
284 $directory_name = $directory_name .
"/";
286 return $directory_name;
293 sub patch_name_for_pc {
296 if (length($name) != 2) {
return $name; }
297 local($colon) = substr($name, 1, 1);
313 sub important_filename {
319 local(
@junk_files) = (
"~",
"^\\.#.*",
"^\\._.*",
"\\.aps",
"\\.bak",
320 "^binaries",
"^bin.ant",
"^bin.eclipse",
321 "\\.clw",
"^cpdiff_tmp\\.txt",
"^\\.ds_store",
"^diffs\\.txt",
322 "^diff_tmp\\.txt",
"\\.dsp",
"\\.dsw",
"\\.gid",
"gmon\\.out",
"\\.isr",
323 "^isconfig\\.ini",
"\\.log",
"^manifest.txt",
"^obj",
324 "\\.obj",
"\\.output",
"\\.plg",
"^RCa.*",
"^Release",
"\\.res",
325 "\\.sbr",
".*scc",
"^Setup\\.dbg",
"^Setup\\.inx",
326 "^Setup\\.map",
"^Setup\\.obs",
"^Selenium_.*Login.html",
327 "\\.stackdump",
"^string1033\\.txt",
"\\.suo",
"\\.swp",
328 "^thumbs.db",
"[a-zA-Z0-9]\\.tmp",
"^trans\\.tbl",
"\\.user",
"_version\\.h",
329 "_version\\.rc",
"^waste",
"\\.ws4",
"\\.wsm");
331 foreach $temp (
@junk_files) {
333 if ($name =~ /${temp}/i) {
return 0; }
343 return &patch_name_for_pc
344 (&remove_trailing_slashes
350 sub get_drive_letter {
352 if (substr($path, 0, 1) =~ /[a-zA-Z]/) {
353 if (substr($path, 1, 1) eq
":") {
return substr($path, 0, 1); }
360 sub remove_drive_letter {
362 if (substr($path, 0, 1) =~ /[a-zA-Z]/) {
363 if (substr($path, 1, 1) eq
":") {
return substr($path, 2); }
374 $name =~ tr/A-Z/a-z/;
380 $name =~ tr/a-z/A-Z/;
388 sub recursive_delete {
392 print "this is not a dir: $dir => should whack it here?";
398 opendir DIR, $dir
or next;
399 while ($_ = readdir DIR) {
401 my $path =
"$dir/$_";
402 unlink $path
if -f $path;
403 recursive_delete($path)
if -d $path;
406 rmdir $dir
or print "error - $!";
413 sub find_directories {
419 opendir DIR, $dir
or next;
420 while ($_ = readdir DIR) {
423 my $path =
"$dir/$_";
426 push
@dirs_found, $path;
437 my
@files_found = ();
442 push
@files_found, $dir;
447 opendir DIR, $dir
or next;
448 while ($_ = readdir DIR) {
451 my $path =
"$dir/$_";
454 push
@files_found, $path;
465 sub recursive_find_directories {
467 my
@toplevel = find_directories(
@_);
470 push(
@to_return,
@toplevel);
475 if (scalar
@toplevel > 0) {
476 my
@subs_found = recursive_find_directories(
@toplevel);
477 push(
@to_return,
@subs_found);
bool same_file(str file1, str file2)
list glob_list(list original_names)
#hmmm: make this lower-level, a script that is inherited by all perl scripts.
str native_canonicalize(str pathname)
str basename(str pathname)
str dirname(str pathname)
str pc_canonicalize(str pathname)
str directory_separator()
list split_filename(str pathname)
str canonicalizer(str directory_name, str dirsep)
str canonicalize(str pathname)
str unix_canonicalize(str pathname)
str remove_trailing_slashes(str pathname)
str non_extension(str pathname)
str extension(str pathname)