Skip to content

Instantly share code, notes, and snippets.

@M4rtinK
Created January 5, 2017 16:38
Show Gist options
  • Select an option

  • Save M4rtinK/4e51b096ecc67dc7adc49d0317830be9 to your computer and use it in GitHub Desktop.

Select an option

Save M4rtinK/4e51b096ecc67dc7adc49d0317830be9 to your computer and use it in GitHub Desktop.
#!/bin/python3
import os
from os.path import expanduser
import requests
import rpm
import shutil
SPEC_FILE_URL = "https://raw.githubusercontent.com/M4rtinK/modrana/master/packaging/modrana.spec"
HOME = expanduser("~")
PROFILE_PATH = os.path.join(HOME, ".config/modrana_dgroc")
TMP_SPEC = os.path.join(PROFILE_PATH, "modrana.spec_temp")
CURRENT_SPEC = os.path.join(PROFILE_PATH, "modrana.spec")
if not os.path.exists(PROFILE_PATH):
os.makedirs(PROFILE_PATH)
def download_file(url, local_filename):
print("Downloading URL: %s" % url)
print("Into file: %s" % local_filename)
r = requests.get(url)
f = open(local_filename, 'wb')
for chunk in r.iter_content(chunk_size=512 * 1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.close()
return
# delete previous file (if any)
if os.path.exists(TMP_SPEC):
os.remove(TMP_SPEC)
# download the spec file
download_file(SPEC_FILE_URL, TMP_SPEC)
if os.path.exists(CURRENT_SPEC):
# get headers from the spec files
git_spec = rpm.spec(TMP_SPEC)
current_spec = rpm.spec(CURRENT_SPEC)
git_spec_hdr = git_spec.packages[0].header
current_spec_hdr = current_spec.packages[0].header
rc = rpm.versionCompare(git_spec_hdr, current_spec_hdr)
if rc > 0:
# the spec file from git has a higher version number - update the currently used specfile
print("Specfile from Git has higher version - replacing current specfile.")
shutil.move(TMP_SPEC, CURRENT_SPEC)
else:
print("Current specfile has higher version that spec file from Git.")
else:
# not yet any current spec file - create it
shutil.move(TMP_SPEC, CURRENT_SPEC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment