generate_rpm_spec.py 10.5 KB
Newer Older
Victor Yacovlev's avatar
Victor Yacovlev committed
1
import functools
Victor Yacovlev's avatar
Victor Yacovlev committed
2 3 4 5
import os
import sys
import json

Victor Yacovlev's avatar
Victor Yacovlev committed
6
import query_version_info
Victor Yacovlev's avatar
Victor Yacovlev committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20

TEMPLATE = """# norootforbuild
Name:       $name
Summary:    Kumir education system $unstableNotice
License:    GPL2+
Version:    $version
Group:      $rpmGroup
Release:    $release
BuildRoot:	%_tmppath/%name-%version
Vendor:		NIISI RAS
URL:    	http://www.niisi.ru/kumir/
Packager:	$packager
Source:     $source
Requires:   $kumirLibsPackageName = %version
Victor Yacovlev's avatar
Victor Yacovlev committed
21
Requires:   /usr/bin/gtk-update-icon-cache
Victor Yacovlev's avatar
Victor Yacovlev committed
22 23
Requires:   /usr/bin/xdg-mime
Conflicts:  $mainPackageConflictName
Victor Yacovlev's avatar
Victor Yacovlev committed
24
$buildRequiresList
Victor Yacovlev's avatar
Victor Yacovlev committed
25 26 27 28 29 30

%description
Kumir education system for schools 
$unstableNotice

%prep
Victor Yacovlev's avatar
Victor Yacovlev committed
31
%setup -q -n $archivePrefix
Victor Yacovlev's avatar
Victor Yacovlev committed
32 33

%build
Victor Yacovlev's avatar
Victor Yacovlev committed
34 35 36
$removeBundledBoostIfNeed
$cmakeCommand \
    -DUSE_QT=5 \
37
    -DKUMIR2_DISABLED_SUBDIRS="$disabledSubdirs" \
38 39
    -DKUMIR2_SDK_CMAKE_DIR=$cmakeFilesLocation \
    -DKUMIR2_SDK_SCRIPTS_DIR=$develScriptsLocation \
Victor Yacovlev's avatar
Victor Yacovlev committed
40 41 42 43 44 45 46
    -DPROVIDED_VERSION_INFO:BOOL=ON \
    -DGIT_TAG="$gitTag" \
    -DGIT_BRANCH="$gitBranch" \
    -DGIT_HASH="$gitHash" \
    -DGIT_TIMESTAMP="$gitTimestamp" \
    ../
$makeCommand
Victor Yacovlev's avatar
Victor Yacovlev committed
47 48

%install
Victor Yacovlev's avatar
Victor Yacovlev committed
49
$makeInstallCommand
Victor Yacovlev's avatar
Victor Yacovlev committed
50

51 52

$cleanSection
Victor Yacovlev's avatar
Victor Yacovlev committed
53

Victor Yacovlev's avatar
Victor Yacovlev committed
54 55
%files
$mainFilesPrefs
Victor Yacovlev's avatar
Victor Yacovlev committed
56 57 58 59 60 61 62 63 64 65 66 67 68
$binDir/kumir2-ide
$binDir/kumir2-classic
$binDir/kumir2-highgrade
$binDir/kumir2-teacher
$binDir/kumir2-open
$binDir/kumir2-courseeditor
$dataDir/applications/kumir2-ide.desktop
$dataDir/icons/hicolor/*/apps/kumir2-ide.*
$dataDir/applications/kumir2-classic.desktop
$dataDir/icons/hicolor/*/apps/kumir2-classic.*
$dataDir/applications/kumir2-highgrade.desktop
$dataDir/icons/hicolor/*/apps/kumir2-highgrade.*
$dataDir/applications/kumir2-teacher.desktop
69 70 71
$dataDir/icons/hicolor/*/apps/kumir2-teacher.*
$dataDir/icons/hicolor/*/mimetypes/*
$dataDir/mime/packages/kumir2-mimetypes.xml
Victor Yacovlev's avatar
Victor Yacovlev committed
72 73


Victor Yacovlev's avatar
Victor Yacovlev committed
74
%post
Victor Yacovlev's avatar
Victor Yacovlev committed
75
/bin/touch --no-create $dataDir/icons/hicolor/ &>/dev/null || :
Victor Yacovlev's avatar
Victor Yacovlev committed
76
/usr/bin/xdg-mime install $dataDir/mime/packages/kumir2-mimetypes.xml || true
Victor Yacovlev's avatar
Victor Yacovlev committed
77

Victor Yacovlev's avatar
Victor Yacovlev committed
78
%postun
Victor Yacovlev's avatar
Victor Yacovlev committed
79 80 81 82 83
if [ $1 -eq 0 ] ; then
  /bin/touch --no-create $dataDir/icons/hicolor/ &>/dev/null
  /usr/bin/gtk-update-icon-cache -f $dataDir/icons/hicolor/ &>/dev/null || :
fi

Victor Yacovlev's avatar
Victor Yacovlev committed
84
%posttrans
Victor Yacovlev's avatar
Victor Yacovlev committed
85 86 87
/usr/bin/gtk-update-icon-cache -f $dataDir/icons/hicolor &>/dev/null || :

%package $kumirLibsPackageFullName
Victor Yacovlev's avatar
Victor Yacovlev committed
88 89 90
Summary:        Common files for all Kumir-2 components
Conflicts:      $libsPackageConflictName
$requiresList
Victor Yacovlev's avatar
Victor Yacovlev committed
91 92 93

%description $kumirLibsPackageFullName
Common files for Kumir-2 platform
Victor Yacovlev's avatar
Victor Yacovlev committed
94
$unstableNotice
Victor Yacovlev's avatar
Victor Yacovlev committed
95

Victor Yacovlev's avatar
Victor Yacovlev committed
96 97
%files $kumirLibsPackageFullName
$libsFilesPrefs
Victor Yacovlev's avatar
Victor Yacovlev committed
98 99 100 101 102 103 104 105
$libDir/kumir2/*.so
$libDir/kumir2/plugins/*.so
$dataDir/kumir2/*

%post $kumirLibsPackageFullName -p /sbin/ldconfig
%postun $kumirLibsPackageFullName -p /sbin/ldconfig

%package $kumirDevelPackageFullName
Victor Yacovlev's avatar
Victor Yacovlev committed
106 107 108 109
Summary:        Kumir-2 development files
Requires:       $kumirLibsPackageName = %version
Conflicts:      $develPackageConflictName
$buildRequiresAsRequiresList
Victor Yacovlev's avatar
Victor Yacovlev committed
110 111 112

%description $kumirDevelPackageFullName
Development files to build new Actors and plugins for Kumir-2
Victor Yacovlev's avatar
Victor Yacovlev committed
113
$unstableNotice
Victor Yacovlev's avatar
Victor Yacovlev committed
114

Victor Yacovlev's avatar
Victor Yacovlev committed
115 116
%files $kumirDevelPackageFullName
$develFilesPrefs
Victor Yacovlev's avatar
Victor Yacovlev committed
117 118 119 120 121 122 123
$includesDir/kumir2/*
$includesDir/kumir2-libs/*
$cmakeFilesLocation/*
$develScriptsLocation/gen_actor_source.py


%package $kumirToolsPackageFullName
Victor Yacovlev's avatar
Victor Yacovlev committed
124 125 126
Summary:        Kumir-2 non-GUI tools
Requires:       $kumirLibsPackageName = %version
Conflicts:      $toolsPackageConflictName
Victor Yacovlev's avatar
Victor Yacovlev committed
127 128 129

%description $kumirToolsPackageFullName
Kumir-2 tools for using in non-GUI mode
Victor Yacovlev's avatar
Victor Yacovlev committed
130
$unstableNotice
Victor Yacovlev's avatar
Victor Yacovlev committed
131 132

%files $kumirToolsPackageFullName
Victor Yacovlev's avatar
Victor Yacovlev committed
133
$toolsFilesPrefs
Victor Yacovlev's avatar
Victor Yacovlev committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147
$binDir/kumir2-run
$binDir/kumir2-xrun
$binDir/kumir2-bc
$binDir/docbook2latex
$binDir/pictomir2course

%changelog
* Fri Apr 21 2017 - Kumir Team <kumir@lpm.org.ru>
- TODO: generate changelog from git log

"""


def render(template: str, substitutions: dict):
Victor Yacovlev's avatar
Victor Yacovlev committed
148 149 150 151 152
    keys = substitutions.keys()
    sorted_keys = sorted(keys, key=len, reverse=True)
    for key in sorted_keys:
        value = substitutions[key]
        template = template.replace("$"+key, value)
Victor Yacovlev's avatar
Victor Yacovlev committed
153 154 155 156 157 158 159 160
    return template


def read_conventions(dist: str):
    file_name = "rpm-conventions.{}.json".format(dist.lower())
    if not os.path.exists(file_name):
        sys.stderr.write("Warning: No RPM conventions for {}. Using openSUSE\n".format(dist))
        file_name = "rpm-conventions.opensuse.json"
Victor Yacovlev's avatar
Victor Yacovlev committed
161 162
    with open(file_name, "r") as f:
        return json.load(f)
Victor Yacovlev's avatar
Victor Yacovlev committed
163 164 165 166


def generate_rpm_spec_contents(dist: str, release: int, packager: str):
    conventions = read_conventions(dist)
Victor Yacovlev's avatar
Victor Yacovlev committed
167 168 169 170 171 172 173 174 175 176

    lib_prefix = conventions["commonUsedLibsPackage"]["name"]["prefix"]
    lib_suffix = conventions["commonUsedLibsPackage"]["name"]["suffix"]
    dev_prefix = conventions["develPackage"]["name"]["prefix"]
    dev_suffix = conventions["develPackage"]["name"]["suffix"]
    tools_prefix = conventions["toolsPackage"]["name"]["prefix"]
    tools_suffix = conventions["toolsPackage"]["name"]["suffix"]

    unstable_suffix = conventions["unstablePackageSuffix"]

Victor Yacovlev's avatar
Victor Yacovlev committed
177 178
    subst = {}
    version_info = query_version_info.get_version_information(os.getcwd())
Victor Yacovlev's avatar
Victor Yacovlev committed
179 180
    timestamp = query_version_info.get_timestamp(os.getcwd())
    subst["gitTimestamp"] = str(timestamp)
181 182
    disabled_subdirs = query_version_info.disabled_modules()
    subst["disabledSubdirs"] = ";".join(disabled_subdirs)
Victor Yacovlev's avatar
Victor Yacovlev committed
183 184 185
    if version_info["taggedRelease"]:
        subst["name"] = "kumir2"
        subst["version"] = version_info["version"]
Victor Yacovlev's avatar
Victor Yacovlev committed
186 187 188 189 190 191 192 193 194
        subst["unstableNotice"] = ""
        subst["mainPackageConflictName"] = "kumir2" + unstable_suffix
        subst["libsPackageConflictName"] = lib_prefix + "kumir2" + unstable_suffix + lib_suffix
        subst["develPackageConflictName"] = dev_prefix + "kumir2" + unstable_suffix + dev_suffix
        subst["toolsPackageConflictName"] = tools_prefix + "kumir2" + unstable_suffix + tools_suffix
        subst["archivePrefix"] = "kumir2-" + version_info["version"]
        subst["gitTag"] = version_info["version"]
        subst["gitHash"] = "unknown"
        subst["gitBranch"] = "unknown"
Victor Yacovlev's avatar
Victor Yacovlev committed
195
    else:
Victor Yacovlev's avatar
Victor Yacovlev committed
196
        subst["name"] = "kumir2" + unstable_suffix
Victor Yacovlev's avatar
Victor Yacovlev committed
197
        subst["version"] = version_info["date"]
Victor Yacovlev's avatar
Victor Yacovlev committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        subst["unstableNotice"] = "(Nightly *unstable* builds)"
        subst["mainPackageConflictName"] = "kumir2"
        subst["libsPackageConflictName"] = lib_prefix + "kumir2" + lib_suffix
        subst["develPackageConflictName"] = dev_prefix + "kumir2" + dev_suffix
        subst["toolsPackageConflictName"] = tools_prefix + "kumir2" + tools_suffix
        subst["archivePrefix"] = "kumir2-" + version_info["branch"] + "-"+ version_info["hash"] + "-" + version_info["date"]
        subst["gitTag"] = "unknown"
        subst["gitHash"] = version_info["hash"]
        subst["gitBranch"] = version_info["branch"]
    subst["kumirLibsPackageName"] = lib_prefix + subst["name"] + lib_suffix
    if lib_prefix:
        subst["kumirLibsPackageFullName"] = "-n " + subst["kumirLibsPackageName"]
    else:
        subst["kumirLibsPackageFullName"] = lib_suffix[1:]
    if dev_prefix:
        subst["kumirDevelPackageFullName"] = "-n " + dev_prefix + subst["name"] + dev_suffix
    else:
        subst["kumirDevelPackageFullName"] = dev_suffix[1:]
    if tools_prefix:
        subst["kumirToolsPackageFullName"] = "-n " + tools_prefix + subst["name"] + tools_suffix
    else:
        subst["kumirToolsPackageFullName"] = tools_suffix[1:]

Victor Yacovlev's avatar
Victor Yacovlev committed
221
    subst["release"] = str(release)
Victor Yacovlev's avatar
Victor Yacovlev committed
222
    subst["packager"] = packager
Victor Yacovlev's avatar
Victor Yacovlev committed
223
    subst["rpmGroup"] = conventions["rpmGroup"]
Victor Yacovlev's avatar
Victor Yacovlev committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    subst["source"] = query_version_info.source_file_name("kumir2-", ".tar.gz")
    subst["cmakeCommand"] = conventions["cmakeCommand"]
    subst["makeCommand"] = conventions["makeCommand"]
    subst["makeInstallCommand"] = conventions["makeInstallCommand"]
    subst["rpmBuildRoot"] = conventions["rpmBuildRoot"]
    subst["installPrefix"] = conventions["installPrefix"]
    subst["binDir"] = conventions["binDir"]
    subst["libDir"] = conventions["libDir"]
    subst["dataDir"] = conventions["dataDir"]
    subst["includesDir"] = conventions["includesDir"]
    subst["rpmBuildRoot"] = conventions["rpmBuildRoot"]

    subst["mainFilesPrefs"] = conventions["defAttr"]
    subst["libsFilesPrefs"] = conventions["defAttr"]
    subst["develFilesPrefs"] = conventions["defAttr"]
    subst["toolsFilesPrefs"] = conventions["defAttr"]

    if not conventions["useBundledBoost"]:
        subst["removeBundledBoostIfNeed"] = "rm -rf src/3rdparty/boost*"
    else:
        subst["removeBundledBoostIfNeed"] = ""

246 247 248 249 250
    if not conventions["skipCleanSection"]:
        subst["cleanSection"] = "%clean\nrm -rf $rpmBuildRoot\n"
    else:
        subst["cleanSection"] = ""

Victor Yacovlev's avatar
Victor Yacovlev committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    if conventions["dirsOwnership"]:
        subst["mainFilesPrefs"] += "\n%dir $binDir"
        subst["mainFilesPrefs"] += "\n%dir $dataDir/applications"
        subst["mainFilesPrefs"] += "\n%dir $dataDir/icons/hicolor/*/apps"
        subst["mainFilesPrefs"] += "\n%dir $dataDir/mime/packages"
        subst["libsFilesPrefs"] += "\n%dir $libDir/kumir2"
        subst["libsFilesPrefs"] += "\n%dir $libDir/kumir2/plugins"
        subst["libsFilesPrefs"] += "\n%dir $dataDir/kumir2"
        subst["develFilesPrefs"] += "\n%dir $includesDir/kumir2"
        subst["develFilesPrefs"] += "\n%dir $includesDir/kumir2-libs"
        subst["develFilesPrefs"] += "\n%dir $cmakeFilesLocation"
        subst["develFilesPrefs"] += "\n%dir $develScriptsLocation"
        subst["toolsFilesPrefs"] += "\n%dir $binDir"

    subst["requiresList"] = "\n".join(["Requires:\t"+entry for entry in conventions["requires"]])
    subst["buildRequiresList"] = "\n".join(["BuildRequires:\t" + entry for entry in conventions["buildRequires"]])
    subst["buildRequiresAsRequiresList"] = "\n".join(["Requires:\t" + entry for entry in conventions["buildRequires"]])
    subst["cmakeFilesLocation"] = conventions["develPackage"]["cmakeFilesLocation"]
    subst["develScriptsLocation"] = conventions["develPackage"]["develScriptsLocation"]

    data = TEMPLATE
    for i in range(0, 3):
        data = render(data, subst)
    return data
Victor Yacovlev's avatar
Victor Yacovlev committed
275 276 277 278 279


def main():
    out_file = sys.stdout
    conventions = "openSUSE"
Victor Yacovlev's avatar
Victor Yacovlev committed
280
    release = 1
Victor Yacovlev's avatar
Victor Yacovlev committed
281
    out_file_name = None
Victor Yacovlev's avatar
Victor Yacovlev committed
282
    packager = "Anonymous <anon@example.com>"
Victor Yacovlev's avatar
Victor Yacovlev committed
283 284 285 286 287
    for arg in sys.argv:
        if arg.startswith("--dist="):
            conventions = arg[7:]
        elif arg.startswith("--out="):
            out_file_name = arg[6:]
Victor Yacovlev's avatar
Victor Yacovlev committed
288 289 290 291 292 293 294 295
        elif arg.startswith("--release="):
            release = int(arg[10:])
        elif arg.startswith("--packager="):
            packager = arg[11:]
            if packager.startswith('"') and packager.endswith('"'):
                packager = packager[1:-1]
            if packager.startswith("'") and packager.endswith("'"):
                packager = packager[1:-1]
Victor Yacovlev's avatar
Victor Yacovlev committed
296 297
    if out_file_name:
        out_file = open(out_file_name, "w")
Victor Yacovlev's avatar
Victor Yacovlev committed
298
    data = generate_rpm_spec_contents(conventions, release, packager)
Victor Yacovlev's avatar
Victor Yacovlev committed
299 300 301 302 303 304
    out_file.write(data)
    out_file.close()


if __name__ == "__main__":
    main()