[r87]: / trunk / cforge / cforge / Package / CFORGE / Scripts / create-package.py  Maximize  Restore  History

Download this file

160 lines (142 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import sys, os
import zipfile
import cds_script
import uuid
import ui

ManifestSkeleton="""<?xml version="1.0" encoding="ISO-8859-1"?>
<Package>
  <Strings>
    <String Id="GeneralName">
      <Neutral>%s</Neutral>
    </String>
    <String Id="GeneralVendor">
      <Neutral>%s</Neutral>
    </String>
    <String Id="GeneralCopyright">
      <Neutral>%s</Neutral>
    </String>
    <String Id="GeneralDescription">
      <Neutral>%s</Neutral>
    </String>    
    <String Id="license">
      <Neutral>%s</Neutral>
    </String>        
    <String Id="TargetDir">
      <Neutral>Target directory</Neutral>
    </String>
    <String Id="TargetDirDescription">
      <Neutral>The target directory where the projects are saved.</Neutral>
    </String>
  </Strings>
  <General>
    <Id>{%s}</Id>
    <Version>1.0.0.0</Version>
    <Name>$GeneralName</Name>
    <Vendor>$GeneralVendor</Vendor>
    <Copyright>$GeneralCopyright</Copyright>
    <Description>$GeneralDescription</Description>    
    <LicenseAgreement>$license</LicenseAgreement>        
    <RequiredInstallerVersion>3.5.10.0</RequiredInstallerVersion>
  </General>
  <TargetDirectoryDefinitions>  
    <TargetDirectoryDefinition>
      <Id>1</Id>
      <Name>$TargetDir</Name>
      <Description>$TargetDirDescription</Description>
      <PromptUser>True</PromptUser>
      <DefaultValue>%%USERPROFILE%%\CODESYS Projects\</DefaultValue>
    </TargetDirectoryDefinition>
  </TargetDirectoryDefinitions>
  <Components>                
    <Component>
      <General>
    <Id>1</Id>
    <Name>Default</Name>
    <Description>Default Package</Description>
    <Selectable>false</Selectable>
    <SelectedByDefault>true</SelectedByDefault>
      </General>
      <Dependencies>            
      </Dependencies>
      <Items>
%s
      </Items>            
    </Component>     
  </Components>
</Package>
"""

ManifestItemLibrary = """
        <Library>
          <Path>%s</Path>
        </Library>
"""

ManifestItemDevDesc = """
        <DeviceDescription>
          <Path>%s</Path>
        </DeviceDescription>
"""

ManifestItemProject = """
        <File>
          <Path>%s</Path>
          <IgnoreArchiveFolder>True</IgnoreArchiveFolder>
          <TargetFolder>$1</TargetFolder>
          <Overwrite>
            <Option>Yes</Option>
          </Overwrite>
        </File>
"""

# This is a cforge command (script file)
# this will be run as ironpython script.
# the filename of this script is automatically the corresponding cforge command 
# with some magic functions you can easily integrate it nicely into cforge tool

# cforge_usage: 
# here you can return an array of all possible arguments and options that can 
# be passed to this command script
def cforge_usage():
    help = [ 
        ["<path to local repository>", "---"]
    ]

    return help


if len(sys.argv) <= 1:
    print("Oh, there are no arguments. Perhaps you forgot something?")
    sys.exit()

folder = sys.argv[1]
scriptpath = os.path.abspath(os.path.dirname(sys.argv[0]))



def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            filename=os.path.join(root, file)
            archname=filename[len(path)+1:]
            print("Adding file: %s -> %s" % (filename, archname))
            ziph.write(filename, arcname=archname)


# Discover package content
packageItems = ""
license = ""
for root, dirs, files in os.walk(folder):
    for file in files:
        filename=os.path.join(root, file)
        archname=filename[len(folder)+1:]
        if file.endswith(".project"):
            packageItems += ManifestItemProject % (archname)
        if file.endswith(".devdesc.xml"):
            packageItems += ManifestItemDevDesc % (archname)
        if file.endswith(".library"):
            packageItems += ManifestItemLibrary % (archname)
        if file == "license.txt":
            license = archname;

if license == "":
    file = open(os.path.join(folder, "license.txt"), "w")
    if file:
        file.write("Add license text...")
        file.close();
    license = "license.txt"

config = ui.Dialog("Create Package", PkgName=True, PkgVendor=True, PkgCopyright=True, PkgDescription=True)

if config != None:
    guid = uuid.uuid1();
    packageManifest = ManifestSkeleton % (config["PkgName"], config["PkgVendor"], config["PkgCopyright"], config["PkgDescription"], "LICENSE.TXT", guid, packageItems)
    file = open(os.path.join(folder, "package.manifest"), "w")
    if file:
        file.write(packageManifest)
        file.close();
    print packageManifest