Skip to content

Instantly share code, notes, and snippets.

@matejcik
Created August 18, 2017 14:03
Show Gist options
  • Save matejcik/99c0180e7d7614a0601db2b993f013f6 to your computer and use it in GitHub Desktop.
Save matejcik/99c0180e7d7614a0601db2b993f013f6 to your computer and use it in GitHub Desktop.
osclib toys
#!/usr/bin/python3
# Download all spec files for Factory (or specified project)
#
# The bulk of the code is parralelism through concurrent.futures,
# counting sizes for the progress bar, etc.
# The meat of it is the api.download() call at line 38
import sys, os
import concurrent.futures
from osclib.api import default_api
DISTRO = "openSUSE:Factory"
api = default_api()
try:
DISTRO = sys.argv[1]
except:
pass
x = api.get_xml(['source', DISTRO])
packages = [node.get('name') for node in x.findall('entry')]
print("found {} packages in {}".format(len(packages), DISTRO))
try:
os.mkdir(DISTRO)
except:
pass
quit = False
def download(package):
if quit:
return (package, None)
try:
api.download(['source', DISTRO, package, package + '.spec'],
query={'rev': 'latest', 'expand': 1},
directory=DISTRO)
return (package, True)
except HTTPError as e:
print(package, e.msg)
return (package, False)
ok = 0
failed = 0
total = len(packages)
MAXLEN = 15
def progbar(ok, failed, total):
done = ok + failed
perc = done/total * 100
h = int(done/total * MAXLEN)
d = MAXLEN - h
return "[{h}{d}] {done}/{total} packages ({perc:.1f}%)".format(
h='#' * h, d= '.' * d, done=done, total=total, perc=perc
)
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as exe:
try:
futures = [exe.submit(download, package) for package in packages]
for future in concurrent.futures.as_completed(futures):
if quit:
break
try:
pck, is_ok = future.result()
if is_ok:
ok += 1
prog = progbar(ok, failed, total)
else:
failed += 1
prog = progbar(ok, failed, total)
print("\r" + prog + " ", end="", flush=True)
except Exception as e:
print("future failed", e)
except KeyboardInterrupt:
print("Shutting down...")
quit = True
print()
print("done")
print("{} packages ok, {} packages failed".format(ok, failed))
#!/usr/bin/python3
# Branch all packages and upload the spec files as found in current directory
#
# This script is much dumber than the previous one. It also has a particular
# problem: it doesn't fork directly from Factory, but from the respective devel
# project, without checking if the devel version is identical.
from osclib.api import default_api
from osclib import package
from glob import glob
api = default_api()
PROJECT = "openSUSE:Factory"
BRANCHPRJ = "home:matejcik:singlespec-staging"
for spec in glob("*.spec"):
print(spec)
name = spec[:-5]
try:
x = api.get_xml(["source", PROJECT, name])
linkinfo = x.find("linkinfo")
if linkinfo is not None:
target = linkinfo.get("package")
print("{} is a link to {}".format(name, target))
name = target
except:
print("failed to check {}: {}".format(name, PROJECT))
continue
pkg = (BRANCHPRJ, name)
try:
x = api.get_xml(["source", *pkg])
print("{1} already exists in {0}, not branching".format(*pkg))
except:
package.branch(api, (PROJECT, name), pkg)
package.commit(api, pkg, [spec], "matejcik", "automation")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment