Compare two projects using Python Script

2020-01-03
2020-10-15
  • hpulletikurti3 - 2020-01-03

    Hi,

    I am trying to use python scripting to compare two Codesys projects. Can someone please help me how to achieve this ? I am very new to python scripting. I am using Codesys V3.

    Thanks,
    Haritha

     
  • mkeller - 2020-01-09

    Hi Haritha.

    hpulletikurti3 hat geschrieben:
    I am trying to use python scripting to compare two Codesys projects. Can someone please help me how to achieve this ? I am very new to python scripting. I am using Codesys V3.

    Here is a simple example:

    from __future__ import print_function
    import string
    def get_name(obj):
        if obj is None:
            return '<None>'
        return obj.get_name(False)
    def get_object(change):
        if change.left_object is None:
            return change.right_object
        else:
            return change.left_object
    project1 = projects.primary
    project2 = projects.open(r"c:\temp\Project-B.project", primary=False)
    \# project compare which ignores whitespace differences
    diff = project1.compare_to(project2, ComparisonFlags.IGNORE_WHITESPACE)
    for change in diff:
        obj = get_object(change)
        name = get_name(obj)
        print("Object %s: %s" % (name, str(change.differences)))
        
    project2.close()
    

    BR
    Martin

     
  • hpulletikurti3 - 2020-01-09

    Thanks a million for this help. This is all I need.

    -Haritha

     
  • hpulletikurti3 - 2020-01-09

    Hi Martin,

    I tried the above code. Got the change.differences printed.

    Is there a way that I can generate the compare report using this i.e the report that gets generated when we select Project Menu > Compare...

    I tried to use system.commands["project","compare"].execute(r"C:\TEMP\codesys\version3\Data\Test2.project")
    but I am getting the error : wrong argument type for the batch command "project compare": Type "FILE/SC" expected for the argument 0

    Can you please help me solve this?

    Thanks,
    Haritha

     
  • mkeller - 2020-01-15

    Hi Haritha.

    hpulletikurti3 hat geschrieben:
    Is there a way that I can generate the compare report using this i.e the report that gets generated when we select Project Menu > Compare...

    Do you want just some form of report or do you want to do depending of the compare result? What is your use case?

    hpulletikurti3 hat geschrieben:
    I tried to use system.commands["project","compare"].execute(r"C:\TEMP\codesys\version3\Data\Test2.project")
    but I am getting the error : wrong argument type for the batch command "project compare": Type "FILE/SC" expected for the argument 0

    Executing commands should only be used if the feature is missing in the Scripting which is not the case here. Please use the Scripting API for project compare.

    BR
    Martin

     
  • hpulletikurti3 - 2020-01-22

    Hi Martin,

    I am trying to generate a compare report that Codesys generate using Menu > Project> Compare > Report is attached

    Can you please help me how I can get this using the API.

    Below is my code.

    from future import print_function
    import os
    import scriptengine
    from scriptengine import projects
    import string

    scriptdir = os.path.dirname(file) # Directory of our script.
    print(scriptdir);
    datadir = os.path.join(scriptdir, "Data") # Enter the subdirectory.
    print(datadir);
    projectpathone = os.path.join(datadir, "Test1.project") # And add the project name.
    print(projectpathone);
    projectpathtwo = os.path.join(datadir,"Test2.project") # second project for comparision
    print(projectpathtwo);

    def get_name(obj):
    if obj is None:
    return '<none>'
    return obj.get_name(False)</none>

    def get_object(change):
    if change.left_object is None:
    return change.right_object
    else:
    return change.left_object

    # Now, we can open the project.
    proj = projects.open(projectpathone)
    # We fetch the active application.
    app = proj.active_application

    project1 = projects.primary
    project2 = projects.open(r"C:\TEMP\codesys\version3\Data\Test2.project", primary=False)

    # project compare which ignores whitespace differences
    diff = project1.compare_to(project2, ComparisonFlags.IGNORE_WHITESPACE)

    for change in diff:
    obj = get_object(change)
    name = get_name(obj)

    print("Object %s: %s" % (name, str(change.differences)))
    

    project2.close()

    Please see the output in ScriptReport.PNG that I am seeing in the command prompt using the above script.

    Thanks for the help.

    -Haritha

    IMG: ScriptReport.PNG

    IMG: CompareReportCapture.PNG

     
  • mkeller - 2020-01-28

    Hi Haritha.

    hpulletikurti3 hat geschrieben:
    I am trying to generate a compare report that Codesys generate using Menu > Project> Compare > Report is attached
    Can you please help me how I can get this using the API.

    I still don't understand what you want. The screenshot CompareReportCapture.PNG shows the Project Compare view with the differences of the two projects. There is no report only some statistics at the bottom of the view.

    BR
    Martin

     
  • hpulletikurti3 - 2020-02-13

    Hi Martin,

    My intension is when I run the script it should open the Codesys V3 and simulate the Project Menu > Compare ( given two projects ) and shown the report that is shown in the CompareReportCapture.png.

    I mean something like directing the mouse click to Project -> Compare -> and check the boxes for compare options click ok resulting in the report.

    I want to run the script and get the above stated report.

    Currently my script tells me whether difference of the object exists or not but does not show the actual report in the Codesys IDE.

    My question is if the API is capable of doing this kind of directing? If API is capable, what am I missing in the script?

    Hope I make some sense

    Thanks for the help!

    -Haritha

     
  • i-campbell

    i-campbell - 2020-02-14

    Maybe don't use script, just use command line switch is what you are after:
    https://help.codesys.com/webapp/_cds_co ... n=3.5.15.0
    If you figure out the --compare syntax that works for you please post back.

     
    • asivakov - 2020-10-15

      I cannot use that... for some reason, instead of comparison, codesys just starts as normal, thats all. Maybe there is more exceptions then described on help page

       

      Last edit: asivakov 2020-10-15

Log in to post a comment.