improve translators:

- translator flags
  - translatedBy
  - translatorParams
This commit is contained in:
DavHau 2021-09-18 01:06:48 +01:00
parent d23fb76ef6
commit aa7902a09e
9 changed files with 65 additions and 21 deletions

View File

@ -163,9 +163,6 @@ Potery uses `pyproject.toml` and `poetry.lock` to lock dependencies
// this indicates which builder must be used // this indicates which builder must be used
"buildSystem": "python", "buildSystem": "python",
// versioning the format to ensure builder compatibility
"buildSystemFormatVersion": 1,
// translator which generated this file // translator which generated this file
// (not relevant for building) // (not relevant for building)
"producedBy": "translator-poetry-1", "producedBy": "translator-poetry-1",

View File

@ -14,9 +14,9 @@
"generic": { "generic": {
"buildSystem": "python", "buildSystem": "python",
"buildSystemFormatVersion": 1, "translatedBy": "python.pure.poetry",
"producedBy": "translator-poetry-1", "translatorParams": "--flag_application",
"rootPackage": "requests", "mainPackage": "requests",
"dependencyGraph": { "dependencyGraph": {
"requests": [ "requests": [
"certifi" "certifi"

View File

@ -74,7 +74,6 @@
"type": "object", "type": "object",
"properties": { "properties": {
"buildSystem": { "type": "string" }, "buildSystem": { "type": "string" },
"buildSystemFormatVersion": { "type": "integer" },
"producedBy": { "type": "string" }, "producedBy": { "type": "string" },
"dependencyGraph": { "dependencyGraph": {
"type": "object", "type": "object",

View File

@ -16,6 +16,11 @@ def strip_hashes_from_lock(lock):
del source['hash'] del source['hash']
def order_dict(d):
return {k: order_dict(v) if isinstance(v, dict) else v
for k, v in sorted(d.items())}
def list_translators(args): def list_translators(args):
out = "Available translators per build system" out = "Available translators per build system"
for subsystem, trans_types in translators.items(): for subsystem, trans_types in translators.items():
@ -30,11 +35,20 @@ def list_translators(args):
f"\n special args:", f"\n special args:",
) )
for argName, argData in translator.items(): for argName, argData in translator.items():
lines += ( if argData['type'] == 'argument':
f"\n --arg_{argName} {{value}}", lines += (
f"\n default: {argData['default']}", f"\n --arg_{argName} {{value}}",
f"\n examples: {', '.join(argData['examples'])}", f"\n description: {argData['description']}",
) f"\n default: {argData['default']}",
f"\n examples: {', '.join(argData['examples'])}",
)
elif argData['type'] == 'flag':
lines += (
f"\n --flag_{argName}",
f"\n description: {argData['description']}",
)
else:
raise Exception(f"Unknown type '{argData['type']}' of argument '{arg_Name}'")
displayed.append(''.join(lines)) displayed.append(''.join(lines))
nl = '\n' nl = '\n'
out += f"\n\n - {subsystem}.{f'{nl} - {subsystem}.'.join(displayed)}" out += f"\n\n - {subsystem}.{f'{nl} - {subsystem}.'.join(displayed)}"
@ -52,6 +66,8 @@ def translate(args):
for argName, argVal in vars(args).items(): for argName, argVal in vars(args).items():
if argName.startswith("arg_"): if argName.startswith("arg_"):
specialArgs[argName[4:]] = argVal specialArgs[argName[4:]] = argVal
elif argName.startswith("flag_"):
specialArgs[argName[5:]] = True
# check if all inputs exist # check if all inputs exist
for path in inputPaths: for path in inputPaths:
@ -141,8 +157,12 @@ def translate(args):
# read produced lock file # read produced lock file
with open(output) as f: with open(output) as f:
lock = json.load(f) lock = json.load(f)
# write translator information to lock file
lock['generic']['translatedBy'] = f"{subsystem}.{trans_type}.{trans_name}"
lock['generic']['translatorParams'] = " ".join(sys.argv[2:])
# calculate combined hash # calculate combined hash if --combined was specified
if args.combined: if args.combined:
print("Building FOD of combined sources to retrieve output hash") print("Building FOD of combined sources to retrieve output hash")
@ -174,7 +194,7 @@ def translate(args):
# store the hash in the lock # store the hash in the lock
lock['generic']['sourcesCombinedHash'] = hash lock['generic']['sourcesCombinedHash'] = hash
with open(output, 'w') as f: with open(output, 'w') as f:
json.dump(lock, f, indent=2) json.dump(order_dict(lock), f, indent=2)
print(f"Created {output}") print(f"Created {output}")
@ -241,6 +261,8 @@ def parse_args():
for arg in unknown: for arg in unknown:
if arg.startswith("--arg_"): if arg.startswith("--arg_"):
translate_parser.add_argument(arg.split('=')[0]) translate_parser.add_argument(arg.split('=')[0])
if arg.startswith("--flag_"):
translate_parser.add_argument(arg.split('=')[0], action='store_true')
args = parser.parse_args() args = parser.parse_args()

View File

@ -20,6 +20,7 @@ let
externals = { externals = {
npmlock2nix = pkgs.callPackage "${externalSources}/npmlock2nix/internal.nix" {}; npmlock2nix = pkgs.callPackage "${externalSources}/npmlock2nix/internal.nix" {};
}; };
in in
rec { rec {

View File

@ -171,7 +171,12 @@ let
data = { data = {
SpecialArgsDefaults = SpecialArgsDefaults =
lib.mapAttrs lib.mapAttrs
(name: def: def.default) (name: def:
if def.type == "flag" then
false
else
def.default
)
translator.specialArgs or {}; translator.specialArgs or {};
inherit (translator) subsystem type name; inherit (translator) subsystem type name;
}; };

View File

@ -26,8 +26,8 @@
generic = { generic = {
buildSystem = "nodejs"; buildSystem = "nodejs";
buildSystemFormatVersion = 1;
producedBy = translatorName; producedBy = translatorName;
mainPackage = null;
dependencyGraph = null; dependencyGraph = null;
sourcesCombinedHash = null; sourcesCombinedHash = null;
}; };

View File

@ -46,7 +46,7 @@
-r ''${inputFiles/$'\n'/$' -r '} -r ''${inputFiles/$'\n'/$' -r '}
# generate the generic lock from the downloaded list of files # generate the generic lock from the downloaded list of files
$tmpBuild/python/bin/python ${./generate-generic-lock.py} $tmp $outputFile $tmpBuild/python/bin/python ${./generate-generic-lock.py} $tmp $jsonInput
rm -rf $tmp $tmpBuild rm -rf $tmp $tmpBuild
''; '';
@ -75,6 +75,22 @@
"python39" "python39"
"python310" "python310"
]; ];
type = "argument";
}; };
main = {
default = "";
description = "name of the main package";
examples = [
"some-package"
];
type = "argument";
};
application = {
description = "build application instead of package";
type = "flag";
};
}; };
} }

View File

@ -7,7 +7,9 @@ import sys
def main(): def main():
directory = sys.argv[1] directory = sys.argv[1]
output_file = sys.argv[2]
with open(sys.argv[2]) as f:
jsonInput = json.load(f)
packages = {} packages = {}
@ -46,8 +48,7 @@ def main():
sources={}, sources={},
generic={ generic={
"buildSystem": "python", "buildSystem": "python",
"buildSystemFormatVersion": 1, "mainPackage": None,
"producedBy": "external-pip",
# This translator is not aware of the exact dependency graph. # This translator is not aware of the exact dependency graph.
# This restricts us to use a single derivation builder later, # This restricts us to use a single derivation builder later,
@ -57,6 +58,8 @@ def main():
"sourcesCombinedHash": None, "sourcesCombinedHash": None,
}, },
buildSystem={ buildSystem={
"main": jsonInput['main'],
"application": jsonInput['application'],
"pythonAttr": f"python{sys.version_info.major}{sys.version_info.minor}", "pythonAttr": f"python{sys.version_info.major}{sys.version_info.minor}",
"sourceFormats": "sourceFormats":
{pname: data['format'] for pname, data in packages.items()} {pname: data['format'] for pname, data in packages.items()}
@ -72,7 +75,8 @@ def main():
) )
# dump generic lock to stdout (json) # dump generic lock to stdout (json)
with open(output_file, 'w') as lock: print(jsonInput['outputFile'])
with open(jsonInput['outputFile'], 'w') as lock:
json.dump(generic_lock, lock, indent=2) json.dump(generic_lock, lock, indent=2)