Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #22 from NoRedInk/native-deps-sync
Browse files Browse the repository at this point in the history
Native deps sync + native_package_install fixes
  • Loading branch information
ento authored Nov 17, 2016
2 parents f31332d + 2d602cd commit 8701cf7
Show file tree
Hide file tree
Showing 12 changed files with 436 additions and 119 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ python:
- "nightly"
# command to install dependencies
install:
- "pip install -r requirements.txt"
- "pip install -r dev-requirements.txt"
# command to run tests
script:
- "python -m pytest tests/ -v"
- "python -m pytest tests/ *.py -v --doctest-modules"
notifications:
email: false
3 changes: 3 additions & 0 deletions dev-requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hypothesis
pytest
pytest-mock
17 changes: 14 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
enum34==1.1.6
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file dev-requirements.txt dev-requirements.in
#
enum34==1.1.6 # via hypothesis
funcsigs==1.0.2 # via mock
hypothesis==3.6.0
py==1.4.31
pytest==3.0.3
mock==2.0.0 # via pytest-mock
pbr==1.10.0 # via mock
py==1.4.31 # via pytest
pytest-mock==1.4.0
pytest==3.0.4
six==1.10.0 # via mock
68 changes: 25 additions & 43 deletions elm_deps_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,48 @@
from __future__ import print_function

import sys
from collections import OrderedDict
import json
import argparse

import elm_package


def sync_versions(top_level_file, spec_file, quiet=False, dry=False, note_test_deps=True):
""" first file should be the top level elm-package.json
second file should be the spec file
""" first file should be the top level elm-package.json.
second file should be the spec level elm-package.json.
"""

with open(top_level_file) as f:
top_level = json.load(f)
top_level = elm_package.load(f)

with open(spec_file) as f:
spec = json.load(f, object_pairs_hook=OrderedDict)

messages = []

for (package_name, package_version) in top_level['dependencies'].items():
if package_name not in spec['dependencies']:
spec['dependencies'][package_name] = package_version

messages.append('Package {package_name} inserted to {spec_file} for the first time at version "{package_version}"'.format(
package_name=package_name, spec_file=spec_file, package_version=package_version)
)
elif spec['dependencies'][package_name] != package_version:
messages.append('Changing {package_name} from version {package_version} to {other_package_version}'.format(
package_version=package_version, package_name=package_name,
other_package_version=spec['dependencies'][package_name])
)

spec['dependencies'][package_name] = package_version
spec = elm_package.load(f)

test_deps = {}

for (package_name, package_version) in spec['dependencies'].items():
if package_name not in top_level['dependencies']:
test_deps[package_name] = package_version
(messages, new_deps) = elm_package.sync_deps(top_level['dependencies'], spec['dependencies'])
spec['dependencies'] = new_deps

if note_test_deps:
spec['test-dependencies'] = sorted_deps(test_deps)

if len(messages) > 0 or note_test_deps:
print('{number} packages changed.'.format(number=len(messages)))
test_deps = {}

if not dry:
spec['dependencies'] = sorted_deps(spec['dependencies'])
with open(spec_file, 'w') as f:
json.dump(spec, f, sort_keys=False, indent=4, separators=(',', ': '))
else:
print("No changes written.")
for (package_name, package_version) in spec['dependencies'].items():
if package_name not in top_level['dependencies']:
test_deps[package_name] = package_version
spec['test-dependencies'] = elm_package.sorted_deps(test_deps)

if not quiet:
print('\n'.join(messages))
else:
if len(messages) == 0 and not note_test_deps:
print('No changes needed.')
return

print('{number} packages changed.'.format(number=len(messages)))

if not quiet:
print('\n'.join(messages))

if dry:
print("No changes written.")
return

def sorted_deps(deps):
return OrderedDict(sorted(deps.items()))
with open(spec_file, 'w') as f:
elm_package.dump(spec, f)


def main():
Expand Down
49 changes: 49 additions & 0 deletions elm_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#! /usr/bin/env python
"""
Load and save elm-package.json safely.
"""

# from typing import Dict, Tuple, IO
import copy
from collections import OrderedDict
import json


def load(fileobj):
# type: (IO[str]) -> Dict
return json.load(fileobj, object_pairs_hook=OrderedDict)


def dump(package, fileobj):
# type: (Dict, IO[str]) -> None
to_save = copy.deepcopy(package)
to_save['dependencies'] = sorted_deps(to_save['dependencies'])
json.dump(to_save, fileobj, sort_keys=False, indent=4, separators=(',', ': '))


def sorted_deps(deps):
# type: (Dict) -> Dict
return OrderedDict(sorted(deps.items()))


def sync_deps(from_deps, to_deps):
# type: (Dict, Dict) -> Tuple[List[str], Dict]
messages = []
result = copy.deepcopy(to_deps)

for (package_name, package_version) in from_deps.items():
if package_name not in to_deps:
result[package_name] = package_version

messages.append('Inserting new package {package_name} at version {package_version}'.format(
package_name=package_name, package_version=package_version)
)
elif to_deps[package_name] != package_version:
result[package_name] = package_version

messages.append('Changing {package_name} from version {package_version} to {other_package_version}'.format(
package_version=package_version, package_name=package_name,
other_package_version=to_deps[package_name])
)

return messages, result
25 changes: 25 additions & 0 deletions exact_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /usr/bin/env python
"""
Load and save exact-dependencies.json safely.
The format of exact-dependencies is simply a dictionary of
package name and its exact version:
{
"elm-lang/core": "4.0.5"
}
"""

# from typing import Dict, IO
import json

import elm_package


load = elm_package.load


def dump(package, fileobj):
# type: (Dict, IO[str]) -> None
to_save = elm_package.sorted_deps(package)
json.dump(to_save, fileobj, sort_keys=False, indent=4, separators=(',', ': '))
57 changes: 57 additions & 0 deletions native_deps_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#! /usr/bin/env python
from __future__ import print_function

import sys
import argparse

import elm_package
import exact_dependencies


def sync_versions(top_level_file, spec_file, quiet=False, dry=False, note_test_deps=True):
""" first file should be the top level elm-native-package.json.
second file should be the spec level elm-native-package.json.
"""

with open(top_level_file) as f:
top_level = exact_dependencies.load(f)

with open(spec_file) as f:
spec = exact_dependencies.load(f)

(messages, new_deps) = elm_package.sync_deps(top_level, spec)
spec = new_deps

if len(messages) == 0:
print('No changes needed.')
return

print('{number} packages changed.'.format(number=len(messages)))

if not quiet:
print('\n'.join(messages))

if dry:
print("No changes written.")
return

with open(spec_file, 'w') as f:
exact_dependencies.dump(spec, f)



def main():

parser = argparse.ArgumentParser(description='Sync deps between a parent and a sub')

parser.add_argument('--quiet', '-q', action='store_true', help='don\'t print anything', default=False)
parser.add_argument('--dry', '-d', action='store_true', help='only print possible changes', default=False)
parser.add_argument('top_level_file')
parser.add_argument('spec_file')
args = parser.parse_args()

sync_versions(args.top_level_file, args.spec_file, quiet=args.quiet, dry=args.dry)


if __name__ == '__main__':
main()
Loading

0 comments on commit 8701cf7

Please sign in to comment.