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

Commit

Permalink
user -> owner: orgs can own repos too
Browse files Browse the repository at this point in the history
  • Loading branch information
ento committed Nov 16, 2016
1 parent bcfe86c commit e5f9e8c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
56 changes: 28 additions & 28 deletions native_package_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,39 @@ def read_native_elm_package(package_file):
def format_tarball_url(package):
"""
Creates the url to fetch the tar from github.
>>> format_tarball_url({'user': 'elm-lang', 'project': 'navigation', 'version': '2.0.0'})
>>> format_tarball_url({'owner': 'elm-lang', 'project': 'navigation', 'version': '2.0.0'})
'https://github.com/elm-lang/navigation/archive/2.0.0.tar.gz'
"""
return "https://github.com/{user}/{project}/archive/{version}.tar.gz".format(**package)
return "https://github.com/{owner}/{project}/archive/{version}.tar.gz".format(**package)


def packages_from_exact_deps(exact_dependencies):
"""
Parses the json and returns a list of {version, user, project}.
Parses the json and returns a list of {version, owner, project}.
>>> packages_from_exact_deps({'elm-lang/navigation': '2.0.0'}) \
== [{'version': '2.0.0', 'user': 'elm-lang', 'project': 'navigation'}]
== [{'version': '2.0.0', 'owner': 'elm-lang', 'project': 'navigation'}]
True
"""
result = []

for package, version in exact_dependencies.items():
user, project = package.split('/')
owner, project = package.split('/')
result.append({
'user': user,
'owner': owner,
'project': project,
'version': version
})

return result


def ensure_vendor_user_dir(base, user):
def ensure_vendor_owner_dir(base, owner):
"""
Creates the path in the vendor folder.
>>> ensure_vendor_user_dir('foo', 'bar')
>>> ensure_vendor_owner_dir('foo', 'bar')
'foo/bar'
"""
path = os.path.join(base, user)
path = os.path.join(base, owner)

try:
os.makedirs(path)
Expand All @@ -76,12 +76,12 @@ def ensure_vendor_user_dir(base, user):
def vendor_package_dir(vendor_dir, package):
"""
Returns the path to the elm package. Also creates the parent directory if misisng.
>>> vendor_package_dir('vendor/assets/elm', {'version': '2.0.0', 'user': 'elm-lang', 'project': 'navigation'})
>>> vendor_package_dir('vendor/assets/elm', {'version': '2.0.0', 'owner': 'elm-lang', 'project': 'navigation'})
'vendor/assets/elm/elm-lang/navigation-2.0.0'
"""
vendor_user_dir = ensure_vendor_user_dir(vendor_dir, package['user'])
return "{vendor_user_dir}/{project}-{version}".format(
vendor_user_dir=vendor_user_dir,
vendor_owner_dir = ensure_vendor_owner_dir(vendor_dir, package['owner'])
return "{vendor_owner_dir}/{project}-{version}".format(
vendor_owner_dir=vendor_owner_dir,
project=package['project'],
version=package['version']
)
Expand All @@ -93,53 +93,53 @@ def fetch_packages(vendor_dir, packages):
"""
for package in packages:
tar_filename = format_tar_path(vendor_dir, package)
vendor_user_dir = ensure_vendor_user_dir(vendor_dir, package['user'])
vendor_owner_dir = ensure_vendor_owner_dir(vendor_dir, package['owner'])
url = format_tarball_url(package)

print("Downloading {user}/{project} {version}".format(**package))
print("Downloading {owner}/{project} {version}".format(**package))
tar_file = urlopen(url)
with open(tar_filename, 'w') as tar:
tar.write(tar_file.read())

with tarfile.open(tar_filename) as tar:
tar.extractall(vendor_user_dir, members=tar.getmembers())
tar.extractall(vendor_owner_dir, members=tar.getmembers())

return packages


def format_tar_path(vendor_dir, package):
"""
The path of the tar.
>>> format_tar_path('vendor/assets/elm', {'user': 'elm-lang', 'project': 'navigation', 'version': '2.0.0'})
>>> format_tar_path('vendor/assets/elm', {'owner': 'elm-lang', 'project': 'navigation', 'version': '2.0.0'})
'vendor/assets/elm/elm-lang/navigation-2.0.0-tar.gz'
"""
ensure_vendor_user_dir(vendor_dir, package['user'])
ensure_vendor_owner_dir(vendor_dir, package['owner'])
return vendor_package_dir(vendor_dir, package) + "-tar.gz"


def format_native_name(user, project):
def format_native_name(owner, project):
"""
Formates the package to the user used in elm native.
Formates the package to the owner used in elm native.
>>> format_native_name('elm-lang', 'navigation')
'_elm_lang$navigation'
"""

underscored_user = user.replace("-", "_")
underscored_owner = owner.replace("-", "_")
underscored_project = project.replace("-", "_")
return "_{owner}${repo}".format(owner=underscored_user, repo=underscored_project)
return "_{owner}${repo}".format(owner=underscored_owner, repo=underscored_project)


def package_name_from_repo(repository):
"""
User and project from repository.
Owner and project from repository.
>>> package_name_from_repo('https://github.com/NoRedInk/noredink.git')
('NoRedInk', 'noredink')
"""

repo_without_domain = repository.split('https://github.com/')[1].split('.git')[0]

(user, project) = repo_without_domain.split('/')
return (user, project)
(owner, project) = repo_without_domain.split('/')
return (owner, project)


def get_source_dirs(vendor_dir, package):
Expand Down Expand Up @@ -175,14 +175,14 @@ def munge_names(vendor_dir, repository, packages):
"""
Replaces the namespaced function names in all native code by the namespace from the given elm-package.json.
"""
user, project = package_name_from_repo(repository)
owner, project = package_name_from_repo(repository)
for package in packages:
native_files = find_all_native_files(vendor_package_dir(vendor_dir, package))
for native_file in native_files:
replace_in_file(
native_file,
format_native_name(package['user'], package['project']),
format_native_name(user, project)
format_native_name(package['owner'], package['project']),
format_native_name(owner, project)
)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_native_package_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_update_source_directories_makes_minimum_changes(tmpdir):
'dependencies': {},
}, indent=4, separators=(',', ': ')))

native_packages = [{'user': 'elm-lang', 'project': 'core', 'version': '1.0.0'}]
native_packages = [{'owner': 'elm-lang', 'project': 'core', 'version': '1.0.0'}]
vendor_package_dir = vendor_dir.mkdir('elm-lang').mkdir('core-1.0.0')
fake_elm_package = vendor_package_dir.join('elm-package.json')
fake_elm_package.write(json.dumps({
Expand Down

0 comments on commit e5f9e8c

Please sign in to comment.