Changeset 16329


Ignore:
Timestamp:
Jul 18, 2016, 10:58:12 AM (3 years ago)
Author:
dcorreia
Message:

Added support for creating directories and copying files.
Abstracted debug printing and command execution.
Updated package installation.
Added make pkcs11 hash links and get crl file features.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • internals/2016/linuxloginsmartcardwizard/trunk/project1/configpkcs11.py

    r16313 r16329  
    33import subprocess
    44import sys
     5import shutil
     6import inspect
    57
    6 PAMPKCS11_PACKAGE = "libpam-pkcs11"
     8INSTALL_COMMAND = "apt-get install "
     9HASHLINK_COMMAND = "pkcs11_make_hash_link"
     10WGET_COMMAND = "wget "
    711
    8 # TODO Install PKCS#11 PAM package
     12
     13def executeCommand(command, outputPath=None):
     14    '''
     15    Executes a shell command and redirects output if necessary
     16    :param command: command to execute
     17    :param outputPath: path to redirect output if necessary
     18    :return: no return value
     19    '''
     20    try:
     21        process = subprocess.Popen(command, shell=True, stdout=outputPath, stderr=subprocess.PIPE)
     22        process.wait()
     23        print("DEBUG: executeCommand (%s) - %s" % (command, process.stderr.read()), file=sys.stderr)
     24    except subprocess.CalledProcessError as error:
     25        print("DEBUG: executeCommand (%s) - %s" % (command, error), file=sys.stderr)
     26
     27
     28def printDebugInfo(debugData):
     29    '''
     30    Prints debug information
     31    :param debugData: string of debug data
     32    :return: no return value
     33    '''
     34    print("DEBUG: %s - %s" % (inspect.stack()[1][3], debugData), file=sys.stderr)
    935
    1036
     
    1339    Installs package using apt-get
    1440    :param packageName: name of the package
    15     :return: True if package was installed with success and False otherwise
     41    :return: no return value
    1642    '''
    17     if not hasPackage(packageName):
    18         # install package
    19         print("DEBUG: installPackage - need to install", packageName, file=sys.stderr)
    20         command = "apt-get install " + packageName
    21         try:
    22             proc = subprocess.Popen(command, shell=True, stdout=open(os.devnull, "wb"), stderr=subprocess.PIPE)
    23             print("DEBUG: installPackage - ", proc.stderr.read(), file=sys.stderr)
    24 
    25         except subprocess.CalledProcessError as error:
    26             print("DEBUG: installPackage - ", error, file=sys.stderr)
    27     return hasPackage(packageName)
    28 
    29 def hasPackage(packageName):
    30     '''
    31     Checks if system has package installed
    32     :param packageName: name of the package
    33     :return: True if system has package installed and False otherwise
    34     '''
    35     command = "dpkg -s " + packageName
    36     proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    37     print("DEBUG: hasPackage - ", proc.stderr.read(), file=sys.stderr)
    38     try:
    39         subprocess.check_output("grep Status", shell=True, stdin=proc.stdout, stderr=sys.stdout)
    40     except subprocess.CalledProcessError as error:
    41         print("DEBUG: hasPackage - ", error.output, file=sys.stderr)
    42         return False
    43     else:
    44         return True
    45 
    46 # Tests for package installation module
    47 if installPackage(PAMPKCS11_PACKAGE):
    48     print("Package", PAMPKCS11_PACKAGE, "is installed")
    49 if installPackage("hello1"):
    50     print("Package", "hello1", "is installed")
    51 else:
    52     print("Failed to install package", "hello1")
    53 
    54 # TODO Create directories to support configuration files
     43    command = INSTALL_COMMAND + packageName
     44    output = open(os.devnull, "wb")
     45    executeCommand(command, output)
    5546
    5647
    57 # TODO Copy certificate files to cacerts directory
     48def makeDirectory(directoryPath):
     49    '''
     50    Creates a new directory
     51    :param directoryPath: path of the new directory
     52    :return: no return value
     53    '''
     54    try:
     55        os.mkdir(directoryPath)
     56        printDebugInfo("created directory with path "+directoryPath)
     57    except OSError as error:
     58        printDebugInfo(error)
    5859
    5960
    60 # TODO Get crl file from web link
     61def copyFileToDirectory(filePath, directoryPath):
     62    '''
     63    Copy a file to a directory
     64    :param filePath: path of the file to copy
     65    :param directoryPath: path of the directory
     66    :return: no return value
     67    '''
     68    try:
     69        shutil.copy2(filePath, directoryPath)
     70    except FileNotFoundError as error:
     71        printDebugInfo(error)
     72
     73
     74def makeHashLinks(directoryPath):
     75    '''
     76    Make pkcs11 hash links in a directory
     77    :param directoryPath: path of directory
     78    :return: no return value
     79    '''
     80    currentDirectoryPath = os.getcwd()
     81    os.chdir(directoryPath)
     82    executeCommand(HASHLINK_COMMAND)
     83    os.chdir(currentDirectoryPath)
     84
     85
     86def getCRLFile(crlWebLink, directoryPath):
     87    '''
     88    Download crl file into a directory
     89    :param crlWebLink: link of the crl file
     90    :param directoryPath: path of the directory
     91    :return: no return value
     92    '''
     93    currentDirectoryPath = os.getcwd()
     94    os.chdir(directoryPath)
     95    executeCommand(WGET_COMMAND+crlWebLink)
     96    os.chdir(currentDirectoryPath)
    6197
    6298
Note: See TracChangeset for help on using the changeset viewer.