Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Anders Blomdell
apa
Commits
2aa96576
Commit
2aa96576
authored
Aug 26, 2019
by
Anders Blomdell
Browse files
Ported to python3
parent
fca8768d
Changes
1
Hide whitespace changes
Inline
Side-by-side
apa
View file @
2aa96576
#!/usr/bin/python
#!/usr/bin/python
3
"""
Anders Python Archiver
...
...
@@ -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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
from
__future__
import
print_function
import
optparse
import
os
...
...
@@ -33,20 +34,20 @@ import sys
def
getModuleName
(
s
):
m
=
re
.
match
(
"^(.*)/__init__\.py$"
,
s
)
if
m
:
return
string
.
replace
(
m
.
group
(
1
)
,
"/"
,
"."
)
return
m
.
group
(
1
)
.
replace
(
"/"
,
"."
)
m
=
re
.
match
(
"^(.*)\.py$"
,
s
)
if
m
:
return
string
.
replace
(
m
.
group
(
1
)
,
"/"
,
"."
)
return
m
.
group
(
1
)
.
replace
(
"/"
,
"."
)
return
None
def
emitCode
(
f
,
module
,
filename
,
code
):
print
>>
sys
.
stderr
,
"EMIT:"
,
module
print
>>
f
,
'"%s" : ( "%s", """%s""")'
%
(
quote
(
module
),
quote
(
filename
),
quote
(
code
))
print
(
"EMIT:"
,
module
,
file
=
sys
.
stderr
)
print
(
'"%s" : ( "%s", """%s""")'
%
(
quote
(
module
),
quote
(
filename
),
quote
(
code
))
,
file
=
f
)
def
quote
(
s
):
return
s
tring
.
replace
(
string
.
replace
(
s
,
'
\\
'
,
'
\\\\
'
)
,
'"'
,
'
\\
"'
)
return
s
.
replace
(
'
\\
'
,
'
\\\\
'
)
.
replace
(
'"'
,
'
\\
"'
)
if
__name__
==
'__main__'
:
usage
=
"%prog [options] <main module> <additional modules>*"
...
...
@@ -68,7 +69,7 @@ if __name__ == '__main__':
(
options
,
args
)
=
optParser
.
parse_args
(
sys
.
argv
[
1
:])
if
options
.
documentation
:
print
__doc__
print
(
__doc__
)
sys
.
exit
(
0
)
# Read all python files
...
...
@@ -92,32 +93,32 @@ if __name__ == '__main__':
code
.
append
((
name
,
archivename
,
f
.
read
()))
f
.
close
()
else
:
print
"Illegal filename '%s'"
%
filename
print
(
"Illegal filename '%s'"
%
filename
)
sys
.
exit
(
1
)
if
options
.
output
:
apa
=
file
(
options
.
output
,
'w'
)
apa
=
open
(
options
.
output
,
'w'
)
else
:
apa
=
None
# Emit code
interpreter
=
code
[
0
][
2
].
splitlines
()[
0
]
if
options
.
interpreter
!=
None
:
print
>>
apa
,
"#!%s"
%
options
.
interpreter
print
(
"#!%s"
%
options
.
interpreter
,
file
=
apa
)
elif
interpreter
.
find
(
"python"
)
>=
0
:
# Take interpreter version from main file
print
>>
apa
,
interpreter
print
(
interpreter
,
file
=
apa
)
else
:
# Default interpreter
print
>>
sys
.
stderr
,
"Interpreter defaulting to '#!/usr/bin/python3'"
print
>>
apa
,
"#!/usr/bin/python3"
print
>>
apa
,
"
\"\"\"
"
print
>>
apa
,
"Executable archive of '%s'"
%
code
[
0
][
0
]
print
>>
apa
,
"
\n
The following modules are archived:"
print
>>
apa
,
" %-20s %s"
%
(
'__main__'
,
code
[
0
][
1
])
print
(
"Interpreter defaulting to '#!/usr/bin/python3'"
,
file
=
sys
.
stderr
)
print
(
"#!/usr/bin/python3"
,
file
=
apa
)
print
(
"
\"\"\"
"
,
file
=
apa
)
print
(
"Executable archive of '%s'"
%
code
[
0
][
0
]
,
file
=
apa
)
print
(
"
\n
The following modules are archived:"
,
file
=
apa
)
print
(
" %-20s %s"
%
(
'__main__'
,
code
[
0
][
1
])
,
file
=
apa
)
for
(
n
,
f
,
c
)
in
code
[
1
:]:
print
>>
apa
,
" %-20s %s"
%
(
n
,
f
)
print
>>
apa
,
"""
print
(
" %-20s %s"
%
(
n
,
f
)
,
file
=
apa
)
print
(
"""
Archive created by 'apa' (http://www.control.lth.se/user/andersb/software/apa)
To extract as individual files:
...
...
@@ -128,15 +129,15 @@ To extract as individual files:
import <archive name>
<archive name>.extract(<optional destination dir>)
\"\"\"
"""
print
>>
apa
,
"code = {"
"""
,
file
=
apa
)
print
(
"code = {"
,
file
=
apa
)
(
n
,
f
,
c
)
=
code
[
0
]
emitCode
(
apa
,
'__main__'
,
f
,
c
)
for
(
n
,
f
,
c
)
in
code
[
1
:]:
print
>>
apa
,
","
print
(
","
,
file
=
apa
)
emitCode
(
apa
,
n
,
f
,
c
)
print
>>
apa
,
"}"
print
>>
apa
,
"""
print
(
"}"
,
file
=
apa
)
print
(
"""
class Importer:
\"\"\"
Loads modules from local code dictionary
...
...
@@ -228,10 +229,10 @@ if __name__ == '__main__':
traceback.print_exception(*BAD)
pass
sys.exit(1)
"""
"""
,
file
=
apa
)
if
apa
:
apa
.
close
()
try
:
os
.
chmod
(
options
.
output
,
0755
)
os
.
chmod
(
options
.
output
,
0
o
755
)
except
:
pass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment