Commit 2aa96576 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Ported to python3

parent fca8768d
#!/usr/bin/python #!/usr/bin/python3
""" """
Anders Python Archiver Anders Python Archiver
...@@ -23,6 +23,7 @@ You should have received a copy of the GNU General Public License ...@@ -23,6 +23,7 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" """
from __future__ import print_function
import optparse import optparse
import os import os
...@@ -33,20 +34,20 @@ import sys ...@@ -33,20 +34,20 @@ import sys
def getModuleName(s): def getModuleName(s):
m = re.match("^(.*)/__init__\.py$", s) m = re.match("^(.*)/__init__\.py$", s)
if m: if m:
return string.replace(m.group(1), "/", ".") return m.group(1).replace("/", ".")
m = re.match("^(.*)\.py$", s) m = re.match("^(.*)\.py$", s)
if m: if m:
return string.replace(m.group(1), "/", ".") return m.group(1).replace("/", ".")
return None return None
def emitCode(f, module, filename, code): def emitCode(f, module, filename, code):
print >> sys.stderr, "EMIT:", module print("EMIT:", module, file=sys.stderr)
print >> f, '"%s" : ( "%s", """%s""")' % (quote(module), print('"%s" : ( "%s", """%s""")' % (quote(module),
quote(filename), quote(filename),
quote(code)) quote(code)), file=f)
def quote(s): def quote(s):
return string.replace(string.replace(s, '\\', '\\\\'), '"', '\\"') return s.replace('\\', '\\\\').replace('"', '\\"')
if __name__ == '__main__': if __name__ == '__main__':
usage = "%prog [options] <main module> <additional modules>*" usage = "%prog [options] <main module> <additional modules>*"
...@@ -68,7 +69,7 @@ if __name__ == '__main__': ...@@ -68,7 +69,7 @@ if __name__ == '__main__':
(options, args) = optParser.parse_args(sys.argv[1:]) (options, args) = optParser.parse_args(sys.argv[1:])
if options.documentation: if options.documentation:
print __doc__ print(__doc__)
sys.exit(0) sys.exit(0)
# Read all python files # Read all python files
...@@ -92,32 +93,32 @@ if __name__ == '__main__': ...@@ -92,32 +93,32 @@ if __name__ == '__main__':
code.append((name, archivename, f.read())) code.append((name, archivename, f.read()))
f.close() f.close()
else: else:
print "Illegal filename '%s'" % filename print("Illegal filename '%s'" % filename)
sys.exit(1) sys.exit(1)
if options.output: if options.output:
apa = file(options.output, 'w') apa = open(options.output, 'w')
else: else:
apa = None apa = None
# Emit code # Emit code
interpreter = code[0][2].splitlines()[0] interpreter = code[0][2].splitlines()[0]
if options.interpreter != None: if options.interpreter != None:
print >> apa, "#!%s" % options.interpreter print("#!%s" % options.interpreter, file=apa)
elif interpreter.find("python") >= 0: elif interpreter.find("python") >= 0:
# Take interpreter version from main file # Take interpreter version from main file
print >> apa, interpreter print(interpreter, file=apa)
else: else:
# Default interpreter # Default interpreter
print >> sys.stderr, "Interpreter defaulting to '#!/usr/bin/python3'" print("Interpreter defaulting to '#!/usr/bin/python3'", file=sys.stderr)
print >> apa, "#!/usr/bin/python3" print("#!/usr/bin/python3", file=apa)
print >> apa, "\"\"\"" print("\"\"\"", file=apa)
print >> apa, "Executable archive of '%s'" % code[0][0] print("Executable archive of '%s'" % code[0][0], file=apa)
print >> apa, "\nThe following modules are archived:" print("\nThe following modules are archived:", file=apa)
print >> apa, " %-20s %s" % ('__main__', code[0][1]) print(" %-20s %s" % ('__main__', code[0][1]), file=apa)
for (n, f, c) in code[1:]: for (n, f, c) in code[1:]:
print >> apa, " %-20s %s" % (n, f) print(" %-20s %s" % (n, f), file=apa)
print >> apa, """ print("""
Archive created by 'apa' (http://www.control.lth.se/user/andersb/software/apa) Archive created by 'apa' (http://www.control.lth.se/user/andersb/software/apa)
To extract as individual files: To extract as individual files:
...@@ -128,15 +129,15 @@ To extract as individual files: ...@@ -128,15 +129,15 @@ To extract as individual files:
import <archive name> import <archive name>
<archive name>.extract(<optional destination dir>) <archive name>.extract(<optional destination dir>)
\"\"\" \"\"\"
""" """, file=apa)
print >> apa, "code = {" print("code = {", file=apa)
(n, f, c) = code[0] (n, f, c) = code[0]
emitCode(apa, '__main__', f, c) emitCode(apa, '__main__', f, c)
for (n, f, c) in code[1:]: for (n, f, c) in code[1:]:
print >> apa, "," print(",", file=apa)
emitCode(apa, n, f, c) emitCode(apa, n, f, c)
print >> apa, "}" print("}", file=apa)
print >> apa, """ print("""
class Importer: class Importer:
\"\"\"Loads modules from local code dictionary \"\"\"Loads modules from local code dictionary
...@@ -228,10 +229,10 @@ if __name__ == '__main__': ...@@ -228,10 +229,10 @@ if __name__ == '__main__':
traceback.print_exception(*BAD) traceback.print_exception(*BAD)
pass pass
sys.exit(1) sys.exit(1)
""" """, file=apa)
if apa: if apa:
apa.close() apa.close()
try: try:
os.chmod(options.output, 0755) os.chmod(options.output, 0o755)
except: except:
pass pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment