nextcloud-rpm/utils/get-composer-requires.py

110 lines
3.5 KiB
Python
Raw Normal View History

2020-12-30 11:03:33 +01:00
#!/bin/env python
import os
import json
import re
import subprocess
2020-12-30 14:08:09 +01:00
from colorama import Fore, Style
2020-12-30 11:03:33 +01:00
defaultconstraint = 2
2020-12-30 13:54:10 +01:00
excludes = ('php')
2020-12-30 14:08:09 +01:00
basedir = '..'
srcdir = os.path.join(basedir, 'nextcloud')
outdir = basedir
jsonfiles = ('3rdparty/composer.json', 'apps/files_external/3rdparty/composer.json')
2020-12-30 11:03:33 +01:00
constraintmap = ('any', 'loose', 'strict', 'exact')
2020-12-30 14:08:09 +01:00
def eval_version(version, constraint=3):
version = version.replace('v', '')
if version == '*':
constraint = 0
version = '0'
elif version.startswith('^'):
constraint = 1
version = version.strip('^')
elif version.startswith('~'):
constraint = 2
version = version.strip('~')
if re.search('[^.0-9]', version):
print('Unparseable version:', version)
version = '0.0.0'
constraint = 3
return version, constraint
2020-12-30 11:03:33 +01:00
def get_requires(name, version, constraint):
2020-12-30 14:08:09 +01:00
splitver = version.split('.')
2020-12-30 11:03:33 +01:00
if constraint == 0:
requires = 'php-composer(' + name + ')'
elif constraint == 1:
wipe = False
2020-12-30 14:08:09 +01:00
for i in range(0, len(splitver)):
if splitver[i] == '0' or wipe:
splitver[i] = '0'
2020-12-30 11:03:33 +01:00
else:
2020-12-30 14:08:09 +01:00
splitver[i] = str(int(splitver[i]) + 1)
2020-12-30 11:03:33 +01:00
wipe = True
2020-12-30 14:08:09 +01:00
requires = '(php-composer(' + name + ') >= ' + version + ' with php-composer(' + name + ') < ' + '.'.join(splitver) + ')'
2020-12-30 11:03:33 +01:00
elif constraint == 2:
2020-12-30 14:08:09 +01:00
splitver[-1] = '0'
splitver[-2] = str(int(splitver[-2]) + 1)
requires = '(php-composer(' + name + ') >= ' + version + ' with php-composer(' + name + ') < ' + '.'.join(splitver) + ')'
2020-12-30 11:03:33 +01:00
else:
2020-12-30 14:08:09 +01:00
requires = 'php-composer(' + name + ') = ' + version
2020-12-30 11:03:33 +01:00
return requires
def repoquery(query):
stdout = subprocess.run(['dnf', 'repoquery', '-q', '--whatprovides', requires], stdout=subprocess.PIPE, universal_newlines=True, check = True).stdout
return stdout
2020-12-30 14:08:09 +01:00
requirefile = open(os.path.join(outdir, 'require.pkgtmp'), 'w')
requirefile.write('# PHP composer dependencies\n')
providefile = open(os.path.join(outdir, 'provide.pkgtmp'), 'w')
providefile.write('# Bundled libraries\n')
for file in jsonfiles:
requirefile.write(f"# From {file}\n")
providefile.write(f"# From {file}\n")
with open(os.path.join(srcdir, file)) as f:
jsondata = json.load(f)
packages = jsondata['require'].items()
2020-12-30 11:03:33 +01:00
2020-12-30 14:08:09 +01:00
print(f"\n{Style.BRIGHT}Parsing '{file}'...{Style.RESET_ALL}")
for name, verrange in packages:
2020-12-30 13:54:10 +01:00
if name in excludes:
2020-12-30 14:08:09 +01:00
print(f"{name} {verrange} -> in exclude list, skipping.")
2020-12-30 11:03:33 +01:00
continue
2020-12-30 14:08:09 +01:00
version, constraint = eval_version(verrange, defaultconstraint)
2020-12-30 11:03:33 +01:00
requires = get_requires(name, version, constraint)
package = repoquery(requires)
2020-12-30 14:08:09 +01:00
color = Fore.GREEN
found = True
2020-12-30 11:03:33 +01:00
while len(package) == 0:
2020-12-30 14:08:09 +01:00
color = Fore.YELLOW
2020-12-30 11:03:33 +01:00
constraint -= 1
if constraint < 0:
2020-12-30 14:08:09 +01:00
constraint = 0
color = Fore.RED
package = 'none\n'
found = False
2020-12-30 11:03:33 +01:00
break
requires = get_requires(name, version, constraint)
package = repoquery(requires)
2020-12-30 14:08:09 +01:00
print(f"{name} {verrange} -> {Style.BRIGHT}{color}{package.rstrip()}{Style.RESET_ALL} ({constraintmap[constraint]})")
if found:
requirefile.write(f"Requires: {requires}\n")
2020-12-30 11:03:33 +01:00
else:
2020-12-30 14:08:09 +01:00
providefile.write(f"Provides: bundled(php-composer({name})) = {version}\n")