Use PollScheduler iteration method.
[portage.git] / bin / dohtml.py
1 #!/usr/bin/python
2 # Copyright 1999-2006 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 #
6 # Typical usage:
7 # dohtml -r docs/*
8 #  - put all files and directories in docs into /usr/share/doc/${PF}/html
9 # dohtml foo.html
10 #  - put foo.html into /usr/share/doc/${PF}/html
11 #
12 #
13 # Detailed usage:
14 # dohtml <list-of-files> 
15 #  - will install the files in the list of files (space-separated list) into 
16 #    /usr/share/doc/${PF}/html, provided the file ends in .htm, .html, .css,
17 #      .js, ,gif, .jpeg, .jpg, or .png.
18 # dohtml -r <list-of-files-and-directories>
19 #  - will do as 'dohtml', but recurse into all directories, as long as the 
20 #    directory name is not CVS
21 # dohtml -A jpe,java [-r] <list-of-files[-and-directories]>
22 #  - will do as 'dohtml' but add .jpe,.java (default filter list is
23 #    added to your list)
24 # dohtml -a png,gif,html,htm [-r] <list-of-files[-and-directories]>
25 #  - will do as 'dohtml' but filter on .png,.gif,.html,.htm (default filter 
26 #    list is ignored)
27 # dohtml -x CVS,SCCS,RCS -r <list-of-files-and-directories>
28 #  - will do as 'dohtml -r', but ignore directories named CVS, SCCS, RCS
29 #
30
31 from __future__ import print_function
32
33 import os
34 import sys
35
36 def dodir(path):
37         os.spawnlp(os.P_WAIT, "install", "install", "-d", path)
38
39 def dofile(src,dst):
40         os.spawnlp(os.P_WAIT, "install", "install", "-m0644", src, dst)
41
42 def eqawarn(lines):
43         cmd = "source '%s/isolated-functions.sh' ; " % \
44                 os.environ["PORTAGE_BIN_PATH"]
45         for line in lines:
46                 cmd += "eqawarn \"%s\" ; " % line
47         os.spawnlp(os.P_WAIT, "bash", "bash", "-c", cmd)
48
49 skipped_directories = []
50
51 def install(basename, dirname, options, prefix=""):
52         fullpath = basename
53         if prefix:
54                 fullpath = prefix + "/" + fullpath
55         if dirname:
56                 fullpath = dirname + "/" + fullpath
57
58         if options.DOCDESTTREE:
59                 destdir = options.ED + "usr/share/doc/" + options.PF + "/" + options.DOCDESTTREE + "/" + options.doc_prefix + "/" + prefix
60         else:
61                 destdir = options.ED + "usr/share/doc/" + options.PF + "/html/" + options.doc_prefix + "/" + prefix
62
63         if not os.path.exists(fullpath):
64                 sys.stderr.write("!!! dohtml: %s does not exist\n" % fullpath)
65                 return False
66         elif os.path.isfile(fullpath):
67                 ext = os.path.splitext(basename)[1]
68                 if (len(ext) and ext[1:] in options.allowed_exts) or basename in options.allowed_files:
69                         dodir(destdir)
70                         dofile(fullpath, destdir + "/" + basename)
71         elif options.recurse and os.path.isdir(fullpath) and \
72              basename not in options.disallowed_dirs:
73                 for i in os.listdir(fullpath):
74                         pfx = basename
75                         if prefix: pfx = prefix + "/" + pfx
76                         install(i, dirname, options, pfx)
77         elif not options.recurse and os.path.isdir(fullpath):
78                 global skipped_directories
79                 skipped_directories.append(fullpath)
80                 return False
81         else:
82                 return False
83         return True
84
85
86 class OptionsClass:
87         def __init__(self):
88                 self.PF = ""
89                 self.ED = ""
90                 self.DOCDESTTREE = ""
91                 
92                 if "PF" in os.environ:
93                         self.PF = os.environ["PF"]
94                 if os.environ.get("EAPI", "0") in ("0", "1", "2"):
95                         self.ED = os.environ.get("D", "")
96                 else:
97                         self.ED = os.environ.get("ED", "")
98                 if "_E_DOCDESTTREE_" in os.environ:
99                         self.DOCDESTTREE = os.environ["_E_DOCDESTTREE_"]
100                 
101                 self.allowed_exts = [ 'htm', 'html', 'css', 'js',
102                         'gif', 'jpeg', 'jpg', 'png' ]
103                 self.allowed_files = []
104                 self.disallowed_dirs = [ 'CVS' ]
105                 self.recurse = False
106                 self.verbose = False
107                 self.doc_prefix = ""
108
109 def print_help():
110         opts = OptionsClass()
111
112         print("dohtml [-a .foo,.bar] [-A .foo,.bar] [-f foo,bar] [-x foo,bar]")
113         print("       [-r] [-V] <file> [file ...]")
114         print()
115         print(" -a   Set the list of allowed to those that are specified.")
116         print("      Default:", ",".join(opts.allowed_exts))
117         print(" -A   Extend the list of allowed file types.")
118         print(" -f   Set list of allowed extensionless file names.")
119         print(" -x   Set directories to be excluded from recursion.")
120         print("      Default:", ",".join(opts.disallowed_dirs))
121         print(" -p   Set a document prefix for installed files (empty by default).")
122         print(" -r   Install files and directories recursively.")
123         print(" -V   Be verbose.")
124         print()
125
126 def parse_args():
127         options = OptionsClass()
128         args = []
129         
130         x = 1
131         while x < len(sys.argv):
132                 arg = sys.argv[x]
133                 if arg in ["-h","-r","-V"]:
134                         if arg == "-h":
135                                 print_help()
136                                 sys.exit(0)
137                         elif arg == "-r":
138                                 options.recurse = True
139                         elif arg == "-V":
140                                 options.verbose = True
141                 elif sys.argv[x] in ["-A","-a","-f","-x","-p"]:
142                         x += 1
143                         if x == len(sys.argv):
144                                 print_help()
145                                 sys.exit(0)
146                         elif arg == "-p":
147                                 options.doc_prefix = sys.argv[x]
148                         else:
149                                 values = sys.argv[x].split(",")
150                                 if arg == "-A":
151                                         options.allowed_exts.extend(values)
152                                 elif arg == "-a":
153                                         options.allowed_exts = values
154                                 elif arg == "-f":
155                                         options.allowed_files = values
156                                 elif arg == "-x":
157                                         options.disallowed_dirs = values
158                 else:
159                         args.append(sys.argv[x])
160                 x += 1
161         
162         return (options, args)
163
164 def main():
165
166         (options, args) = parse_args()
167
168         if options.verbose:
169                 print("Allowed extensions:", options.allowed_exts)
170                 print("Document prefix : '" + options.doc_prefix         + "'")
171                 print("Allowed files :", options.allowed_files)
172
173         success = False
174         
175         for x in args:
176                 basename = os.path.basename(x)
177                 dirname  = os.path.dirname(x)
178                 success |= install(basename, dirname, options)
179
180         global skipped_directories
181         for x in skipped_directories:
182                 eqawarn(["QA Notice: dohtml on directory " + \
183                         "'%s' without recursion option" % x])
184
185         if success:
186                 retcode = 0
187         else:
188                 retcode = 1
189
190         sys.exit(retcode)
191
192 if __name__ == "__main__":
193         main()