1 #! /usr/bin/env python3
3 __author__ = "Oleg Broytman <phd@phdru.name>"
4 __copyright__ = "Copyright (C) 2004-2024 PhiloSoft Design"
11 from m_lib.m_path import get_homedir
13 home_dir = get_homedir()
14 os.umask(0o022) # octal; -rw-r--r--
18 ("bashrc", "sh"), ("profile", "sh"), ("shellrc", "sh"),
19 ("mailcap", "conf"), ("mime.types", "conf"),
20 ("muttrc", "muttrc"), ("procmailrc", "procmail"),
21 ("gitconfig", "gitconfig"), ("vimrc", "vim"),
24 mc_file_types = (("mc.ext", "conf"), ("mc.ext.i", "conf"), ("menu", "conf"))
27 ("init.py", "python"),
28 ("pdbrc", "python"), ("pdbrc.py", "python")
32 def add_headers(fname, first, prev, next, last):
33 head_fname = os.path.join(home_dir, fname + ".head")
34 head_file = open(head_fname, 'r')
35 head = head_file.read()
38 html_fname = fname + ".html"
39 html_file = open(html_fname, 'r')
40 html = html_file.read()
43 if fname in ("init.py", "pdbrc.py"):
45 txt_name = fname + '.txt'
47 dot_name = '.' + fname
50 html_file = open(html_fname, 'w')
51 if fname in ("muttrc", "procmailrc", "vimrc"):
52 html_file.write("#encoding koi8-r\n")
53 html_file.write("#extends phd_site\n")
54 html_file.write("#implements respond\n")
55 html_file.write("#attr $Title = \"%s\"\n" % dot_name)
56 html_file.write("#attr $Copyright = 2003\n")
57 html_file.write("#attr $First = \"%s.html\"\n" % first)
59 html_file.write("#attr $Prev = \"%s.html\"\n" % prev)
61 html_file.write("#attr $Next = \"%s.html\"\n" % next)
62 html_file.write("#attr $Last = \"%s.html\"\n" % last)
64 '#attr $alternates = (("Plain text version", "text/plain", "%s"),)'
68 html_file.write("#def body_html\n")
69 html_file.write("#raw\n")
73 html_file.write("#end raw\n")
74 html_file.write("#end def\n")
75 html_file.write("$phd_site.respond(self)\n")
79 def process_dotfile(i, fname, ftype, file_types):
80 file = open(fname, 'r')
84 text = re.sub('cgmem_nice \\d+ ', '', text)
86 if fname in ('mailcap', 'mc.ext', 'mc.ext.ini', 'menu'):
87 text = text.replace('mplay', 'mplayer')
88 text = text.replace('mplayerer', 'mplayer')
89 if fname == 'mailcap':
91 '-a -n "`which mplayer 2>/dev/null`" '
92 '-a -n "`which mplayer 2>/dev/null`"',
93 '-a -n "`which mplayer 2>/dev/null`"')
94 file = open(fname, 'w')
99 'exec gvim -f -c "set filetype=%s" '
100 '-c ":source %s/dotfile2html.vim" "%s"'
101 % (ftype, home_dir, fname)
104 first = file_types[0][0]
108 prev = file_types[i-1][0]
109 if i >= len(file_types) - 1:
112 next = file_types[i+1][0]
113 last = file_types[-1][0]
114 add_headers(fname, first, prev, next, last)
116 if os.path.exists(fname + ".tmpl") \
117 and filecmp.cmp(fname + ".html", fname + ".tmpl"):
118 os.remove(fname + ".html")
120 os.rename(fname + ".html", fname + ".tmpl")
122 if fname.endswith('.py'):
123 new_name = fname + '.txt'
124 os.rename(fname, new_name)
127 if os.path.exists(fname):
128 os.chmod(fname, 0o644)
131 def process_files(file_types):
132 for i, (fname, ftype) in enumerate(file_types):
133 if os.path.exists('.' + fname) and not os.path.exists(fname):
134 shutil.copy2('.' + fname, fname)
136 if not os.path.exists(fname):
139 if fname == 'mc.ext.i' and not os.path.exists('mc.ext.ini'):
140 os.rename('mc.ext.i', 'mc.ext.ini')
141 if fname == 'mc.ext.i':
144 process_dotfile(i, fname, ftype, file_types)
147 process_files(home_file_types + mc_file_types + python_file_types)