Added the --set-passwd switch and the set_library_password() subroutine to manage the library password.

This commit is contained in:
依瑪貓 2017-05-15 14:51:15 +08:00
parent ffbff69f2b
commit f64e7d3404
2 changed files with 74 additions and 6 deletions

View File

@ -66,6 +66,10 @@ LIBRARY The name of the Basic library. Default to the same
uploads the source files onto the uploads the source files onto the
OpenOffice/LibreOffice Basic storage. OpenOffice/LibreOffice Basic storage.
--set-passwd Sets the password of the library after upload. Supply
nothing when prompting the new password to remove the
password protection. This does not work with --get.
-p, --port N The TCP port to communicate with -p, --port N The TCP port to communicate with
OpenOffice/LibreOffice. The default is 2002. You can OpenOffice/LibreOffice. The default is 2002. You can
change it if port 2002 is already in use. change it if port 2002 is already in use.
@ -130,6 +134,7 @@ def append_uno_path():
append_uno_path() append_uno_path()
import uno import uno
from com.sun.star.connection import NoConnectException from com.sun.star.connection import NoConnectException
from com.sun.star.lang import IllegalArgumentException
def main(): def main():
@ -163,7 +168,7 @@ def main():
return return
oo = Office(args.port) oo = Office(args.port)
storage = find_storage(oo, args.storage_type, args.target) storage = find_storage(oo, args.storage_type, args.target)
update_basic_modules(storage, args.library, modules) update_basic_modules(storage, args.library, modules, args.setpass)
if args.run is not None: if args.run is not None:
run_macro(storage, args.library, args.run) run_macro(storage, args.library, args.run)
@ -190,6 +195,10 @@ def parse_args():
parser.add_argument( parser.add_argument(
"--get", action="store_true", "--get", action="store_true",
help="Downloads the macros instead of upload.") help="Downloads the macros instead of upload.")
parser.add_argument(
"--set-passwd", dest="setpass", action="store_true",
help=("Sets the password of the library after upload. "
"This does not work with --get."))
parser.add_argument( parser.add_argument(
"-p", "--port", metavar="N", type=int, default=2002, "-p", "--port", metavar="N", type=int, default=2002,
help=("The TCP port to communicate with " help=("The TCP port to communicate with "
@ -221,7 +230,7 @@ def parse_args():
" an \"Untitied 1\" (in your language) if it is a new" " an \"Untitied 1\" (in your language) if it is a new"
" file.")) " file."))
parser.add_argument( parser.add_argument(
"-v", "--version", action="version", version="%(prog)s 0.8") "-v", "--version", action="version", version="%(prog)s 0.9")
args = parser.parse_args() args = parser.parse_args()
# Obtain the absolute path # Obtain the absolute path
@ -240,6 +249,11 @@ def parse_args():
if sys.version_info.major == 2: if sys.version_info.major == 2:
if args.target is not None: if args.target is not None:
args.target = args.target.decode(locale.getpreferredencoding()) args.target = args.target.decode(locale.getpreferredencoding())
if args.get and args.setpass:
print("ERROR: --get does not work with --set-passwd.",
file=sys.stderr)
sys.exit(1)
return return
@ -354,7 +368,7 @@ def get_doc_paths(docs, file_content_provider):
if doc.hasLocation(): if doc.hasLocation():
paths.append( paths.append(
file_content_provider.getSystemPathFromFileURL( file_content_provider.getSystemPathFromFileURL(
doc.getLocation())) doc.getLocation()))
else: else:
paths.append(doc.getTitle()) paths.append(doc.getTitle())
return sorted(paths) return sorted(paths)
@ -452,7 +466,7 @@ def read_basic_modules(storage, libname):
return modules return modules
def update_basic_modules(storage, libname, modules): def update_basic_modules(storage, libname, modules, setpass):
"""Update the OpenOffice/LibreOffice Basic macro storage. """Update the OpenOffice/LibreOffice Basic macro storage.
Arguments: Arguments:
@ -466,12 +480,16 @@ def update_basic_modules(storage, libname, modules):
storage.libs.createLibrary(libname) storage.libs.createLibrary(libname)
print("Script library %s created." % libname, file=sys.stderr) print("Script library %s created." % libname, file=sys.stderr)
create_dialog_library(storage, libname) create_dialog_library(storage, libname)
if setpass:
set_library_password(storage, libname)
verify_library_password(storage, libname) verify_library_password(storage, libname)
library = storage.libs.getByName(libname) library = storage.libs.getByName(libname)
for modname in sorted(modules.keys()): for modname in sorted(modules.keys()):
library.insertByName(modname, modules[modname]) library.insertByName(modname, modules[modname])
print("Module %s added." % modname, file=sys.stderr) print("Module %s added." % modname, file=sys.stderr)
else: else:
if setpass:
set_library_password(storage, libname)
verify_library_password(storage, libname) verify_library_password(storage, libname)
storage.libs.loadLibrary(libname) storage.libs.loadLibrary(libname)
library = storage.libs.getByName(libname) library = storage.libs.getByName(libname)
@ -524,7 +542,6 @@ def verify_library_password(storage, libname):
Arguments: Arguments:
storage: The Basic macro storage, as a Storage object. storage: The Basic macro storage, as a Storage object.
libname: The name of the dialog library. libname: The name of the dialog library.
password: The password for the library.
""" """
if not storage.libs.isLibraryPasswordProtected(libname): if not storage.libs.isLibraryPasswordProtected(libname):
return return
@ -539,6 +556,57 @@ def verify_library_password(storage, libname):
return return
def set_library_password(storage, libname):
"""Sets the password of the library.
Arguments:
storage: The Basic macro storage, as a Storage object.
libname: The name of the dialog library.
"""
if not storage.libs.isLibraryPasswordProtected(libname):
while True:
newpass = getpass.getpass("New password: ")
newpass2 = getpass.getpass("Repeat new password: ")
if newpass != newpass2:
print("ERROR: Two new passwords are not the same.",
file=sys.stderr)
continue
else:
break
storage.libs.changeLibraryPassword(libname, "", newpass)
return
else:
while True:
oldpass = getpass.getpass("Old password: ")
if oldpass == "":
print("ERROR: Please enter the old password.",
file=sys.stderr)
continue
tmppass = oldpass + "tmp"
try:
storage.libs.changeLibraryPassword(
libname, oldpass, tmppass)
except IllegalArgumentException:
print("ERROR: Incorrect old password.",
file=sys.stderr)
continue
else:
storage.libs.changeLibraryPassword(
libname, tmppass, oldpass)
break
while True:
newpass = getpass.getpass("New password: ")
newpass2 = getpass.getpass("Repeat new password: ")
if newpass != newpass2:
print("ERROR: Two new passwords are not the same.",
file=sys.stderr)
continue
else:
break
storage.libs.changeLibraryPassword(libname, oldpass, newpass)
return
def run_macro(storage, libname, macro): def run_macro(storage, libname, macro):
"""Run a Basic macro. """Run a Basic macro.

View File

@ -29,7 +29,7 @@ if os.path.basename(sys.executable) == "python.bin":
os.path.dirname(sys.executable), "python") os.path.dirname(sys.executable), "python")
setup(name="obasync", setup(name="obasync",
version="0.8", version="0.9",
description="Office Basic macro source synchronizer", description="Office Basic macro source synchronizer",
url="https://pypi.python.org/pypi/obasync", url="https://pypi.python.org/pypi/obasync",
author="imacat", author="imacat",