-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiple repository support and config file support
- Loading branch information
1 parent
a63e728
commit 6430d45
Showing
10 changed files
with
267 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"bin-dir" : "$SUBUSERDIR/bin", | ||
"installed-programs.json" : "$SUBUSERDIR/installed-programs.json", | ||
"user-set-permissions-dir" : "$SUBUSERDIR/permissions/", | ||
"program-home-dirs-dir" : "$SUBUSERDIR/homes/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
The config.json file format | ||
-------------------------------- | ||
|
||
In the context of subuser, a `config.json` file is a file which subuser settings. | ||
|
||
The `config.json` files are to be arranged into a fallback-hierarchy. Subuser will first look up properties in the file `~/.subuser/config.json` falling back to: | ||
|
||
* `/etc/subuser/config.json` | ||
|
||
* `$SUBUSERDIR/config.json` | ||
|
||
Each config file may be partial. That is, if the user wants to specify some options, but leave others as their default values, they can simply ommit those options that they do not wish to change. | ||
|
||
`$SUBUSERDIR` is the directory where the subuser source resides. It corresponds to the root of this git repository. | ||
|
||
Each config.json file is to be a valid [json](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) file containing a single json object. | ||
|
||
Properties: | ||
----------- | ||
The defaults settings for these properties may be found in `$SUBUSERDIR/config.json`. | ||
|
||
**Note on paths**: Each path is to be absolute. Any environment variables of the form `$VARIABLE_NAME` will be expanded. | ||
|
||
* `bin-dir`: This is the directory where subuser "executables" are to be installed. This directory should be in the user's path. | ||
|
||
`type`: path to directory | ||
|
||
* `installed-programs.json` : This is the path to the installed-programs registry file. | ||
|
||
`type`: path to file | ||
|
||
* `user-set-permissions-dir`: NOT YET IMPLEMENTED | ||
|
||
`type`: path to directory | ||
|
||
* `program-home-dirs-dir`: The directory where subuser is to store the home directories of each subuser program. | ||
|
||
`type`: path to directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
The repositories.json file format | ||
-------------------------------- | ||
|
||
In the context of subuser, a `repositories.json` file is a file which describes where subuser programs may be installed from. | ||
|
||
The `repositories.json` files are to be arranged into a fallback-hierarchy. Subuser will first look up properties in the file `~/.subuser/repositories.json` falling back to: | ||
|
||
* `/etc/subuser/repositories.json` | ||
|
||
* `$SUBUSERDIR/repositories.json` | ||
|
||
`$SUBUSERDIR` is the directory where the subuser source resides. It corresponds to the root of this git repository. | ||
|
||
|
||
Each repositories.json file is to be a valid [json](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) file containing a single json object. | ||
|
||
This object is a set of key value pairs where each key is the name of a subuser repository. The value is a json object with the following properties: | ||
|
||
Properties: | ||
----------- | ||
**Note on paths**: Each path is to be absolute. Any environment variables of the form `$VARIABLE_NAME` will be expanded. | ||
|
||
* `path`: The path to the repository. | ||
|
||
Ex: | ||
|
||
```` | ||
"path" : "$SUBUSERDIR/programsThatCanBeInstalled/" | ||
```` | ||
|
||
Example repositories.json file: | ||
|
||
```` | ||
{ | ||
"default" : {"path" : "$SUBUSERDIR/programsThatCanBeInstalled/"} | ||
} | ||
```` | ||
|
||
This file states that there is one repository named `default` which can be found in the `$SUBUSERDIR/programsThatCanBeInstalled` directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python | ||
# This file should be compatible with both Python 2 and 3. | ||
# If it is not, please file a bug report. | ||
import os | ||
import inspect | ||
import json | ||
|
||
home = os.path.expanduser("~") | ||
|
||
def _getSubuserDir(): | ||
""" Get the toplevel directory for subuser. """ | ||
return os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))))) # BLEGH! | ||
|
||
def _getSubuserConfigPaths(): | ||
""" Returns a list of paths to config.json files in order that they should be looked in. """ | ||
_configsPaths = [] | ||
_configsPaths.append(os.path.join(home,".subuser","config.json")) | ||
_configsPaths.append("/etc/subuser/config.json") # TODO how does this work on windows? | ||
_configsPaths.append(os.path.join(_getSubuserDir(),"config.json")) | ||
configsPaths = [] | ||
for path in _configsPaths: | ||
if os.path.exists(path): | ||
configsPaths.append(path) | ||
return configsPaths | ||
|
||
def _addIfUnrepresented(identifier,path,paths): | ||
""" Add the tuple to the dictionary if it's key is not yet in the dictionary. """ | ||
if not identifier in paths.keys(): | ||
paths[identifier] = path | ||
|
||
def expandPathInConfig(path,config): | ||
""" Expand the path of a given setting. """ | ||
config[path] = os.path.expandvars(config[path]) | ||
|
||
def expandPathsInConfig(paths,config): | ||
for path in paths: | ||
expandPathInConfig(path,config) | ||
|
||
def expandVarsInPaths(config): | ||
""" Go through a freshly loaded config file and expand any environment variables in the paths. """ | ||
os.environ["SUBUSERDIR"] = _getSubuserDir() | ||
expandPathsInConfig(["bin-dir","installed-programs.json","user-set-permissions-dir","program-home-dirs-dir"],config) | ||
|
||
def getConfig(): | ||
""" Returns a dictionary of settings used by subuser. """ | ||
configPaths = _getSubuserConfigPaths() | ||
config = {} | ||
for _configFile in configPaths: | ||
with open(_configFile, 'r') as configFile: | ||
_config = json.load(configFile) | ||
for identifier,setting in _config.iteritems(): | ||
_addIfUnrepresented(identifier,setting,config) | ||
expandVarsInPaths(config) | ||
return config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python | ||
# This file should be compatible with both Python 2 and 3. | ||
# If it is not, please file a bug report. | ||
|
||
# TODO, refactor by putting helper functions for both repositories.py and configs.py in one place. | ||
|
||
import os | ||
import inspect | ||
import json | ||
import collections | ||
|
||
home = os.path.expanduser("~") | ||
|
||
def _getSubuserDir(): | ||
""" Get the toplevel directory for subuser. """ | ||
return os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))))) # BLEGH! | ||
|
||
def _getRepositoryListPaths(): | ||
""" Returns a list of paths to repositories.json files in order that they should be looked in. """ | ||
_repositoryListPaths = [] | ||
_repositoryListPaths.append(os.path.join(home,".subuser","repositories.json")) | ||
_repositoryListPaths.append("/etc/subuser/repositories.json") # TODO how does this work on windows? | ||
_repositoryListPaths.append(os.path.join(_getSubuserDir(),"repositories.json")) | ||
repositoryListPaths = [] | ||
for path in _repositoryListPaths: | ||
if os.path.exists(path): | ||
repositoryListPaths.append(path) | ||
return repositoryListPaths | ||
|
||
def _addIfUnrepresented(identifier,path,paths): | ||
""" Add the tuple to the dictionary if it's key is not yet in the dictionary. """ | ||
if not identifier in paths.keys(): | ||
paths[identifier] = path | ||
|
||
def expandVarsInPaths(repositories): | ||
""" Go through a freshly loaded list of repositories and expand any environment variables in the paths. """ | ||
os.environ["SUBUSERDIR"] = _getSubuserDir() | ||
for reponame,info in repositories.iteritems(): | ||
info["path"] = os.path.expandvars(info["path"]) | ||
|
||
def getRepositories(): | ||
""" Returns a dictionary of repositories used by subuser. """ | ||
repositoryListPaths = _getRepositoryListPaths() | ||
repositories = {} | ||
for _repositoryListFile in repositoryListPaths: | ||
with open(_repositoryListFile, 'r') as repositoryListFile: | ||
_repositories = json.load(repositoryListFile, object_pairs_hook=collections.OrderedDict) | ||
for identifier,repository in _repositories.iteritems(): | ||
_addIfUnrepresented(identifier,repository,repositories) | ||
expandVarsInPaths(repositories) | ||
return repositories |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"default" : {"path" : "$SUBUSERDIR/programsThatCanBeInstalled/"} | ||
} |