Scripting automatic downloading of missing libraries

jelle
2020-07-02
2020-07-14
  • jelle - 2020-07-02

    I have been trying to automatically resolve missing libraries on our build PC.

    First I searched the forum and found some old topics that are relevant to automate CoDeSys prompts:
    - https://forge.codesys.com/forge/talk/Engineering/thread/cb4c263657/
    - https://forge.codesys.com/forge/talk/Engineering/thread/bf6c8291a5/

    The following is now implemented as Python script that runs in CoDeSys V3.5 SP16:

    system.prompt_handling |= PromptHandling.LogMessageKeys
    system.prompt_handling &= ~PromptHandling.ForwardSimplePrompts
    
    system.prompt_answers["DownloadMissingLibrariesDialog_Prompt"]=PromptResult.OK
    system.commands["libman","opendownloadmissinglibrariesdialog"].execute()
    

    I expect now that I can automate the "Download" button in the prompted window. But it does not work and also the prompt message key is not logged.

    I am now completely stuck. Can anyone please help.

     

    Last edit: jelle 2020-07-02
  • bhess - 2020-07-02

    I recommend saving your project as an archive before opening on the build PC, as this will save every device description and library required to build it so no downloading is needed.

     

    Last edit: bhess 2020-07-02
    • jelle - 2020-07-03

      Good advice as workaround but I am searching for an answer to my scripting problem.

       

      Last edit: jelle 2020-07-03
  • jelle - 2020-07-13

    I solved the issue by creating 3 scripts:
    1. script that lists all library dependencies
    2. script in Python 3 that downloads the missing libraries
    3. script that install the missing libraries

     
  • aliazzz

    aliazzz - 2020-07-13

    Hi, would you be so kind to share the scripts with us?
    That would be very nice if you did! ;-)

     
  • jelle - 2020-07-14

    Some snippets that are interesting :)

    Script that lists all the library dependencies:

    proj = projects.open(projectPath)
    objects = proj.get_children(recursive=True)
        for candidate in objects:
            if candidate.is_libman:
                for libref in iter(candidate):
                    if libref.is_placeholder:
                        if libref.effective_resolution is not None:
                            libInfo = str(libref.effective_resolution)
                            libMissing = False
                        elif libref.default_resolution is not None:
                            libInfo = str(libref.default_resolution)
                            libMissing = True
                    elif libref.is_managed:   
                        libInfo = str(libref.managed_library)
                    else:               
                        libInfo = str(libref.name)            
    

    Script that downloads the libraries:

    libraryServers  = ["https://store.codesys.com/CODESYSLibs/"]
                for server in libraryServers:
                    libraryFileName = None
                    url = str("%s%s/%s/%s/" % (server, data['supplier'], data['name'], data['version']))
                    response = requests.get(url + "index")
                fileUrl = str("%s%s" % (url, libraryFileName)).rstrip()
                response = requests.get(fileUrl)
                f.write(response.content)
    

    Script that installs the downloaded libraries:

    repo = librarymanager.repositories[0]
    librarymanager.install_library(absLibPath, repo, overwrite=True)
    
     
    πŸ‘
    3
    • aliazzz

      aliazzz - 2020-07-14

      Why don't you submit these under tools, scripts, that way they are easy to find for any interested user, I hope you understand and many thanks for sharing them with us.

      Ps have you got a solution for generating a project archive in headless Codesys mode too? In the version I am using it does not work.

       
      • jelle - 2020-07-14

        Do you have a link? I am not aware that there is a central place to share scripts

         
        • aliazzz

          aliazzz - 2020-07-14

          In the upper-right there is a Hamburger Menu button.
          See the attached .png for hints

           

Log in to post a comment.